scene_tree_editor.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. /**************************************************************************/
  2. /* scene_tree_editor.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 "scene_tree_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/object/message_queue.h"
  33. #include "editor/editor_file_system.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "editor/node_dock.h"
  39. #include "editor/plugins/animation_player_editor_plugin.h"
  40. #include "editor/plugins/canvas_item_editor_plugin.h"
  41. #include "editor/plugins/script_editor_plugin.h"
  42. #include "scene/gui/label.h"
  43. #include "scene/gui/tab_container.h"
  44. #include "scene/gui/texture_rect.h"
  45. #include "scene/main/window.h"
  46. #include "scene/resources/packed_scene.h"
  47. Node *SceneTreeEditor::get_scene_node() {
  48. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  49. return get_tree()->get_edited_scene_root();
  50. }
  51. void SceneTreeEditor::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  52. if (p_button != MouseButton::LEFT) {
  53. return;
  54. }
  55. if (connect_to_script_mode) {
  56. return; //don't do anything in this mode
  57. }
  58. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  59. ERR_FAIL_COND(!item);
  60. NodePath np = item->get_metadata(0);
  61. Node *n = get_node(np);
  62. ERR_FAIL_COND(!n);
  63. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  64. if (p_id == BUTTON_SUBSCENE) {
  65. if (n == get_scene_node()) {
  66. if (n && n->get_scene_inherited_state().is_valid()) {
  67. emit_signal(SNAME("open"), n->get_scene_inherited_state()->get_path());
  68. }
  69. } else {
  70. emit_signal(SNAME("open"), n->get_scene_file_path());
  71. }
  72. } else if (p_id == BUTTON_SCRIPT) {
  73. Ref<Script> script_typed = n->get_script();
  74. if (!script_typed.is_null()) {
  75. emit_signal(SNAME("open_script"), script_typed);
  76. }
  77. } else if (p_id == BUTTON_VISIBILITY) {
  78. undo_redo->create_action(TTR("Toggle Visible"));
  79. _toggle_visible(n);
  80. List<Node *> selection = editor_selection->get_selected_node_list();
  81. if (selection.size() > 1 && selection.find(n) != nullptr) {
  82. for (Node *nv : selection) {
  83. ERR_FAIL_COND(!nv);
  84. if (nv == n) {
  85. continue;
  86. }
  87. _toggle_visible(nv);
  88. }
  89. }
  90. undo_redo->commit_action();
  91. } else if (p_id == BUTTON_LOCK) {
  92. undo_redo->create_action(TTR("Unlock Node"));
  93. if (n->is_class("CanvasItem") || n->is_class("Node3D")) {
  94. undo_redo->add_do_method(n, "remove_meta", "_edit_lock_");
  95. undo_redo->add_undo_method(n, "set_meta", "_edit_lock_", true);
  96. undo_redo->add_do_method(this, "_update_tree");
  97. undo_redo->add_undo_method(this, "_update_tree");
  98. undo_redo->add_do_method(this, "emit_signal", "node_changed");
  99. undo_redo->add_undo_method(this, "emit_signal", "node_changed");
  100. }
  101. undo_redo->commit_action();
  102. } else if (p_id == BUTTON_PIN) {
  103. if (n->is_class("AnimationPlayer")) {
  104. AnimationPlayerEditor::get_singleton()->unpin();
  105. _update_tree();
  106. }
  107. } else if (p_id == BUTTON_GROUP) {
  108. undo_redo->create_action(TTR("Button Group"));
  109. if (n->is_class("CanvasItem") || n->is_class("Node3D")) {
  110. undo_redo->add_do_method(n, "remove_meta", "_edit_group_");
  111. undo_redo->add_undo_method(n, "set_meta", "_edit_group_", true);
  112. undo_redo->add_do_method(this, "_update_tree");
  113. undo_redo->add_undo_method(this, "_update_tree");
  114. undo_redo->add_do_method(this, "emit_signal", "node_changed");
  115. undo_redo->add_undo_method(this, "emit_signal", "node_changed");
  116. }
  117. undo_redo->commit_action();
  118. } else if (p_id == BUTTON_WARNING) {
  119. String config_err = n->get_configuration_warnings_as_string();
  120. if (config_err.is_empty()) {
  121. return;
  122. }
  123. const PackedInt32Array boundaries = TS->string_get_word_breaks(config_err, "", 80);
  124. PackedStringArray lines;
  125. for (int i = 0; i < boundaries.size(); i += 2) {
  126. const int start = boundaries[i];
  127. const int end = boundaries[i + 1];
  128. lines.append(config_err.substr(start, end - start + 1));
  129. }
  130. warning->set_text(String("\n").join(lines));
  131. warning->popup_centered();
  132. } else if (p_id == BUTTON_SIGNALS) {
  133. editor_selection->clear();
  134. editor_selection->add_node(n);
  135. set_selected(n);
  136. TabContainer *tab_container = Object::cast_to<TabContainer>(NodeDock::get_singleton()->get_parent());
  137. NodeDock::get_singleton()->get_parent()->call("set_current_tab", tab_container->get_tab_idx_from_control(NodeDock::get_singleton()));
  138. NodeDock::get_singleton()->show_connections();
  139. } else if (p_id == BUTTON_GROUPS) {
  140. editor_selection->clear();
  141. editor_selection->add_node(n);
  142. set_selected(n);
  143. TabContainer *tab_container = Object::cast_to<TabContainer>(NodeDock::get_singleton()->get_parent());
  144. NodeDock::get_singleton()->get_parent()->call("set_current_tab", tab_container->get_tab_idx_from_control(NodeDock::get_singleton()));
  145. NodeDock::get_singleton()->show_groups();
  146. } else if (p_id == BUTTON_UNIQUE) {
  147. undo_redo->create_action(TTR("Disable Scene Unique Name"));
  148. undo_redo->add_do_method(n, "set_unique_name_in_owner", false);
  149. undo_redo->add_undo_method(n, "set_unique_name_in_owner", true);
  150. undo_redo->add_do_method(this, "_update_tree");
  151. undo_redo->add_undo_method(this, "_update_tree");
  152. undo_redo->commit_action();
  153. }
  154. }
  155. void SceneTreeEditor::_toggle_visible(Node *p_node) {
  156. if (p_node->has_method("is_visible") && p_node->has_method("set_visible")) {
  157. bool v = bool(p_node->call("is_visible"));
  158. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  159. undo_redo->add_do_method(p_node, "set_visible", !v);
  160. undo_redo->add_undo_method(p_node, "set_visible", v);
  161. }
  162. }
  163. void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent) {
  164. if (!p_node) {
  165. return;
  166. }
  167. // only owned nodes are editable, since nodes can create their own (manually owned) child nodes,
  168. // which the editor needs not to know about.
  169. bool part_of_subscene = false;
  170. if (!display_foreign && p_node->get_owner() != get_scene_node() && p_node != get_scene_node()) {
  171. if ((show_enabled_subscene || can_open_instance) && p_node->get_owner() && (get_scene_node()->is_editable_instance(p_node->get_owner()))) {
  172. part_of_subscene = true;
  173. //allow
  174. } else {
  175. return;
  176. }
  177. } else {
  178. part_of_subscene = p_node != get_scene_node() && get_scene_node()->get_scene_inherited_state().is_valid() && get_scene_node()->get_scene_inherited_state()->find_node_by_path(get_scene_node()->get_path_to(p_node)) >= 0;
  179. }
  180. TreeItem *item = tree->create_item(p_parent);
  181. item->set_text(0, p_node->get_name());
  182. if (can_rename && !part_of_subscene) {
  183. item->set_editable(0, true);
  184. }
  185. item->set_selectable(0, true);
  186. if (can_rename) {
  187. bool collapsed = p_node->is_displayed_folded();
  188. if (collapsed) {
  189. item->set_collapsed(true);
  190. }
  191. }
  192. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(p_node, "Node");
  193. item->set_icon(0, icon);
  194. item->set_metadata(0, p_node->get_path());
  195. if (connect_to_script_mode) {
  196. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  197. Ref<Script> scr = p_node->get_script();
  198. if (!scr.is_null() && EditorNode::get_singleton()->get_object_custom_type_base(p_node) != scr) {
  199. //has script
  200. item->add_button(0, get_theme_icon(SNAME("Script"), SNAME("EditorIcons")), BUTTON_SCRIPT);
  201. } else {
  202. //has no script (or script is a custom type)
  203. item->set_custom_color(0, get_theme_color(SNAME("disabled_font_color"), SNAME("Editor")));
  204. item->set_selectable(0, false);
  205. if (!scr.is_null()) { // make sure to mark the script if a custom type
  206. item->add_button(0, get_theme_icon(SNAME("Script"), SNAME("EditorIcons")), BUTTON_SCRIPT);
  207. item->set_button_disabled(0, item->get_button_count(0) - 1, true);
  208. }
  209. accent.a *= 0.7;
  210. }
  211. if (marked.has(p_node)) {
  212. String node_name = p_node->get_name();
  213. if (connecting_signal) {
  214. node_name += " " + TTR("(Connecting From)");
  215. }
  216. item->set_text(0, node_name);
  217. item->set_custom_color(0, accent);
  218. }
  219. } else if (part_of_subscene) {
  220. if (valid_types.size() == 0) {
  221. item->set_custom_color(0, get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  222. }
  223. } else if (marked.has(p_node)) {
  224. String node_name = p_node->get_name();
  225. if (connecting_signal) {
  226. node_name += " " + TTR("(Connecting From)");
  227. }
  228. item->set_text(0, node_name);
  229. item->set_selectable(0, marked_selectable);
  230. item->set_custom_color(0, get_theme_color(SNAME("accent_color"), SNAME("Editor")));
  231. } else if (!p_node->can_process()) {
  232. item->set_custom_color(0, get_theme_color(SNAME("disabled_font_color"), SNAME("Editor")));
  233. } else if (!marked_selectable && !marked_children_selectable) {
  234. Node *node = p_node;
  235. while (node) {
  236. if (marked.has(node)) {
  237. item->set_selectable(0, false);
  238. item->set_custom_color(0, get_theme_color(SNAME("error_color"), SNAME("Editor")));
  239. break;
  240. }
  241. node = node->get_parent();
  242. }
  243. }
  244. if (can_rename) { //should be can edit..
  245. String conf_warning = p_node->get_configuration_warnings_as_string();
  246. if (!conf_warning.is_empty()) {
  247. const int num_warnings = p_node->get_configuration_warnings().size();
  248. String warning_icon;
  249. if (num_warnings == 1) {
  250. warning_icon = SNAME("NodeWarning");
  251. } else if (num_warnings <= 3) {
  252. warning_icon = vformat("NodeWarnings%d", num_warnings);
  253. } else {
  254. warning_icon = SNAME("NodeWarnings4Plus");
  255. }
  256. // Improve looks on tooltip, extra spacing on non-bullet point newlines.
  257. const String bullet_point = String::utf8("• ");
  258. int next_newline = 0;
  259. while (next_newline != -1) {
  260. next_newline = conf_warning.find("\n", next_newline + 2);
  261. if (conf_warning.substr(next_newline + 1, bullet_point.length()) != bullet_point) {
  262. conf_warning = conf_warning.insert(next_newline + 1, " ");
  263. }
  264. }
  265. String newline = (num_warnings == 1 ? "\n" : "\n\n");
  266. item->add_button(0, get_theme_icon(warning_icon, SNAME("EditorIcons")), BUTTON_WARNING, false, TTR("Node configuration warning:") + newline + conf_warning);
  267. }
  268. if (p_node->is_unique_name_in_owner()) {
  269. item->add_button(0, get_theme_icon(SNAME("SceneUniqueName"), SNAME("EditorIcons")), BUTTON_UNIQUE, false, vformat(TTR("This node can be accessed from within anywhere in the scene by preceding it with the '%s' prefix in a node path.\nClick to disable this."), UNIQUE_NODE_PREFIX));
  270. }
  271. int num_connections = p_node->get_persistent_signal_connection_count();
  272. int num_groups = p_node->get_persistent_group_count();
  273. String msg_temp;
  274. if (num_connections >= 1) {
  275. Array arr;
  276. arr.push_back(num_connections);
  277. msg_temp += TTRN("Node has one connection.", "Node has {num} connections.", num_connections).format(arr, "{num}");
  278. if (num_groups >= 1) {
  279. msg_temp += "\n";
  280. }
  281. }
  282. if (num_groups >= 1) {
  283. msg_temp += TTRN("Node is in this group:", "Node is in the following groups:", num_groups) + "\n";
  284. List<GroupInfo> groups;
  285. p_node->get_groups(&groups);
  286. for (const GroupInfo &E : groups) {
  287. if (E.persistent) {
  288. msg_temp += String::utf8("• ") + String(E.name) + "\n";
  289. }
  290. }
  291. }
  292. if (num_connections >= 1 || num_groups >= 1) {
  293. if (num_groups < 1) {
  294. msg_temp += "\n";
  295. }
  296. msg_temp += TTR("Click to show signals dock.");
  297. }
  298. Ref<Texture2D> icon_temp;
  299. SceneTreeEditorButton signal_temp = BUTTON_SIGNALS;
  300. if (num_connections >= 1 && num_groups >= 1) {
  301. icon_temp = get_theme_icon(SNAME("SignalsAndGroups"), SNAME("EditorIcons"));
  302. } else if (num_connections >= 1) {
  303. icon_temp = get_theme_icon(SNAME("Signals"), SNAME("EditorIcons"));
  304. } else if (num_groups >= 1) {
  305. icon_temp = get_theme_icon(SNAME("Groups"), SNAME("EditorIcons"));
  306. signal_temp = BUTTON_GROUPS;
  307. }
  308. if (num_connections >= 1 || num_groups >= 1) {
  309. item->add_button(0, icon_temp, signal_temp, false, msg_temp);
  310. }
  311. }
  312. {
  313. // Display the node name in all tooltips so that long node names can be previewed
  314. // without having to rename them.
  315. String tooltip = String(p_node->get_name());
  316. if (p_node == get_scene_node() && p_node->get_scene_inherited_state().is_valid()) {
  317. item->add_button(0, get_theme_icon(SNAME("InstanceOptions"), SNAME("EditorIcons")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
  318. tooltip += String("\n" + TTR("Inherits:") + " " + p_node->get_scene_inherited_state()->get_path());
  319. } else if (p_node != get_scene_node() && !p_node->get_scene_file_path().is_empty() && can_open_instance) {
  320. item->add_button(0, get_theme_icon(SNAME("InstanceOptions"), SNAME("EditorIcons")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
  321. tooltip += String("\n" + TTR("Instance:") + " " + p_node->get_scene_file_path());
  322. }
  323. StringName custom_type = EditorNode::get_singleton()->get_object_custom_type_name(p_node);
  324. tooltip += String("\n" + TTR("Type:") + " " + (custom_type != StringName() ? String(custom_type) : p_node->get_class()));
  325. if (!p_node->get_editor_description().is_empty()) {
  326. const PackedInt32Array boundaries = TS->string_get_word_breaks(p_node->get_editor_description(), "", 80);
  327. tooltip += "\n";
  328. for (int i = 0; i < boundaries.size(); i += 2) {
  329. const int start = boundaries[i];
  330. const int end = boundaries[i + 1];
  331. tooltip += "\n" + p_node->get_editor_description().substr(start, end - start + 1).rstrip("\n");
  332. }
  333. }
  334. item->set_tooltip_text(0, tooltip);
  335. }
  336. if (can_open_instance && is_scene_tree_dock) { // Show buttons only when necessary (SceneTreeDock) to avoid crashes.
  337. if (!p_node->is_connected("script_changed", callable_mp(this, &SceneTreeEditor::_node_script_changed))) {
  338. p_node->connect("script_changed", callable_mp(this, &SceneTreeEditor::_node_script_changed).bind(p_node));
  339. }
  340. Ref<Script> scr = p_node->get_script();
  341. if (!scr.is_null()) {
  342. String additional_notes;
  343. Color button_color = Color(1, 1, 1);
  344. // Can't set tooltip after adding button, need to do it before.
  345. if (scr->is_tool()) {
  346. additional_notes += "\n" + TTR("This script is currently running in the editor.");
  347. button_color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  348. }
  349. if (EditorNode::get_singleton()->get_object_custom_type_base(p_node) == scr) {
  350. additional_notes += "\n" + TTR("This script is a custom type.");
  351. button_color.a = 0.5;
  352. }
  353. item->add_button(0, get_theme_icon(SNAME("Script"), SNAME("EditorIcons")), BUTTON_SCRIPT, false, TTR("Open Script:") + " " + scr->get_path() + additional_notes);
  354. item->set_button_color(0, item->get_button_count(0) - 1, button_color);
  355. }
  356. if (p_node->is_class("CanvasItem")) {
  357. if (p_node->has_meta("_edit_lock_")) {
  358. item->add_button(0, get_theme_icon(SNAME("Lock"), SNAME("EditorIcons")), BUTTON_LOCK, false, TTR("Node is locked.\nClick to unlock it."));
  359. }
  360. if (p_node->has_meta("_edit_group_")) {
  361. item->add_button(0, get_theme_icon(SNAME("Group"), SNAME("EditorIcons")), BUTTON_GROUP, false, TTR("Children are not selectable.\nClick to make them selectable."));
  362. }
  363. bool v = p_node->call("is_visible");
  364. if (v) {
  365. item->add_button(0, get_theme_icon(SNAME("GuiVisibilityVisible"), SNAME("EditorIcons")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  366. } else {
  367. item->add_button(0, get_theme_icon(SNAME("GuiVisibilityHidden"), SNAME("EditorIcons")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  368. }
  369. if (!p_node->is_connected("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed))) {
  370. p_node->connect("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed).bind(p_node));
  371. }
  372. _update_visibility_color(p_node, item);
  373. } else if (p_node->is_class("CanvasLayer") || p_node->is_class("Window")) {
  374. bool v = p_node->call("is_visible");
  375. if (v) {
  376. item->add_button(0, get_theme_icon(SNAME("GuiVisibilityVisible"), SNAME("EditorIcons")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  377. } else {
  378. item->add_button(0, get_theme_icon(SNAME("GuiVisibilityHidden"), SNAME("EditorIcons")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  379. }
  380. if (!p_node->is_connected("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed))) {
  381. p_node->connect("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed).bind(p_node));
  382. }
  383. } else if (p_node->is_class("Node3D")) {
  384. if (p_node->has_meta("_edit_lock_")) {
  385. item->add_button(0, get_theme_icon(SNAME("Lock"), SNAME("EditorIcons")), BUTTON_LOCK, false, TTR("Node is locked.\nClick to unlock it."));
  386. }
  387. if (p_node->has_meta("_edit_group_")) {
  388. item->add_button(0, get_theme_icon(SNAME("Group"), SNAME("EditorIcons")), BUTTON_GROUP, false, TTR("Children are not selectable.\nClick to make them selectable."));
  389. }
  390. bool v = p_node->call("is_visible");
  391. if (v) {
  392. item->add_button(0, get_theme_icon(SNAME("GuiVisibilityVisible"), SNAME("EditorIcons")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  393. } else {
  394. item->add_button(0, get_theme_icon(SNAME("GuiVisibilityHidden"), SNAME("EditorIcons")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  395. }
  396. if (!p_node->is_connected("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed))) {
  397. p_node->connect("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed).bind(p_node));
  398. }
  399. _update_visibility_color(p_node, item);
  400. } else if (p_node->is_class("AnimationPlayer")) {
  401. bool is_pinned = AnimationPlayerEditor::get_singleton()->get_player() == p_node && AnimationPlayerEditor::get_singleton()->is_pinned();
  402. if (is_pinned) {
  403. item->add_button(0, get_theme_icon(SNAME("Pin"), SNAME("EditorIcons")), BUTTON_PIN, false, TTR("AnimationPlayer is pinned.\nClick to unpin."));
  404. }
  405. }
  406. }
  407. if (editor_selection) {
  408. if (editor_selection->is_selected(p_node)) {
  409. item->select(0);
  410. }
  411. }
  412. if (selected == p_node) {
  413. if (!editor_selection) {
  414. item->select(0);
  415. }
  416. item->set_as_cursor(0);
  417. }
  418. for (int i = 0; i < p_node->get_child_count(); i++) {
  419. _add_nodes(p_node->get_child(i), item);
  420. }
  421. if (valid_types.size()) {
  422. bool valid = false;
  423. for (const StringName &E : valid_types) {
  424. if (p_node->is_class(E) ||
  425. EditorNode::get_singleton()->is_object_of_custom_type(p_node, E)) {
  426. valid = true;
  427. break;
  428. }
  429. }
  430. if (!valid) {
  431. item->set_custom_color(0, get_theme_color(SNAME("disabled_font_color"), SNAME("Editor")));
  432. item->set_selectable(0, false);
  433. }
  434. }
  435. }
  436. void SceneTreeEditor::_node_visibility_changed(Node *p_node) {
  437. if (!p_node || (p_node != get_scene_node() && !p_node->get_owner())) {
  438. return;
  439. }
  440. TreeItem *item = _find(tree->get_root(), p_node->get_path());
  441. if (!item) {
  442. return;
  443. }
  444. int idx = item->get_button_by_id(0, BUTTON_VISIBILITY);
  445. ERR_FAIL_COND(idx == -1);
  446. bool node_visible = false;
  447. if (p_node->is_class("CanvasItem") || p_node->is_class("CanvasLayer") || p_node->is_class("Window")) {
  448. node_visible = p_node->call("is_visible");
  449. CanvasItemEditor::get_singleton()->get_viewport_control()->queue_redraw();
  450. } else if (p_node->is_class("Node3D")) {
  451. node_visible = p_node->call("is_visible");
  452. }
  453. if (node_visible) {
  454. item->set_button(0, idx, get_theme_icon(SNAME("GuiVisibilityVisible"), SNAME("EditorIcons")));
  455. } else {
  456. item->set_button(0, idx, get_theme_icon(SNAME("GuiVisibilityHidden"), SNAME("EditorIcons")));
  457. }
  458. _update_visibility_color(p_node, item);
  459. }
  460. void SceneTreeEditor::_update_visibility_color(Node *p_node, TreeItem *p_item) {
  461. if (p_node->is_class("CanvasItem") || p_node->is_class("Node3D")) {
  462. Color color(1, 1, 1, 1);
  463. bool visible_on_screen = p_node->call("is_visible_in_tree");
  464. if (!visible_on_screen) {
  465. color.a = 0.6;
  466. }
  467. int idx = p_item->get_button_by_id(0, BUTTON_VISIBILITY);
  468. p_item->set_button_color(0, idx, color);
  469. }
  470. }
  471. void SceneTreeEditor::_node_script_changed(Node *p_node) {
  472. if (tree_dirty) {
  473. return;
  474. }
  475. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  476. tree_dirty = true;
  477. }
  478. void SceneTreeEditor::_node_removed(Node *p_node) {
  479. if (EditorNode::get_singleton()->is_exiting()) {
  480. return; //speed up exit
  481. }
  482. if (p_node->is_connected("script_changed", callable_mp(this, &SceneTreeEditor::_node_script_changed))) {
  483. p_node->disconnect("script_changed", callable_mp(this, &SceneTreeEditor::_node_script_changed));
  484. }
  485. if (p_node->is_class("Node3D") || p_node->is_class("CanvasItem") || p_node->is_class("CanvasLayer") || p_node->is_class("Window")) {
  486. if (p_node->is_connected("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed))) {
  487. p_node->disconnect("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed));
  488. }
  489. }
  490. if (p_node == selected) {
  491. selected = nullptr;
  492. emit_signal(SNAME("node_selected"));
  493. }
  494. }
  495. void SceneTreeEditor::_node_renamed(Node *p_node) {
  496. if (p_node != get_scene_node() && !get_scene_node()->is_ancestor_of(p_node)) {
  497. return;
  498. }
  499. emit_signal(SNAME("node_renamed"));
  500. if (!tree_dirty) {
  501. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  502. tree_dirty = true;
  503. }
  504. }
  505. void SceneTreeEditor::_update_tree(bool p_scroll_to_selected) {
  506. if (!is_inside_tree()) {
  507. tree_dirty = false;
  508. return;
  509. }
  510. if (tree->is_editing()) {
  511. return;
  512. }
  513. updating_tree = true;
  514. tree->clear();
  515. if (get_scene_node()) {
  516. _add_nodes(get_scene_node(), nullptr);
  517. last_hash = hash_djb2_one_64(0);
  518. _compute_hash(get_scene_node(), last_hash);
  519. }
  520. updating_tree = false;
  521. tree_dirty = false;
  522. if (!filter.strip_edges().is_empty()) {
  523. _update_filter(nullptr, p_scroll_to_selected);
  524. }
  525. }
  526. bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_selected) {
  527. if (!p_parent) {
  528. p_parent = tree->get_root();
  529. filter_term_warning.clear();
  530. }
  531. if (!p_parent) {
  532. // Tree is empty, nothing to do here.
  533. return false;
  534. }
  535. bool keep_for_children = false;
  536. for (TreeItem *child = p_parent->get_first_child(); child; child = child->get_next()) {
  537. // Always keep if at least one of the children are kept.
  538. keep_for_children = _update_filter(child, p_scroll_to_selected) || keep_for_children;
  539. }
  540. // Now find other reasons to keep this Node, too.
  541. PackedStringArray terms = filter.to_lower().split_spaces();
  542. bool keep = _item_matches_all_terms(p_parent, terms);
  543. p_parent->set_visible(keep_for_children || keep);
  544. if (keep && !valid_types.is_empty()) {
  545. keep = false;
  546. Node *n = get_node(p_parent->get_metadata(0));
  547. for (const StringName &E : valid_types) {
  548. if (n->is_class(E) ||
  549. EditorNode::get_singleton()->is_object_of_custom_type(n, E)) {
  550. keep = true;
  551. break;
  552. }
  553. }
  554. }
  555. if (keep) {
  556. p_parent->clear_custom_color(0);
  557. p_parent->set_selectable(0, true);
  558. } else if (keep_for_children) {
  559. p_parent->set_custom_color(0, get_theme_color(SNAME("disabled_font_color"), SNAME("Editor")));
  560. p_parent->set_selectable(0, false);
  561. p_parent->deselect(0);
  562. }
  563. if (editor_selection) {
  564. Node *n = get_node(p_parent->get_metadata(0));
  565. if (keep) {
  566. if (p_scroll_to_selected && n && editor_selection->is_selected(n)) {
  567. tree->scroll_to_item(p_parent);
  568. }
  569. } else {
  570. if (n && p_parent->is_selected(0)) {
  571. editor_selection->remove_node(n);
  572. p_parent->deselect(0);
  573. }
  574. }
  575. }
  576. return keep || keep_for_children;
  577. }
  578. bool SceneTreeEditor::_item_matches_all_terms(TreeItem *p_item, PackedStringArray p_terms) {
  579. if (p_terms.is_empty()) {
  580. return true;
  581. }
  582. for (int i = 0; i < p_terms.size(); i++) {
  583. String term = p_terms[i];
  584. // Recognize special filter.
  585. if (term.contains(":") && !term.get_slicec(':', 0).is_empty()) {
  586. String parameter = term.get_slicec(':', 0);
  587. String argument = term.get_slicec(':', 1);
  588. if (parameter == "type" || parameter == "t") {
  589. // Filter by Type.
  590. String type = get_node(p_item->get_metadata(0))->get_class();
  591. bool term_in_inherited_class = false;
  592. // Every Node is is a Node, duh!
  593. while (type != "Node") {
  594. if (type.to_lower().contains(argument)) {
  595. term_in_inherited_class = true;
  596. break;
  597. }
  598. type = ClassDB::get_parent_class(type);
  599. }
  600. if (!term_in_inherited_class) {
  601. return false;
  602. }
  603. } else if (parameter == "group" || parameter == "g") {
  604. // Filter by Group.
  605. Node *node = get_node(p_item->get_metadata(0));
  606. if (argument.is_empty()) {
  607. // When argument is empty, match all Nodes belonging to any exposed group.
  608. if (node->get_persistent_group_count() == 0) {
  609. return false;
  610. }
  611. } else {
  612. List<Node::GroupInfo> group_info_list;
  613. node->get_groups(&group_info_list);
  614. bool term_in_groups = false;
  615. for (int j = 0; j < group_info_list.size(); j++) {
  616. if (!group_info_list[j].persistent) {
  617. continue; // Ignore internal groups.
  618. }
  619. if (String(group_info_list[j].name).to_lower().contains(argument)) {
  620. term_in_groups = true;
  621. break;
  622. }
  623. }
  624. if (!term_in_groups) {
  625. return false;
  626. }
  627. }
  628. } else if (filter_term_warning.is_empty()) {
  629. filter_term_warning = vformat(TTR("\"%s\" is not a known filter."), parameter);
  630. continue;
  631. }
  632. } else {
  633. // Default.
  634. if (!p_item->get_text(0).to_lower().contains(term)) {
  635. return false;
  636. }
  637. }
  638. }
  639. return true;
  640. }
  641. void SceneTreeEditor::_compute_hash(Node *p_node, uint64_t &hash) {
  642. hash = hash_djb2_one_64(p_node->get_instance_id(), hash);
  643. if (p_node->get_parent()) {
  644. hash = hash_djb2_one_64(p_node->get_parent()->get_instance_id(), hash); //so a reparent still produces a different hash
  645. }
  646. for (int i = 0; i < p_node->get_child_count(); i++) {
  647. _compute_hash(p_node->get_child(i), hash);
  648. }
  649. }
  650. void SceneTreeEditor::_test_update_tree() {
  651. pending_test_update = false;
  652. if (!is_inside_tree()) {
  653. return;
  654. }
  655. if (tree_dirty) {
  656. return; // don't even bother
  657. }
  658. uint64_t hash = hash_djb2_one_64(0);
  659. if (get_scene_node()) {
  660. _compute_hash(get_scene_node(), hash);
  661. }
  662. //test hash
  663. if (hash == last_hash) {
  664. return; // did not change
  665. }
  666. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  667. tree_dirty = true;
  668. }
  669. void SceneTreeEditor::_tree_process_mode_changed() {
  670. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  671. tree_dirty = true;
  672. }
  673. void SceneTreeEditor::_tree_changed() {
  674. if (EditorNode::get_singleton()->is_exiting()) {
  675. return; //speed up exit
  676. }
  677. if (pending_test_update) {
  678. return;
  679. }
  680. if (tree_dirty) {
  681. return;
  682. }
  683. MessageQueue::get_singleton()->push_call(this, "_test_update_tree");
  684. pending_test_update = true;
  685. }
  686. void SceneTreeEditor::_selected_changed() {
  687. TreeItem *s = tree->get_selected();
  688. ERR_FAIL_COND(!s);
  689. NodePath np = s->get_metadata(0);
  690. Node *n = get_node(np);
  691. if (n == selected) {
  692. return;
  693. }
  694. selected = get_node(np);
  695. blocked++;
  696. emit_signal(SNAME("node_selected"));
  697. blocked--;
  698. }
  699. void SceneTreeEditor::_deselect_items() {
  700. // Clear currently selected items in scene tree dock.
  701. if (editor_selection) {
  702. editor_selection->clear();
  703. emit_signal(SNAME("node_changed"));
  704. }
  705. }
  706. void SceneTreeEditor::_cell_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  707. TreeItem *item = Object::cast_to<TreeItem>(p_object);
  708. ERR_FAIL_COND(!item);
  709. if (!item->is_visible()) {
  710. return;
  711. }
  712. NodePath np = item->get_metadata(0);
  713. Node *n = get_node(np);
  714. if (!n) {
  715. return;
  716. }
  717. if (!editor_selection) {
  718. return;
  719. }
  720. if (p_selected) {
  721. editor_selection->add_node(n);
  722. } else {
  723. editor_selection->remove_node(n);
  724. }
  725. // Emitted "selected" in _selected_changed() when select single node, so select multiple node emit "changed"
  726. if (editor_selection->get_selected_nodes().size() > 1) {
  727. emit_signal(SNAME("node_changed"));
  728. }
  729. }
  730. void SceneTreeEditor::_notification(int p_what) {
  731. switch (p_what) {
  732. case NOTIFICATION_ENTER_TREE: {
  733. get_tree()->connect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
  734. get_tree()->connect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
  735. get_tree()->connect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
  736. get_tree()->connect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
  737. get_tree()->connect("node_configuration_warning_changed", callable_mp(this, &SceneTreeEditor::_warning_changed));
  738. tree->connect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
  739. _update_tree();
  740. } break;
  741. case NOTIFICATION_EXIT_TREE: {
  742. get_tree()->disconnect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
  743. get_tree()->disconnect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
  744. get_tree()->disconnect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
  745. get_tree()->disconnect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
  746. tree->disconnect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
  747. get_tree()->disconnect("node_configuration_warning_changed", callable_mp(this, &SceneTreeEditor::_warning_changed));
  748. } break;
  749. case NOTIFICATION_THEME_CHANGED: {
  750. tree->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), SNAME("Editor")));
  751. _update_tree();
  752. } break;
  753. }
  754. }
  755. TreeItem *SceneTreeEditor::_find(TreeItem *p_node, const NodePath &p_path) {
  756. if (!p_node) {
  757. return nullptr;
  758. }
  759. NodePath np = p_node->get_metadata(0);
  760. if (np == p_path) {
  761. return p_node;
  762. }
  763. TreeItem *children = p_node->get_first_child();
  764. while (children) {
  765. TreeItem *n = _find(children, p_path);
  766. if (n) {
  767. return n;
  768. }
  769. children = children->get_next();
  770. }
  771. return nullptr;
  772. }
  773. void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
  774. ERR_FAIL_COND(blocked > 0);
  775. if (pending_test_update) {
  776. _test_update_tree();
  777. }
  778. if (tree_dirty) {
  779. _update_tree();
  780. }
  781. if (selected == p_node) {
  782. return;
  783. }
  784. TreeItem *item = p_node ? _find(tree->get_root(), p_node->get_path()) : nullptr;
  785. if (item) {
  786. if (auto_expand_selected) {
  787. // Make visible when it's collapsed.
  788. TreeItem *node = item->get_parent();
  789. while (node && node != tree->get_root()) {
  790. node->set_collapsed(false);
  791. node = node->get_parent();
  792. }
  793. item->select(0);
  794. item->set_as_cursor(0);
  795. selected = p_node;
  796. tree->ensure_cursor_is_visible();
  797. }
  798. } else {
  799. if (!p_node) {
  800. selected = nullptr;
  801. }
  802. _update_tree();
  803. selected = p_node;
  804. }
  805. if (p_emit_selected) {
  806. emit_signal(SNAME("node_selected"));
  807. }
  808. }
  809. void SceneTreeEditor::_rename_node(Node *p_node, const String &p_name) {
  810. TreeItem *item = _find(tree->get_root(), p_node->get_path());
  811. ERR_FAIL_COND(!item);
  812. String new_name = p_name.validate_node_name();
  813. if (new_name != p_name) {
  814. error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n" + String::get_invalid_node_name_characters());
  815. error->popup_centered();
  816. if (new_name.is_empty()) {
  817. item->set_text(0, p_node->get_name());
  818. return;
  819. }
  820. item->set_text(0, new_name);
  821. }
  822. if (new_name == p_node->get_name()) {
  823. if (item->get_text(0).is_empty()) {
  824. item->set_text(0, new_name);
  825. }
  826. return;
  827. }
  828. // Trim leading/trailing whitespace to prevent node names from containing accidental whitespace, which would make it more difficult to get the node via `get_node()`.
  829. new_name = new_name.strip_edges();
  830. if (!is_scene_tree_dock) {
  831. p_node->set_name(new_name);
  832. item->set_metadata(0, p_node->get_path());
  833. emit_signal(SNAME("node_renamed"));
  834. } else {
  835. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  836. undo_redo->create_action("Rename Node", UndoRedo::MERGE_DISABLE, p_node);
  837. emit_signal(SNAME("node_prerename"), p_node, new_name);
  838. undo_redo->add_undo_method(p_node, "set_name", p_node->get_name());
  839. undo_redo->add_undo_method(item, "set_metadata", 0, p_node->get_path());
  840. undo_redo->add_undo_method(item, "set_text", 0, p_node->get_name());
  841. p_node->set_name(p_name);
  842. undo_redo->add_do_method(p_node, "set_name", new_name);
  843. undo_redo->add_do_method(item, "set_metadata", 0, p_node->get_path());
  844. undo_redo->add_do_method(item, "set_text", 0, new_name);
  845. undo_redo->commit_action();
  846. }
  847. }
  848. void SceneTreeEditor::_renamed() {
  849. TreeItem *which = tree->get_edited();
  850. ERR_FAIL_COND(!which);
  851. NodePath np = which->get_metadata(0);
  852. Node *n = get_node(np);
  853. ERR_FAIL_COND(!n);
  854. String new_name = which->get_text(0);
  855. if (new_name.strip_edges().is_empty()) {
  856. // If name is empty, fallback to class name.
  857. if (GLOBAL_GET("editor/naming/node_name_casing").operator int() != NAME_CASING_PASCAL_CASE) {
  858. new_name = Node::adjust_name_casing(n->get_class());
  859. } else {
  860. new_name = n->get_class();
  861. }
  862. }
  863. if (n->is_unique_name_in_owner()) {
  864. Node *existing = get_tree()->get_edited_scene_root()->get_node_or_null("%" + new_name);
  865. if (existing == n) {
  866. which->set_text(0, n->get_name());
  867. return;
  868. }
  869. if (existing != nullptr) {
  870. error->set_text(TTR("Another node already uses this unique name in the scene."));
  871. error->popup_centered();
  872. which->set_text(0, n->get_name());
  873. return;
  874. }
  875. }
  876. _rename_node(n, new_name);
  877. }
  878. Node *SceneTreeEditor::get_selected() {
  879. return selected;
  880. }
  881. void SceneTreeEditor::set_marked(const HashSet<Node *> &p_marked, bool p_selectable, bool p_children_selectable) {
  882. if (tree_dirty) {
  883. _update_tree();
  884. }
  885. marked = p_marked;
  886. marked_selectable = p_selectable;
  887. marked_children_selectable = p_children_selectable;
  888. _update_tree();
  889. }
  890. void SceneTreeEditor::set_marked(Node *p_marked, bool p_selectable, bool p_children_selectable) {
  891. HashSet<Node *> s;
  892. if (p_marked) {
  893. s.insert(p_marked);
  894. }
  895. set_marked(s, p_selectable, p_children_selectable);
  896. }
  897. void SceneTreeEditor::set_filter(const String &p_filter) {
  898. filter = p_filter;
  899. _update_filter(nullptr, true);
  900. }
  901. String SceneTreeEditor::get_filter() const {
  902. return filter;
  903. }
  904. String SceneTreeEditor::get_filter_term_warning() {
  905. return filter_term_warning;
  906. }
  907. void SceneTreeEditor::set_as_scene_tree_dock() {
  908. is_scene_tree_dock = true;
  909. }
  910. void SceneTreeEditor::set_display_foreign_nodes(bool p_display) {
  911. display_foreign = p_display;
  912. _update_tree();
  913. }
  914. void SceneTreeEditor::set_valid_types(const Vector<StringName> &p_valid) {
  915. valid_types = p_valid;
  916. }
  917. void SceneTreeEditor::set_editor_selection(EditorSelection *p_selection) {
  918. editor_selection = p_selection;
  919. tree->set_select_mode(Tree::SELECT_MULTI);
  920. tree->set_cursor_can_exit_tree(false);
  921. editor_selection->connect("selection_changed", callable_mp(this, &SceneTreeEditor::_selection_changed));
  922. }
  923. void SceneTreeEditor::_update_selection(TreeItem *item) {
  924. ERR_FAIL_COND(!item);
  925. NodePath np = item->get_metadata(0);
  926. if (!has_node(np)) {
  927. return;
  928. }
  929. Node *n = get_node(np);
  930. if (!n) {
  931. return;
  932. }
  933. if (editor_selection->is_selected(n)) {
  934. if (!item->is_selected(0)) {
  935. item->select(0);
  936. }
  937. } else {
  938. if (item->is_selected(0)) {
  939. TreeItem *previous_cursor_item = tree->get_selected();
  940. item->deselect(0);
  941. previous_cursor_item->set_as_cursor(0);
  942. }
  943. }
  944. TreeItem *c = item->get_first_child();
  945. while (c) {
  946. _update_selection(c);
  947. c = c->get_next();
  948. }
  949. }
  950. void SceneTreeEditor::_selection_changed() {
  951. if (!editor_selection) {
  952. return;
  953. }
  954. TreeItem *root = tree->get_root();
  955. if (!root) {
  956. return;
  957. }
  958. _update_selection(root);
  959. }
  960. void SceneTreeEditor::_cell_collapsed(Object *p_obj) {
  961. if (updating_tree) {
  962. return;
  963. }
  964. if (!can_rename) {
  965. return;
  966. }
  967. TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
  968. if (!ti) {
  969. return;
  970. }
  971. bool collapsed = ti->is_collapsed();
  972. NodePath np = ti->get_metadata(0);
  973. Node *n = get_node(np);
  974. ERR_FAIL_COND(!n);
  975. n->set_display_folded(collapsed);
  976. }
  977. Variant SceneTreeEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  978. if (!can_rename) {
  979. return Variant(); //not editable tree
  980. }
  981. if (tree->get_button_id_at_position(p_point) != -1) {
  982. return Variant(); //dragging from button
  983. }
  984. Vector<Node *> selected_nodes;
  985. Vector<Ref<Texture2D>> icons;
  986. TreeItem *next = tree->get_next_selected(nullptr);
  987. while (next) {
  988. NodePath np = next->get_metadata(0);
  989. Node *n = get_node(np);
  990. if (n) {
  991. // Only allow selection if not part of an instantiated scene.
  992. if (!n->get_owner() || n->get_owner() == get_scene_node() || n->get_owner()->get_scene_file_path().is_empty()) {
  993. selected_nodes.push_back(n);
  994. icons.push_back(next->get_icon(0));
  995. }
  996. }
  997. next = tree->get_next_selected(next);
  998. }
  999. if (selected_nodes.is_empty()) {
  1000. return Variant();
  1001. }
  1002. VBoxContainer *vb = memnew(VBoxContainer);
  1003. Array objs;
  1004. int list_max = 10;
  1005. float opacity_step = 1.0f / list_max;
  1006. float opacity_item = 1.0f;
  1007. for (int i = 0; i < selected_nodes.size(); i++) {
  1008. if (i < list_max) {
  1009. HBoxContainer *hb = memnew(HBoxContainer);
  1010. TextureRect *tf = memnew(TextureRect);
  1011. int icon_size = get_theme_constant(SNAME("class_icon_size"), SNAME("Editor"));
  1012. tf->set_custom_minimum_size(Size2(icon_size, icon_size));
  1013. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1014. tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1015. tf->set_texture(icons[i]);
  1016. hb->add_child(tf);
  1017. Label *label = memnew(Label(selected_nodes[i]->get_name()));
  1018. hb->add_child(label);
  1019. vb->add_child(hb);
  1020. hb->set_modulate(Color(1, 1, 1, opacity_item));
  1021. opacity_item -= opacity_step;
  1022. }
  1023. NodePath p = selected_nodes[i]->get_path();
  1024. objs.push_back(p);
  1025. }
  1026. set_drag_preview(vb);
  1027. Dictionary drag_data;
  1028. drag_data["type"] = "nodes";
  1029. drag_data["nodes"] = objs;
  1030. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1031. emit_signal(SNAME("nodes_dragged"));
  1032. return drag_data;
  1033. }
  1034. bool SceneTreeEditor::_is_script_type(const StringName &p_type) const {
  1035. return (script_types->find(p_type));
  1036. }
  1037. bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1038. if (!can_rename) {
  1039. return false; //not editable tree
  1040. }
  1041. Dictionary d = p_data;
  1042. if (!d.has("type")) {
  1043. return false;
  1044. }
  1045. TreeItem *item = tree->get_item_at_position(p_point);
  1046. if (!item) {
  1047. return false;
  1048. }
  1049. int section = tree->get_drop_section_at_position(p_point);
  1050. if (section < -1 || (section == -1 && !item->get_parent())) {
  1051. return false;
  1052. }
  1053. if (String(d["type"]) == "files") {
  1054. Vector<String> files = d["files"];
  1055. if (files.size() == 0) {
  1056. return false; //weird
  1057. }
  1058. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(files[0]))) {
  1059. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1060. return true;
  1061. }
  1062. bool scene_drop = true;
  1063. for (int i = 0; i < files.size(); i++) {
  1064. String file = files[i];
  1065. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  1066. if (ftype != "PackedScene") {
  1067. scene_drop = false;
  1068. break;
  1069. }
  1070. }
  1071. if (scene_drop) {
  1072. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1073. } else {
  1074. if (files.size() > 1) {
  1075. return false;
  1076. }
  1077. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1078. }
  1079. return true;
  1080. }
  1081. if (String(d["type"]) == "script_list_element") {
  1082. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  1083. if (se) {
  1084. String sp = se->get_edited_resource()->get_path();
  1085. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  1086. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1087. return true;
  1088. }
  1089. }
  1090. }
  1091. return String(d["type"]) == "nodes" && filter.is_empty();
  1092. }
  1093. void SceneTreeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1094. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  1095. return;
  1096. }
  1097. TreeItem *item = tree->get_item_at_position(p_point);
  1098. if (!item) {
  1099. return;
  1100. }
  1101. int section = tree->get_drop_section_at_position(p_point);
  1102. if (section < -1) {
  1103. return;
  1104. }
  1105. NodePath np = item->get_metadata(0);
  1106. Node *n = get_node(np);
  1107. if (!n) {
  1108. return;
  1109. }
  1110. Dictionary d = p_data;
  1111. if (String(d["type"]) == "nodes") {
  1112. Array nodes = d["nodes"];
  1113. emit_signal(SNAME("nodes_rearranged"), nodes, np, section);
  1114. }
  1115. if (String(d["type"]) == "files") {
  1116. Vector<String> files = d["files"];
  1117. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]);
  1118. if (_is_script_type(ftype)) {
  1119. emit_signal(SNAME("script_dropped"), files[0], np);
  1120. } else {
  1121. emit_signal(SNAME("files_dropped"), files, np, section);
  1122. }
  1123. }
  1124. if (String(d["type"]) == "script_list_element") {
  1125. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  1126. if (se) {
  1127. String sp = se->get_edited_resource()->get_path();
  1128. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  1129. emit_signal(SNAME("script_dropped"), sp, np);
  1130. }
  1131. }
  1132. }
  1133. }
  1134. void SceneTreeEditor::_empty_clicked(const Vector2 &p_pos, MouseButton p_button) {
  1135. if (p_button != MouseButton::RIGHT) {
  1136. return;
  1137. }
  1138. _rmb_select(p_pos);
  1139. }
  1140. void SceneTreeEditor::_rmb_select(const Vector2 &p_pos, MouseButton p_button) {
  1141. if (p_button != MouseButton::RIGHT) {
  1142. return;
  1143. }
  1144. emit_signal(SNAME("rmb_pressed"), tree->get_screen_position() + p_pos);
  1145. }
  1146. void SceneTreeEditor::update_warning() {
  1147. _warning_changed(nullptr);
  1148. }
  1149. void SceneTreeEditor::_warning_changed(Node *p_for_node) {
  1150. //should use a timer
  1151. update_timer->start();
  1152. }
  1153. void SceneTreeEditor::set_auto_expand_selected(bool p_auto, bool p_update_settings) {
  1154. if (p_update_settings) {
  1155. EditorSettings::get_singleton()->set("docks/scene_tree/auto_expand_to_selected", p_auto);
  1156. }
  1157. auto_expand_selected = p_auto;
  1158. }
  1159. void SceneTreeEditor::set_connect_to_script_mode(bool p_enable) {
  1160. connect_to_script_mode = p_enable;
  1161. update_tree();
  1162. }
  1163. void SceneTreeEditor::set_connecting_signal(bool p_enable) {
  1164. connecting_signal = p_enable;
  1165. update_tree();
  1166. }
  1167. void SceneTreeEditor::_bind_methods() {
  1168. ClassDB::bind_method(D_METHOD("_update_tree"), &SceneTreeEditor::_update_tree, DEFVAL(false)); // Still used by UndoRedo.
  1169. ClassDB::bind_method("_rename_node", &SceneTreeEditor::_rename_node);
  1170. ClassDB::bind_method("_test_update_tree", &SceneTreeEditor::_test_update_tree);
  1171. ClassDB::bind_method(D_METHOD("update_tree"), &SceneTreeEditor::update_tree);
  1172. ADD_SIGNAL(MethodInfo("node_selected"));
  1173. ADD_SIGNAL(MethodInfo("node_renamed"));
  1174. ADD_SIGNAL(MethodInfo("node_prerename"));
  1175. ADD_SIGNAL(MethodInfo("node_changed"));
  1176. ADD_SIGNAL(MethodInfo("nodes_dragged"));
  1177. ADD_SIGNAL(MethodInfo("nodes_rearranged", PropertyInfo(Variant::ARRAY, "paths"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1178. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1179. ADD_SIGNAL(MethodInfo("script_dropped", PropertyInfo(Variant::STRING, "file"), PropertyInfo(Variant::NODE_PATH, "to_path")));
  1180. ADD_SIGNAL(MethodInfo("rmb_pressed", PropertyInfo(Variant::VECTOR2, "position")));
  1181. ADD_SIGNAL(MethodInfo("open"));
  1182. ADD_SIGNAL(MethodInfo("open_script"));
  1183. }
  1184. SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_open_instance) {
  1185. selected = nullptr;
  1186. can_rename = p_can_rename;
  1187. can_open_instance = p_can_open_instance;
  1188. editor_selection = nullptr;
  1189. if (p_label) {
  1190. Label *label = memnew(Label);
  1191. label->set_theme_type_variation("HeaderSmall");
  1192. label->set_position(Point2(10, 0));
  1193. label->set_text(TTR("Scene Tree (Nodes):"));
  1194. add_child(label);
  1195. }
  1196. tree = memnew(Tree);
  1197. tree->set_anchor(SIDE_RIGHT, ANCHOR_END);
  1198. tree->set_anchor(SIDE_BOTTOM, ANCHOR_END);
  1199. tree->set_begin(Point2(0, p_label ? 18 : 0));
  1200. tree->set_end(Point2(0, 0));
  1201. tree->set_allow_reselect(true);
  1202. tree->add_theme_constant_override("button_margin", 0);
  1203. add_child(tree);
  1204. SET_DRAG_FORWARDING_GCD(tree, SceneTreeEditor);
  1205. if (p_can_rename) {
  1206. tree->set_allow_rmb_select(true);
  1207. tree->connect("item_mouse_selected", callable_mp(this, &SceneTreeEditor::_rmb_select));
  1208. tree->connect("empty_clicked", callable_mp(this, &SceneTreeEditor::_empty_clicked));
  1209. }
  1210. tree->connect("cell_selected", callable_mp(this, &SceneTreeEditor::_selected_changed));
  1211. tree->connect("item_edited", callable_mp(this, &SceneTreeEditor::_renamed));
  1212. tree->connect("multi_selected", callable_mp(this, &SceneTreeEditor::_cell_multi_selected));
  1213. tree->connect("button_clicked", callable_mp(this, &SceneTreeEditor::_cell_button_pressed));
  1214. tree->connect("nothing_selected", callable_mp(this, &SceneTreeEditor::_deselect_items));
  1215. error = memnew(AcceptDialog);
  1216. add_child(error);
  1217. warning = memnew(AcceptDialog);
  1218. add_child(warning);
  1219. warning->set_title(TTR("Node Configuration Warning!"));
  1220. last_hash = 0;
  1221. blocked = 0;
  1222. update_timer = memnew(Timer);
  1223. update_timer->connect("timeout", callable_mp(this, &SceneTreeEditor::_update_tree).bind(false));
  1224. update_timer->set_one_shot(true);
  1225. update_timer->set_wait_time(0.5);
  1226. add_child(update_timer);
  1227. script_types = memnew(List<StringName>);
  1228. ClassDB::get_inheriters_from_class("Script", script_types);
  1229. }
  1230. SceneTreeEditor::~SceneTreeEditor() {
  1231. memdelete(script_types);
  1232. }
  1233. /******** DIALOG *********/
  1234. void SceneTreeDialog::popup_scenetree_dialog() {
  1235. popup_centered_clamped(Size2(350, 700) * EDSCALE);
  1236. }
  1237. void SceneTreeDialog::_update_theme() {
  1238. filter->set_right_icon(tree->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  1239. }
  1240. void SceneTreeDialog::_notification(int p_what) {
  1241. switch (p_what) {
  1242. case NOTIFICATION_VISIBILITY_CHANGED: {
  1243. if (is_visible()) {
  1244. tree->update_tree();
  1245. // Select the search bar by default.
  1246. filter->call_deferred(SNAME("grab_focus"));
  1247. }
  1248. } break;
  1249. case NOTIFICATION_ENTER_TREE: {
  1250. connect("confirmed", callable_mp(this, &SceneTreeDialog::_select));
  1251. _update_theme();
  1252. } break;
  1253. case NOTIFICATION_THEME_CHANGED: {
  1254. _update_theme();
  1255. } break;
  1256. case NOTIFICATION_EXIT_TREE: {
  1257. disconnect("confirmed", callable_mp(this, &SceneTreeDialog::_select));
  1258. } break;
  1259. }
  1260. }
  1261. void SceneTreeDialog::_cancel() {
  1262. hide();
  1263. }
  1264. void SceneTreeDialog::_select() {
  1265. if (tree->get_selected()) {
  1266. // The signal may cause another dialog to be displayed, so be sure to hide this one first.
  1267. hide();
  1268. emit_signal(SNAME("selected"), tree->get_selected()->get_path());
  1269. }
  1270. }
  1271. void SceneTreeDialog::_selected_changed() {
  1272. get_ok_button()->set_disabled(!tree->get_selected());
  1273. }
  1274. void SceneTreeDialog::_filter_changed(const String &p_filter) {
  1275. tree->set_filter(p_filter);
  1276. }
  1277. void SceneTreeDialog::_bind_methods() {
  1278. ClassDB::bind_method("_cancel", &SceneTreeDialog::_cancel);
  1279. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::NODE_PATH, "path")));
  1280. }
  1281. SceneTreeDialog::SceneTreeDialog() {
  1282. set_title(TTR("Select a Node"));
  1283. VBoxContainer *vbc = memnew(VBoxContainer);
  1284. add_child(vbc);
  1285. filter = memnew(LineEdit);
  1286. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1287. filter->set_placeholder(TTR("Filter Nodes"));
  1288. filter->set_clear_button_enabled(true);
  1289. filter->add_theme_constant_override("minimum_character_width", 0);
  1290. filter->connect("text_changed", callable_mp(this, &SceneTreeDialog::_filter_changed));
  1291. vbc->add_child(filter);
  1292. tree = memnew(SceneTreeEditor(false, false, true));
  1293. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1294. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &SceneTreeDialog::_select));
  1295. vbc->add_child(tree);
  1296. // Disable the OK button when no node is selected.
  1297. get_ok_button()->set_disabled(!tree->get_selected());
  1298. tree->connect("node_selected", callable_mp(this, &SceneTreeDialog::_selected_changed));
  1299. }
  1300. SceneTreeDialog::~SceneTreeDialog() {
  1301. }