summaryrefslogtreecommitdiffstats
path: root/scene/gui/tree.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/tree.cpp')
-rw-r--r--scene/gui/tree.cpp72
1 files changed, 62 insertions, 10 deletions
diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp
index c67c3cd98d..10f4962c48 100644
--- a/scene/gui/tree.cpp
+++ b/scene/gui/tree.cpp
@@ -584,12 +584,30 @@ Variant TreeItem::get_metadata(int p_column) const {
return cells[p_column].meta;
}
+#ifndef DISABLE_DEPRECATED
void TreeItem::set_custom_draw(int p_column, Object *p_object, const StringName &p_callback) {
+ WARN_DEPRECATED_MSG(R"*(The "set_custom_draw()" method is deprecated, use "set_custom_draw_callback()" instead.)*");
ERR_FAIL_INDEX(p_column, cells.size());
ERR_FAIL_NULL(p_object);
- cells.write[p_column].custom_draw_obj = p_object->get_instance_id();
+ cells.write[p_column].custom_draw_callback = Callable(p_object, p_callback);
+
+ _changed_notify(p_column);
+}
+#endif // DISABLE_DEPRECATED
+
+void TreeItem::set_custom_draw_callback(int p_column, const Callable &p_callback) {
+ ERR_FAIL_INDEX(p_column, cells.size());
+
cells.write[p_column].custom_draw_callback = p_callback;
+
+ _changed_notify(p_column);
+}
+
+Callable TreeItem::get_custom_draw_callback(int p_column) const {
+ ERR_FAIL_INDEX_V(p_column, cells.size(), Callable());
+
+ return cells[p_column].custom_draw_callback;
}
void TreeItem::set_collapsed(bool p_collapsed) {
@@ -850,7 +868,7 @@ TreeItem *TreeItem::_get_prev_in_tree(bool p_wrap, bool p_include_invisible) {
} else if (!current) {
if (p_wrap) {
current = this;
- TreeItem *temp = this->get_next_visible();
+ TreeItem *temp = get_next_visible();
while (temp) {
current = temp;
temp = temp->get_next_visible();
@@ -876,7 +894,7 @@ TreeItem *TreeItem::_get_prev_in_tree(bool p_wrap, bool p_include_invisible) {
TreeItem *TreeItem::get_prev_visible(bool p_wrap) {
TreeItem *loop = this;
- TreeItem *prev_item = this->_get_prev_in_tree(p_wrap);
+ TreeItem *prev_item = _get_prev_in_tree(p_wrap);
while (prev_item && !prev_item->is_visible()) {
prev_item = prev_item->_get_prev_in_tree(p_wrap);
if (prev_item == loop) {
@@ -917,7 +935,7 @@ TreeItem *TreeItem::_get_next_in_tree(bool p_wrap, bool p_include_invisible) {
TreeItem *TreeItem::get_next_visible(bool p_wrap) {
TreeItem *loop = this;
- TreeItem *next_item = this->_get_next_in_tree(p_wrap);
+ TreeItem *next_item = _get_next_in_tree(p_wrap);
while (next_item && !next_item->is_visible()) {
next_item = next_item->_get_next_in_tree(p_wrap);
if (next_item == loop) {
@@ -930,12 +948,12 @@ TreeItem *TreeItem::get_next_visible(bool p_wrap) {
}
TreeItem *TreeItem::get_prev_in_tree(bool p_wrap) {
- TreeItem *prev_item = this->_get_prev_in_tree(p_wrap, true);
+ TreeItem *prev_item = _get_prev_in_tree(p_wrap, true);
return prev_item;
}
TreeItem *TreeItem::get_next_in_tree(bool p_wrap) {
- TreeItem *next_item = this->_get_next_in_tree(p_wrap, true);
+ TreeItem *next_item = _get_next_in_tree(p_wrap, true);
return next_item;
}
@@ -1204,6 +1222,12 @@ int TreeItem::get_button_by_id(int p_column, int p_id) const {
return -1;
}
+Color TreeItem::get_button_color(int p_column, int p_index) const {
+ ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
+ ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), Color());
+ return cells[p_column].buttons[p_index].color;
+}
+
void TreeItem::set_button_tooltip_text(int p_column, int p_index, const String &p_tooltip) {
ERR_FAIL_INDEX(p_column, cells.size());
ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
@@ -1306,8 +1330,14 @@ void TreeItem::clear_custom_color(int p_column) {
void TreeItem::set_custom_font(int p_column, const Ref<Font> &p_font) {
ERR_FAIL_INDEX(p_column, cells.size());
+ if (cells[p_column].custom_font == p_font) {
+ return;
+ }
+
cells.write[p_column].custom_font = p_font;
cells.write[p_column].cached_minimum_size_dirty = true;
+
+ _changed_notify(p_column);
}
Ref<Font> TreeItem::get_custom_font(int p_column) const {
@@ -1318,8 +1348,14 @@ Ref<Font> TreeItem::get_custom_font(int p_column) const {
void TreeItem::set_custom_font_size(int p_column, int p_font_size) {
ERR_FAIL_INDEX(p_column, cells.size());
+ if (cells[p_column].custom_font_size == p_font_size) {
+ return;
+ }
+
cells.write[p_column].custom_font_size = p_font_size;
cells.write[p_column].cached_minimum_size_dirty = true;
+
+ _changed_notify(p_column);
}
int TreeItem::get_custom_font_size(int p_column) const {
@@ -1368,8 +1404,14 @@ Color TreeItem::get_custom_bg_color(int p_column) const {
void TreeItem::set_custom_as_button(int p_column, bool p_button) {
ERR_FAIL_INDEX(p_column, cells.size());
+ if (cells[p_column].custom_button == p_button) {
+ return;
+ }
+
cells.write[p_column].custom_button = p_button;
cells.write[p_column].cached_minimum_size_dirty = true;
+
+ _changed_notify(p_column);
}
bool TreeItem::is_custom_set_as_button(int p_column) const {
@@ -1574,7 +1616,11 @@ void TreeItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata);
ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata);
+#ifndef DISABLE_DEPRECATED
ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw);
+#endif // DISABLE_DEPRECATED
+ 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_collapsed", "enable"), &TreeItem::set_collapsed);
ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
@@ -1622,6 +1668,7 @@ void TreeItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_button_tooltip_text", "column", "button_index"), &TreeItem::get_button_tooltip_text);
ClassDB::bind_method(D_METHOD("get_button_id", "column", "button_index"), &TreeItem::get_button_id);
ClassDB::bind_method(D_METHOD("get_button_by_id", "column", "id"), &TreeItem::get_button_by_id);
+ ClassDB::bind_method(D_METHOD("get_button_color", "column", "id"), &TreeItem::get_button_color);
ClassDB::bind_method(D_METHOD("get_button", "column", "button_index"), &TreeItem::get_button);
ClassDB::bind_method(D_METHOD("set_button_tooltip_text", "column", "button_index", "tooltip"), &TreeItem::set_button_tooltip_text);
ClassDB::bind_method(D_METHOD("set_button", "column", "button_index", "button"), &TreeItem::set_button);
@@ -2297,10 +2344,15 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
} break;
case TreeItem::CELL_MODE_CUSTOM: {
- if (p_item->cells[i].custom_draw_obj.is_valid()) {
- Object *cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj);
- if (cdo) {
- cdo->call(p_item->cells[i].custom_draw_callback, p_item, Rect2(item_rect));
+ if (p_item->cells[i].custom_draw_callback.is_valid()) {
+ Variant args[] = { p_item, Rect2(item_rect) };
+ const Variant *argptrs[] = { &args[0], &args[1] };
+
+ Callable::CallError ce;
+ Variant ret;
+ p_item->cells[i].custom_draw_callback.callp(argptrs, 2, ret, ce);
+ if (ce.error != Callable::CallError::CALL_OK) {
+ ERR_PRINT("Error calling custom draw method: " + Variant::get_callable_error_text(p_item->cells[i].custom_draw_callback, argptrs, 2, ce) + ".");
}
}