editor_debugger_tree.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_file_dialog.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/scene_tree_dock.h"
  34. #include "scene/debugger/scene_debugger.h"
  35. #include "scene/resources/packed_scene.h"
  36. #include "servers/display_server.h"
  37. EditorDebuggerTree::EditorDebuggerTree() {
  38. set_v_size_flags(SIZE_EXPAND_FILL);
  39. set_allow_rmb_select(true);
  40. // Popup
  41. item_menu = memnew(PopupMenu);
  42. item_menu->connect("id_pressed", callable_mp(this, &EditorDebuggerTree::_item_menu_id_pressed));
  43. add_child(item_menu);
  44. // File Dialog
  45. file_dialog = memnew(EditorFileDialog);
  46. file_dialog->connect("file_selected", callable_mp(this, &EditorDebuggerTree::_file_selected));
  47. add_child(file_dialog);
  48. }
  49. void EditorDebuggerTree::_notification(int p_what) {
  50. switch (p_what) {
  51. case NOTIFICATION_POSTINITIALIZE: {
  52. connect("cell_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_selected));
  53. connect("item_collapsed", callable_mp(this, &EditorDebuggerTree::_scene_tree_folded));
  54. connect("item_mouse_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_rmb_selected));
  55. } break;
  56. }
  57. }
  58. void EditorDebuggerTree::_bind_methods() {
  59. ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::INT, "debugger")));
  60. ADD_SIGNAL(MethodInfo("save_node", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "filename"), PropertyInfo(Variant::INT, "debugger")));
  61. }
  62. void EditorDebuggerTree::_scene_tree_selected() {
  63. if (updating_scene_tree) {
  64. return;
  65. }
  66. TreeItem *item = get_selected();
  67. if (!item) {
  68. return;
  69. }
  70. inspected_object_id = uint64_t(item->get_metadata(0));
  71. emit_signal(SNAME("object_selected"), inspected_object_id, debugger_id);
  72. }
  73. void EditorDebuggerTree::_scene_tree_folded(Object *p_obj) {
  74. if (updating_scene_tree) {
  75. return;
  76. }
  77. TreeItem *item = Object::cast_to<TreeItem>(p_obj);
  78. if (!item) {
  79. return;
  80. }
  81. ObjectID id = ObjectID(uint64_t(item->get_metadata(0)));
  82. if (unfold_cache.has(id)) {
  83. unfold_cache.erase(id);
  84. } else {
  85. unfold_cache.insert(id);
  86. }
  87. }
  88. void EditorDebuggerTree::_scene_tree_rmb_selected(const Vector2 &p_position, MouseButton p_button) {
  89. if (p_button != MouseButton::RIGHT) {
  90. return;
  91. }
  92. TreeItem *item = get_item_at_position(p_position);
  93. if (!item) {
  94. return;
  95. }
  96. item->select(0);
  97. item_menu->clear();
  98. item_menu->add_icon_item(get_theme_icon(SNAME("CreateNewSceneFrom"), SNAME("EditorIcons")), TTR("Save Branch as Scene"), ITEM_MENU_SAVE_REMOTE_NODE);
  99. item_menu->add_icon_item(get_theme_icon(SNAME("CopyNodePath"), SNAME("EditorIcons")), TTR("Copy Node Path"), ITEM_MENU_COPY_NODE_PATH);
  100. item_menu->set_position(get_screen_position() + get_local_mouse_position());
  101. item_menu->popup();
  102. }
  103. /// Populates inspect_scene_tree given data in nodes as a flat list, encoded depth first.
  104. ///
  105. /// Given a nodes array like [R,A,B,C,D,E] the following Tree will be generated, assuming
  106. /// filter is an empty String, R and A child count are 2, B is 1 and C, D and E are 0.
  107. ///
  108. /// R
  109. /// |-A
  110. /// | |-B
  111. /// | | |-C
  112. /// | |
  113. /// | |-D
  114. /// |
  115. /// |-E
  116. ///
  117. void EditorDebuggerTree::update_scene_tree(const SceneDebuggerTree *p_tree, int p_debugger) {
  118. updating_scene_tree = true;
  119. const String last_path = get_selected_path();
  120. const String filter = SceneTreeDock::get_singleton()->get_filter();
  121. bool filter_changed = filter != last_filter;
  122. TreeItem *scroll_item = nullptr;
  123. // Nodes are in a flatten list, depth first. Use a stack of parents, avoid recursion.
  124. List<Pair<TreeItem *, int>> parents;
  125. for (int i = 0; i < p_tree->nodes.size(); i++) {
  126. TreeItem *parent = nullptr;
  127. if (parents.size()) { // Find last parent.
  128. Pair<TreeItem *, int> &p = parents[0];
  129. parent = p.first;
  130. if (!(--p.second)) { // If no child left, remove it.
  131. parents.pop_front();
  132. }
  133. }
  134. // Add this node.
  135. const SceneDebuggerTree::RemoteNode &node = p_tree->nodes[i];
  136. TreeItem *item = create_item(parent);
  137. item->set_text(0, node.name);
  138. item->set_tooltip_text(0, TTR("Type:") + " " + node.type_name);
  139. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(node.type_name, "");
  140. if (icon.is_valid()) {
  141. item->set_icon(0, icon);
  142. }
  143. item->set_metadata(0, node.id);
  144. // Set current item as collapsed if necessary (root is never collapsed)
  145. if (parent) {
  146. if (!unfold_cache.has(node.id)) {
  147. item->set_collapsed(true);
  148. }
  149. }
  150. // Select previously selected node.
  151. if (debugger_id == p_debugger) { // Can use remote id.
  152. if (node.id == inspected_object_id) {
  153. item->select(0);
  154. if (filter_changed) {
  155. scroll_item = item;
  156. }
  157. }
  158. } else { // Must use path
  159. if (last_path == _get_path(item)) {
  160. updating_scene_tree = false; // Force emission of new selection
  161. item->select(0);
  162. if (filter_changed) {
  163. scroll_item = item;
  164. }
  165. updating_scene_tree = true;
  166. }
  167. }
  168. // Add in front of the parents stack if children are expected.
  169. if (node.child_count) {
  170. parents.push_front(Pair<TreeItem *, int>(item, node.child_count));
  171. } else {
  172. // Apply filters.
  173. while (parent) {
  174. const bool had_siblings = item->get_prev() || item->get_next();
  175. if (filter.is_subsequence_ofn(item->get_text(0))) {
  176. break; // Filter matches, must survive.
  177. }
  178. parent->remove_child(item);
  179. memdelete(item);
  180. if (scroll_item == item) {
  181. scroll_item = nullptr;
  182. }
  183. if (had_siblings) {
  184. break; // Parent must survive.
  185. }
  186. item = parent;
  187. parent = item->get_parent();
  188. // Check if parent expects more children.
  189. for (int j = 0; j < parents.size(); j++) {
  190. if (parents[j].first == item) {
  191. parent = nullptr;
  192. break; // Might have more children.
  193. }
  194. }
  195. }
  196. }
  197. }
  198. debugger_id = p_debugger; // Needed by hook, could be avoided if every debugger had its own tree
  199. if (scroll_item) {
  200. call_deferred(SNAME("scroll_to_item"), scroll_item);
  201. }
  202. last_filter = filter;
  203. updating_scene_tree = false;
  204. }
  205. Variant EditorDebuggerTree::get_drag_data(const Point2 &p_point) {
  206. if (get_button_id_at_position(p_point) != -1) {
  207. return Variant();
  208. }
  209. TreeItem *selected = get_selected();
  210. if (!selected) {
  211. return Variant();
  212. }
  213. String path = selected->get_text(0);
  214. HBoxContainer *hb = memnew(HBoxContainer);
  215. TextureRect *tf = memnew(TextureRect);
  216. tf->set_texture(selected->get_icon(0));
  217. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  218. hb->add_child(tf);
  219. Label *label = memnew(Label(path));
  220. hb->add_child(label);
  221. set_drag_preview(hb);
  222. if (!selected->get_parent() || !selected->get_parent()->get_parent()) {
  223. path = ".";
  224. } else {
  225. while (selected->get_parent()->get_parent() != get_root()) {
  226. selected = selected->get_parent();
  227. path = selected->get_text(0) + "/" + path;
  228. }
  229. }
  230. return vformat("\"%s\"", path);
  231. }
  232. String EditorDebuggerTree::get_selected_path() {
  233. if (!get_selected()) {
  234. return "";
  235. }
  236. return _get_path(get_selected());
  237. }
  238. String EditorDebuggerTree::_get_path(TreeItem *p_item) {
  239. ERR_FAIL_COND_V(!p_item, "");
  240. if (p_item->get_parent() == nullptr) {
  241. return "/root";
  242. }
  243. String text = p_item->get_text(0);
  244. TreeItem *cur = p_item->get_parent();
  245. while (cur) {
  246. text = cur->get_text(0) + "/" + text;
  247. cur = cur->get_parent();
  248. }
  249. return "/" + text;
  250. }
  251. void EditorDebuggerTree::_item_menu_id_pressed(int p_option) {
  252. switch (p_option) {
  253. case ITEM_MENU_SAVE_REMOTE_NODE: {
  254. file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  255. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  256. List<String> extensions;
  257. Ref<PackedScene> sd = memnew(PackedScene);
  258. ResourceSaver::get_recognized_extensions(sd, &extensions);
  259. file_dialog->clear_filters();
  260. for (int i = 0; i < extensions.size(); i++) {
  261. file_dialog->add_filter("*." + extensions[i], extensions[i].to_upper());
  262. }
  263. file_dialog->popup_file_dialog();
  264. } break;
  265. case ITEM_MENU_COPY_NODE_PATH: {
  266. String text = get_selected_path();
  267. if (text.is_empty()) {
  268. return;
  269. } else if (text == "/root") {
  270. text = ".";
  271. } else {
  272. text = text.replace("/root/", "");
  273. int slash = text.find("/");
  274. if (slash < 0) {
  275. text = ".";
  276. } else {
  277. text = text.substr(slash + 1);
  278. }
  279. }
  280. DisplayServer::get_singleton()->clipboard_set(text);
  281. } break;
  282. }
  283. }
  284. void EditorDebuggerTree::_file_selected(const String &p_file) {
  285. if (inspected_object_id.is_null()) {
  286. return;
  287. }
  288. emit_signal(SNAME("save_node"), inspected_object_id, p_file, debugger_id);
  289. }