From 257d03681cd9a61a67c6d69637aa87c45be71ebb Mon Sep 17 00:00:00 2001
From: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>
Date: Sat, 20 Jan 2024 17:55:34 +0100
Subject: [PATCH] Use callable for `TreeItem` custom draw
Deprecating old functionality
---
doc/classes/TreeItem.xml | 19 ++++++++++++++++++-
editor/find_in_files.cpp | 3 +--
scene/gui/tree.cpp | 35 ++++++++++++++++++++++++++++++-----
scene/gui/tree.h | 7 +++++--
4 files changed, 54 insertions(+), 10 deletions(-)
diff --git a/doc/classes/TreeItem.xml b/doc/classes/TreeItem.xml
index 597a363514af..3b8a4c8872ed 100644
--- a/doc/classes/TreeItem.xml
+++ b/doc/classes/TreeItem.xml
@@ -160,6 +160,13 @@
Returns the custom color of column [param column].
+
+
+
+
+ Returns the custom callback of column [param column].
+
+
@@ -553,7 +560,7 @@
Sets the given column's custom color.
-
+
@@ -561,6 +568,16 @@
Sets the given column's custom draw callback to [param callback] method on [param object].
The [param callback] should accept two arguments: the [TreeItem] that is drawn and its position and size as a [Rect2].
+ [i]Deprecated.[/i] Use [method TreeItem.set_custom_draw_callback] instead.
+
+
+
+
+
+
+
+ Sets the given column's custom draw callback. Use an empty [Callable] ([code skip-lint]Callable()[/code]) to clear the custom callback.
+ The [param callback] should accept two arguments: the [TreeItem] that is drawn and its position and size as a [Rect2].
diff --git a/editor/find_in_files.cpp b/editor/find_in_files.cpp
index c708e7771989..c6087f5b13f3 100644
--- a/editor/find_in_files.cpp
+++ b/editor/find_in_files.cpp
@@ -747,7 +747,7 @@ void FindInFilesPanel::_on_result_found(String fpath, int line_number, int begin
String start = vformat("%3s: ", line_number);
item->set_text(text_index, start + text);
- item->set_custom_draw(text_index, this, "_draw_result_text");
+ item->set_custom_draw_callback(text_index, callable_mp(this, &FindInFilesPanel::draw_result_text));
Result r;
r.line_number = line_number;
@@ -988,7 +988,6 @@ void FindInFilesPanel::set_progress_visible(bool p_visible) {
void FindInFilesPanel::_bind_methods() {
ClassDB::bind_method("_on_result_found", &FindInFilesPanel::_on_result_found);
ClassDB::bind_method("_on_finished", &FindInFilesPanel::_on_finished);
- ClassDB::bind_method("_draw_result_text", &FindInFilesPanel::draw_result_text);
ADD_SIGNAL(MethodInfo(SIGNAL_RESULT_SELECTED,
PropertyInfo(Variant::STRING, "path"),
diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp
index 11d5a01908dd..4ecbc173b7f7 100644
--- a/scene/gui/tree.cpp
+++ b/scene/gui/tree.cpp
@@ -584,16 +584,32 @@ 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) {
if (collapsed == p_collapsed || !tree) {
return;
@@ -1594,7 +1610,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);
@@ -2317,10 +2337,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) + ".");
}
}
diff --git a/scene/gui/tree.h b/scene/gui/tree.h
index 2dda408dd758..8ec003be9c7a 100644
--- a/scene/gui/tree.h
+++ b/scene/gui/tree.h
@@ -99,8 +99,7 @@ class TreeItem : public Object {
Variant meta;
String tooltip;
- ObjectID custom_draw_obj;
- StringName custom_draw_callback;
+ Callable custom_draw_callback;
struct Button {
int id = 0;
@@ -285,7 +284,11 @@ class TreeItem : public Object {
void set_metadata(int p_column, const Variant &p_meta);
Variant get_metadata(int p_column) const;
+#ifndef DISABLE_DEPRECATED
void set_custom_draw(int p_column, Object *p_object, const StringName &p_callback);
+#endif // DISABLE_DEPRECATED
+ void set_custom_draw_callback(int p_column, const Callable &p_callback);
+ Callable get_custom_draw_callback(int p_column) const;
void set_collapsed(bool p_collapsed);
bool is_collapsed();