فهرست منبع

Add custom `StyleBox` to `TreeItem`

DeeJayLSP 2 ماه پیش
والد
کامیت
b30b11b012
3فایلهای تغییر یافته به همراه45 افزوده شده و 0 حذف شده
  1. 17 0
      doc/classes/TreeItem.xml
  2. 24 0
      scene/gui/tree.cpp
  3. 4 0
      scene/gui/tree.h

+ 17 - 0
doc/classes/TreeItem.xml

@@ -203,6 +203,13 @@
 				Returns custom font size used to draw text in the column [param column].
 			</description>
 		</method>
+		<method name="get_custom_stylebox" qualifiers="const">
+			<return type="StyleBox" />
+			<param index="0" name="column" type="int" />
+			<description>
+				Returns the given column's custom [StyleBox] used to draw the background.
+			</description>
+		</method>
 		<method name="get_description" qualifiers="const">
 			<return type="String" />
 			<param index="0" name="column" type="int" />
@@ -614,6 +621,7 @@
 			<param index="2" name="just_outline" type="bool" default="false" />
 			<description>
 				Sets the given column's custom background color and whether to just use it as an outline.
+				[b]Note:[/b] If a custom [StyleBox] is set, the background color will be drawn behind it.
 			</description>
 		</method>
 		<method name="set_custom_color">
@@ -659,6 +667,15 @@
 				Sets custom font size used to draw text in the given [param column].
 			</description>
 		</method>
+		<method name="set_custom_stylebox">
+			<return type="void" />
+			<param index="0" name="column" type="int" />
+			<param index="1" name="stylebox" type="StyleBox" />
+			<description>
+				Sets the given column's custom [StyleBox] used to draw the background.
+				[b]Note:[/b] If a custom background color is set, the [StyleBox] will be drawn in front of it.
+			</description>
+		</method>
 		<method name="set_description">
 			<return type="void" />
 			<param index="0" name="column" type="int" />

+ 24 - 0
scene/gui/tree.cpp

@@ -878,6 +878,17 @@ int TreeItem::get_custom_minimum_height() const {
 	return custom_min_height;
 }
 
+void TreeItem::set_custom_stylebox(int p_column, const Ref<StyleBox> &p_stylebox) {
+	ERR_FAIL_INDEX(p_column, cells.size());
+	cells.write[p_column].custom_stylebox = p_stylebox;
+	_changed_notify(p_column);
+}
+
+Ref<StyleBox> TreeItem::get_custom_stylebox(int p_column) const {
+	ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<StyleBox>());
+	return cells[p_column].custom_stylebox;
+}
+
 TreeItem *TreeItem::create_child(int p_index) {
 	TreeItem *ti = memnew(TreeItem(tree));
 	if (tree) {
@@ -1843,6 +1854,9 @@ void TreeItem::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_custom_draw_callback", "column", "callback"), &TreeItem::set_custom_draw_callback);
 	ClassDB::bind_method(D_METHOD("get_custom_draw_callback", "column"), &TreeItem::get_custom_draw_callback);
 
+	ClassDB::bind_method(D_METHOD("set_custom_stylebox", "column", "stylebox"), &TreeItem::set_custom_stylebox);
+	ClassDB::bind_method(D_METHOD("get_custom_stylebox", "column"), &TreeItem::get_custom_stylebox);
+
 	ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
 	ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
 
@@ -2436,6 +2450,16 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 				}
 			}
 
+			if (p_item->cells[i].custom_stylebox.is_valid()) {
+				Rect2 r = cell_rect;
+				if (i == 0) {
+					r.position.x = p_draw_ofs.x;
+					r.size.x = item_width + ofs;
+				}
+				r = convert_rtl_rect(r);
+				draw_style_box(p_item->cells[i].custom_stylebox, r);
+			}
+
 			if (drop_mode_flags && drop_mode_over) {
 				Rect2 r = convert_rtl_rect(cell_rect);
 				if (drop_mode_over == p_item) {

+ 4 - 0
scene/gui/tree.h

@@ -98,6 +98,7 @@ private:
 		bool custom_button = false;
 		bool expand_right = false;
 		Color icon_color = Color(1, 1, 1);
+		Ref<StyleBox> custom_stylebox;
 
 		Size2i cached_minimum_size;
 		bool cached_minimum_size_dirty = true;
@@ -359,6 +360,9 @@ public:
 	void set_custom_minimum_height(int p_height);
 	int get_custom_minimum_height() const;
 
+	void set_custom_stylebox(int p_column, const Ref<StyleBox> &p_stylebox);
+	Ref<StyleBox> get_custom_stylebox(int p_column) const;
+
 	void set_selectable(int p_column, bool p_selectable);
 	bool is_selectable(int p_column) const;