Merge pull request #87417 from AThousandShips/tree_clear

Use callable for `TreeItem` custom draw
This commit is contained in:
Yuri Sizov 2024-01-24 14:08:36 +01:00
commit a32a2eaedc
4 changed files with 54 additions and 10 deletions

View file

@ -160,6 +160,13 @@
Returns the custom color of column [param column].
</description>
</method>
<method name="get_custom_draw_callback" qualifiers="const">
<return type="Callable" />
<param index="0" name="column" type="int" />
<description>
Returns the custom callback of column [param column].
</description>
</method>
<method name="get_custom_font" qualifiers="const">
<return type="Font" />
<param index="0" name="column" type="int" />
@ -553,7 +560,7 @@
Sets the given column's custom color.
</description>
</method>
<method name="set_custom_draw">
<method name="set_custom_draw" is_deprecated="true">
<return type="void" />
<param index="0" name="column" type="int" />
<param index="1" name="object" type="Object" />
@ -561,6 +568,16 @@
<description>
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.
</description>
</method>
<method name="set_custom_draw_callback">
<return type="void" />
<param index="0" name="column" type="int" />
<param index="1" name="callback" type="Callable" />
<description>
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].
</description>
</method>
<method name="set_custom_font">

View file

@ -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"),

View file

@ -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) + ".");
}
}

View file

@ -99,8 +99,7 @@ private:
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 @@ public:
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();