editor_debugger_tree.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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/debugger/editor_debugger_node.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/gui/editor_file_dialog.h"
  36. #include "editor/scene_tree_dock.h"
  37. #include "scene/debugger/scene_debugger.h"
  38. #include "scene/gui/texture_rect.h"
  39. #include "scene/resources/packed_scene.h"
  40. #include "servers/display_server.h"
  41. EditorDebuggerTree::EditorDebuggerTree() {
  42. set_v_size_flags(SIZE_EXPAND_FILL);
  43. set_allow_rmb_select(true);
  44. // Popup
  45. item_menu = memnew(PopupMenu);
  46. item_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorDebuggerTree::_item_menu_id_pressed));
  47. add_child(item_menu);
  48. // File Dialog
  49. file_dialog = memnew(EditorFileDialog);
  50. file_dialog->connect("file_selected", callable_mp(this, &EditorDebuggerTree::_file_selected));
  51. add_child(file_dialog);
  52. }
  53. void EditorDebuggerTree::_notification(int p_what) {
  54. switch (p_what) {
  55. case NOTIFICATION_POSTINITIALIZE: {
  56. set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  57. connect("cell_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_selected));
  58. connect("item_collapsed", callable_mp(this, &EditorDebuggerTree::_scene_tree_folded));
  59. connect("item_mouse_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_rmb_selected));
  60. } break;
  61. case NOTIFICATION_ENTER_TREE: {
  62. update_icon_max_width();
  63. } break;
  64. }
  65. }
  66. void EditorDebuggerTree::_bind_methods() {
  67. ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::INT, "debugger")));
  68. ADD_SIGNAL(MethodInfo("save_node", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "filename"), PropertyInfo(Variant::INT, "debugger")));
  69. ADD_SIGNAL(MethodInfo("open"));
  70. }
  71. void EditorDebuggerTree::_scene_tree_selected() {
  72. if (updating_scene_tree) {
  73. return;
  74. }
  75. TreeItem *item = get_selected();
  76. if (!item) {
  77. return;
  78. }
  79. inspected_object_id = uint64_t(item->get_metadata(0));
  80. emit_signal(SNAME("object_selected"), inspected_object_id, debugger_id);
  81. }
  82. void EditorDebuggerTree::_scene_tree_folded(Object *p_obj) {
  83. if (updating_scene_tree) {
  84. return;
  85. }
  86. TreeItem *item = Object::cast_to<TreeItem>(p_obj);
  87. if (!item) {
  88. return;
  89. }
  90. ObjectID id = ObjectID(uint64_t(item->get_metadata(0)));
  91. if (unfold_cache.has(id)) {
  92. unfold_cache.erase(id);
  93. } else {
  94. unfold_cache.insert(id);
  95. }
  96. }
  97. void EditorDebuggerTree::_scene_tree_rmb_selected(const Vector2 &p_position, MouseButton p_button) {
  98. if (p_button != MouseButton::RIGHT) {
  99. return;
  100. }
  101. TreeItem *item = get_item_at_position(p_position);
  102. if (!item) {
  103. return;
  104. }
  105. item->select(0);
  106. item_menu->clear();
  107. item_menu->add_icon_item(get_editor_theme_icon(SNAME("CreateNewSceneFrom")), TTR("Save Branch as Scene"), ITEM_MENU_SAVE_REMOTE_NODE);
  108. item_menu->add_icon_item(get_editor_theme_icon(SNAME("CopyNodePath")), TTR("Copy Node Path"), ITEM_MENU_COPY_NODE_PATH);
  109. item_menu->add_icon_item(get_editor_theme_icon(SNAME("Collapse")), TTR("Expand/Collapse Branch"), ITEM_MENU_EXPAND_COLLAPSE);
  110. item_menu->set_position(get_screen_position() + get_local_mouse_position());
  111. item_menu->reset_size();
  112. item_menu->popup();
  113. }
  114. /// Populates inspect_scene_tree given data in nodes as a flat list, encoded depth first.
  115. ///
  116. /// Given a nodes array like [R,A,B,C,D,E] the following Tree will be generated, assuming
  117. /// filter is an empty String, R and A child count are 2, B is 1 and C, D and E are 0.
  118. ///
  119. /// R
  120. /// |-A
  121. /// | |-B
  122. /// | | |-C
  123. /// | |
  124. /// | |-D
  125. /// |
  126. /// |-E
  127. ///
  128. void EditorDebuggerTree::update_scene_tree(const SceneDebuggerTree *p_tree, int p_debugger) {
  129. set_hide_root(false);
  130. updating_scene_tree = true;
  131. const String last_path = get_selected_path();
  132. const String filter = SceneTreeDock::get_singleton()->get_filter();
  133. TreeItem *select_item = nullptr;
  134. bool hide_filtered_out_parents = EDITOR_GET("docks/scene_tree/hide_filtered_out_parents");
  135. bool should_scroll = scrolling_to_item || filter != last_filter;
  136. scrolling_to_item = false;
  137. TreeItem *scroll_item = nullptr;
  138. // Nodes are in a flatten list, depth first. Use a stack of parents, avoid recursion.
  139. List<ParentItem> parents;
  140. for (const SceneDebuggerTree::RemoteNode &node : p_tree->nodes) {
  141. TreeItem *parent = nullptr;
  142. Pair<TreeItem *, TreeItem *> move_from_to;
  143. if (parents.size()) { // Find last parent.
  144. ParentItem &p = parents.front()->get();
  145. parent = p.tree_item;
  146. if (!(--p.child_count)) { // If no child left, remove it.
  147. parents.pop_front();
  148. if (hide_filtered_out_parents && !filter.is_subsequence_ofn(parent->get_text(0))) {
  149. if (parent == get_root()) {
  150. set_hide_root(true);
  151. } else {
  152. move_from_to.first = parent;
  153. // Find the closest ancestor that matches the filter.
  154. for (const ParentItem p2 : parents) {
  155. move_from_to.second = p2.tree_item;
  156. if (p2.matches_filter || move_from_to.second == get_root()) {
  157. break;
  158. }
  159. }
  160. if (!move_from_to.second) {
  161. move_from_to.second = get_root();
  162. }
  163. }
  164. }
  165. }
  166. }
  167. // Add this node.
  168. TreeItem *item = create_item(parent);
  169. item->set_text(0, node.name);
  170. if (node.scene_file_path.is_empty()) {
  171. item->set_tooltip_text(0, node.name + "\n" + TTR("Type:") + " " + node.type_name);
  172. } else {
  173. item->set_tooltip_text(0, node.name + "\n" + TTR("Instance:") + " " + node.scene_file_path + "\n" + TTR("Type:") + " " + node.type_name);
  174. }
  175. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(node.type_name, "");
  176. if (icon.is_valid()) {
  177. item->set_icon(0, icon);
  178. }
  179. item->set_metadata(0, node.id);
  180. String current_path;
  181. if (parent) {
  182. current_path += (String)parent->get_meta("node_path");
  183. // Set current item as collapsed if necessary (root is never collapsed).
  184. if (!unfold_cache.has(node.id)) {
  185. item->set_collapsed(true);
  186. }
  187. }
  188. item->set_meta("node_path", current_path + "/" + item->get_text(0));
  189. // Select previously selected node.
  190. if (debugger_id == p_debugger) { // Can use remote id.
  191. if (node.id == inspected_object_id) {
  192. if (selection_uncollapse_all) {
  193. selection_uncollapse_all = false;
  194. // Temporarily set to `false`, to allow caching the unfolds.
  195. updating_scene_tree = false;
  196. item->uncollapse_tree();
  197. updating_scene_tree = true;
  198. }
  199. select_item = item;
  200. if (should_scroll) {
  201. scroll_item = item;
  202. }
  203. }
  204. } else if (last_path == (String)item->get_meta("node_path")) { // Must use path.
  205. updating_scene_tree = false; // Force emission of new selection.
  206. select_item = item;
  207. if (should_scroll) {
  208. scroll_item = item;
  209. }
  210. updating_scene_tree = true;
  211. }
  212. // Add buttons.
  213. const Color remote_button_color = Color(1, 1, 1, 0.8);
  214. if (!node.scene_file_path.is_empty()) {
  215. String node_scene_file_path = node.scene_file_path;
  216. Ref<Texture2D> button_icon = get_editor_theme_icon(SNAME("InstanceOptions"));
  217. 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);
  218. item->set_meta("scene_file_path", node_scene_file_path);
  219. item->add_button(0, button_icon, BUTTON_SUBSCENE, false, tooltip);
  220. item->set_button_color(0, item->get_button_count(0) - 1, remote_button_color);
  221. }
  222. if (node.view_flags & SceneDebuggerTree::RemoteNode::VIEW_HAS_VISIBLE_METHOD) {
  223. bool node_visible = node.view_flags & SceneDebuggerTree::RemoteNode::VIEW_VISIBLE;
  224. bool node_visible_in_tree = node.view_flags & SceneDebuggerTree::RemoteNode::VIEW_VISIBLE_IN_TREE;
  225. Ref<Texture2D> button_icon = get_editor_theme_icon(node_visible ? SNAME("GuiVisibilityVisible") : SNAME("GuiVisibilityHidden"));
  226. String tooltip = TTR("Toggle Visibility");
  227. item->set_meta("visible", node_visible);
  228. item->add_button(0, button_icon, BUTTON_VISIBILITY, false, tooltip);
  229. if (ClassDB::is_parent_class(node.type_name, "CanvasItem") || ClassDB::is_parent_class(node.type_name, "Node3D")) {
  230. item->set_button_color(0, item->get_button_count(0) - 1, node_visible_in_tree ? remote_button_color : Color(1, 1, 1, 0.6));
  231. } else {
  232. item->set_button_color(0, item->get_button_count(0) - 1, remote_button_color);
  233. }
  234. }
  235. // Add in front of the parents stack if children are expected.
  236. if (node.child_count) {
  237. parents.push_front(ParentItem(item, node.child_count, filter.is_subsequence_ofn(item->get_text(0))));
  238. } else {
  239. // Apply filters.
  240. while (parent) {
  241. const bool had_siblings = item->get_prev() || item->get_next();
  242. if (filter.is_subsequence_ofn(item->get_text(0))) {
  243. break; // Filter matches, must survive.
  244. }
  245. parent->remove_child(item);
  246. memdelete(item);
  247. if (select_item == item || scroll_item == item) {
  248. select_item = nullptr;
  249. scroll_item = nullptr;
  250. }
  251. if (had_siblings) {
  252. break; // Parent must survive.
  253. }
  254. item = parent;
  255. parent = item->get_parent();
  256. // Check if parent expects more children.
  257. for (ParentItem &pair : parents) {
  258. if (pair.tree_item == item) {
  259. parent = nullptr;
  260. break; // Might have more children.
  261. }
  262. }
  263. }
  264. }
  265. // Move all children to the ancestor that matches the filter, if picked.
  266. if (move_from_to.first) {
  267. TreeItem *from = move_from_to.first;
  268. TypedArray<TreeItem> children = from->get_children();
  269. if (!children.is_empty()) {
  270. for (Variant &c : children) {
  271. TreeItem *ti = Object::cast_to<TreeItem>(c);
  272. from->remove_child(ti);
  273. move_from_to.second->add_child(ti);
  274. }
  275. from->get_parent()->remove_child(from);
  276. memdelete(from);
  277. if (select_item == from || scroll_item == from) {
  278. select_item = nullptr;
  279. scroll_item = nullptr;
  280. }
  281. }
  282. }
  283. }
  284. debugger_id = p_debugger; // Needed by hook, could be avoided if every debugger had its own tree.
  285. if (select_item) {
  286. select_item->select(0);
  287. }
  288. if (scroll_item) {
  289. scroll_to_item(scroll_item, false);
  290. }
  291. last_filter = filter;
  292. updating_scene_tree = false;
  293. }
  294. void EditorDebuggerTree::select_node(ObjectID p_id) {
  295. // Manually select, as the tree control may be out-of-date for some reason (e.g. not shown yet).
  296. selection_uncollapse_all = true;
  297. inspected_object_id = uint64_t(p_id);
  298. scrolling_to_item = true;
  299. emit_signal(SNAME("object_selected"), inspected_object_id, debugger_id);
  300. if (!updating_scene_tree) {
  301. // Request a tree refresh.
  302. EditorDebuggerNode::get_singleton()->request_remote_tree();
  303. }
  304. // Set the value immediately, so no update flooding happens and causes a crash.
  305. updating_scene_tree = true;
  306. }
  307. Variant EditorDebuggerTree::get_drag_data(const Point2 &p_point) {
  308. if (get_button_id_at_position(p_point) != -1) {
  309. return Variant();
  310. }
  311. TreeItem *selected = get_selected();
  312. if (!selected) {
  313. return Variant();
  314. }
  315. String path = selected->get_text(0);
  316. const int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  317. HBoxContainer *hb = memnew(HBoxContainer);
  318. TextureRect *tf = memnew(TextureRect);
  319. tf->set_texture(selected->get_icon(0));
  320. tf->set_custom_minimum_size(Size2(icon_size, icon_size));
  321. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  322. tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  323. hb->add_child(tf);
  324. Label *label = memnew(Label(path));
  325. hb->add_child(label);
  326. set_drag_preview(hb);
  327. if (!selected->get_parent() || !selected->get_parent()->get_parent()) {
  328. path = ".";
  329. } else {
  330. while (selected->get_parent()->get_parent() != get_root()) {
  331. selected = selected->get_parent();
  332. path = selected->get_text(0) + "/" + path;
  333. }
  334. }
  335. return vformat("\"%s\"", path);
  336. }
  337. void EditorDebuggerTree::update_icon_max_width() {
  338. add_theme_constant_override("icon_max_width", get_theme_constant("class_icon_size", EditorStringName(Editor)));
  339. }
  340. String EditorDebuggerTree::get_selected_path() {
  341. if (!get_selected()) {
  342. return "";
  343. }
  344. return get_selected()->get_meta("node_path");
  345. }
  346. void EditorDebuggerTree::_item_menu_id_pressed(int p_option) {
  347. switch (p_option) {
  348. case ITEM_MENU_SAVE_REMOTE_NODE: {
  349. file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  350. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  351. List<String> extensions;
  352. Ref<PackedScene> sd = memnew(PackedScene);
  353. ResourceSaver::get_recognized_extensions(sd, &extensions);
  354. file_dialog->clear_filters();
  355. for (const String &extension : extensions) {
  356. file_dialog->add_filter("*." + extension, extension.to_upper());
  357. }
  358. String filename = get_selected_path().get_file() + "." + extensions.front()->get().to_lower();
  359. file_dialog->set_current_path(filename);
  360. file_dialog->popup_file_dialog();
  361. } break;
  362. case ITEM_MENU_COPY_NODE_PATH: {
  363. String text = get_selected_path();
  364. if (text.is_empty()) {
  365. return;
  366. } else if (text == "/root") {
  367. text = ".";
  368. } else {
  369. text = text.replace("/root/", "");
  370. int slash = text.find_char('/');
  371. if (slash < 0) {
  372. text = ".";
  373. } else {
  374. text = text.substr(slash + 1);
  375. }
  376. }
  377. DisplayServer::get_singleton()->clipboard_set(text);
  378. } break;
  379. case ITEM_MENU_EXPAND_COLLAPSE: {
  380. TreeItem *s_item = get_selected();
  381. if (!s_item) {
  382. s_item = get_root();
  383. if (!s_item) {
  384. break;
  385. }
  386. }
  387. bool collapsed = s_item->is_any_collapsed();
  388. s_item->set_collapsed_recursive(!collapsed);
  389. ensure_cursor_is_visible();
  390. }
  391. }
  392. }
  393. void EditorDebuggerTree::_file_selected(const String &p_file) {
  394. if (inspected_object_id.is_null()) {
  395. return;
  396. }
  397. emit_signal(SNAME("save_node"), inspected_object_id, p_file, debugger_id);
  398. }