diff options
Diffstat (limited to 'scene/gui/split_container.cpp')
-rw-r--r-- | scene/gui/split_container.cpp | 164 |
1 files changed, 78 insertions, 86 deletions
diff --git a/scene/gui/split_container.cpp b/scene/gui/split_container.cpp index de892a4fb9..4736a1ad37 100644 --- a/scene/gui/split_container.cpp +++ b/scene/gui/split_container.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -34,18 +34,20 @@ #include "margin_container.h" Control *SplitContainer::_getch(int p_idx) const { - int idx = 0; for (int i = 0; i < get_child_count(); i++) { Control *c = Object::cast_to<Control>(get_child(i)); - if (!c || !c->is_visible_in_tree()) + if (!c || !c->is_visible()) { continue; - if (c->is_set_as_toplevel()) + } + if (c->is_set_as_top_level()) { continue; + } - if (idx == p_idx) + if (idx == p_idx) { return c; + } idx++; } @@ -74,8 +76,8 @@ void SplitContainer::_resort() { bool second_expanded = (vertical ? second->get_v_size_flags() : second->get_h_size_flags()) & SIZE_EXPAND; // Determine the separation between items - Ref<Texture2D> g = get_theme_icon("grabber"); - int sep = get_theme_constant("separation"); + Ref<Texture2D> g = get_theme_icon(SNAME("grabber")); + int sep = get_theme_constant(SNAME("separation")); sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(sep, vertical ? g->get_height() : g->get_width()) : 0; // Compute the minimum size @@ -100,7 +102,7 @@ void SplitContainer::_resort() { middle_sep += clamped_split_offset; if (should_clamp_split_offset) { split_offset = clamped_split_offset; - _change_notify("split_offset"); + should_clamp_split_offset = false; } } @@ -110,44 +112,48 @@ void SplitContainer::_resort() { int sofs = middle_sep + sep; fit_child_in_rect(second, Rect2(Point2(0, sofs), Size2(get_size().width, get_size().height - sofs))); } else { - fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height))); - int sofs = middle_sep + sep; - fit_child_in_rect(second, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height))); + if (is_layout_rtl()) { + middle_sep = get_size().width - middle_sep - sep; + fit_child_in_rect(second, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height))); + int sofs = middle_sep + sep; + fit_child_in_rect(first, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height))); + } else { + fit_child_in_rect(first, Rect2(Point2(0, 0), Size2(middle_sep, get_size().height))); + int sofs = middle_sep + sep; + fit_child_in_rect(second, Rect2(Point2(sofs, 0), Size2(get_size().width - sofs, get_size().height))); + } } update(); } Size2 SplitContainer::get_minimum_size() const { - /* Calculate MINIMUM SIZE */ Size2i minimum; - Ref<Texture2D> g = get_theme_icon("grabber"); - int sep = get_theme_constant("separation"); + Ref<Texture2D> g = get_theme_icon(SNAME("grabber")); + int sep = get_theme_constant(SNAME("separation")); sep = (dragger_visibility != DRAGGER_HIDDEN_COLLAPSED) ? MAX(sep, vertical ? g->get_height() : g->get_width()) : 0; for (int i = 0; i < 2; i++) { - - if (!_getch(i)) + if (!_getch(i)) { break; + } if (i == 1) { - - if (vertical) + if (vertical) { minimum.height += sep; - else + } else { minimum.width += sep; + } } Size2 ms = _getch(i)->get_combined_minimum_size(); if (vertical) { - minimum.height += ms.height; minimum.width = MAX(minimum.width, ms.width); } else { - minimum.width += ms.width; minimum.height = MAX(minimum.height, ms.height); } @@ -157,80 +163,77 @@ Size2 SplitContainer::get_minimum_size() const { } void SplitContainer::_notification(int p_what) { - switch (p_what) { - + case NOTIFICATION_TRANSLATION_CHANGED: + case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { + queue_sort(); + } break; case NOTIFICATION_SORT_CHILDREN: { - _resort(); } break; case NOTIFICATION_MOUSE_EXIT: { - mouse_inside = false; - if (get_theme_constant("autohide")) + if (get_theme_constant(SNAME("autohide"))) { update(); + } } break; case NOTIFICATION_DRAW: { - - if (!_getch(0) || !_getch(1)) + if (!_getch(0) || !_getch(1)) { return; + } - if (collapsed || (!dragging && !mouse_inside && get_theme_constant("autohide"))) + if (collapsed || (!dragging && !mouse_inside && get_theme_constant(SNAME("autohide")))) { return; + } - if (dragger_visibility != DRAGGER_VISIBLE) + if (dragger_visibility != DRAGGER_VISIBLE) { return; + } - int sep = dragger_visibility != DRAGGER_HIDDEN_COLLAPSED ? get_theme_constant("separation") : 0; - Ref<Texture2D> tex = get_theme_icon("grabber"); + int sep = dragger_visibility != DRAGGER_HIDDEN_COLLAPSED ? get_theme_constant(SNAME("separation")) : 0; + Ref<Texture2D> tex = get_theme_icon(SNAME("grabber")); Size2 size = get_size(); - if (vertical) + if (vertical) { draw_texture(tex, Point2i((size.x - tex->get_width()) / 2, middle_sep + (sep - tex->get_height()) / 2)); - else + } else { draw_texture(tex, Point2i(middle_sep + (sep - tex->get_width()) / 2, (size.y - tex->get_height()) / 2)); + } } break; case NOTIFICATION_THEME_CHANGED: { - minimum_size_changed(); } break; } } -void SplitContainer::_gui_input(const Ref<InputEvent> &p_event) { +void SplitContainer::gui_input(const Ref<InputEvent> &p_event) { + ERR_FAIL_COND(p_event.is_null()); - if (collapsed || !_getch(0) || !_getch(1) || dragger_visibility != DRAGGER_VISIBLE) + if (collapsed || !_getch(0) || !_getch(1) || dragger_visibility != DRAGGER_VISIBLE) { return; + } Ref<InputEventMouseButton> mb = p_event; if (mb.is_valid()) { - - if (mb->get_button_index() == BUTTON_LEFT) { - + if (mb->get_button_index() == MOUSE_BUTTON_LEFT) { if (mb->is_pressed()) { - - int sep = get_theme_constant("separation"); + int sep = get_theme_constant(SNAME("separation")); if (vertical) { - if (mb->get_position().y > middle_sep && mb->get_position().y < middle_sep + sep) { - dragging = true; drag_from = mb->get_position().y; drag_ofs = split_offset; } } else { - if (mb->get_position().x > middle_sep && mb->get_position().x < middle_sep + sep) { - dragging = true; drag_from = mb->get_position().x; drag_ofs = split_offset; } } } else { - dragging = false; } } @@ -239,47 +242,51 @@ void SplitContainer::_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseMotion> mm = p_event; if (mm.is_valid()) { - bool mouse_inside_state = false; - if (vertical) - mouse_inside_state = mm->get_position().y > middle_sep && mm->get_position().y < middle_sep + get_theme_constant("separation"); - else - mouse_inside_state = mm->get_position().x > middle_sep && mm->get_position().x < middle_sep + get_theme_constant("separation"); + if (vertical) { + mouse_inside_state = mm->get_position().y > middle_sep && mm->get_position().y < middle_sep + get_theme_constant(SNAME("separation")); + } else { + mouse_inside_state = mm->get_position().x > middle_sep && mm->get_position().x < middle_sep + get_theme_constant(SNAME("separation")); + } if (mouse_inside != mouse_inside_state) { - mouse_inside = mouse_inside_state; - if (get_theme_constant("autohide")) + if (get_theme_constant(SNAME("autohide"))) { update(); + } } - if (!dragging) + if (!dragging) { return; + } - split_offset = drag_ofs + ((vertical ? mm->get_position().y : mm->get_position().x) - drag_from); + if (!vertical && is_layout_rtl()) { + split_offset = drag_ofs + (drag_from - (vertical ? mm->get_position().y : mm->get_position().x)); + } else { + split_offset = drag_ofs + ((vertical ? mm->get_position().y : mm->get_position().x) - drag_from); + } should_clamp_split_offset = true; queue_sort(); - emit_signal("dragged", get_split_offset()); + emit_signal(SNAME("dragged"), get_split_offset()); } } Control::CursorShape SplitContainer::get_cursor_shape(const Point2 &p_pos) const { - - if (dragging) + if (dragging) { return (vertical ? CURSOR_VSPLIT : CURSOR_HSPLIT); + } if (!collapsed && _getch(0) && _getch(1) && dragger_visibility == DRAGGER_VISIBLE) { - - int sep = get_theme_constant("separation"); + int sep = get_theme_constant(SNAME("separation")); if (vertical) { - - if (p_pos.y > middle_sep && p_pos.y < middle_sep + sep) + if (p_pos.y > middle_sep && p_pos.y < middle_sep + sep) { return CURSOR_VSPLIT; + } } else { - - if (p_pos.x > middle_sep && p_pos.x < middle_sep + sep) + if (p_pos.x > middle_sep && p_pos.x < middle_sep + sep) { return CURSOR_HSPLIT; + } } } @@ -287,9 +294,9 @@ Control::CursorShape SplitContainer::get_cursor_shape(const Point2 &p_pos) const } void SplitContainer::set_split_offset(int p_offset) { - - if (split_offset == p_offset) + if (split_offset == p_offset) { return; + } split_offset = p_offset; @@ -297,7 +304,6 @@ void SplitContainer::set_split_offset(int p_offset) { } int SplitContainer::get_split_offset() const { - return split_offset; } @@ -308,35 +314,29 @@ void SplitContainer::clamp_split_offset() { } void SplitContainer::set_collapsed(bool p_collapsed) { - - if (collapsed == p_collapsed) + if (collapsed == p_collapsed) { return; + } collapsed = p_collapsed; queue_sort(); } void SplitContainer::set_dragger_visibility(DraggerVisibility p_visibility) { - dragger_visibility = p_visibility; queue_sort(); update(); } SplitContainer::DraggerVisibility SplitContainer::get_dragger_visibility() const { - return dragger_visibility; } bool SplitContainer::is_collapsed() const { - return collapsed; } void SplitContainer::_bind_methods() { - - ClassDB::bind_method(D_METHOD("_gui_input"), &SplitContainer::_gui_input); - ClassDB::bind_method(D_METHOD("set_split_offset", "offset"), &SplitContainer::set_split_offset); ClassDB::bind_method(D_METHOD("get_split_offset"), &SplitContainer::get_split_offset); ClassDB::bind_method(D_METHOD("clamp_split_offset"), &SplitContainer::clamp_split_offset); @@ -351,7 +351,7 @@ void SplitContainer::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "split_offset"), "set_split_offset", "get_split_offset"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility", PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden & Collapsed"), "set_dragger_visibility", "get_dragger_visibility"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "dragger_visibility", PROPERTY_HINT_ENUM, "Visible,Hidden,Hidden and Collapsed"), "set_dragger_visibility", "get_dragger_visibility"); BIND_ENUM_CONSTANT(DRAGGER_VISIBLE); BIND_ENUM_CONSTANT(DRAGGER_HIDDEN); @@ -359,13 +359,5 @@ void SplitContainer::_bind_methods() { } SplitContainer::SplitContainer(bool p_vertical) { - - mouse_inside = false; - split_offset = 0; - should_clamp_split_offset = false; - middle_sep = 0; vertical = p_vertical; - dragging = false; - collapsed = false; - dragger_visibility = DRAGGER_VISIBLE; } |