Godot Engine – Multi-platform 2D and 3D game engine
Fix some issues with `Tree`
Michael Alexsandera6bbca496894
diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cppindex 2e2e4bf6536..83e0bb305dc 100644--- a/scene/gui/tree.cpp+++ b/scene/gui/tree.cpp@@ -2322,8 +2322,9 @@ void Tree::update_item_cache(TreeItem *p_item) const {}int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item, int &r_self_height, const RID &p_ci) {- const real_t bottom_margin = theme_cache.panel_style->get_margin(SIDE_BOTTOM); // Extra stylebox space below the content- const real_t draw_height = p_draw_size.height + bottom_margin; // Visible height including bottom margin+ // The space below the scrolling content is also valid drawing area.+ real_t draw_height = p_draw_size.height + theme_cache.panel_style->get_margin(SIDE_BOTTOM);+ draw_height += h_scroll->is_visible() ? h_scroll->get_bound_minimum_size().height : 0;// Cull item if it's beyond the bottom of the visible area.if (p_pos.y - theme_cache.offset.y > draw_height) {@@ -3135,13 +3136,14 @@ void Tree::_range_click_timeout() {Ref<InputEventMouseButton> mb;mb.instantiate();- int x_limit = _get_content_rect().size.x;+ Size2 limit = _get_content_rect().size;+ limit.width += theme_cache.offset.width;cache.rtl = is_layout_rtl();propagate_mouse_activated = false; // Done from outside, so signal handler can't clear the tree in the middle of emit (which is a common case).blocked++;- propagate_mouse_event(pos + theme_cache.offset, 0, 0, x_limit + theme_cache.offset.width, false, root, MouseButton::LEFT, mb);+ propagate_mouse_event(pos + theme_cache.offset, 0, 0, limit, false, root, MouseButton::LEFT, mb);blocked--;if (range_click_timer->is_one_shot()) {@@ -3164,7 +3166,7 @@ void Tree::_range_click_timeout() {}}-int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int x_limit, bool p_double_click, TreeItem *p_item, MouseButton p_button, const Ref<InputEventWithModifiers> &p_mod, bool p_first_call, bool p_skip_children) {+int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, const Size2 p_limit, bool p_double_click, TreeItem *p_item, MouseButton p_button, const Ref<InputEventWithModifiers> &p_mod, bool p_first_call, bool p_skip_children) {if (p_first_call) {// Handle sticky item input firstfor (TreeItem *item : sticky_list) {@@ -3174,7 +3176,7 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, intif (relative_pos.y > item->cached_label_height) {continue;}- int result = propagate_mouse_event(relative_pos, item->sticky_offset.x, item->sticky_offset.y, x_limit, p_double_click, item, p_button, p_mod, false, true);+ int result = propagate_mouse_event(relative_pos, item->sticky_offset.x, item->sticky_offset.y, p_limit, p_double_click, item, p_button, p_mod, false, true);if (result < 0) {return result;}@@ -3186,7 +3188,12 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int// Skip any processing of invisible items.return 0;}- if (p_pos.x > x_limit) {++ float limit_height = p_limit.height;+ if (v_scroll->is_visible()) {+ limit_height += v_scroll->get_value();+ }+ if (p_pos.x > p_limit.width || p_pos.y > limit_height) {// Inside the scroll area.return -1;}@@ -3195,8 +3202,8 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, intbool skip = (p_item == root && hide_root);- if (!skip && p_pos.y < item_h) {- // Check event!+ if (!skip && p_pos.y >= 0 && p_pos.y < item_h) {+ // Check event.if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {return -1;}@@ -3210,7 +3217,7 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, intreturn -1;}- FindColumnButtonResult find_result = _find_column_and_button_at_pos(p_pos.x, p_item, x_ofs, x_limit);+ FindColumnButtonResult find_result = _find_column_and_button_at_pos(p_pos.x, p_item, x_ofs, p_limit.width);if (find_result.column_index == -1) {return -1;@@ -3458,7 +3465,7 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, intTreeItem *c = p_item->first_child;while (c) {- int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, x_limit, p_double_click, c, p_button, p_mod, false, false);+ int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, p_limit, p_double_click, c, p_button, p_mod, false, false);if (child_h < 0) {return -1; // Break, stop propagating, no need to anymore.@@ -3900,8 +3907,8 @@ Rect2 Tree::_get_content_rect() const {const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));// Scrollbars won't affect Tree's content rect if they're not visible or placed inside the stylebox margin area.- const real_t v_size = v_scroll->is_visible() ? (v_scroll->get_bound_minimum_size().x + theme_cache.scrollbar_h_separation) : 0;- const real_t h_size = h_scroll->is_visible() ? (h_scroll->get_bound_minimum_size().y + theme_cache.scrollbar_v_separation) : 0;+ const real_t v_size = v_scroll->is_visible() ? v_scroll->get_bound_minimum_size().x : 0;+ const real_t h_size = h_scroll->is_visible() ? h_scroll->get_bound_minimum_size().y : 0;const Point2 scroll_begin = _get_scrollbar_layout_rect().get_end() - Vector2(v_size, h_size);const Size2 offset = (content_rect.get_end() - scroll_begin).maxf(0);@@ -4253,9 +4260,10 @@ void Tree::gui_input(const Ref<InputEvent> &p_event) {}pos -= theme_cache.panel_style->get_offset();if (show_column_titles) {- pos.y -= _get_title_button_height();+ const int title_button_height = _get_title_button_height();+ pos.y -= title_button_height;- if (pos.y < 0) {+ if (pos.y < 0 && pos.y >= -title_button_height) {pos.x += theme_cache.offset.x;int len = 0;for (int i = 0; i < columns.size(); i++) {@@ -4356,9 +4364,10 @@ void Tree::gui_input(const Ref<InputEvent> &p_event) {pos -= bg->get_offset();cache.click_type = Cache::CLICK_NONE;if (show_column_titles) {- pos.y -= _get_title_button_height();+ const int title_button_height = _get_title_button_height();+ pos.y -= title_button_height;- if (pos.y < 0) {+ if (pos.y < 0 && pos.y >= -title_button_height) {pos.x += theme_cache.offset.x;int len = 0;for (int i = 0; i < columns.size(); i++) {@@ -4383,11 +4392,12 @@ void Tree::gui_input(const Ref<InputEvent> &p_event) {pressing_for_editor = false;propagate_mouse_activated = false;- int x_limit = _get_content_rect().size.x;+ Size2 limit = _get_content_rect().size;+ limit.width += theme_cache.offset.width;cache.rtl = is_layout_rtl();blocked++;- propagate_mouse_event(pos + theme_cache.offset, 0, 0, x_limit + theme_cache.offset.width, mb->is_double_click(), root, mb->get_button_index(), mb);+ propagate_mouse_event(pos + theme_cache.offset, 0, 0, limit, mb->is_double_click(), root, mb->get_button_index(), mb);blocked--;if (pressing_for_editor) {@@ -4823,7 +4833,7 @@ void Tree::update_scrollbars() {for (int i = 0; i < 2; i++) {// Check twice, as both values are dependent on each other.if (display_hscroll) {- tree_content_size.height = content_rect.get_size().height - title_button_height - hmin.height;+ tree_content_size.height = content_rect.get_size().height - title_button_height - hmin.height - theme_cache.scrollbar_v_separation;display_vscroll = internal_min_size.height > tree_content_size.height;}if (display_vscroll) {@@ -5376,9 +5386,10 @@ void Tree::_notification(int p_what) {draw_ofs.y += tbh;draw_size.y -= tbh;- const real_t clip_top = show_column_titles ? draw_ofs.y : 0.0f; // Start clipping below headers when they are visible- const real_t bottom_margin = theme_cache.panel_style->get_margin(SIDE_BOTTOM);- const real_t clip_bottom = draw_ofs.y + draw_size.y + bottom_margin; // End clip after the bottom margin+ const real_t clip_top = show_column_titles ? draw_ofs.y : 0.0f; // Start clipping below headers when they are visible.+ // End clip after the bottom scroll and margin.+ real_t clip_bottom = draw_ofs.y + draw_size.y + theme_cache.panel_style->get_margin(SIDE_BOTTOM);+ clip_bottom += h_scroll->is_visible() ? h_scroll->get_bound_minimum_size().height : 0;const Rect2 main_clip_rect = Rect2(0.0f, clip_top, get_size().x, clip_bottom - clip_top);RenderingServer *rendering_server = RenderingServer::get_singleton();@@ -5952,6 +5963,9 @@ void Tree::update_min_size_for_item_change() {void Tree::set_select_mode(SelectMode p_mode) {select_mode = p_mode;+ if (selected_item) {+ queue_redraw();+ }}Tree::SelectMode Tree::get_select_mode() const {@@ -7016,11 +7030,19 @@ void Tree::_find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_co}pos += theme_cache.offset; // Scrolling.- int x_limit = _get_content_rect().size.x + theme_cache.offset.width;+ if (pos.y < 0) {+ return; // Above all items.+ }- if (pos.x > x_limit) {- // Inside the scroll area.- return;+ Size2 limit = _get_content_rect().size;+ limit.width += theme_cache.offset.width;++ float limit_height = limit.height;+ if (v_scroll->is_visible()) {+ limit_height += v_scroll->get_value();+ }+ if (pos.x > limit.width || pos.y > limit_height) {+ return; // Inside the scroll area.}int col, h, section;@@ -7039,7 +7061,7 @@ void Tree::_find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_co}// Search the button index.- FindColumnButtonResult result = _find_column_and_button_at_pos(pos.x, it, 0, x_limit);+ FindColumnButtonResult result = _find_column_and_button_at_pos(pos.x, it, 0, limit.width);r_index = result.button_index;}@@ -7217,13 +7239,19 @@ Variant Tree::get_drag_data(const Point2 &p_point) {}TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {- if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {+ if (!root) {+ return nullptr;+ }++ Size2 size = get_size();+ size.height -= theme_cache.panel_style->get_margin(SIDE_BOTTOM);+ if (!Rect2(Vector2(), size).has_point(p_pos)) {return nullptr;}Point2 pos = p_pos;if (is_layout_rtl()) {- pos.x = get_size().width - pos.x - 1;+ pos.x = size.width - pos.x - 1;}Point2 margin_offset = theme_cache.panel_style->get_offset();pos -= margin_offset;@@ -7258,7 +7286,9 @@ int Tree::get_button_id_at_position(const Point2 &p_pos) const {return selected_item->cells[selected_col].buttons[selected_button].id;}} else {- if (!Rect2(Vector2(), get_size()).has_point(p_pos)) {+ Size2 size = get_size();+ size.height -= theme_cache.panel_style->get_margin(SIDE_BOTTOM);+ if (!Rect2(Vector2(), size).has_point(p_pos)) {return -1;}diff --git a/scene/gui/tree.h b/scene/gui/tree.hindex b335aa8ab32..04486635547 100644--- a/scene/gui/tree.h+++ b/scene/gui/tree.h@@ -604,7 +604,7 @@ private:void draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color, int p_ol_size, const Color &p_ol_color, const RID &p_ci) const;int draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item, int &r_self_height, const RID &p_ci);void select_single_item(TreeItem *p_selected, TreeItem *p_current, int p_col, TreeItem *p_prev = nullptr, bool *r_in_range = nullptr, bool p_force_deselect = false);- int propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int x_limit, bool p_double_click, TreeItem *p_item, MouseButton p_button, const Ref<InputEventWithModifiers> &p_mod, bool p_first_call = true, bool p_skip_children = false);+ int propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, const Size2 p_limit, bool p_double_click, TreeItem *p_item, MouseButton p_button, const Ref<InputEventWithModifiers> &p_mod, bool p_first_call = true, bool p_skip_children = false);void _line_editor_submit(String p_text);void _apply_multiline_edit(bool p_hide_focus = false);void _text_editor_popup_modal_close();