editor_debugger_tree.cpp 13 KB

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