scene_tree_editor.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347
  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/script_language.h"
  33. #include "editor/editor_dock_manager.h"
  34. #include "editor/editor_file_system.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/node_dock.h"
  40. #include "editor/plugins/animation_player_editor_plugin.h"
  41. #include "editor/plugins/canvas_item_editor_plugin.h"
  42. #include "editor/plugins/script_editor_plugin.h"
  43. #include "editor/themes/editor_scale.h"
  44. #include "scene/2d/node_2d.h"
  45. #include "scene/gui/flow_container.h"
  46. #include "scene/gui/label.h"
  47. #include "scene/gui/texture_rect.h"
  48. #include "scene/main/window.h"
  49. #include "scene/resources/packed_scene.h"
  50. Node *SceneTreeEditor::get_scene_node() const {
  51. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  52. return get_tree()->get_edited_scene_root();
  53. }
  54. PackedStringArray SceneTreeEditor::_get_node_configuration_warnings(Node *p_node) {
  55. PackedStringArray warnings = p_node->get_configuration_warnings();
  56. if (p_node == get_scene_node()) {
  57. Node2D *node_2d = Object::cast_to<Node2D>(p_node);
  58. if (node_2d) {
  59. // Note: Warn for Node2D but not all CanvasItems, don't warn for Control nodes.
  60. // Control nodes may have reasons to use a transformed root node like anchors.
  61. if (!node_2d->get_transform().is_equal_approx(Transform2D())) {
  62. warnings.append(TTR("The root node of a scene is recommended to not be transformed, since instances of the scene will usually override this. Reset the transform and reload the scene to remove this warning."));
  63. }
  64. }
  65. Node3D *node_3d = Object::cast_to<Node3D>(p_node);
  66. if (node_3d) {
  67. if (!node_3d->get_transform().is_equal_approx(Transform3D())) {
  68. warnings.append(TTR("The root node of a scene is recommended to not be transformed, since instances of the scene will usually override this. Reset the transform and reload the scene to remove this warning."));
  69. }
  70. }
  71. }
  72. return warnings;
  73. }
  74. void SceneTreeEditor::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  75. if (p_button != MouseButton::LEFT) {
  76. return;
  77. }
  78. if (connect_to_script_mode) {
  79. return; // Don't do anything in this mode.
  80. }
  81. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  82. ERR_FAIL_NULL(item);
  83. NodePath np = item->get_metadata(0);
  84. Node *n = get_node(np);
  85. ERR_FAIL_NULL(n);
  86. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  87. if (p_id == BUTTON_SUBSCENE) {
  88. if (n == get_scene_node()) {
  89. if (n && n->get_scene_inherited_state().is_valid()) {
  90. emit_signal(SNAME("open"), n->get_scene_inherited_state()->get_path());
  91. }
  92. } else {
  93. emit_signal(SNAME("open"), n->get_scene_file_path());
  94. }
  95. } else if (p_id == BUTTON_SCRIPT) {
  96. Ref<Script> script_typed = n->get_script();
  97. if (!script_typed.is_null()) {
  98. emit_signal(SNAME("open_script"), script_typed);
  99. }
  100. } else if (p_id == BUTTON_VISIBILITY) {
  101. undo_redo->create_action(TTR("Toggle Visible"));
  102. _toggle_visible(n);
  103. List<Node *> selection = editor_selection->get_selected_node_list();
  104. if (selection.size() > 1 && selection.find(n) != nullptr) {
  105. for (Node *nv : selection) {
  106. ERR_FAIL_NULL(nv);
  107. if (nv == n) {
  108. continue;
  109. }
  110. _toggle_visible(nv);
  111. }
  112. }
  113. undo_redo->commit_action();
  114. } else if (p_id == BUTTON_LOCK) {
  115. undo_redo->create_action(TTR("Unlock Node"));
  116. undo_redo->add_do_method(n, "remove_meta", "_edit_lock_");
  117. undo_redo->add_undo_method(n, "set_meta", "_edit_lock_", true);
  118. undo_redo->add_do_method(this, "_update_tree");
  119. undo_redo->add_undo_method(this, "_update_tree");
  120. undo_redo->add_do_method(this, "emit_signal", "node_changed");
  121. undo_redo->add_undo_method(this, "emit_signal", "node_changed");
  122. undo_redo->commit_action();
  123. } else if (p_id == BUTTON_PIN) {
  124. if (n->is_class("AnimationMixer")) {
  125. AnimationPlayerEditor::get_singleton()->unpin();
  126. _update_tree();
  127. }
  128. } else if (p_id == BUTTON_GROUP) {
  129. undo_redo->create_action(TTR("Ungroup Children"));
  130. if (n->is_class("CanvasItem") || n->is_class("Node3D")) {
  131. undo_redo->add_do_method(n, "remove_meta", "_edit_group_");
  132. undo_redo->add_undo_method(n, "set_meta", "_edit_group_", true);
  133. undo_redo->add_do_method(this, "_update_tree");
  134. undo_redo->add_undo_method(this, "_update_tree");
  135. undo_redo->add_do_method(this, "emit_signal", "node_changed");
  136. undo_redo->add_undo_method(this, "emit_signal", "node_changed");
  137. }
  138. undo_redo->commit_action();
  139. } else if (p_id == BUTTON_WARNING) {
  140. const PackedStringArray warnings = _get_node_configuration_warnings(n);
  141. if (warnings.is_empty()) {
  142. return;
  143. }
  144. // Improve looks on tooltip, extra spacing on non-bullet point newlines.
  145. const String bullet_point = U"• ";
  146. String all_warnings;
  147. for (const String &w : warnings) {
  148. all_warnings += "\n" + bullet_point + w;
  149. }
  150. // Limit the line width while keeping some padding.
  151. // It is not efficient, but it does not have to be.
  152. const PackedInt32Array boundaries = TS->string_get_word_breaks(all_warnings, "", 80);
  153. PackedStringArray lines;
  154. for (int i = 0; i < boundaries.size(); i += 2) {
  155. const int start = boundaries[i];
  156. const int end = boundaries[i + 1];
  157. const String line = all_warnings.substr(start, end - start);
  158. lines.append(line);
  159. }
  160. // We don't want the first two newlines.
  161. all_warnings = String("\n").join(lines).indent(" ").replace(U" •", U"\n•").substr(2);
  162. warning->set_text(all_warnings);
  163. warning->popup_centered();
  164. } else if (p_id == BUTTON_SIGNALS) {
  165. editor_selection->clear();
  166. editor_selection->add_node(n);
  167. set_selected(n);
  168. EditorDockManager::get_singleton()->focus_dock(NodeDock::get_singleton());
  169. NodeDock::get_singleton()->show_connections();
  170. } else if (p_id == BUTTON_GROUPS) {
  171. editor_selection->clear();
  172. editor_selection->add_node(n);
  173. set_selected(n);
  174. EditorDockManager::get_singleton()->focus_dock(NodeDock::get_singleton());
  175. NodeDock::get_singleton()->show_groups();
  176. } else if (p_id == BUTTON_UNIQUE) {
  177. bool ask_before_revoking_unique_name = EDITOR_GET("docks/scene_tree/ask_before_revoking_unique_name");
  178. revoke_node = n;
  179. if (ask_before_revoking_unique_name) {
  180. String msg = vformat(TTR("Revoke unique name for node \"%s\"?"), n->get_name());
  181. ask_before_revoke_checkbox->set_pressed(false);
  182. revoke_dialog_label->set_text(msg);
  183. revoke_dialog->reset_size();
  184. revoke_dialog->popup_centered();
  185. } else {
  186. _revoke_unique_name();
  187. }
  188. }
  189. }
  190. void SceneTreeEditor::_update_ask_before_revoking_unique_name() {
  191. if (ask_before_revoke_checkbox->is_pressed()) {
  192. EditorSettings::get_singleton()->set("docks/scene_tree/ask_before_revoking_unique_name", false);
  193. ask_before_revoke_checkbox->set_pressed(false);
  194. }
  195. _revoke_unique_name();
  196. }
  197. void SceneTreeEditor::_revoke_unique_name() {
  198. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  199. undo_redo->create_action(TTR("Disable Scene Unique Name"));
  200. undo_redo->add_do_method(revoke_node, "set_unique_name_in_owner", false);
  201. undo_redo->add_undo_method(revoke_node, "set_unique_name_in_owner", true);
  202. undo_redo->add_do_method(this, "_update_tree");
  203. undo_redo->add_undo_method(this, "_update_tree");
  204. undo_redo->commit_action();
  205. }
  206. void SceneTreeEditor::_toggle_visible(Node *p_node) {
  207. if (p_node->has_method("is_visible") && p_node->has_method("set_visible")) {
  208. bool v = bool(p_node->call("is_visible"));
  209. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  210. undo_redo->add_do_method(p_node, "set_visible", !v);
  211. undo_redo->add_undo_method(p_node, "set_visible", v);
  212. }
  213. }
  214. void SceneTreeEditor::_update_node_path(Node *p_node, bool p_recursive) {
  215. if (!p_node) {
  216. return;
  217. }
  218. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node);
  219. if (!I) {
  220. return;
  221. }
  222. I->value.item->set_metadata(0, p_node->get_path());
  223. if (!p_recursive) {
  224. return;
  225. }
  226. int cc = p_node->get_child_count(false);
  227. for (int i = 0; i < cc; i++) {
  228. Node *c = p_node->get_child(i, false);
  229. _update_node_path(c, p_recursive);
  230. }
  231. }
  232. void SceneTreeEditor::_update_node_subtree(Node *p_node, TreeItem *p_parent, bool p_force) {
  233. if (!p_node) {
  234. return;
  235. }
  236. // Only owned nodes are editable, since nodes can create their own (manually owned) child nodes,
  237. // which the editor needs not to know about.
  238. bool part_of_subscene = false;
  239. if (!display_foreign && p_node->get_owner() != get_scene_node() && p_node != get_scene_node()) {
  240. if ((show_enabled_subscene || can_open_instance) && p_node->get_owner() && (get_scene_node()->is_editable_instance(p_node->get_owner()))) {
  241. part_of_subscene = true;
  242. // Allow.
  243. } else {
  244. // Stale node, remove recursively.
  245. node_cache.remove(p_node, true);
  246. return;
  247. }
  248. } else {
  249. 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;
  250. }
  251. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node);
  252. TreeItem *item = nullptr;
  253. bool is_new = false;
  254. if (I) {
  255. item = I->value.item;
  256. TreeItem *current_parent = item->get_parent();
  257. // Our parent might be re-created because of a changed type.
  258. if (p_parent && p_parent != current_parent) {
  259. if (current_parent) {
  260. current_parent->remove_child(item);
  261. }
  262. p_parent->add_child(item);
  263. I->value.removed = false;
  264. _move_node_item(p_parent, I);
  265. }
  266. if (I->value.has_moved_children) {
  267. _move_node_children(I);
  268. }
  269. } else {
  270. int index = -1;
  271. // Check to see if there is a root node for us to reuse.
  272. if (!p_parent) {
  273. item = tree->get_root();
  274. if (!item) {
  275. item = tree->create_item(nullptr);
  276. index = 0;
  277. }
  278. } else {
  279. index = p_node->get_index(false);
  280. item = tree->create_item(p_parent, index);
  281. }
  282. I = node_cache.add(p_node, item);
  283. I->value.index = index;
  284. is_new = true;
  285. }
  286. if (!(p_force || I->value.dirty)) {
  287. // Nothing to do.
  288. return;
  289. }
  290. _update_node(p_node, item, part_of_subscene);
  291. I->value.dirty = false;
  292. I->value.can_process = p_node->can_process();
  293. // Force update all our children if we are new or if we were forced to update.
  294. bool force_update_children = p_force || is_new;
  295. // Update all our children.
  296. for (int i = 0; i < p_node->get_child_count(false); i++) {
  297. _update_node_subtree(p_node->get_child(i, false), item, force_update_children);
  298. }
  299. if (valid_types.size()) {
  300. bool valid = false;
  301. for (const StringName &E : valid_types) {
  302. if (p_node->is_class(E) ||
  303. EditorNode::get_singleton()->is_object_of_custom_type(p_node, E)) {
  304. valid = true;
  305. break;
  306. } else {
  307. Ref<Script> node_script = p_node->get_script();
  308. while (node_script.is_valid()) {
  309. if (node_script->get_path() == E) {
  310. valid = true;
  311. break;
  312. }
  313. node_script = node_script->get_base_script();
  314. }
  315. if (valid) {
  316. break;
  317. }
  318. }
  319. }
  320. if (!valid) {
  321. _set_item_custom_color(item, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  322. item->set_selectable(0, false);
  323. item->deselect(0);
  324. if (selected == p_node) {
  325. selected = nullptr;
  326. }
  327. }
  328. }
  329. }
  330. void SceneTreeEditor::_update_node(Node *p_node, TreeItem *p_item, bool p_part_of_subscene) {
  331. // Reset item properties that are not explicitly set in the default case.
  332. p_item->clear_buttons();
  333. p_item->remove_meta(SNAME("custom_color"));
  334. p_item->clear_custom_color(0);
  335. p_item->set_selectable(0, true);
  336. p_item->set_text(0, p_node->get_name());
  337. p_item->set_text_overrun_behavior(0, TextServer::OVERRUN_NO_TRIMMING);
  338. if (can_rename && !p_part_of_subscene) {
  339. p_item->set_editable(0, true);
  340. }
  341. if (can_rename) {
  342. bool collapsed = p_node->is_displayed_folded();
  343. if (collapsed) {
  344. p_item->set_collapsed(true);
  345. }
  346. }
  347. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(p_node, "Node");
  348. p_item->set_icon(0, icon);
  349. p_item->set_metadata(0, p_node->get_path());
  350. if (!p_node->is_connected("child_order_changed", callable_mp(this, &SceneTreeEditor::_node_child_order_changed))) {
  351. p_node->connect("child_order_changed", callable_mp(this, &SceneTreeEditor::_node_child_order_changed).bind(p_node));
  352. }
  353. if (!p_node->is_connected("editor_state_changed", callable_mp(this, &SceneTreeEditor::_node_editor_state_changed))) {
  354. p_node->connect("editor_state_changed", callable_mp(this, &SceneTreeEditor::_node_editor_state_changed).bind(p_node));
  355. }
  356. if (connecting_signal || (can_open_instance && is_scene_tree_dock)) {
  357. if (!p_node->is_connected(CoreStringName(script_changed), callable_mp(this, &SceneTreeEditor::_node_script_changed))) {
  358. p_node->connect(CoreStringName(script_changed), callable_mp(this, &SceneTreeEditor::_node_script_changed).bind(p_node));
  359. }
  360. }
  361. if (connecting_signal) {
  362. // Add script icons for all scripted nodes.
  363. Ref<Script> scr = p_node->get_script();
  364. if (scr.is_valid()) {
  365. p_item->add_button(0, get_editor_theme_icon(SNAME("Script")), BUTTON_SCRIPT);
  366. if (EditorNode::get_singleton()->get_object_custom_type_base(p_node) == scr) {
  367. // Disable button on custom scripts (pure visual cue).
  368. p_item->set_button_disabled(0, p_item->get_button_count(0) - 1, true);
  369. }
  370. }
  371. }
  372. if (connect_to_script_mode) {
  373. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  374. Ref<Script> scr = p_node->get_script();
  375. bool has_custom_script = scr.is_valid() && EditorNode::get_singleton()->get_object_custom_type_base(p_node) == scr;
  376. if (scr.is_null() || has_custom_script) {
  377. _set_item_custom_color(p_item, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  378. p_item->set_selectable(0, false);
  379. accent.a *= 0.7;
  380. }
  381. if (marked.has(p_node)) {
  382. String node_name = p_node->get_name();
  383. if (connecting_signal) {
  384. node_name += " " + TTR("(Connecting From)");
  385. }
  386. p_item->set_text(0, node_name);
  387. _set_item_custom_color(p_item, accent);
  388. }
  389. } else if (p_part_of_subscene) {
  390. if (valid_types.size() == 0) {
  391. _set_item_custom_color(p_item, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  392. }
  393. } else if (marked.has(p_node)) {
  394. String node_name = p_node->get_name();
  395. if (connecting_signal) {
  396. node_name += " " + TTR("(Connecting From)");
  397. }
  398. p_item->set_text(0, node_name);
  399. p_item->set_selectable(0, marked_selectable);
  400. _set_item_custom_color(p_item, get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
  401. } else if (!p_node->can_process()) {
  402. _set_item_custom_color(p_item, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  403. } else if (!marked_selectable && !marked_children_selectable) {
  404. Node *node = p_node;
  405. while (node) {
  406. if (marked.has(node)) {
  407. p_item->set_selectable(0, false);
  408. _set_item_custom_color(p_item, get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  409. break;
  410. }
  411. node = node->get_parent();
  412. }
  413. }
  414. if (can_rename) { // TODO Should be can edit..
  415. const PackedStringArray warnings = _get_node_configuration_warnings(p_node);
  416. const int num_warnings = warnings.size();
  417. if (num_warnings > 0) {
  418. StringName warning_icon;
  419. if (num_warnings == 1) {
  420. warning_icon = SNAME("NodeWarning");
  421. } else if (num_warnings <= 3) {
  422. warning_icon = vformat("NodeWarnings%d", num_warnings);
  423. } else {
  424. warning_icon = SNAME("NodeWarnings4Plus");
  425. }
  426. // Improve looks on tooltip, extra spacing on non-bullet point newlines.
  427. const String bullet_point = U"• ";
  428. String all_warnings;
  429. for (const String &w : warnings) {
  430. all_warnings += "\n\n" + bullet_point + w.replace("\n", "\n ");
  431. }
  432. if (num_warnings == 1) {
  433. all_warnings.remove_at(0); // With only one warning, two newlines do not look great.
  434. }
  435. p_item->add_button(0, get_editor_theme_icon(warning_icon), BUTTON_WARNING, false, TTR("Node configuration warning:") + all_warnings);
  436. }
  437. if (p_node->is_unique_name_in_owner()) {
  438. const bool disabled = p_node->get_owner() != EditorNode::get_singleton()->get_edited_scene();
  439. String button_text = vformat(TTR("This node can be accessed from anywhere within the scene it belongs to by using the '%s' prefix in the node path."), UNIQUE_NODE_PREFIX);
  440. if (!disabled) {
  441. button_text += "\n" + TTR("Click to disable this.");
  442. }
  443. p_item->add_button(0, get_editor_theme_icon(SNAME("SceneUniqueName")), BUTTON_UNIQUE, disabled, button_text);
  444. }
  445. int num_connections = p_node->get_persistent_signal_connection_count();
  446. int num_groups = p_node->get_persistent_group_count();
  447. String msg_temp;
  448. if (num_connections >= 1) {
  449. Array arr;
  450. arr.push_back(num_connections);
  451. msg_temp += TTRN("Node has one connection.", "Node has {num} connections.", num_connections).format(arr, "{num}");
  452. if (num_groups >= 1) {
  453. msg_temp += "\n";
  454. }
  455. }
  456. if (num_groups >= 1) {
  457. msg_temp += TTRN("Node is in this group:", "Node is in the following groups:", num_groups) + "\n";
  458. List<GroupInfo> groups;
  459. p_node->get_groups(&groups);
  460. for (const GroupInfo &E : groups) {
  461. if (E.persistent) {
  462. msg_temp += String::utf8("• ") + String(E.name) + "\n";
  463. }
  464. }
  465. } else {
  466. msg_temp += "\n";
  467. }
  468. Ref<Texture2D> icon_temp;
  469. SceneTreeEditorButton signal_temp = BUTTON_SIGNALS;
  470. String msg_temp_end = TTR("Click to show signals dock.");
  471. if (num_connections >= 1 && num_groups >= 1) {
  472. icon_temp = get_editor_theme_icon(SNAME("SignalsAndGroups"));
  473. } else if (num_connections >= 1) {
  474. icon_temp = get_editor_theme_icon(SNAME("Signals"));
  475. } else if (num_groups >= 1) {
  476. icon_temp = get_editor_theme_icon(SNAME("Groups"));
  477. signal_temp = BUTTON_GROUPS;
  478. msg_temp_end = TTR("Click to show groups dock.");
  479. }
  480. if (num_connections >= 1 || num_groups >= 1) {
  481. msg_temp += msg_temp_end;
  482. p_item->add_button(0, icon_temp, signal_temp, false, msg_temp);
  483. }
  484. }
  485. {
  486. _update_node_tooltip(p_node, p_item);
  487. Callable delay_update_tooltip = callable_mp(this, &SceneTreeEditor::_queue_update_node_tooltip);
  488. if (p_node->is_connected("editor_description_changed", delay_update_tooltip)) {
  489. p_node->disconnect("editor_description_changed", delay_update_tooltip);
  490. }
  491. p_node->connect("editor_description_changed", delay_update_tooltip.bind(p_item));
  492. }
  493. // Show buttons only when necessary (SceneTreeDock) to avoid crashes.
  494. if (can_open_instance && is_scene_tree_dock) {
  495. Ref<Script> scr = p_node->get_script();
  496. if (!scr.is_null()) {
  497. String additional_notes;
  498. Color button_color = Color(1, 1, 1);
  499. // Can't set tooltip after adding button, need to do it before.
  500. if (scr->is_tool()) {
  501. additional_notes += "\n" + TTR("This script is currently running in the editor.");
  502. button_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  503. }
  504. if (EditorNode::get_singleton()->get_object_custom_type_base(p_node) == scr) {
  505. additional_notes += "\n" + TTR("This script is a custom type.");
  506. button_color.a = 0.5;
  507. }
  508. p_item->add_button(0, get_editor_theme_icon(SNAME("Script")), BUTTON_SCRIPT, false, TTR("Open Script:") + " " + scr->get_path() + additional_notes);
  509. p_item->set_button_color(0, p_item->get_button_count(0) - 1, button_color);
  510. }
  511. if (p_node->has_meta("_edit_lock_")) {
  512. p_item->add_button(0, get_editor_theme_icon(SNAME("Lock")), BUTTON_LOCK, false, TTR("Node is locked.\nClick to unlock it."));
  513. }
  514. if (p_node->has_meta("_edit_group_")) {
  515. p_item->add_button(0, get_editor_theme_icon(SNAME("Group")), BUTTON_GROUP, false, TTR("Children are not selectable.\nClick to make them selectable."));
  516. }
  517. if (p_node->has_method("is_visible") && p_node->has_method("set_visible") && p_node->has_signal(SceneStringName(visibility_changed))) {
  518. bool is_visible = p_node->call("is_visible");
  519. if (is_visible) {
  520. p_item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityVisible")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  521. } else {
  522. p_item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityHidden")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  523. }
  524. const Callable vis_changed = callable_mp(this, &SceneTreeEditor::_node_visibility_changed);
  525. if (!p_node->is_connected(SceneStringName(visibility_changed), vis_changed)) {
  526. p_node->connect(SceneStringName(visibility_changed), vis_changed.bind(p_node));
  527. }
  528. _update_visibility_color(p_node, p_item);
  529. }
  530. if (p_node->is_class("AnimationMixer")) {
  531. bool is_pinned = AnimationPlayerEditor::get_singleton()->get_editing_node() == p_node && AnimationPlayerEditor::get_singleton()->is_pinned();
  532. if (is_pinned) {
  533. p_item->add_button(0, get_editor_theme_icon(SNAME("Pin")), BUTTON_PIN, false, TTR("AnimationPlayer is pinned.\nClick to unpin."));
  534. }
  535. }
  536. }
  537. if (editor_selection) {
  538. if (editor_selection->is_selected(p_node)) {
  539. p_item->select(0);
  540. }
  541. }
  542. if (selected == p_node) {
  543. if (!editor_selection) {
  544. p_item->select(0);
  545. }
  546. p_item->set_as_cursor(0);
  547. }
  548. }
  549. void SceneTreeEditor::_update_if_clean() {
  550. if (tree_dirty) {
  551. return;
  552. }
  553. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  554. tree_dirty = true;
  555. }
  556. void SceneTreeEditor::_queue_update_node_tooltip(Node *p_node, TreeItem *p_item) {
  557. Callable update_tooltip = callable_mp(this, &SceneTreeEditor::_update_node_tooltip);
  558. if (update_node_tooltip_delay->is_connected("timeout", update_tooltip)) {
  559. update_node_tooltip_delay->disconnect("timeout", update_tooltip);
  560. }
  561. update_node_tooltip_delay->connect("timeout", update_tooltip.bind(p_node, p_item));
  562. update_node_tooltip_delay->start();
  563. }
  564. void SceneTreeEditor::_update_node_tooltip(Node *p_node, TreeItem *p_item) {
  565. // Display the node name in all tooltips so that long node names can be previewed
  566. // without having to rename them.
  567. String tooltip = p_node->get_name();
  568. if (p_node == get_scene_node() && p_node->get_scene_inherited_state().is_valid()) {
  569. if (p_item->get_button_by_id(0, BUTTON_SUBSCENE) == -1) {
  570. p_item->add_button(0, get_editor_theme_icon(SNAME("InstanceOptions")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
  571. }
  572. tooltip += String("\n" + TTR("Inherits:") + " " + p_node->get_scene_inherited_state()->get_path());
  573. } else if (p_node != get_scene_node() && !p_node->get_scene_file_path().is_empty() && can_open_instance) {
  574. if (p_item->get_button_by_id(0, BUTTON_SUBSCENE) == -1) {
  575. p_item->add_button(0, get_editor_theme_icon(SNAME("InstanceOptions")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
  576. }
  577. tooltip += String("\n" + TTR("Instance:") + " " + p_node->get_scene_file_path());
  578. }
  579. StringName custom_type = EditorNode::get_singleton()->get_object_custom_type_name(p_node);
  580. tooltip += "\n" + TTR("Type:") + " " + (custom_type != StringName() ? String(custom_type) : p_node->get_class());
  581. if (!p_node->get_editor_description().is_empty()) {
  582. const PackedInt32Array boundaries = TS->string_get_word_breaks(p_node->get_editor_description(), "", 80);
  583. tooltip += "\n";
  584. for (int i = 0; i < boundaries.size(); i += 2) {
  585. const int start = boundaries[i];
  586. const int end = boundaries[i + 1];
  587. tooltip += "\n" + p_node->get_editor_description().substr(start, end - start + 1).rstrip("\n");
  588. }
  589. }
  590. p_item->set_tooltip_text(0, tooltip);
  591. }
  592. void SceneTreeEditor::_node_visibility_changed(Node *p_node) {
  593. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node, false);
  594. if (!I) {
  595. // We leave these signals connected when switching tabs.
  596. // If the node is not in cache it was for a different tab.
  597. return;
  598. }
  599. if (!p_node || (p_node != get_scene_node() && !p_node->get_owner())) {
  600. return;
  601. }
  602. TreeItem *item = _find(tree->get_root(), p_node->get_path());
  603. if (!item) {
  604. return;
  605. }
  606. int idx = item->get_button_by_id(0, BUTTON_VISIBILITY);
  607. ERR_FAIL_COND(idx == -1);
  608. bool node_visible = false;
  609. if (p_node->has_method("is_visible")) {
  610. node_visible = p_node->call("is_visible");
  611. if (p_node->is_class("CanvasItem") || p_node->is_class("CanvasLayer") || p_node->is_class("Window")) {
  612. CanvasItemEditor::get_singleton()->get_viewport_control()->queue_redraw();
  613. }
  614. }
  615. if (node_visible) {
  616. item->set_button(0, idx, get_editor_theme_icon(SNAME("GuiVisibilityVisible")));
  617. } else {
  618. item->set_button(0, idx, get_editor_theme_icon(SNAME("GuiVisibilityHidden")));
  619. }
  620. _update_visibility_color(p_node, item);
  621. }
  622. void SceneTreeEditor::_update_visibility_color(Node *p_node, TreeItem *p_item) {
  623. if (p_node->has_method("is_visible_in_tree")) {
  624. Color color(1, 1, 1, 1);
  625. bool visible_on_screen = p_node->call("is_visible_in_tree");
  626. if (!visible_on_screen) {
  627. color.a = 0.6;
  628. }
  629. int idx = p_item->get_button_by_id(0, BUTTON_VISIBILITY);
  630. p_item->set_button_color(0, idx, color);
  631. }
  632. }
  633. void SceneTreeEditor::_set_item_custom_color(TreeItem *p_item, Color p_color) {
  634. p_item->set_custom_color(0, p_color);
  635. p_item->set_meta(SNAME("custom_color"), p_color);
  636. }
  637. void SceneTreeEditor::_node_script_changed(Node *p_node) {
  638. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node, false);
  639. if (!I) {
  640. // We leave these signals connected when switching tabs.
  641. // If the node is not in cache it was for a different tab.
  642. return;
  643. }
  644. node_cache.mark_dirty(p_node);
  645. _update_if_clean();
  646. }
  647. void SceneTreeEditor::_move_node_children(HashMap<Node *, CachedNode>::Iterator &p_I) {
  648. TreeItem *item = p_I->value.item;
  649. Node *node = p_I->key;
  650. int cc = node->get_child_count(false);
  651. for (int i = 0; i < cc; i++) {
  652. HashMap<Node *, CachedNode>::Iterator CI = node_cache.get(node->get_child(i, false));
  653. if (CI) {
  654. _move_node_item(item, CI);
  655. }
  656. }
  657. p_I->value.has_moved_children = false;
  658. }
  659. void SceneTreeEditor::_move_node_item(TreeItem *p_parent, HashMap<Node *, CachedNode>::Iterator &p_I) {
  660. if (!p_parent) {
  661. return;
  662. }
  663. Node *node = p_I->key;
  664. int current_node_index = node->get_index(false);
  665. int current_item_index = -1;
  666. TreeItem *item = p_I->value.item;
  667. if (item->get_parent() != p_parent) {
  668. TreeItem *p = item->get_parent();
  669. if (p) {
  670. item->get_parent()->remove_child(item);
  671. }
  672. p_parent->add_child(item);
  673. p_I->value.removed = false;
  674. current_item_index = p_parent->get_child_count() - 1;
  675. p_I->value.index = current_item_index;
  676. }
  677. if (p_I->value.index != current_node_index) {
  678. // If we just re-parented we know our index.
  679. if (current_item_index == -1) {
  680. current_item_index = item->get_index();
  681. }
  682. // Are we already in the right place?
  683. if (current_node_index == current_item_index) {
  684. p_I->value.index = current_node_index;
  685. return;
  686. }
  687. // Are we the first node?
  688. if (current_node_index == 0) {
  689. // There has to be at least 1 other node, otherwise we would not have gotten here.
  690. TreeItem *neighbor_item = p_parent->get_child(0);
  691. item->move_before(neighbor_item);
  692. } else {
  693. TreeItem *neighbor_item = p_parent->get_child(CLAMP(current_node_index - 1, 0, p_parent->get_child_count() - 1));
  694. item->move_after(neighbor_item);
  695. }
  696. p_I->value.index = current_node_index;
  697. }
  698. }
  699. void SceneTreeEditor::_node_child_order_changed(Node *p_node) {
  700. // Do not try to change children on nodes currently marked for removal.
  701. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node, false);
  702. if (I) {
  703. node_cache.mark_dirty(I->key);
  704. I->value.has_moved_children = true;
  705. }
  706. _update_if_clean();
  707. }
  708. void SceneTreeEditor::_node_editor_state_changed(Node *p_node) {
  709. node_cache.mark_dirty(p_node);
  710. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node, false);
  711. if (I) {
  712. if (p_node->is_inside_tree() && p_node->can_process() != I->value.can_process) {
  713. // All our children also change process mode.
  714. node_cache.mark_children_dirty(p_node, true);
  715. }
  716. }
  717. _update_if_clean();
  718. }
  719. void SceneTreeEditor::_node_added(Node *p_node) {
  720. if (!get_scene_node()) {
  721. return;
  722. }
  723. if (p_node != get_scene_node() && !get_scene_node()->is_ancestor_of(p_node)) {
  724. return;
  725. }
  726. node_cache.mark_dirty(p_node);
  727. _update_if_clean();
  728. }
  729. void SceneTreeEditor::_node_removed(Node *p_node) {
  730. if (EditorNode::get_singleton()->is_exiting()) {
  731. return; // Speed up exit.
  732. }
  733. if (EditorNode::get_singleton()->is_changing_scene()) {
  734. return; // Switching tabs we will be destroying node cache anyway.
  735. }
  736. if (!get_scene_node()) {
  737. return;
  738. }
  739. if (p_node != get_scene_node() && !get_scene_node()->is_ancestor_of(p_node)) {
  740. return;
  741. }
  742. node_cache.remove(p_node);
  743. _update_if_clean();
  744. }
  745. void SceneTreeEditor::_node_renamed(Node *p_node) {
  746. if (!get_scene_node()) {
  747. return;
  748. }
  749. if (p_node != get_scene_node() && !get_scene_node()->is_ancestor_of(p_node)) {
  750. return;
  751. }
  752. node_cache.mark_dirty(p_node);
  753. // Recursively update child node paths.
  754. _update_node_path(p_node, true);
  755. emit_signal(SNAME("node_renamed"));
  756. _update_if_clean();
  757. }
  758. void SceneTreeEditor::_update_tree(bool p_scroll_to_selected) {
  759. if (!is_inside_tree()) {
  760. tree_dirty = false;
  761. return;
  762. }
  763. if (!update_when_invisible && !is_visible_in_tree()) {
  764. return;
  765. }
  766. if (tree->is_editing()) {
  767. return;
  768. }
  769. updating_tree = true;
  770. last_hash = hash_djb2_one_64(0);
  771. Node *scene_node = get_scene_node();
  772. if (node_cache.current_scene_node != scene_node) {
  773. _reset();
  774. node_cache.current_scene_node = scene_node;
  775. node_cache.force_update = true;
  776. }
  777. if (node_cache.current_scene_node) {
  778. // Handle pinning/unpinning the animation player only do this once per iteration.
  779. Node *pinned_node = AnimationPlayerEditor::get_singleton()->get_editing_node();
  780. // If pinned state changed, update the currently pinned node.
  781. if (AnimationPlayerEditor::get_singleton()->is_pinned() != node_cache.current_has_pin) {
  782. node_cache.current_has_pin = AnimationPlayerEditor::get_singleton()->is_pinned();
  783. node_cache.mark_dirty(pinned_node);
  784. }
  785. // If the current pinned node changed update both the old and new node.
  786. if (node_cache.current_pinned_node != pinned_node) {
  787. node_cache.mark_dirty(pinned_node);
  788. node_cache.mark_dirty(node_cache.current_pinned_node);
  789. node_cache.current_pinned_node = pinned_node;
  790. }
  791. _update_node_subtree(get_scene_node(), nullptr, node_cache.force_update);
  792. _compute_hash(get_scene_node(), last_hash);
  793. node_cache.delete_pending();
  794. }
  795. updating_tree = false;
  796. tree_dirty = false;
  797. node_cache.force_update = false;
  798. if (!filter.strip_edges().is_empty() || !show_all_nodes) {
  799. _update_filter(nullptr, p_scroll_to_selected);
  800. }
  801. }
  802. bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_selected) {
  803. if (!p_parent) {
  804. p_parent = tree->get_root();
  805. filter_term_warning.clear();
  806. }
  807. if (!p_parent) {
  808. // Tree is empty, nothing to do here.
  809. return false;
  810. }
  811. bool keep_for_children = false;
  812. for (TreeItem *child = p_parent->get_first_child(); child; child = child->get_next()) {
  813. // Always keep if at least one of the children are kept.
  814. keep_for_children = _update_filter(child, p_scroll_to_selected) || keep_for_children;
  815. }
  816. // Now find other reasons to keep this Node, too.
  817. PackedStringArray terms = filter.to_lower().split_spaces();
  818. bool keep = _item_matches_all_terms(p_parent, terms);
  819. bool selectable = keep;
  820. if (keep && !valid_types.is_empty()) {
  821. selectable = false;
  822. Node *n = get_node(p_parent->get_metadata(0));
  823. for (const StringName &E : valid_types) {
  824. if (n->is_class(E) ||
  825. EditorNode::get_singleton()->is_object_of_custom_type(n, E)) {
  826. selectable = true;
  827. break;
  828. } else {
  829. Ref<Script> node_script = n->get_script();
  830. while (node_script.is_valid()) {
  831. if (node_script->get_path() == E) {
  832. selectable = true;
  833. break;
  834. }
  835. node_script = node_script->get_base_script();
  836. }
  837. if (selectable) {
  838. break;
  839. }
  840. }
  841. }
  842. }
  843. if (show_all_nodes) {
  844. p_parent->set_visible(keep_for_children || keep);
  845. } else {
  846. // Show only selectable nodes, or parents of selectable.
  847. p_parent->set_visible(keep_for_children || selectable);
  848. }
  849. if (selectable) {
  850. Color custom_color = p_parent->get_meta(SNAME("custom_color"), Color(0, 0, 0, 0));
  851. if (custom_color == Color(0, 0, 0, 0)) {
  852. p_parent->clear_custom_color(0);
  853. } else {
  854. p_parent->set_custom_color(0, custom_color);
  855. }
  856. p_parent->set_selectable(0, true);
  857. } else if (keep_for_children) {
  858. p_parent->set_custom_color(0, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  859. p_parent->set_selectable(0, false);
  860. p_parent->deselect(0);
  861. }
  862. if (editor_selection) {
  863. Node *n = get_node(p_parent->get_metadata(0));
  864. if (selectable) {
  865. if (p_scroll_to_selected && n && editor_selection->is_selected(n)) {
  866. tree->scroll_to_item(p_parent);
  867. }
  868. } else {
  869. if (n && p_parent->is_selected(0)) {
  870. editor_selection->remove_node(n);
  871. p_parent->deselect(0);
  872. }
  873. }
  874. }
  875. return p_parent->is_visible();
  876. }
  877. bool SceneTreeEditor::_item_matches_all_terms(TreeItem *p_item, const PackedStringArray &p_terms) {
  878. if (p_terms.is_empty()) {
  879. return true;
  880. }
  881. for (int i = 0; i < p_terms.size(); i++) {
  882. const String &term = p_terms[i];
  883. // Recognize special filter.
  884. if (term.contains_char(':') && !term.get_slicec(':', 0).is_empty()) {
  885. String parameter = term.get_slicec(':', 0);
  886. String argument = term.get_slicec(':', 1);
  887. if (parameter == "type" || parameter == "t") {
  888. // Filter by Type.
  889. String type = get_node(p_item->get_metadata(0))->get_class();
  890. bool term_in_inherited_class = false;
  891. // Every Node is a Node, duh!
  892. while (type != "Node") {
  893. if (type.to_lower().contains(argument)) {
  894. term_in_inherited_class = true;
  895. break;
  896. }
  897. type = ClassDB::get_parent_class(type);
  898. }
  899. if (!term_in_inherited_class) {
  900. return false;
  901. }
  902. } else if (parameter == "group" || parameter == "g") {
  903. // Filter by Group.
  904. Node *node = get_node(p_item->get_metadata(0));
  905. if (argument.is_empty()) {
  906. // When argument is empty, match all Nodes belonging to any exposed group.
  907. if (node->get_persistent_group_count() == 0) {
  908. return false;
  909. }
  910. } else {
  911. List<Node::GroupInfo> group_info_list;
  912. node->get_groups(&group_info_list);
  913. bool term_in_groups = false;
  914. for (const Node::GroupInfo &group_info : group_info_list) {
  915. if (!group_info.persistent) {
  916. continue; // Ignore internal groups.
  917. }
  918. if (String(group_info.name).to_lower().contains(argument)) {
  919. term_in_groups = true;
  920. break;
  921. }
  922. }
  923. if (!term_in_groups) {
  924. return false;
  925. }
  926. }
  927. } else if (filter_term_warning.is_empty()) {
  928. filter_term_warning = vformat(TTR("\"%s\" is not a known filter."), parameter);
  929. continue;
  930. }
  931. } else {
  932. // Default.
  933. if (!p_item->get_text(0).to_lower().contains(term)) {
  934. return false;
  935. }
  936. }
  937. }
  938. return true;
  939. }
  940. void SceneTreeEditor::_compute_hash(Node *p_node, uint64_t &hash) {
  941. // Nodes are added and removed by Node* pointers.
  942. hash = hash_djb2_one_64((ptrdiff_t)p_node, hash);
  943. // This hash is non-commutative: if the node order changes so will the hash.
  944. for (int i = 0; i < p_node->get_child_count(); i++) {
  945. _compute_hash(p_node->get_child(i), hash);
  946. }
  947. }
  948. void SceneTreeEditor::_reset() {
  949. tree->clear();
  950. node_cache.clear();
  951. }
  952. void SceneTreeEditor::_test_update_tree() {
  953. pending_test_update = false;
  954. if (!is_inside_tree()) {
  955. return;
  956. }
  957. if (tree_dirty) {
  958. return; // Don't even bother.
  959. }
  960. uint64_t hash = hash_djb2_one_64(0);
  961. if (get_scene_node()) {
  962. _compute_hash(get_scene_node(), hash);
  963. }
  964. // Test hash.
  965. if (hash == last_hash) {
  966. return; // Did not change.
  967. }
  968. _update_if_clean();
  969. }
  970. void SceneTreeEditor::_tree_process_mode_changed() {
  971. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  972. tree_dirty = true;
  973. }
  974. void SceneTreeEditor::_tree_changed() {
  975. if (EditorNode::get_singleton()->is_exiting()) {
  976. return; // Speed up exit.
  977. }
  978. if (pending_test_update) {
  979. return;
  980. }
  981. if (tree_dirty) {
  982. return;
  983. }
  984. callable_mp(this, &SceneTreeEditor::_test_update_tree).call_deferred();
  985. pending_test_update = true;
  986. }
  987. void SceneTreeEditor::_selected_changed() {
  988. TreeItem *s = tree->get_selected();
  989. ERR_FAIL_NULL(s);
  990. NodePath np = s->get_metadata(0);
  991. Node *n = get_node(np);
  992. if (n == selected) {
  993. return;
  994. }
  995. selected = n;
  996. blocked++;
  997. emit_signal(SNAME("node_selected"));
  998. blocked--;
  999. }
  1000. void SceneTreeEditor::_deselect_items() {
  1001. // Clear currently selected items in scene tree dock.
  1002. if (editor_selection) {
  1003. editor_selection->clear();
  1004. emit_signal(SNAME("node_changed"));
  1005. }
  1006. }
  1007. void SceneTreeEditor::_cell_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  1008. TreeItem *item = Object::cast_to<TreeItem>(p_object);
  1009. ERR_FAIL_NULL(item);
  1010. if (!item->is_visible()) {
  1011. return;
  1012. }
  1013. NodePath np = item->get_metadata(0);
  1014. Node *n = get_node(np);
  1015. if (!n) {
  1016. return;
  1017. }
  1018. if (!editor_selection) {
  1019. return;
  1020. }
  1021. if (p_selected) {
  1022. editor_selection->add_node(n);
  1023. } else {
  1024. editor_selection->remove_node(n);
  1025. }
  1026. // Emitted "selected" in _selected_changed() when select single node, so select multiple node emit "changed".
  1027. if (editor_selection->get_selected_nodes().size() > 1) {
  1028. emit_signal(SNAME("node_changed"));
  1029. }
  1030. }
  1031. void SceneTreeEditor::_tree_scroll_to_item(ObjectID p_item_id) {
  1032. ERR_FAIL_NULL(tree);
  1033. TreeItem *item = Object::cast_to<TreeItem>(ObjectDB::get_instance(p_item_id));
  1034. if (item) {
  1035. tree->scroll_to_item(item, true);
  1036. }
  1037. }
  1038. void SceneTreeEditor::_notification(int p_what) {
  1039. switch (p_what) {
  1040. case NOTIFICATION_ENTER_TREE: {
  1041. get_tree()->connect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
  1042. get_tree()->connect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
  1043. get_tree()->connect("node_added", callable_mp(this, &SceneTreeEditor::_node_added));
  1044. get_tree()->connect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
  1045. get_tree()->connect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
  1046. get_tree()->connect(SceneStringName(node_configuration_warning_changed), callable_mp(this, &SceneTreeEditor::_warning_changed));
  1047. tree->connect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
  1048. _update_tree();
  1049. } break;
  1050. case NOTIFICATION_EXIT_TREE: {
  1051. get_tree()->disconnect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
  1052. get_tree()->disconnect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
  1053. get_tree()->disconnect("node_added", callable_mp(this, &SceneTreeEditor::_node_added));
  1054. get_tree()->disconnect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
  1055. get_tree()->disconnect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
  1056. tree->disconnect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
  1057. get_tree()->disconnect(SceneStringName(node_configuration_warning_changed), callable_mp(this, &SceneTreeEditor::_warning_changed));
  1058. } break;
  1059. case NOTIFICATION_THEME_CHANGED: {
  1060. tree->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
  1061. // When we change theme we need to re-do everything.
  1062. _reset();
  1063. _update_tree();
  1064. } break;
  1065. case NOTIFICATION_VISIBILITY_CHANGED: {
  1066. if (is_visible()) {
  1067. TreeItem *item = nullptr;
  1068. if (selected) {
  1069. // Scroll to selected node.
  1070. item = _find(tree->get_root(), selected->get_path());
  1071. } else if (marked.size() == 1) {
  1072. // Scroll to a single marked node.
  1073. Node *marked_node = *marked.begin();
  1074. if (marked_node) {
  1075. item = _find(tree->get_root(), marked_node->get_path());
  1076. }
  1077. }
  1078. bool has_item = item;
  1079. if (update_when_invisible) {
  1080. if (has_item) {
  1081. ObjectID item_id = item->get_instance_id();
  1082. callable_mp(this, &SceneTreeEditor::_tree_scroll_to_item).call_deferred(item_id);
  1083. }
  1084. } else {
  1085. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(has_item);
  1086. }
  1087. }
  1088. } break;
  1089. }
  1090. }
  1091. TreeItem *SceneTreeEditor::_find(TreeItem *p_node, const NodePath &p_path) {
  1092. if (!p_node) {
  1093. return nullptr;
  1094. }
  1095. NodePath np = p_node->get_metadata(0);
  1096. if (np == p_path) {
  1097. return p_node;
  1098. }
  1099. TreeItem *children = p_node->get_first_child();
  1100. while (children) {
  1101. TreeItem *n = _find(children, p_path);
  1102. if (n) {
  1103. return n;
  1104. }
  1105. children = children->get_next();
  1106. }
  1107. return nullptr;
  1108. }
  1109. void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
  1110. ERR_FAIL_COND(blocked > 0);
  1111. if (pending_test_update) {
  1112. _test_update_tree();
  1113. }
  1114. if (tree_dirty) {
  1115. _update_tree();
  1116. }
  1117. if (selected == p_node) {
  1118. return;
  1119. }
  1120. TreeItem *item = p_node ? _find(tree->get_root(), p_node->get_path()) : nullptr;
  1121. if (item) {
  1122. selected = p_node;
  1123. if (auto_expand_selected) {
  1124. // Make visible when it's collapsed.
  1125. TreeItem *node = item->get_parent();
  1126. while (node && node != tree->get_root()) {
  1127. node->set_collapsed(false);
  1128. node = node->get_parent();
  1129. }
  1130. item->select(0);
  1131. item->set_as_cursor(0);
  1132. tree->ensure_cursor_is_visible();
  1133. } else {
  1134. // Ensure the node is selected and visible for the user if the node
  1135. // is not collapsed.
  1136. bool collapsed = false;
  1137. TreeItem *node = item;
  1138. while (node && node != tree->get_root()) {
  1139. if (node->is_collapsed()) {
  1140. collapsed = true;
  1141. break;
  1142. }
  1143. node = node->get_parent();
  1144. }
  1145. if (!collapsed) {
  1146. item->select(0);
  1147. item->set_as_cursor(0);
  1148. tree->ensure_cursor_is_visible();
  1149. }
  1150. }
  1151. } else {
  1152. if (!p_node) {
  1153. selected = nullptr;
  1154. }
  1155. selected = p_node;
  1156. }
  1157. if (p_emit_selected) {
  1158. emit_signal(SNAME("node_selected"));
  1159. }
  1160. }
  1161. void SceneTreeEditor::rename_node(Node *p_node, const String &p_name, TreeItem *p_item) {
  1162. TreeItem *item;
  1163. if (p_item) {
  1164. item = p_item; // During batch rename the paths may change, so using _find() is unreliable.
  1165. } else {
  1166. item = _find(tree->get_root(), p_node->get_path());
  1167. }
  1168. ERR_FAIL_NULL(item);
  1169. String new_name = p_name.validate_node_name();
  1170. if (new_name != p_name) {
  1171. String text = TTR("Invalid node name, the following characters are not allowed:") + "\n" + String::get_invalid_node_name_characters();
  1172. if (error->is_visible()) {
  1173. if (!error->get_meta("invalid_character", false)) {
  1174. error->set_text(error->get_text() + "\n\n" + text);
  1175. error->set_meta("invalid_character", true);
  1176. }
  1177. } else {
  1178. error->set_text(text);
  1179. error->set_meta("invalid_character", true);
  1180. error->set_meta("same_unique_name", false);
  1181. error->popup_centered();
  1182. }
  1183. }
  1184. // Trim leading/trailing whitespace to prevent node names from containing accidental whitespace,
  1185. // which would make it more difficult to get the node via `get_node()`.
  1186. new_name = new_name.strip_edges();
  1187. if (new_name.is_empty() && p_node->get_owner() != nullptr && !p_node->get_scene_file_path().is_empty()) {
  1188. // If name is empty and node is root of an instance, revert to the original name.
  1189. const Ref<PackedScene> node_scene = ResourceLoader::load(p_node->get_scene_file_path());
  1190. if (node_scene.is_valid()) {
  1191. const Ref<SceneState> &state = node_scene->get_state();
  1192. if (state->get_node_count() > 0) {
  1193. new_name = state->get_node_name(0); // Root's name.
  1194. }
  1195. }
  1196. }
  1197. if (new_name.is_empty()) {
  1198. // If name is still empty, fallback to class name.
  1199. if (GLOBAL_GET("editor/naming/node_name_casing").operator int() != NAME_CASING_PASCAL_CASE) {
  1200. new_name = Node::adjust_name_casing(p_node->get_class());
  1201. } else {
  1202. new_name = p_node->get_class();
  1203. }
  1204. }
  1205. new_name = p_node->get_parent()->prevalidate_child_name(p_node, new_name);
  1206. if (new_name == p_node->get_name()) {
  1207. item->set_text(0, new_name);
  1208. return;
  1209. }
  1210. // We previously made sure name is not the same as current name
  1211. // so that it won't complain about already used unique name when not changing name.
  1212. if (p_node->is_unique_name_in_owner() && get_tree()->get_edited_scene_root()->get_node_or_null("%" + new_name)) {
  1213. String text = vformat(TTR("A node with the unique name %s already exists in this scene."), new_name);
  1214. if (error->is_visible()) {
  1215. if (!error->get_meta("same_unique_name", false)) {
  1216. error->set_text(error->get_text() + "\n\n" + text);
  1217. error->set_meta("same_unique_name", true);
  1218. }
  1219. } else {
  1220. error->set_text(text);
  1221. error->set_meta("same_unique_name", true);
  1222. error->set_meta("invalid_character", false);
  1223. error->popup_centered();
  1224. }
  1225. item->set_text(0, p_node->get_name());
  1226. return;
  1227. }
  1228. if (!is_scene_tree_dock) {
  1229. p_node->set_name(new_name);
  1230. item->set_metadata(0, p_node->get_path());
  1231. emit_signal(SNAME("node_renamed"));
  1232. } else {
  1233. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1234. undo_redo->create_action(TTR("Rename Node"), UndoRedo::MERGE_DISABLE, p_node);
  1235. emit_signal(SNAME("node_prerename"), p_node, new_name);
  1236. undo_redo->add_undo_method(p_node, "set_name", p_node->get_name());
  1237. undo_redo->add_undo_method(item, "set_metadata", 0, p_node->get_path());
  1238. undo_redo->add_undo_method(item, "set_text", 0, p_node->get_name());
  1239. p_node->set_name(new_name);
  1240. undo_redo->add_do_method(p_node, "set_name", new_name);
  1241. undo_redo->add_do_method(item, "set_metadata", 0, p_node->get_path());
  1242. undo_redo->add_do_method(item, "set_text", 0, new_name);
  1243. undo_redo->commit_action();
  1244. }
  1245. }
  1246. void SceneTreeEditor::_edited() {
  1247. TreeItem *which = tree->get_next_selected(nullptr);
  1248. ERR_FAIL_NULL(which);
  1249. TreeItem *edited = tree->get_edited();
  1250. ERR_FAIL_NULL(edited);
  1251. if (is_scene_tree_dock && tree->get_next_selected(which)) {
  1252. List<Node *> nodes_to_rename;
  1253. for (TreeItem *item = which; item; item = tree->get_next_selected(item)) {
  1254. Node *n = get_node(item->get_metadata(0));
  1255. ERR_FAIL_NULL(n);
  1256. nodes_to_rename.push_back(n);
  1257. }
  1258. ERR_FAIL_COND(nodes_to_rename.is_empty());
  1259. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1260. undo_redo->create_action(TTR("Rename Nodes"), UndoRedo::MERGE_DISABLE, nodes_to_rename.front()->get(), true);
  1261. TreeItem *item = which;
  1262. String new_name = edited->get_text(0);
  1263. for (Node *n : nodes_to_rename) {
  1264. rename_node(n, new_name, item);
  1265. item = tree->get_next_selected(item);
  1266. }
  1267. undo_redo->commit_action();
  1268. } else {
  1269. Node *n = get_node(which->get_metadata(0));
  1270. ERR_FAIL_NULL(n);
  1271. rename_node(n, which->get_text(0));
  1272. }
  1273. }
  1274. Node *SceneTreeEditor::get_selected() {
  1275. return selected;
  1276. }
  1277. void SceneTreeEditor::_update_marking_list(const HashSet<Node *> &p_marked) {
  1278. for (Node *N : p_marked) {
  1279. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(N);
  1280. if (I) {
  1281. node_cache.mark_dirty(N);
  1282. node_cache.mark_children_dirty(N, true);
  1283. }
  1284. }
  1285. }
  1286. void SceneTreeEditor::set_marked(const HashSet<Node *> &p_marked, bool p_selectable, bool p_children_selectable) {
  1287. _update_if_clean();
  1288. _update_marking_list(marked);
  1289. _update_marking_list(p_marked);
  1290. marked = p_marked;
  1291. marked_selectable = p_selectable;
  1292. marked_children_selectable = p_children_selectable;
  1293. _update_tree();
  1294. }
  1295. void SceneTreeEditor::set_marked(Node *p_marked, bool p_selectable, bool p_children_selectable) {
  1296. HashSet<Node *> s;
  1297. if (p_marked) {
  1298. s.insert(p_marked);
  1299. }
  1300. set_marked(s, p_selectable, p_children_selectable);
  1301. }
  1302. void SceneTreeEditor::set_filter(const String &p_filter) {
  1303. filter = p_filter;
  1304. _update_filter(nullptr, true);
  1305. }
  1306. String SceneTreeEditor::get_filter() const {
  1307. return filter;
  1308. }
  1309. String SceneTreeEditor::get_filter_term_warning() {
  1310. return filter_term_warning;
  1311. }
  1312. void SceneTreeEditor::set_show_all_nodes(bool p_show_all_nodes) {
  1313. show_all_nodes = p_show_all_nodes;
  1314. _update_filter(nullptr, true);
  1315. }
  1316. void SceneTreeEditor::set_as_scene_tree_dock() {
  1317. is_scene_tree_dock = true;
  1318. }
  1319. void SceneTreeEditor::set_display_foreign_nodes(bool p_display) {
  1320. display_foreign = p_display;
  1321. _update_tree();
  1322. }
  1323. void SceneTreeEditor::set_valid_types(const Vector<StringName> &p_valid) {
  1324. valid_types = p_valid;
  1325. node_cache.force_update = true;
  1326. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  1327. tree_dirty = true;
  1328. }
  1329. void SceneTreeEditor::set_editor_selection(EditorSelection *p_selection) {
  1330. editor_selection = p_selection;
  1331. tree->set_select_mode(Tree::SELECT_MULTI);
  1332. tree->set_cursor_can_exit_tree(false);
  1333. editor_selection->connect("selection_changed", callable_mp(this, &SceneTreeEditor::_selection_changed));
  1334. }
  1335. void SceneTreeEditor::_update_selection(TreeItem *item) {
  1336. ERR_FAIL_NULL(item);
  1337. NodePath np = item->get_metadata(0);
  1338. if (!has_node(np)) {
  1339. return;
  1340. }
  1341. Node *n = get_node(np);
  1342. if (!n) {
  1343. return;
  1344. }
  1345. if (editor_selection->is_selected(n)) {
  1346. if (!item->is_selected(0)) {
  1347. item->select(0);
  1348. }
  1349. } else {
  1350. if (item->is_selected(0)) {
  1351. TreeItem *previous_cursor_item = tree->get_selected();
  1352. item->deselect(0);
  1353. if (previous_cursor_item) {
  1354. previous_cursor_item->set_as_cursor(0);
  1355. }
  1356. }
  1357. }
  1358. TreeItem *c = item->get_first_child();
  1359. while (c) {
  1360. _update_selection(c);
  1361. c = c->get_next();
  1362. }
  1363. }
  1364. void SceneTreeEditor::_selection_changed() {
  1365. if (!editor_selection) {
  1366. return;
  1367. }
  1368. TreeItem *root = tree->get_root();
  1369. if (!root) {
  1370. return;
  1371. }
  1372. _update_selection(root);
  1373. }
  1374. void SceneTreeEditor::_cell_collapsed(Object *p_obj) {
  1375. if (updating_tree) {
  1376. return;
  1377. }
  1378. if (!can_rename) {
  1379. return;
  1380. }
  1381. TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
  1382. if (!ti) {
  1383. return;
  1384. }
  1385. bool collapsed = ti->is_collapsed();
  1386. NodePath np = ti->get_metadata(0);
  1387. Node *n = get_node(np);
  1388. ERR_FAIL_NULL(n);
  1389. n->set_display_folded(collapsed);
  1390. }
  1391. Variant SceneTreeEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1392. if (!can_rename) {
  1393. return Variant(); // Not editable tree.
  1394. }
  1395. if (tree->get_button_id_at_position(p_point) != -1) {
  1396. return Variant(); // Dragging from button.
  1397. }
  1398. Vector<Node *> selected_nodes;
  1399. Vector<Ref<Texture2D>> icons;
  1400. TreeItem *next = tree->get_next_selected(nullptr);
  1401. while (next) {
  1402. NodePath np = next->get_metadata(0);
  1403. Node *n = get_node(np);
  1404. if (n) {
  1405. selected_nodes.push_back(n);
  1406. icons.push_back(next->get_icon(0));
  1407. }
  1408. next = tree->get_next_selected(next);
  1409. }
  1410. if (selected_nodes.is_empty()) {
  1411. return Variant();
  1412. }
  1413. VBoxContainer *vb = memnew(VBoxContainer);
  1414. Array objs;
  1415. int list_max = 10;
  1416. float opacity_step = 1.0f / list_max;
  1417. float opacity_item = 1.0f;
  1418. for (int i = 0; i < selected_nodes.size(); i++) {
  1419. if (i < list_max) {
  1420. HBoxContainer *hb = memnew(HBoxContainer);
  1421. TextureRect *tf = memnew(TextureRect);
  1422. int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  1423. tf->set_custom_minimum_size(Size2(icon_size, icon_size));
  1424. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1425. tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1426. tf->set_texture(icons[i]);
  1427. hb->add_child(tf);
  1428. Label *label = memnew(Label(selected_nodes[i]->get_name()));
  1429. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1430. hb->add_child(label);
  1431. vb->add_child(hb);
  1432. hb->set_modulate(Color(1, 1, 1, opacity_item));
  1433. opacity_item -= opacity_step;
  1434. }
  1435. NodePath p = selected_nodes[i]->get_path();
  1436. objs.push_back(p);
  1437. }
  1438. set_drag_preview(vb);
  1439. Dictionary drag_data;
  1440. drag_data["type"] = "nodes";
  1441. drag_data["nodes"] = objs;
  1442. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1443. emit_signal(SNAME("nodes_dragged"));
  1444. return drag_data;
  1445. }
  1446. bool SceneTreeEditor::_is_script_type(const StringName &p_type) const {
  1447. return (script_types->find(p_type));
  1448. }
  1449. bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1450. if (!can_rename) {
  1451. return false; // Not editable tree.
  1452. }
  1453. Dictionary d = p_data;
  1454. if (!d.has("type")) {
  1455. return false;
  1456. }
  1457. TreeItem *item = tree->get_item_at_position(p_point);
  1458. if (!item) {
  1459. return false;
  1460. }
  1461. int section = tree->get_drop_section_at_position(p_point);
  1462. if (section < -1 || (section == -1 && !item->get_parent())) {
  1463. return false;
  1464. }
  1465. if (String(d["type"]) == "files") {
  1466. Vector<String> files = d["files"];
  1467. if (files.size() == 0) {
  1468. return false; // TODO Weird?
  1469. }
  1470. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(files[0]))) {
  1471. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1472. return true;
  1473. }
  1474. bool scene_drop = true;
  1475. bool audio_drop = true;
  1476. for (int i = 0; i < files.size(); i++) {
  1477. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[i]);
  1478. if (ftype != "PackedScene") {
  1479. scene_drop = false;
  1480. }
  1481. if (audio_drop && !ClassDB::is_parent_class(ftype, "AudioStream")) {
  1482. audio_drop = false;
  1483. }
  1484. }
  1485. if (scene_drop) {
  1486. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1487. return true;
  1488. }
  1489. if (audio_drop) {
  1490. if (files.size() > 1) {
  1491. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  1492. } else {
  1493. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1494. }
  1495. return true;
  1496. }
  1497. if (files.size() > 1) {
  1498. return false;
  1499. }
  1500. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1501. return true;
  1502. }
  1503. if (String(d["type"]) == "script_list_element") {
  1504. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  1505. if (se) {
  1506. String sp = se->get_edited_resource()->get_path();
  1507. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  1508. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1509. return true;
  1510. }
  1511. }
  1512. }
  1513. if (filter.is_empty() && String(d["type"]) == "nodes") {
  1514. Array nodes = d["nodes"];
  1515. for (int i = 0; i < nodes.size(); i++) {
  1516. Node *n = get_node(nodes[i]);
  1517. // Nodes from an instantiated scene can't be rearranged.
  1518. if (n && n->get_owner() && n->get_owner() != get_scene_node() && !n->get_owner()->get_scene_file_path().is_empty()) {
  1519. return false;
  1520. }
  1521. }
  1522. return true;
  1523. }
  1524. return false;
  1525. }
  1526. void SceneTreeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1527. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  1528. return;
  1529. }
  1530. TreeItem *item = tree->get_item_at_position(p_point);
  1531. if (!item) {
  1532. return;
  1533. }
  1534. int section = tree->get_drop_section_at_position(p_point);
  1535. if (section < -1) {
  1536. return;
  1537. }
  1538. NodePath np = item->get_metadata(0);
  1539. Node *n = get_node(np);
  1540. if (!n) {
  1541. return;
  1542. }
  1543. Dictionary d = p_data;
  1544. if (String(d["type"]) == "nodes") {
  1545. Array nodes = d["nodes"];
  1546. emit_signal(SNAME("nodes_rearranged"), nodes, np, section);
  1547. }
  1548. if (String(d["type"]) == "files") {
  1549. Vector<String> files = d["files"];
  1550. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]);
  1551. if (_is_script_type(ftype)) {
  1552. emit_signal(SNAME("script_dropped"), files[0], np);
  1553. } else {
  1554. emit_signal(SNAME("files_dropped"), files, np, section);
  1555. }
  1556. }
  1557. if (String(d["type"]) == "script_list_element") {
  1558. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  1559. if (se) {
  1560. String sp = se->get_edited_resource()->get_path();
  1561. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  1562. emit_signal(SNAME("script_dropped"), sp, np);
  1563. }
  1564. }
  1565. }
  1566. }
  1567. void SceneTreeEditor::_empty_clicked(const Vector2 &p_pos, MouseButton p_button) {
  1568. if (p_button != MouseButton::RIGHT) {
  1569. return;
  1570. }
  1571. _rmb_select(p_pos);
  1572. }
  1573. void SceneTreeEditor::_rmb_select(const Vector2 &p_pos, MouseButton p_button) {
  1574. if (p_button != MouseButton::RIGHT) {
  1575. return;
  1576. }
  1577. emit_signal(SNAME("rmb_pressed"), tree->get_screen_position() + p_pos);
  1578. }
  1579. void SceneTreeEditor::update_warning() {
  1580. _warning_changed(nullptr);
  1581. }
  1582. void SceneTreeEditor::_warning_changed(Node *p_for_node) {
  1583. node_cache.mark_dirty(p_for_node);
  1584. // Should use a timer.
  1585. update_timer->start();
  1586. }
  1587. void SceneTreeEditor::set_auto_expand_selected(bool p_auto, bool p_update_settings) {
  1588. if (p_update_settings) {
  1589. EditorSettings::get_singleton()->set("docks/scene_tree/auto_expand_to_selected", p_auto);
  1590. }
  1591. auto_expand_selected = p_auto;
  1592. }
  1593. void SceneTreeEditor::set_connect_to_script_mode(bool p_enable) {
  1594. connect_to_script_mode = p_enable;
  1595. update_tree();
  1596. }
  1597. void SceneTreeEditor::set_connecting_signal(bool p_enable) {
  1598. connecting_signal = p_enable;
  1599. update_tree();
  1600. }
  1601. void SceneTreeEditor::set_update_when_invisible(bool p_enable) {
  1602. update_when_invisible = p_enable;
  1603. update_tree();
  1604. }
  1605. void SceneTreeEditor::_bind_methods() {
  1606. ClassDB::bind_method(D_METHOD("_update_tree"), &SceneTreeEditor::_update_tree, DEFVAL(false)); // Still used by UndoRedo.
  1607. ClassDB::bind_method(D_METHOD("update_tree"), &SceneTreeEditor::update_tree);
  1608. ADD_SIGNAL(MethodInfo("node_selected"));
  1609. ADD_SIGNAL(MethodInfo("node_renamed"));
  1610. ADD_SIGNAL(MethodInfo("node_prerename"));
  1611. ADD_SIGNAL(MethodInfo("node_changed"));
  1612. ADD_SIGNAL(MethodInfo("nodes_dragged"));
  1613. ADD_SIGNAL(MethodInfo("nodes_rearranged", PropertyInfo(Variant::ARRAY, "paths"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1614. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1615. ADD_SIGNAL(MethodInfo("script_dropped", PropertyInfo(Variant::STRING, "file"), PropertyInfo(Variant::NODE_PATH, "to_path")));
  1616. ADD_SIGNAL(MethodInfo("rmb_pressed", PropertyInfo(Variant::VECTOR2, "position")));
  1617. ADD_SIGNAL(MethodInfo("open"));
  1618. ADD_SIGNAL(MethodInfo("open_script"));
  1619. }
  1620. SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_open_instance) :
  1621. node_cache(this) {
  1622. selected = nullptr;
  1623. can_rename = p_can_rename;
  1624. can_open_instance = p_can_open_instance;
  1625. editor_selection = nullptr;
  1626. if (p_label) {
  1627. Label *label = memnew(Label);
  1628. label->set_theme_type_variation("HeaderSmall");
  1629. label->set_position(Point2(10, 0));
  1630. label->set_text(TTR("Scene Tree (Nodes):"));
  1631. add_child(label);
  1632. }
  1633. tree = memnew(Tree);
  1634. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1635. tree->set_anchor(SIDE_RIGHT, ANCHOR_END);
  1636. tree->set_anchor(SIDE_BOTTOM, ANCHOR_END);
  1637. tree->set_begin(Point2(0, p_label ? 18 : 0));
  1638. tree->set_end(Point2(0, 0));
  1639. tree->set_allow_reselect(true);
  1640. tree->add_theme_constant_override("button_margin", 0);
  1641. add_child(tree);
  1642. SET_DRAG_FORWARDING_GCD(tree, SceneTreeEditor);
  1643. if (p_can_rename) {
  1644. tree->set_allow_rmb_select(true);
  1645. tree->connect("item_mouse_selected", callable_mp(this, &SceneTreeEditor::_rmb_select));
  1646. tree->connect("empty_clicked", callable_mp(this, &SceneTreeEditor::_empty_clicked));
  1647. }
  1648. tree->connect("cell_selected", callable_mp(this, &SceneTreeEditor::_selected_changed));
  1649. tree->connect("item_edited", callable_mp(this, &SceneTreeEditor::_edited));
  1650. tree->connect("multi_selected", callable_mp(this, &SceneTreeEditor::_cell_multi_selected));
  1651. tree->connect("button_clicked", callable_mp(this, &SceneTreeEditor::_cell_button_pressed));
  1652. tree->connect("nothing_selected", callable_mp(this, &SceneTreeEditor::_deselect_items));
  1653. error = memnew(AcceptDialog);
  1654. add_child(error);
  1655. warning = memnew(AcceptDialog);
  1656. add_child(warning);
  1657. warning->set_title(TTR("Node Configuration Warning!"));
  1658. warning->set_flag(Window::FLAG_POPUP, true);
  1659. last_hash = 0;
  1660. blocked = 0;
  1661. update_timer = memnew(Timer);
  1662. update_timer->connect("timeout", callable_mp(this, &SceneTreeEditor::_update_tree).bind(false));
  1663. update_timer->set_one_shot(true);
  1664. update_timer->set_wait_time(0.5);
  1665. add_child(update_timer);
  1666. update_node_tooltip_delay = memnew(Timer);
  1667. update_node_tooltip_delay->set_wait_time(0.5);
  1668. update_node_tooltip_delay->set_one_shot(true);
  1669. add_child(update_node_tooltip_delay);
  1670. revoke_dialog = memnew(ConfirmationDialog);
  1671. revoke_dialog->set_ok_button_text(TTR("Revoke"));
  1672. add_child(revoke_dialog);
  1673. revoke_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SceneTreeEditor::_update_ask_before_revoking_unique_name));
  1674. VBoxContainer *vb = memnew(VBoxContainer);
  1675. revoke_dialog->add_child(vb);
  1676. revoke_dialog_label = memnew(Label);
  1677. vb->add_child(revoke_dialog_label);
  1678. ask_before_revoke_checkbox = memnew(CheckBox(TTR("Don't Ask Again")));
  1679. ask_before_revoke_checkbox->set_tooltip_text(TTR("This dialog can also be enabled/disabled in the Editor Settings: Docks > Scene Tree > Ask Before Revoking Unique Name."));
  1680. vb->add_child(ask_before_revoke_checkbox);
  1681. script_types = memnew(List<StringName>);
  1682. ClassDB::get_inheriters_from_class("Script", script_types);
  1683. }
  1684. SceneTreeEditor::~SceneTreeEditor() {
  1685. memdelete(script_types);
  1686. }
  1687. /******** DIALOG *********/
  1688. void SceneTreeDialog::popup_scenetree_dialog(Node *p_selected_node, Node *p_marked_node, bool p_marked_node_selectable, bool p_marked_node_children_selectable) {
  1689. get_scene_tree()->set_marked(p_marked_node, p_marked_node_selectable, p_marked_node_children_selectable);
  1690. get_scene_tree()->set_selected(p_selected_node);
  1691. popup_centered_clamped(Size2(350, 700) * EDSCALE);
  1692. }
  1693. void SceneTreeDialog::_show_all_nodes_changed(bool p_button_pressed) {
  1694. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_all_nodes_for_node_selection", p_button_pressed);
  1695. tree->set_show_all_nodes(p_button_pressed);
  1696. }
  1697. void SceneTreeDialog::set_valid_types(const Vector<StringName> &p_valid) {
  1698. if (allowed_types_hbox) {
  1699. allowed_types_hbox->queue_free();
  1700. allowed_types_hbox = nullptr;
  1701. valid_type_icons.clear();
  1702. }
  1703. tree->set_valid_types(p_valid);
  1704. if (p_valid.is_empty()) {
  1705. return;
  1706. }
  1707. allowed_types_hbox = memnew(HBoxContainer);
  1708. content->add_child(allowed_types_hbox);
  1709. content->move_child(allowed_types_hbox, 0);
  1710. {
  1711. Label *label = memnew(Label);
  1712. allowed_types_hbox->add_child(label);
  1713. label->set_text(TTR("Allowed:"));
  1714. }
  1715. HFlowContainer *hflow = memnew(HFlowContainer);
  1716. allowed_types_hbox->add_child(hflow);
  1717. hflow->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1718. for (const StringName &type : p_valid) {
  1719. HBoxContainer *hb = memnew(HBoxContainer);
  1720. hflow->add_child(hb);
  1721. // Attempt to get the correct name and icon for script path types.
  1722. String name = type;
  1723. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(type);
  1724. // If we can't find a global class icon, try to find one for the script.
  1725. if (icon.is_null() && ResourceLoader::exists(type, "Script")) {
  1726. Ref<Script> node_script = ResourceLoader::load(type);
  1727. if (node_script.is_valid()) {
  1728. name = name.get_file();
  1729. icon = EditorNode::get_singleton()->get_object_icon(node_script.ptr());
  1730. }
  1731. }
  1732. TextureRect *trect = memnew(TextureRect);
  1733. hb->add_child(trect);
  1734. trect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1735. trect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1736. trect->set_meta("icon", icon);
  1737. valid_type_icons.push_back(trect);
  1738. Label *label = memnew(Label);
  1739. hb->add_child(label);
  1740. label->set_text(name);
  1741. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1742. }
  1743. show_all_nodes->show();
  1744. if (is_inside_tree()) {
  1745. _update_valid_type_icons();
  1746. }
  1747. }
  1748. void SceneTreeDialog::_notification(int p_what) {
  1749. switch (p_what) {
  1750. case NOTIFICATION_VISIBILITY_CHANGED: {
  1751. if (is_visible()) {
  1752. tree->update_tree();
  1753. // Select the search bar by default.
  1754. callable_mp((Control *)filter, &Control::grab_focus).call_deferred();
  1755. }
  1756. } break;
  1757. case NOTIFICATION_ENTER_TREE: {
  1758. connect(SceneStringName(confirmed), callable_mp(this, &SceneTreeDialog::_select));
  1759. } break;
  1760. case NOTIFICATION_THEME_CHANGED: {
  1761. _update_valid_type_icons();
  1762. } break;
  1763. case NOTIFICATION_EXIT_TREE: {
  1764. disconnect(SceneStringName(confirmed), callable_mp(this, &SceneTreeDialog::_select));
  1765. } break;
  1766. }
  1767. }
  1768. void SceneTreeDialog::_update_valid_type_icons() {
  1769. filter->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  1770. for (TextureRect *trect : valid_type_icons) {
  1771. trect->set_custom_minimum_size(Vector2(get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)), 0));
  1772. trect->set_texture(trect->get_meta("icon"));
  1773. }
  1774. }
  1775. void SceneTreeDialog::_cancel() {
  1776. hide();
  1777. }
  1778. void SceneTreeDialog::_select() {
  1779. if (tree->get_selected()) {
  1780. // The signal may cause another dialog to be displayed, so be sure to hide this one first.
  1781. hide();
  1782. emit_signal(SNAME("selected"), tree->get_selected()->get_path());
  1783. }
  1784. }
  1785. void SceneTreeDialog::_selected_changed() {
  1786. get_ok_button()->set_disabled(!tree->get_selected());
  1787. }
  1788. void SceneTreeDialog::_filter_changed(const String &p_filter) {
  1789. tree->set_filter(p_filter);
  1790. }
  1791. void SceneTreeDialog::_on_filter_gui_input(const Ref<InputEvent> &p_event) {
  1792. // Redirect navigational key events to the tree.
  1793. Ref<InputEventKey> key = p_event;
  1794. if (key.is_valid()) {
  1795. if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
  1796. tree->get_scene_tree()->gui_input(key);
  1797. filter->accept_event();
  1798. }
  1799. }
  1800. }
  1801. void SceneTreeDialog::_bind_methods() {
  1802. ClassDB::bind_method("_cancel", &SceneTreeDialog::_cancel);
  1803. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::NODE_PATH, "path")));
  1804. }
  1805. SceneTreeDialog::SceneTreeDialog() {
  1806. set_title(TTR("Select a Node"));
  1807. content = memnew(VBoxContainer);
  1808. add_child(content);
  1809. HBoxContainer *filter_hbc = memnew(HBoxContainer);
  1810. content->add_child(filter_hbc);
  1811. filter = memnew(LineEdit);
  1812. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1813. filter->set_placeholder(TTR("Filter Nodes"));
  1814. filter->set_clear_button_enabled(true);
  1815. filter->add_theme_constant_override("minimum_character_width", 0);
  1816. filter->connect(SceneStringName(text_changed), callable_mp(this, &SceneTreeDialog::_filter_changed));
  1817. filter->connect(SceneStringName(gui_input), callable_mp(this, &SceneTreeDialog::_on_filter_gui_input));
  1818. register_text_enter(filter);
  1819. filter_hbc->add_child(filter);
  1820. // Add 'Show All' button to HBoxContainer next to the filter, visible only when valid_types is defined.
  1821. show_all_nodes = memnew(CheckButton);
  1822. show_all_nodes->set_text(TTR("Show All"));
  1823. show_all_nodes->connect(SceneStringName(toggled), callable_mp(this, &SceneTreeDialog::_show_all_nodes_changed));
  1824. show_all_nodes->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);
  1825. show_all_nodes->hide();
  1826. filter_hbc->add_child(show_all_nodes);
  1827. tree = memnew(SceneTreeEditor(false, false, true));
  1828. tree->set_update_when_invisible(false);
  1829. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1830. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &SceneTreeDialog::_select));
  1831. // Initialize button state, must be done after the tree has been created to update its 'show_all_nodes' flag.
  1832. // This is also done before adding the tree to the content to avoid triggering unnecessary tree filtering.
  1833. show_all_nodes->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_all_nodes_for_node_selection", false));
  1834. content->add_child(tree);
  1835. // Disable the OK button when no node is selected.
  1836. get_ok_button()->set_disabled(!tree->get_selected());
  1837. tree->connect("node_selected", callable_mp(this, &SceneTreeDialog::_selected_changed));
  1838. }
  1839. SceneTreeDialog::~SceneTreeDialog() {
  1840. }
  1841. /******** CACHE *********/
  1842. HashMap<Node *, SceneTreeEditor::CachedNode>::Iterator SceneTreeEditor::NodeCache::add(Node *p_node, TreeItem *p_item) {
  1843. if (!p_node) {
  1844. return HashMap<Node *, CachedNode>::Iterator();
  1845. }
  1846. return cache.insert(p_node, CachedNode(p_node, p_item));
  1847. }
  1848. HashMap<Node *, SceneTreeEditor::CachedNode>::Iterator SceneTreeEditor::NodeCache::get(Node *p_node, bool p_deleted_ok) {
  1849. if (!p_node) {
  1850. return HashMap<Node *, CachedNode>::Iterator();
  1851. }
  1852. HashMap<Node *, CachedNode>::Iterator I = cache.find(p_node);
  1853. if (I) {
  1854. if (I->value.delete_serial != UINT16_MAX) {
  1855. // Don't give us a node marked for deletion.
  1856. if (!p_deleted_ok) {
  1857. return HashMap<Node *, CachedNode>::Iterator();
  1858. }
  1859. to_delete.erase(&I->value);
  1860. I->value.delete_serial = UINT16_MAX;
  1861. // If we were resurrected from near-death we might have been renamed.
  1862. // Make sure that we are updated properly.
  1863. mark_dirty(p_node);
  1864. mark_children_dirty(p_node, true);
  1865. }
  1866. }
  1867. return I;
  1868. }
  1869. void SceneTreeEditor::NodeCache::remove(Node *p_node, bool p_recursive) {
  1870. if (!p_node) {
  1871. return;
  1872. }
  1873. if (p_node == editor->selected) {
  1874. editor->selected = nullptr;
  1875. }
  1876. editor->marked.erase(p_node);
  1877. HashMap<Node *, CachedNode>::Iterator I = cache.find(p_node);
  1878. if (I) {
  1879. if (p_recursive) {
  1880. int cc = p_node->get_child_count(false);
  1881. for (int i = 0; i < cc; i++) {
  1882. remove(p_node->get_child(i, false), p_recursive);
  1883. }
  1884. }
  1885. if (current_scene_node != p_node) {
  1886. // Do not remove from the Tree control here. See delete_pending below.
  1887. I->value.item->deselect(0);
  1888. I->value.delete_serial = delete_serial;
  1889. I->value.index = -1;
  1890. I->value.cache_iterator = I;
  1891. to_delete.insert(&I->value);
  1892. } else {
  1893. // If it is the root node, we leave the TreeItem and reuse it later.
  1894. cache.remove(I);
  1895. }
  1896. }
  1897. }
  1898. void SceneTreeEditor::NodeCache::mark_dirty(Node *p_node, bool p_parents) {
  1899. Node *node = p_node;
  1900. while (node) {
  1901. HashMap<Node *, CachedNode>::Iterator I = cache.find(node);
  1902. if (I) {
  1903. I->value.dirty = true;
  1904. }
  1905. if (!p_parents) {
  1906. break;
  1907. }
  1908. node = node->get_parent();
  1909. }
  1910. }
  1911. void SceneTreeEditor::NodeCache::mark_children_dirty(Node *p_node, bool p_recursive) {
  1912. if (!p_node) {
  1913. return;
  1914. }
  1915. int cc = p_node->get_child_count(false);
  1916. for (int i = 0; i < cc; i++) {
  1917. Node *c = p_node->get_child(i, false);
  1918. HashMap<Node *, CachedNode>::Iterator IC = cache.find(c);
  1919. if (IC) {
  1920. IC->value.dirty = true;
  1921. if (p_recursive) {
  1922. mark_children_dirty(c, p_recursive);
  1923. }
  1924. }
  1925. }
  1926. }
  1927. void SceneTreeEditor::NodeCache::delete_pending() {
  1928. HashSet<CachedNode *>::Iterator I = to_delete.begin();
  1929. while (I) {
  1930. // We want to keep TreeItems around just long enough for a Node removal,
  1931. // and immediate reinsertion. This is what happens with moves and
  1932. // type changes.
  1933. if (Math::abs((*I)->delete_serial - delete_serial) >= 2) {
  1934. memdelete((*I)->item);
  1935. cache.remove((*I)->cache_iterator);
  1936. to_delete.remove(I);
  1937. } else if (!(*I)->removed) {
  1938. // We don't remove from the tree until now because if the node got
  1939. // deleted from a @tool script the SceneTreeEditor might have had it
  1940. // marked or selected before the node was removed. If we immediately
  1941. // remove from the Tree control then we end up trying to scroll to an
  1942. // Item without a parent.
  1943. //
  1944. // We might already be removed (and thus not have a parent) by rapid
  1945. // undo/redo.
  1946. if (!(*I)->removed) {
  1947. TreeItem *parent = (*I)->item->get_parent();
  1948. parent->remove_child((*I)->item);
  1949. }
  1950. (*I)->removed = true;
  1951. }
  1952. ++I;
  1953. }
  1954. ++delete_serial;
  1955. }
  1956. void SceneTreeEditor::NodeCache::clear() {
  1957. for (CachedNode *E : to_delete) {
  1958. // Only removed entries won't be automatically cleaned up by Tree::clear().
  1959. if (E->removed) {
  1960. memdelete(E->item);
  1961. }
  1962. }
  1963. cache.clear();
  1964. to_delete.clear();
  1965. }