Skip to content

Commit 6539819

Browse files
committed
Merge pull request #112371 from DeeJayLSP/treeitem-stylebox
Add custom `StyleBox` to `TreeItem`
2 parents a1e22a3 + b30b11b commit 6539819

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

doc/classes/TreeItem.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@
203203
Returns custom font size used to draw text in the column [param column].
204204
</description>
205205
</method>
206+
<method name="get_custom_stylebox" qualifiers="const">
207+
<return type="StyleBox" />
208+
<param index="0" name="column" type="int" />
209+
<description>
210+
Returns the given column's custom [StyleBox] used to draw the background.
211+
</description>
212+
</method>
206213
<method name="get_description" qualifiers="const">
207214
<return type="String" />
208215
<param index="0" name="column" type="int" />
@@ -614,6 +621,7 @@
614621
<param index="2" name="just_outline" type="bool" default="false" />
615622
<description>
616623
Sets the given column's custom background color and whether to just use it as an outline.
624+
[b]Note:[/b] If a custom [StyleBox] is set, the background color will be drawn behind it.
617625
</description>
618626
</method>
619627
<method name="set_custom_color">
@@ -659,6 +667,15 @@
659667
Sets custom font size used to draw text in the given [param column].
660668
</description>
661669
</method>
670+
<method name="set_custom_stylebox">
671+
<return type="void" />
672+
<param index="0" name="column" type="int" />
673+
<param index="1" name="stylebox" type="StyleBox" />
674+
<description>
675+
Sets the given column's custom [StyleBox] used to draw the background.
676+
[b]Note:[/b] If a custom background color is set, the [StyleBox] will be drawn in front of it.
677+
</description>
678+
</method>
662679
<method name="set_description">
663680
<return type="void" />
664681
<param index="0" name="column" type="int" />

scene/gui/tree.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,17 @@ int TreeItem::get_custom_minimum_height() const {
878878
return custom_min_height;
879879
}
880880

881+
void TreeItem::set_custom_stylebox(int p_column, const Ref<StyleBox> &p_stylebox) {
882+
ERR_FAIL_INDEX(p_column, cells.size());
883+
cells.write[p_column].custom_stylebox = p_stylebox;
884+
_changed_notify(p_column);
885+
}
886+
887+
Ref<StyleBox> TreeItem::get_custom_stylebox(int p_column) const {
888+
ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<StyleBox>());
889+
return cells[p_column].custom_stylebox;
890+
}
891+
881892
TreeItem *TreeItem::create_child(int p_index) {
882893
TreeItem *ti = memnew(TreeItem(tree));
883894
if (tree) {
@@ -1843,6 +1854,9 @@ void TreeItem::_bind_methods() {
18431854
ClassDB::bind_method(D_METHOD("set_custom_draw_callback", "column", "callback"), &TreeItem::set_custom_draw_callback);
18441855
ClassDB::bind_method(D_METHOD("get_custom_draw_callback", "column"), &TreeItem::get_custom_draw_callback);
18451856

1857+
ClassDB::bind_method(D_METHOD("set_custom_stylebox", "column", "stylebox"), &TreeItem::set_custom_stylebox);
1858+
ClassDB::bind_method(D_METHOD("get_custom_stylebox", "column"), &TreeItem::get_custom_stylebox);
1859+
18461860
ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
18471861
ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
18481862

@@ -2436,6 +2450,16 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
24362450
}
24372451
}
24382452

2453+
if (p_item->cells[i].custom_stylebox.is_valid()) {
2454+
Rect2 r = cell_rect;
2455+
if (i == 0) {
2456+
r.position.x = p_draw_ofs.x;
2457+
r.size.x = item_width + ofs;
2458+
}
2459+
r = convert_rtl_rect(r);
2460+
draw_style_box(p_item->cells[i].custom_stylebox, r);
2461+
}
2462+
24392463
if (drop_mode_flags && drop_mode_over) {
24402464
Rect2 r = convert_rtl_rect(cell_rect);
24412465
if (drop_mode_over == p_item) {

scene/gui/tree.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class TreeItem : public Object {
9898
bool custom_button = false;
9999
bool expand_right = false;
100100
Color icon_color = Color(1, 1, 1);
101+
Ref<StyleBox> custom_stylebox;
101102

102103
Size2i cached_minimum_size;
103104
bool cached_minimum_size_dirty = true;
@@ -359,6 +360,9 @@ class TreeItem : public Object {
359360
void set_custom_minimum_height(int p_height);
360361
int get_custom_minimum_height() const;
361362

363+
void set_custom_stylebox(int p_column, const Ref<StyleBox> &p_stylebox);
364+
Ref<StyleBox> get_custom_stylebox(int p_column) const;
365+
362366
void set_selectable(int p_column, bool p_selectable);
363367
bool is_selectable(int p_column) const;
364368

0 commit comments

Comments
 (0)