editor_debugger_tree.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*************************************************************************/
  2. /* editor_debugger_tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_debugger_tree.h"
  31. #include "editor/editor_node.h"
  32. #include "scene/debugger/scene_debugger.h"
  33. #include "scene/resources/packed_scene.h"
  34. #include "servers/display_server.h"
  35. EditorDebuggerTree::EditorDebuggerTree() {
  36. set_v_size_flags(SIZE_EXPAND_FILL);
  37. set_allow_rmb_select(true);
  38. // Popup
  39. item_menu = memnew(PopupMenu);
  40. item_menu->connect("id_pressed", callable_mp(this, &EditorDebuggerTree::_item_menu_id_pressed));
  41. add_child(item_menu);
  42. // File Dialog
  43. file_dialog = memnew(EditorFileDialog);
  44. file_dialog->connect("file_selected", callable_mp(this, &EditorDebuggerTree::_file_selected));
  45. add_child(file_dialog);
  46. }
  47. void EditorDebuggerTree::_notification(int p_what) {
  48. if (p_what == NOTIFICATION_POSTINITIALIZE) {
  49. connect("cell_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_selected));
  50. connect("item_collapsed", callable_mp(this, &EditorDebuggerTree::_scene_tree_folded));
  51. connect("item_rmb_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_rmb_selected));
  52. }
  53. }
  54. void EditorDebuggerTree::_bind_methods() {
  55. ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::INT, "debugger")));
  56. ADD_SIGNAL(MethodInfo("save_node", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "filename"), PropertyInfo(Variant::INT, "debugger")));
  57. }
  58. void EditorDebuggerTree::_scene_tree_selected() {
  59. if (updating_scene_tree) {
  60. return;
  61. }
  62. TreeItem *item = get_selected();
  63. if (!item) {
  64. return;
  65. }
  66. inspected_object_id = uint64_t(item->get_metadata(0));
  67. emit_signal("object_selected", inspected_object_id, debugger_id);
  68. }
  69. void EditorDebuggerTree::_scene_tree_folded(Object *p_obj) {
  70. if (updating_scene_tree) {
  71. return;
  72. }
  73. TreeItem *item = Object::cast_to<TreeItem>(p_obj);
  74. if (!item)
  75. return;
  76. ObjectID id = ObjectID(uint64_t(item->get_metadata(0)));
  77. if (unfold_cache.has(id)) {
  78. unfold_cache.erase(id);
  79. } else {
  80. unfold_cache.insert(id);
  81. }
  82. }
  83. void EditorDebuggerTree::_scene_tree_rmb_selected(const Vector2 &p_position) {
  84. TreeItem *item = get_item_at_position(p_position);
  85. if (!item)
  86. return;
  87. item->select(0);
  88. item_menu->clear();
  89. item_menu->add_icon_item(get_icon("CreateNewSceneFrom", "EditorIcons"), TTR("Save Branch as Scene"), ITEM_MENU_SAVE_REMOTE_NODE);
  90. item_menu->add_icon_item(get_icon("CopyNodePath", "EditorIcons"), TTR("Copy Node Path"), ITEM_MENU_COPY_NODE_PATH);
  91. item_menu->set_global_position(get_global_mouse_position());
  92. item_menu->popup();
  93. }
  94. /// Populates inspect_scene_tree given data in nodes as a flat list, encoded depth first.
  95. ///
  96. /// Given a nodes array like [R,A,B,C,D,E] the following Tree will be generated, assuming
  97. /// filter is an empty String, R and A child count are 2, B is 1 and C, D and E are 0.
  98. ///
  99. /// R
  100. /// |-A
  101. /// | |-B
  102. /// | | |-C
  103. /// | |
  104. /// | |-D
  105. /// |
  106. /// |-E
  107. ///
  108. void EditorDebuggerTree::update_scene_tree(const SceneDebuggerTree *p_tree, int p_debugger) {
  109. updating_scene_tree = true;
  110. const String last_path = get_selected_path();
  111. const String filter = EditorNode::get_singleton()->get_scene_tree_dock()->get_filter();
  112. // Nodes are in a flatten list, depth first. Use a stack of parents, avoid recursion.
  113. List<Pair<TreeItem *, int>> parents;
  114. for (int i = 0; i < p_tree->nodes.size(); i++) {
  115. TreeItem *parent = NULL;
  116. if (parents.size()) { // Find last parent.
  117. Pair<TreeItem *, int> &p = parents[0];
  118. parent = p.first;
  119. if (!(--p.second)) { // If no child left, remove it.
  120. parents.pop_front();
  121. }
  122. }
  123. // Add this node.
  124. const SceneDebuggerTree::RemoteNode &node = p_tree->nodes[i];
  125. TreeItem *item = create_item(parent);
  126. item->set_text(0, node.name);
  127. item->set_tooltip(0, TTR("Type:") + " " + node.type_name);
  128. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(node.type_name, "");
  129. if (icon.is_valid()) {
  130. item->set_icon(0, icon);
  131. }
  132. item->set_metadata(0, node.id);
  133. // Set current item as collapsed if necessary (root is never collapsed)
  134. if (parent) {
  135. if (!unfold_cache.has(node.id)) {
  136. item->set_collapsed(true);
  137. }
  138. }
  139. // Select previously selected node.
  140. if (debugger_id == p_debugger) { // Can use remote id.
  141. if (node.id == inspected_object_id) {
  142. item->select(0);
  143. }
  144. } else { // Must use path
  145. if (last_path == _get_path(item)) {
  146. updating_scene_tree = false; // Force emission of new selection
  147. item->select(0);
  148. updating_scene_tree = true;
  149. }
  150. }
  151. // Add in front of the parents stack if children are expected.
  152. if (node.child_count) {
  153. parents.push_front(Pair<TreeItem *, int>(item, node.child_count));
  154. } else {
  155. // Apply filters.
  156. while (parent) {
  157. const bool had_siblings = item->get_prev() || item->get_next();
  158. if (filter.is_subsequence_ofi(item->get_text(0)))
  159. break; // Filter matches, must survive.
  160. parent->remove_child(item);
  161. memdelete(item);
  162. if (had_siblings)
  163. break; // Parent must survive.
  164. item = parent;
  165. parent = item->get_parent();
  166. // Check if parent expects more children.
  167. for (int j = 0; j < parents.size(); j++) {
  168. if (parents[j].first == item) {
  169. parent = NULL;
  170. break; // Might have more children.
  171. }
  172. }
  173. }
  174. }
  175. }
  176. debugger_id = p_debugger; // Needed by hook, could be avoided if every debugger had its own tree
  177. updating_scene_tree = false;
  178. }
  179. String EditorDebuggerTree::get_selected_path() {
  180. if (!get_selected())
  181. return "";
  182. return _get_path(get_selected());
  183. }
  184. String EditorDebuggerTree::_get_path(TreeItem *p_item) {
  185. ERR_FAIL_COND_V(!p_item, "");
  186. if (p_item->get_parent() == NULL) {
  187. return "/root";
  188. }
  189. String text = p_item->get_text(0);
  190. TreeItem *cur = p_item->get_parent();
  191. while (cur) {
  192. text = cur->get_text(0) + "/" + text;
  193. cur = cur->get_parent();
  194. }
  195. return "/" + text;
  196. }
  197. void EditorDebuggerTree::_item_menu_id_pressed(int p_option) {
  198. switch (p_option) {
  199. case ITEM_MENU_SAVE_REMOTE_NODE: {
  200. file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  201. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  202. List<String> extensions;
  203. Ref<PackedScene> sd = memnew(PackedScene);
  204. ResourceSaver::get_recognized_extensions(sd, &extensions);
  205. file_dialog->clear_filters();
  206. for (int i = 0; i < extensions.size(); i++) {
  207. file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  208. }
  209. file_dialog->popup_centered_ratio();
  210. } break;
  211. case ITEM_MENU_COPY_NODE_PATH: {
  212. String text = get_selected_path();
  213. if (text.empty()) {
  214. return;
  215. } else if (text == "/root") {
  216. text = ".";
  217. } else {
  218. text = text.replace("/root/", "");
  219. int slash = text.find("/");
  220. if (slash < 0) {
  221. text = ".";
  222. } else {
  223. text = text.substr(slash + 1);
  224. }
  225. }
  226. DisplayServer::get_singleton()->clipboard_set(text);
  227. } break;
  228. }
  229. }
  230. void EditorDebuggerTree::_file_selected(const String &p_file) {
  231. if (inspected_object_id.is_null())
  232. return;
  233. emit_signal("save_node", inspected_object_id, p_file, debugger_id);
  234. }