editor_debugger_tree.cpp 14 KB

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