Skip to content

Commit

Permalink
Merge pull request #87417 from AThousandShips/tree_clear
Browse files Browse the repository at this point in the history
Use callable for `TreeItem` custom draw
  • Loading branch information
YuriSizov committed Jan 24, 2024
2 parents d454fcc + 257d036 commit a32a2ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
19 changes: 18 additions & 1 deletion doc/classes/TreeItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -553,14 +560,24 @@
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" />
<param index="2" name="callback" type="StringName" />
<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">
Expand Down
3 changes: 1 addition & 2 deletions editor/find_in_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down
35 changes: 30 additions & 5 deletions scene/gui/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) + ".");
}
}

Expand Down
7 changes: 5 additions & 2 deletions scene/gui/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a32a2ea

Please sign in to comment.