From 2f0ff3aeb7dffddd94945f3c46c6f3e168392445 Mon Sep 17 00:00:00 2001 From: BrotherShort <129957860+BrotherShort@users.noreply.github.com> Date: Sat, 15 Nov 2025 17:22:37 +0800 Subject: [PATCH] Fix Treeitem icon overflows column boundary --- scene/gui/tree.cpp | 18 ++++++++++++------ scene/gui/tree.h | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index 17dcfb20f9..e3992845af 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -59,23 +59,23 @@ Size2 TreeItem::Cell::get_icon_size() const { } } -void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size, const Color &p_color) const { +void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size, const Rect2i &p_region, const Color &p_color) const { if (icon.is_null()) { return; } Size2i dsize = (p_size == Size2()) ? icon->get_size() : p_size; - if (icon_region == Rect2i()) { + if (p_region == Rect2i()) { icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size()), p_color); if (icon_overlay.is_valid()) { Vector2 offset = icon->get_size() - icon_overlay->get_size(); icon_overlay->draw_rect_region(p_where, Rect2(p_pos + offset, dsize), Rect2(Point2(), icon_overlay->get_size()), p_color); } } else { - icon->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color); + icon->draw_rect_region(p_where, Rect2(p_pos, dsize), p_region, p_color); if (icon_overlay.is_valid()) { - icon_overlay->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color); + icon_overlay->draw_rect_region(p_where, Rect2(p_pos, dsize), p_region, p_color); } } } @@ -2088,7 +2088,7 @@ void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, co int displayed_width = 0; if (p_cell.icon.is_valid()) { - displayed_width += icon_size.width + theme_cache.icon_h_separation; + displayed_width = MIN(displayed_width + icon_size.width + theme_cache.icon_h_separation, rect.size.width); } if (displayed_width + ts.width > rect.size.width) { ts.width = rect.size.width - displayed_width; @@ -2129,7 +2129,13 @@ void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, co } if (p_cell.icon.is_valid()) { - p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - icon_size.y) / 2)), icon_size, p_icon_color); + Point2 icon_pos = rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - icon_size.y) / 2)); + Rect2i icon_region = (p_cell.icon_region == Rect2i()) ? Rect2i(Point2(), p_cell.icon->get_size()) : p_cell.icon_region; + if (icon_size.width > rect.size.width) { + icon_region.size.width = icon_region.size.width * rect.size.width / icon_size.width; + icon_size.width = rect.size.width; + } + p_cell.draw_icon(ci, icon_pos, icon_size, icon_region, p_icon_color); rect.position.x += icon_size.x + theme_cache.icon_h_separation; } diff --git a/scene/gui/tree.h b/scene/gui/tree.h index e1664c618f..8a6c091642 100644 --- a/scene/gui/tree.h +++ b/scene/gui/tree.h @@ -132,7 +132,7 @@ private: } Size2 get_icon_size() const; - void draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size = Size2(), const Color &p_color = Color()) const; + void draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size = Size2(), const Rect2i &p_region = Rect2i(), const Color &p_color = Color()) const; }; mutable RID accessibility_row_element;