Made print_tree_pretty() function which displays scene tree graphically

(cherry picked from commit 8362ce4769)
This commit is contained in:
Geoffrey 2018-02-28 10:12:06 +01:00 committed by Hein-Pieter van Braam
parent 710b57d493
commit 5d7b7f798a
3 changed files with 44 additions and 5 deletions

View File

@ -438,7 +438,30 @@
<return type="void">
</return>
<description>
Prints the scene to stdout. Used mainly for debugging purposes.
Prints the tree to stdout. Used mainly for debugging purposes. This version displays the path relative to the current node, and is good for copy/pasting into the [method get_node] function. Example output:
[codeblock]
TheGame
TheGame/Menu
TheGame/Menu/Label
TheGame/Menu/Camera2D
TheGame/SplashScreen
TheGame/SplashScreen/Camera2D
[/codeblock]
</description>
</method>
<method name="print_tree_pretty">
<return type="void">
</return>
<description>
Similar to [method print_tree], this prints the tree to stdout. This version displays a more graphical representation similar to what is displayed in the scene inspector. It is useful for inspecting larger trees. Example output:
[codeblock]
┖╴TheGame
┠╴Menu
┃ ┠╴Label
┃ ┖╴Camera2D
┖-SplashScreen
┖╴Camera2D
[/codeblock]
</description>
</method>
<method name="propagate_call">

View File

@ -1868,11 +1868,18 @@ bool Node::has_persistent_groups() const {
return false;
}
void Node::_print_tree(const Node *p_node) {
void Node::_print_tree_pretty(const String prefix, const bool last) {
print_line(String(p_node->get_path_to(this)));
for (int i = 0; i < data.children.size(); i++)
data.children[i]->_print_tree(p_node);
String new_prefix = last ? String::utf8(" ┖╴") : String::utf8(" ┠╴");
print_line(prefix + new_prefix + String(get_name()));
for (int i = 0; i < data.children.size(); i++) {
new_prefix = last ? String::utf8(" ") : String::utf8("");
data.children[i]->_print_tree_pretty(prefix + new_prefix, i == data.children.size() - 1);
}
}
void Node::print_tree_pretty() {
_print_tree_pretty("", true);
}
void Node::print_tree() {
@ -1880,6 +1887,12 @@ void Node::print_tree() {
_print_tree(this);
}
void Node::_print_tree(const Node *p_node) {
print_line(String(p_node->get_path_to(this)));
for (int i = 0; i < data.children.size(); i++)
data.children[i]->_print_tree(p_node);
}
void Node::_propagate_reverse_notification(int p_notification) {
data.blocked++;
@ -2837,6 +2850,7 @@ void Node::_bind_methods() {
ClassDB::bind_method(D_METHOD("remove_and_skip"), &Node::remove_and_skip);
ClassDB::bind_method(D_METHOD("get_index"), &Node::get_index);
ClassDB::bind_method(D_METHOD("print_tree"), &Node::print_tree);
ClassDB::bind_method(D_METHOD("print_tree_pretty"), &Node::print_tree_pretty);
ClassDB::bind_method(D_METHOD("set_filename", "filename"), &Node::set_filename);
ClassDB::bind_method(D_METHOD("get_filename"), &Node::get_filename);
ClassDB::bind_method(D_METHOD("propagate_notification", "what"), &Node::propagate_notification);

View File

@ -151,6 +151,7 @@ private:
NAME_CASING_SNAKE_CASE
};
void _print_tree_pretty(const String prefix, const bool last);
void _print_tree(const Node *p_node);
Node *_get_node(const NodePath &p_path) const;
@ -287,6 +288,7 @@ public:
int get_index() const;
void print_tree();
void print_tree_pretty();
void set_filename(const String &p_filename);
String get_filename() const;