editor_debugger_tree.cpp 12 KB

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