summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2019-10-04 15:32:14 +0200
committerGitHub <noreply@github.com>2019-10-04 15:32:14 +0200
commitee611d149b4b126e7edc4754f87953e3c3fd8c98 (patch)
treeb17ce6368cbb264bd23de66d3c08fd8c72731b88
parentb177051521631f77092dc2513873afbd7015d4bb (diff)
parent306e64a9f58f30a29f256a20ceccafd1ff7d027c (diff)
Merge pull request #22834 from lupoDharkael/recursive-item
Add call_recursive method to TreeItem
-rw-r--r--doc/classes/TreeItem.xml9
-rw-r--r--scene/gui/tree.cpp45
-rw-r--r--scene/gui/tree.h4
3 files changed, 58 insertions, 0 deletions
diff --git a/doc/classes/TreeItem.xml b/doc/classes/TreeItem.xml
index 1ab5c58a30..01d690589b 100644
--- a/doc/classes/TreeItem.xml
+++ b/doc/classes/TreeItem.xml
@@ -585,6 +585,15 @@
Sets the given column's tooltip text.
</description>
</method>
+ <method name="call_recursive" qualifiers="vararg">
+ <return type="Variant">
+ </return>
+ <argument index="0" name="method" type="String">
+ </argument>
+ <description>
+ Calls the [code]method[/code] on the actual TreeItem and its children recursively. Pass parameters as a comma separated list.
+ </description>
+ </method>
</methods>
<members>
<member name="collapsed" type="bool" setter="set_collapsed" getter="is_collapsed">
diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp
index 50b0b12029..1b52796dc7 100644
--- a/scene/gui/tree.cpp
+++ b/scene/gui/tree.cpp
@@ -729,6 +729,43 @@ bool TreeItem::is_folding_disabled() const {
return disable_folding;
}
+Variant TreeItem::_call_recursive_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
+
+ if (p_argcount < 1) {
+ r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
+ r_error.argument = 0;
+ return Variant();
+ }
+
+ if (p_args[0]->get_type() != Variant::STRING) {
+ r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
+ r_error.argument = 0;
+ r_error.expected = Variant::STRING;
+ return Variant();
+ }
+
+ StringName method = *p_args[0];
+
+ call_recursive(method, &p_args[1], p_argcount - 1, r_error);
+ return Variant();
+}
+
+void recursive_call_aux(TreeItem *p_item, const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
+ if (!p_item) {
+ return;
+ }
+ p_item->call(p_method, p_args, p_argcount, r_error);
+ TreeItem *c = p_item->get_children();
+ while (c) {
+ recursive_call_aux(c, p_method, p_args, p_argcount, r_error);
+ c = c->get_next();
+ }
+}
+
+void TreeItem::call_recursive(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
+ recursive_call_aux(this, p_method, p_args, p_argcount, r_error);
+}
+
void TreeItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
@@ -820,6 +857,14 @@ void TreeItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
+ {
+ MethodInfo mi;
+ mi.name = "call_recursive";
+ mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
+
+ ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_recursive", &TreeItem::_call_recursive_bind, mi);
+ }
+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
diff --git a/scene/gui/tree.h b/scene/gui/tree.h
index 47befb0c15..361830173b 100644
--- a/scene/gui/tree.h
+++ b/scene/gui/tree.h
@@ -172,6 +172,8 @@ protected:
remove_child(Object::cast_to<TreeItem>(p_child));
}
+ Variant _call_recursive_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error);
+
public:
/* cell mode */
void set_cell_mode(int p_column, TreeCellMode p_mode);
@@ -280,6 +282,8 @@ public:
void set_disable_folding(bool p_disable);
bool is_folding_disabled() const;
+ void call_recursive(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
+
~TreeItem();
};