scene_tree_editor.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428
  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_valid()) {
  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_valid()) {
  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. if (Engine::get_singleton()->is_recovery_mode_hint()) {
  502. additional_notes += "\n" + TTR("This script can run in the editor.\nIt is currently disabled due to recovery mode.");
  503. button_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  504. } else {
  505. additional_notes += "\n" + TTR("This script is currently running in the editor.");
  506. button_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  507. }
  508. }
  509. if (EditorNode::get_singleton()->get_object_custom_type_base(p_node) == scr) {
  510. additional_notes += "\n" + TTR("This script is a custom type.");
  511. button_color.a = 0.5;
  512. }
  513. p_item->add_button(0, get_editor_theme_icon(SNAME("Script")), BUTTON_SCRIPT, false, TTR("Open Script:") + " " + scr->get_path() + additional_notes);
  514. p_item->set_button_color(0, p_item->get_button_count(0) - 1, button_color);
  515. }
  516. if (p_node->has_meta("_edit_lock_")) {
  517. p_item->add_button(0, get_editor_theme_icon(SNAME("Lock")), BUTTON_LOCK, false, TTR("Node is locked.\nClick to unlock it."));
  518. }
  519. if (p_node->has_meta("_edit_group_")) {
  520. p_item->add_button(0, get_editor_theme_icon(SNAME("Group")), BUTTON_GROUP, false, TTR("Children are not selectable.\nClick to make them selectable."));
  521. }
  522. if (p_node->has_method("is_visible") && p_node->has_method("set_visible") && p_node->has_signal(SceneStringName(visibility_changed))) {
  523. bool is_visible = p_node->call("is_visible");
  524. if (is_visible) {
  525. p_item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityVisible")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  526. } else {
  527. p_item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityHidden")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  528. }
  529. const Callable vis_changed = callable_mp(this, &SceneTreeEditor::_node_visibility_changed);
  530. if (!p_node->is_connected(SceneStringName(visibility_changed), vis_changed)) {
  531. p_node->connect(SceneStringName(visibility_changed), vis_changed.bind(p_node));
  532. }
  533. _update_visibility_color(p_node, p_item);
  534. }
  535. if (p_node->is_class("AnimationMixer")) {
  536. bool is_pinned = AnimationPlayerEditor::get_singleton()->get_editing_node() == p_node && AnimationPlayerEditor::get_singleton()->is_pinned();
  537. if (is_pinned) {
  538. p_item->add_button(0, get_editor_theme_icon(SNAME("Pin")), BUTTON_PIN, false, TTR("AnimationPlayer is pinned.\nClick to unpin."));
  539. }
  540. }
  541. }
  542. if (editor_selection) {
  543. if (editor_selection->is_selected(p_node)) {
  544. p_item->select(0);
  545. }
  546. }
  547. if (selected == p_node) {
  548. if (!editor_selection) {
  549. p_item->select(0);
  550. }
  551. p_item->set_as_cursor(0);
  552. }
  553. }
  554. void SceneTreeEditor::_update_if_clean() {
  555. if (tree_dirty) {
  556. return;
  557. }
  558. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  559. tree_dirty = true;
  560. }
  561. void SceneTreeEditor::_queue_update_node_tooltip(Node *p_node, TreeItem *p_item) {
  562. Callable update_tooltip = callable_mp(this, &SceneTreeEditor::_update_node_tooltip);
  563. if (update_node_tooltip_delay->is_connected("timeout", update_tooltip)) {
  564. update_node_tooltip_delay->disconnect("timeout", update_tooltip);
  565. }
  566. update_node_tooltip_delay->connect("timeout", update_tooltip.bind(p_node, p_item));
  567. update_node_tooltip_delay->start();
  568. }
  569. void SceneTreeEditor::_update_node_tooltip(Node *p_node, TreeItem *p_item) {
  570. // Display the node name in all tooltips so that long node names can be previewed
  571. // without having to rename them.
  572. String tooltip = p_node->get_name();
  573. if (p_node == get_scene_node() && p_node->get_scene_inherited_state().is_valid()) {
  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("Inherits:") + " " + p_node->get_scene_inherited_state()->get_path());
  578. } else if (p_node != get_scene_node() && !p_node->get_scene_file_path().is_empty() && can_open_instance) {
  579. if (p_item->get_button_by_id(0, BUTTON_SUBSCENE) == -1) {
  580. p_item->add_button(0, get_editor_theme_icon(SNAME("InstanceOptions")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
  581. }
  582. tooltip += String("\n" + TTR("Instance:") + " " + p_node->get_scene_file_path());
  583. }
  584. StringName custom_type = EditorNode::get_singleton()->get_object_custom_type_name(p_node);
  585. tooltip += "\n" + TTR("Type:") + " " + (custom_type != StringName() ? String(custom_type) : p_node->get_class());
  586. if (!p_node->get_editor_description().is_empty()) {
  587. const PackedInt32Array boundaries = TS->string_get_word_breaks(p_node->get_editor_description(), "", 80);
  588. tooltip += "\n";
  589. for (int i = 0; i < boundaries.size(); i += 2) {
  590. const int start = boundaries[i];
  591. const int end = boundaries[i + 1];
  592. tooltip += "\n" + p_node->get_editor_description().substr(start, end - start + 1).rstrip("\n");
  593. }
  594. }
  595. p_item->set_tooltip_text(0, tooltip);
  596. }
  597. void SceneTreeEditor::_node_visibility_changed(Node *p_node) {
  598. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node, false);
  599. if (!I) {
  600. // We leave these signals connected when switching tabs.
  601. // If the node is not in cache it was for a different tab.
  602. return;
  603. }
  604. if (!p_node || (p_node != get_scene_node() && !p_node->get_owner())) {
  605. return;
  606. }
  607. TreeItem *item = _find(tree->get_root(), p_node->get_path());
  608. if (!item) {
  609. return;
  610. }
  611. int idx = item->get_button_by_id(0, BUTTON_VISIBILITY);
  612. ERR_FAIL_COND(idx == -1);
  613. bool node_visible = false;
  614. if (p_node->has_method("is_visible")) {
  615. node_visible = p_node->call("is_visible");
  616. if (p_node->is_class("CanvasItem") || p_node->is_class("CanvasLayer") || p_node->is_class("Window")) {
  617. CanvasItemEditor::get_singleton()->get_viewport_control()->queue_redraw();
  618. }
  619. }
  620. if (node_visible) {
  621. item->set_button(0, idx, get_editor_theme_icon(SNAME("GuiVisibilityVisible")));
  622. } else {
  623. item->set_button(0, idx, get_editor_theme_icon(SNAME("GuiVisibilityHidden")));
  624. }
  625. _update_visibility_color(p_node, item);
  626. }
  627. void SceneTreeEditor::_update_visibility_color(Node *p_node, TreeItem *p_item) {
  628. if (p_node->has_method("is_visible_in_tree")) {
  629. Color color(1, 1, 1, 1);
  630. bool visible_on_screen = p_node->call("is_visible_in_tree");
  631. if (!visible_on_screen) {
  632. color.a = 0.6;
  633. }
  634. int idx = p_item->get_button_by_id(0, BUTTON_VISIBILITY);
  635. p_item->set_button_color(0, idx, color);
  636. }
  637. }
  638. void SceneTreeEditor::_set_item_custom_color(TreeItem *p_item, Color p_color) {
  639. p_item->set_custom_color(0, p_color);
  640. p_item->set_meta(SNAME("custom_color"), p_color);
  641. }
  642. void SceneTreeEditor::_node_script_changed(Node *p_node) {
  643. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node, false);
  644. if (!I) {
  645. // We leave these signals connected when switching tabs.
  646. // If the node is not in cache it was for a different tab.
  647. return;
  648. }
  649. node_cache.mark_dirty(p_node);
  650. _update_if_clean();
  651. }
  652. void SceneTreeEditor::_move_node_children(HashMap<Node *, CachedNode>::Iterator &p_I) {
  653. TreeItem *item = p_I->value.item;
  654. Node *node = p_I->key;
  655. int cc = node->get_child_count(false);
  656. for (int i = 0; i < cc; i++) {
  657. HashMap<Node *, CachedNode>::Iterator CI = node_cache.get(node->get_child(i, false));
  658. if (CI) {
  659. _move_node_item(item, CI);
  660. }
  661. }
  662. p_I->value.has_moved_children = false;
  663. }
  664. void SceneTreeEditor::_move_node_item(TreeItem *p_parent, HashMap<Node *, CachedNode>::Iterator &p_I) {
  665. if (!p_parent) {
  666. return;
  667. }
  668. Node *node = p_I->key;
  669. int current_node_index = node->get_index(false);
  670. int current_item_index = -1;
  671. TreeItem *item = p_I->value.item;
  672. if (item->get_parent() != p_parent) {
  673. TreeItem *p = item->get_parent();
  674. if (p) {
  675. item->get_parent()->remove_child(item);
  676. }
  677. p_parent->add_child(item);
  678. p_I->value.removed = false;
  679. current_item_index = p_parent->get_child_count() - 1;
  680. p_I->value.index = current_item_index;
  681. }
  682. if (p_I->value.index != current_node_index) {
  683. // If we just re-parented we know our index.
  684. if (current_item_index == -1) {
  685. current_item_index = item->get_index();
  686. }
  687. // Are we already in the right place?
  688. if (current_node_index == current_item_index) {
  689. p_I->value.index = current_node_index;
  690. return;
  691. }
  692. // Are we the first node?
  693. if (current_node_index == 0) {
  694. // There has to be at least 1 other node, otherwise we would not have gotten here.
  695. TreeItem *neighbor_item = p_parent->get_child(0);
  696. item->move_before(neighbor_item);
  697. } else {
  698. TreeItem *neighbor_item = p_parent->get_child(CLAMP(current_node_index - 1, 0, p_parent->get_child_count() - 1));
  699. item->move_after(neighbor_item);
  700. }
  701. p_I->value.index = current_node_index;
  702. }
  703. }
  704. void SceneTreeEditor::_node_child_order_changed(Node *p_node) {
  705. // Do not try to change children on nodes currently marked for removal.
  706. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node, false);
  707. if (I) {
  708. node_cache.mark_dirty(I->key);
  709. I->value.has_moved_children = true;
  710. }
  711. _update_if_clean();
  712. }
  713. void SceneTreeEditor::_node_editor_state_changed(Node *p_node) {
  714. node_cache.mark_dirty(p_node);
  715. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(p_node, false);
  716. if (I) {
  717. if (p_node->is_inside_tree() && p_node->can_process() != I->value.can_process) {
  718. // All our children also change process mode.
  719. node_cache.mark_children_dirty(p_node, true);
  720. }
  721. }
  722. _update_if_clean();
  723. }
  724. void SceneTreeEditor::_node_added(Node *p_node) {
  725. if (!get_scene_node()) {
  726. return;
  727. }
  728. if (p_node != get_scene_node() && !get_scene_node()->is_ancestor_of(p_node)) {
  729. return;
  730. }
  731. node_cache.mark_dirty(p_node);
  732. _update_if_clean();
  733. }
  734. void SceneTreeEditor::_node_removed(Node *p_node) {
  735. if (EditorNode::get_singleton()->is_exiting()) {
  736. return; // Speed up exit.
  737. }
  738. if (EditorNode::get_singleton()->is_changing_scene()) {
  739. return; // Switching tabs we will be destroying node cache anyway.
  740. }
  741. if (!get_scene_node()) {
  742. return;
  743. }
  744. if (p_node != get_scene_node() && !get_scene_node()->is_ancestor_of(p_node)) {
  745. return;
  746. }
  747. node_cache.remove(p_node);
  748. _update_if_clean();
  749. }
  750. void SceneTreeEditor::_node_renamed(Node *p_node) {
  751. if (!get_scene_node()) {
  752. return;
  753. }
  754. if (p_node != get_scene_node() && !get_scene_node()->is_ancestor_of(p_node)) {
  755. return;
  756. }
  757. node_cache.mark_dirty(p_node);
  758. // Recursively update child node paths.
  759. _update_node_path(p_node, true);
  760. emit_signal(SNAME("node_renamed"));
  761. _update_if_clean();
  762. }
  763. void SceneTreeEditor::_update_tree(bool p_scroll_to_selected) {
  764. if (!is_inside_tree()) {
  765. tree_dirty = false;
  766. return;
  767. }
  768. Node *scene_node = get_scene_node();
  769. if (node_cache.current_scene_node != scene_node) {
  770. _reset();
  771. marked.clear();
  772. node_cache.current_scene_node = scene_node;
  773. node_cache.force_update = true;
  774. }
  775. if (!update_when_invisible && !is_visible_in_tree()) {
  776. return;
  777. }
  778. if (tree->is_editing()) {
  779. return;
  780. }
  781. updating_tree = true;
  782. last_hash = hash_djb2_one_64(0);
  783. if (node_cache.current_scene_node) {
  784. // Handle pinning/unpinning the animation player only do this once per iteration.
  785. Node *pinned_node = AnimationPlayerEditor::get_singleton()->get_editing_node();
  786. // If pinned state changed, update the currently pinned node.
  787. if (AnimationPlayerEditor::get_singleton()->is_pinned() != node_cache.current_has_pin) {
  788. node_cache.current_has_pin = AnimationPlayerEditor::get_singleton()->is_pinned();
  789. node_cache.mark_dirty(pinned_node);
  790. }
  791. // If the current pinned node changed update both the old and new node.
  792. if (node_cache.current_pinned_node != pinned_node) {
  793. node_cache.mark_dirty(pinned_node);
  794. node_cache.mark_dirty(node_cache.current_pinned_node);
  795. node_cache.current_pinned_node = pinned_node;
  796. }
  797. _update_node_subtree(get_scene_node(), nullptr, node_cache.force_update);
  798. _compute_hash(get_scene_node(), last_hash);
  799. node_cache.delete_pending();
  800. }
  801. updating_tree = false;
  802. tree_dirty = false;
  803. node_cache.force_update = false;
  804. if (!filter.strip_edges().is_empty() || !show_all_nodes) {
  805. _update_filter(nullptr, p_scroll_to_selected);
  806. }
  807. }
  808. bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_selected) {
  809. if (!p_parent) {
  810. p_parent = tree->get_root();
  811. filter_term_warning.clear();
  812. }
  813. if (!p_parent) {
  814. // Tree is empty, nothing to do here.
  815. return false;
  816. }
  817. // Now find other reasons to keep this Node, too.
  818. PackedStringArray terms = filter.to_lower().split_spaces();
  819. bool keep = _item_matches_all_terms(p_parent, terms);
  820. bool selectable = keep;
  821. bool is_root = p_parent == tree->get_root();
  822. if (keep) {
  823. Node *n = get_node(p_parent->get_metadata(0));
  824. if (!p_parent->is_visible() || (is_root && tree->is_root_hidden())) {
  825. // Place back moved out children from when this item has hidden.
  826. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(n, false);
  827. if (I && I->value.has_moved_children) {
  828. _update_node_subtree(I->value.node, nullptr, true);
  829. }
  830. }
  831. if (!valid_types.is_empty()) {
  832. selectable = false;
  833. for (const StringName &E : valid_types) {
  834. if (n->is_class(E) ||
  835. EditorNode::get_singleton()->is_object_of_custom_type(n, E)) {
  836. selectable = true;
  837. break;
  838. } else {
  839. Ref<Script> node_script = n->get_script();
  840. while (node_script.is_valid()) {
  841. if (node_script->get_path() == E) {
  842. selectable = true;
  843. break;
  844. }
  845. node_script = node_script->get_base_script();
  846. }
  847. if (selectable) {
  848. break;
  849. }
  850. }
  851. }
  852. }
  853. }
  854. bool keep_for_children = false;
  855. for (TreeItem *child = p_parent->get_first_child(); child; child = child->get_next()) {
  856. // Always keep if at least one of the children are kept.
  857. keep_for_children = _update_filter(child, p_scroll_to_selected) || keep_for_children;
  858. }
  859. if (!is_root) {
  860. if (show_all_nodes) {
  861. p_parent->set_visible(keep_for_children || keep);
  862. } else {
  863. // Show only selectable nodes, or parents of selectable.
  864. p_parent->set_visible(keep_for_children || selectable);
  865. }
  866. }
  867. if (selectable) {
  868. Color custom_color = p_parent->get_meta(SNAME("custom_color"), Color(0, 0, 0, 0));
  869. if (custom_color == Color(0, 0, 0, 0)) {
  870. p_parent->clear_custom_color(0);
  871. } else {
  872. p_parent->set_custom_color(0, custom_color);
  873. }
  874. p_parent->set_selectable(0, true);
  875. } else if (keep_for_children) {
  876. p_parent->set_visible(!hide_filtered_out_parents || is_root);
  877. if (!p_parent->is_visible()) {
  878. TreeItem *filtered_parent = p_parent->get_parent();
  879. while (filtered_parent) {
  880. if (filtered_parent == tree->get_root() || (filtered_parent->is_selectable(0) && filtered_parent->is_visible())) {
  881. break;
  882. }
  883. filtered_parent = filtered_parent->get_parent();
  884. }
  885. if (filtered_parent) {
  886. for (Variant &item : p_parent->get_children()) {
  887. TreeItem *ti = Object::cast_to<TreeItem>(item);
  888. bool is_selected = ti->is_selected(0);
  889. p_parent->remove_child(ti);
  890. filtered_parent->add_child(ti);
  891. TreeItem *prev = p_parent->get_prev();
  892. if (prev) {
  893. ti->move_after(prev);
  894. }
  895. if (is_selected) {
  896. ti->select(0);
  897. }
  898. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(get_node(p_parent->get_metadata(0)), false);
  899. if (I) {
  900. I->value.has_moved_children = true;
  901. }
  902. }
  903. return false;
  904. }
  905. } else {
  906. p_parent->set_custom_color(0, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  907. p_parent->set_selectable(0, false);
  908. p_parent->deselect(0);
  909. }
  910. }
  911. if (is_root) {
  912. tree->set_hide_root(hide_filtered_out_parents && !selectable);
  913. if (tree->is_root_hidden()) {
  914. p_parent->set_collapsed(false);
  915. }
  916. }
  917. if (editor_selection) {
  918. Node *n = get_node(p_parent->get_metadata(0));
  919. if (selectable) {
  920. if (p_scroll_to_selected && n && editor_selection->is_selected(n)) {
  921. // Needs to be deferred to account for possible root visibility change.
  922. callable_mp(tree, &Tree::scroll_to_item).call_deferred(p_parent, false);
  923. }
  924. } else if (n && p_parent->is_selected(0)) {
  925. editor_selection->remove_node(n);
  926. p_parent->deselect(0);
  927. }
  928. }
  929. return p_parent->is_visible();
  930. }
  931. bool SceneTreeEditor::_item_matches_all_terms(TreeItem *p_item, const PackedStringArray &p_terms) {
  932. if (p_terms.is_empty()) {
  933. return true;
  934. }
  935. for (int i = 0; i < p_terms.size(); i++) {
  936. const String &term = p_terms[i];
  937. // Recognize special filter.
  938. if (term.contains_char(':') && !term.get_slicec(':', 0).is_empty()) {
  939. String parameter = term.get_slicec(':', 0);
  940. String argument = term.get_slicec(':', 1);
  941. if (parameter == "type" || parameter == "t") {
  942. // Filter by Type.
  943. String type = get_node(p_item->get_metadata(0))->get_class();
  944. bool term_in_inherited_class = false;
  945. // Every Node is a Node, duh!
  946. while (type != "Node") {
  947. if (type.to_lower().contains(argument)) {
  948. term_in_inherited_class = true;
  949. break;
  950. }
  951. type = ClassDB::get_parent_class(type);
  952. }
  953. if (!term_in_inherited_class) {
  954. return false;
  955. }
  956. } else if (parameter == "group" || parameter == "g") {
  957. // Filter by Group.
  958. Node *node = get_node(p_item->get_metadata(0));
  959. if (argument.is_empty()) {
  960. // When argument is empty, match all Nodes belonging to any exposed group.
  961. if (node->get_persistent_group_count() == 0) {
  962. return false;
  963. }
  964. } else {
  965. List<Node::GroupInfo> group_info_list;
  966. node->get_groups(&group_info_list);
  967. bool term_in_groups = false;
  968. for (const Node::GroupInfo &group_info : group_info_list) {
  969. if (!group_info.persistent) {
  970. continue; // Ignore internal groups.
  971. }
  972. if (String(group_info.name).to_lower().contains(argument)) {
  973. term_in_groups = true;
  974. break;
  975. }
  976. }
  977. if (!term_in_groups) {
  978. return false;
  979. }
  980. }
  981. } else if (filter_term_warning.is_empty()) {
  982. filter_term_warning = vformat(TTR("\"%s\" is not a known filter."), parameter);
  983. continue;
  984. }
  985. } else {
  986. // Default.
  987. if (!p_item->get_text(0).to_lower().contains(term)) {
  988. return false;
  989. }
  990. }
  991. }
  992. return true;
  993. }
  994. void SceneTreeEditor::_compute_hash(Node *p_node, uint64_t &hash) {
  995. // Nodes are added and removed by Node* pointers.
  996. hash = hash_djb2_one_64((ptrdiff_t)p_node, hash);
  997. // This hash is non-commutative: if the node order changes so will the hash.
  998. for (int i = 0; i < p_node->get_child_count(); i++) {
  999. _compute_hash(p_node->get_child(i), hash);
  1000. }
  1001. }
  1002. void SceneTreeEditor::_reset() {
  1003. tree->clear();
  1004. node_cache.clear();
  1005. }
  1006. void SceneTreeEditor::_test_update_tree() {
  1007. pending_test_update = false;
  1008. if (!is_inside_tree()) {
  1009. return;
  1010. }
  1011. if (tree_dirty) {
  1012. return; // Don't even bother.
  1013. }
  1014. uint64_t hash = hash_djb2_one_64(0);
  1015. if (get_scene_node()) {
  1016. _compute_hash(get_scene_node(), hash);
  1017. }
  1018. // Test hash.
  1019. if (hash == last_hash) {
  1020. return; // Did not change.
  1021. }
  1022. _update_if_clean();
  1023. }
  1024. void SceneTreeEditor::_tree_process_mode_changed() {
  1025. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  1026. tree_dirty = true;
  1027. }
  1028. void SceneTreeEditor::_tree_changed() {
  1029. if (EditorNode::get_singleton()->is_exiting()) {
  1030. return; // Speed up exit.
  1031. }
  1032. if (pending_test_update) {
  1033. return;
  1034. }
  1035. if (tree_dirty) {
  1036. return;
  1037. }
  1038. callable_mp(this, &SceneTreeEditor::_test_update_tree).call_deferred();
  1039. pending_test_update = true;
  1040. }
  1041. void SceneTreeEditor::_selected_changed() {
  1042. TreeItem *s = tree->get_selected();
  1043. ERR_FAIL_NULL(s);
  1044. NodePath np = s->get_metadata(0);
  1045. Node *n = get_node(np);
  1046. if (n == selected) {
  1047. return;
  1048. }
  1049. selected = n;
  1050. blocked++;
  1051. emit_signal(SNAME("node_selected"));
  1052. blocked--;
  1053. }
  1054. void SceneTreeEditor::_deselect_items() {
  1055. // Clear currently selected items in scene tree dock.
  1056. if (editor_selection) {
  1057. editor_selection->clear();
  1058. emit_signal(SNAME("node_changed"));
  1059. }
  1060. }
  1061. void SceneTreeEditor::_cell_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  1062. TreeItem *item = Object::cast_to<TreeItem>(p_object);
  1063. ERR_FAIL_NULL(item);
  1064. if (!item->is_visible()) {
  1065. return;
  1066. }
  1067. NodePath np = item->get_metadata(0);
  1068. Node *n = get_node(np);
  1069. if (!n) {
  1070. return;
  1071. }
  1072. if (!editor_selection) {
  1073. return;
  1074. }
  1075. if (p_selected) {
  1076. editor_selection->add_node(n);
  1077. } else {
  1078. editor_selection->remove_node(n);
  1079. }
  1080. // Emitted "selected" in _selected_changed() when select single node, so select multiple node emit "changed".
  1081. if (editor_selection->get_selected_nodes().size() > 1) {
  1082. emit_signal(SNAME("node_changed"));
  1083. }
  1084. }
  1085. void SceneTreeEditor::_tree_scroll_to_item(ObjectID p_item_id) {
  1086. ERR_FAIL_NULL(tree);
  1087. TreeItem *item = Object::cast_to<TreeItem>(ObjectDB::get_instance(p_item_id));
  1088. if (item) {
  1089. tree->scroll_to_item(item, true);
  1090. }
  1091. }
  1092. void SceneTreeEditor::_notification(int p_what) {
  1093. switch (p_what) {
  1094. case NOTIFICATION_ENTER_TREE: {
  1095. get_tree()->connect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
  1096. get_tree()->connect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
  1097. get_tree()->connect("node_added", callable_mp(this, &SceneTreeEditor::_node_added));
  1098. get_tree()->connect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
  1099. get_tree()->connect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
  1100. get_tree()->connect(SceneStringName(node_configuration_warning_changed), callable_mp(this, &SceneTreeEditor::_warning_changed));
  1101. tree->connect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
  1102. _update_tree();
  1103. } break;
  1104. case NOTIFICATION_EXIT_TREE: {
  1105. get_tree()->disconnect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
  1106. get_tree()->disconnect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
  1107. get_tree()->disconnect("node_added", callable_mp(this, &SceneTreeEditor::_node_added));
  1108. get_tree()->disconnect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
  1109. get_tree()->disconnect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
  1110. tree->disconnect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
  1111. get_tree()->disconnect(SceneStringName(node_configuration_warning_changed), callable_mp(this, &SceneTreeEditor::_warning_changed));
  1112. } break;
  1113. case NOTIFICATION_THEME_CHANGED: {
  1114. tree->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
  1115. // When we change theme we need to re-do everything.
  1116. _reset();
  1117. _update_tree();
  1118. } break;
  1119. case NOTIFICATION_VISIBILITY_CHANGED: {
  1120. if (is_visible()) {
  1121. TreeItem *item = nullptr;
  1122. if (selected) {
  1123. // Scroll to selected node.
  1124. item = _find(tree->get_root(), selected->get_path());
  1125. } else if (marked.size() == 1) {
  1126. // Scroll to a single marked node.
  1127. Node *marked_node = *marked.begin();
  1128. if (marked_node) {
  1129. item = _find(tree->get_root(), marked_node->get_path());
  1130. }
  1131. }
  1132. bool has_item = item;
  1133. if (update_when_invisible) {
  1134. if (has_item) {
  1135. ObjectID item_id = item->get_instance_id();
  1136. callable_mp(this, &SceneTreeEditor::_tree_scroll_to_item).call_deferred(item_id);
  1137. }
  1138. } else {
  1139. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(has_item);
  1140. }
  1141. }
  1142. } break;
  1143. }
  1144. }
  1145. TreeItem *SceneTreeEditor::_find(TreeItem *p_node, const NodePath &p_path) {
  1146. if (!p_node) {
  1147. return nullptr;
  1148. }
  1149. NodePath np = p_node->get_metadata(0);
  1150. if (np == p_path) {
  1151. return p_node;
  1152. }
  1153. TreeItem *children = p_node->get_first_child();
  1154. while (children) {
  1155. TreeItem *n = _find(children, p_path);
  1156. if (n) {
  1157. return n;
  1158. }
  1159. children = children->get_next();
  1160. }
  1161. return nullptr;
  1162. }
  1163. void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
  1164. ERR_FAIL_COND(blocked > 0);
  1165. if (pending_test_update) {
  1166. _test_update_tree();
  1167. }
  1168. if (tree_dirty) {
  1169. _update_tree();
  1170. }
  1171. if (selected == p_node) {
  1172. return;
  1173. }
  1174. TreeItem *item = p_node ? _find(tree->get_root(), p_node->get_path()) : nullptr;
  1175. if (item) {
  1176. selected = p_node;
  1177. if (auto_expand_selected) {
  1178. // Make visible when it's collapsed.
  1179. TreeItem *node = item->get_parent();
  1180. while (node && node != tree->get_root()) {
  1181. node->set_collapsed(false);
  1182. node = node->get_parent();
  1183. }
  1184. item->select(0);
  1185. item->set_as_cursor(0);
  1186. tree->ensure_cursor_is_visible();
  1187. } else {
  1188. // Ensure the node is selected and visible for the user if the node
  1189. // is not collapsed.
  1190. bool collapsed = false;
  1191. TreeItem *node = item;
  1192. while (node && node != tree->get_root()) {
  1193. if (node->is_collapsed()) {
  1194. collapsed = true;
  1195. break;
  1196. }
  1197. node = node->get_parent();
  1198. }
  1199. if (!collapsed) {
  1200. item->select(0);
  1201. item->set_as_cursor(0);
  1202. tree->ensure_cursor_is_visible();
  1203. }
  1204. }
  1205. } else {
  1206. if (!p_node) {
  1207. selected = nullptr;
  1208. }
  1209. selected = p_node;
  1210. }
  1211. if (p_emit_selected) {
  1212. emit_signal(SNAME("node_selected"));
  1213. }
  1214. }
  1215. void SceneTreeEditor::rename_node(Node *p_node, const String &p_name, TreeItem *p_item) {
  1216. TreeItem *item;
  1217. if (p_item) {
  1218. item = p_item; // During batch rename the paths may change, so using _find() is unreliable.
  1219. } else {
  1220. item = _find(tree->get_root(), p_node->get_path());
  1221. }
  1222. ERR_FAIL_NULL(item);
  1223. String new_name = p_name.validate_node_name();
  1224. if (new_name != p_name) {
  1225. String text = TTR("Invalid node name, the following characters are not allowed:") + "\n" + String::get_invalid_node_name_characters();
  1226. if (error->is_visible()) {
  1227. if (!error->get_meta("invalid_character", false)) {
  1228. error->set_text(error->get_text() + "\n\n" + text);
  1229. error->set_meta("invalid_character", true);
  1230. }
  1231. } else {
  1232. error->set_text(text);
  1233. error->set_meta("invalid_character", true);
  1234. error->set_meta("same_unique_name", false);
  1235. error->popup_centered();
  1236. }
  1237. }
  1238. // Trim leading/trailing whitespace to prevent node names from containing accidental whitespace,
  1239. // which would make it more difficult to get the node via `get_node()`.
  1240. new_name = new_name.strip_edges();
  1241. if (new_name.is_empty() && p_node->get_owner() != nullptr && !p_node->get_scene_file_path().is_empty()) {
  1242. // If name is empty and node is root of an instance, revert to the original name.
  1243. const Ref<PackedScene> node_scene = ResourceLoader::load(p_node->get_scene_file_path());
  1244. if (node_scene.is_valid()) {
  1245. const Ref<SceneState> &state = node_scene->get_state();
  1246. if (state->get_node_count() > 0) {
  1247. new_name = state->get_node_name(0); // Root's name.
  1248. }
  1249. }
  1250. }
  1251. if (new_name.is_empty()) {
  1252. // If name is still empty, fallback to class name.
  1253. if (GLOBAL_GET("editor/naming/node_name_casing").operator int() != NAME_CASING_PASCAL_CASE) {
  1254. new_name = Node::adjust_name_casing(p_node->get_class());
  1255. } else {
  1256. new_name = p_node->get_class();
  1257. }
  1258. }
  1259. new_name = p_node->get_parent()->prevalidate_child_name(p_node, new_name);
  1260. if (new_name == p_node->get_name()) {
  1261. item->set_text(0, new_name);
  1262. return;
  1263. }
  1264. // We previously made sure name is not the same as current name
  1265. // so that it won't complain about already used unique name when not changing name.
  1266. if (p_node->is_unique_name_in_owner() && get_tree()->get_edited_scene_root()->get_node_or_null("%" + new_name)) {
  1267. String text = vformat(TTR("A node with the unique name %s already exists in this scene."), new_name);
  1268. if (error->is_visible()) {
  1269. if (!error->get_meta("same_unique_name", false)) {
  1270. error->set_text(error->get_text() + "\n\n" + text);
  1271. error->set_meta("same_unique_name", true);
  1272. }
  1273. } else {
  1274. error->set_text(text);
  1275. error->set_meta("same_unique_name", true);
  1276. error->set_meta("invalid_character", false);
  1277. error->popup_centered();
  1278. }
  1279. item->set_text(0, p_node->get_name());
  1280. return;
  1281. }
  1282. if (!is_scene_tree_dock) {
  1283. p_node->set_name(new_name);
  1284. item->set_metadata(0, p_node->get_path());
  1285. emit_signal(SNAME("node_renamed"));
  1286. } else {
  1287. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1288. undo_redo->create_action(TTR("Rename Node"), UndoRedo::MERGE_DISABLE, p_node);
  1289. emit_signal(SNAME("node_prerename"), p_node, new_name);
  1290. undo_redo->add_undo_method(p_node, "set_name", p_node->get_name());
  1291. undo_redo->add_undo_method(item, "set_metadata", 0, p_node->get_path());
  1292. undo_redo->add_undo_method(item, "set_text", 0, p_node->get_name());
  1293. p_node->set_name(new_name);
  1294. undo_redo->add_do_method(p_node, "set_name", new_name);
  1295. undo_redo->add_do_method(item, "set_metadata", 0, p_node->get_path());
  1296. undo_redo->add_do_method(item, "set_text", 0, new_name);
  1297. undo_redo->commit_action();
  1298. }
  1299. }
  1300. void SceneTreeEditor::_edited() {
  1301. TreeItem *which = tree->get_next_selected(nullptr);
  1302. ERR_FAIL_NULL(which);
  1303. TreeItem *edited = tree->get_edited();
  1304. ERR_FAIL_NULL(edited);
  1305. if (is_scene_tree_dock && tree->get_next_selected(which)) {
  1306. List<Node *> nodes_to_rename;
  1307. for (TreeItem *item = which; item; item = tree->get_next_selected(item)) {
  1308. Node *n = get_node(item->get_metadata(0));
  1309. ERR_FAIL_NULL(n);
  1310. nodes_to_rename.push_back(n);
  1311. }
  1312. ERR_FAIL_COND(nodes_to_rename.is_empty());
  1313. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1314. undo_redo->create_action(TTR("Rename Nodes"), UndoRedo::MERGE_DISABLE, nodes_to_rename.front()->get(), true);
  1315. TreeItem *item = which;
  1316. String new_name = edited->get_text(0);
  1317. for (Node *n : nodes_to_rename) {
  1318. rename_node(n, new_name, item);
  1319. item = tree->get_next_selected(item);
  1320. }
  1321. undo_redo->commit_action();
  1322. } else {
  1323. Node *n = get_node(which->get_metadata(0));
  1324. ERR_FAIL_NULL(n);
  1325. rename_node(n, which->get_text(0));
  1326. }
  1327. }
  1328. Node *SceneTreeEditor::get_selected() {
  1329. return selected;
  1330. }
  1331. void SceneTreeEditor::_update_marking_list(const HashSet<Node *> &p_marked) {
  1332. for (Node *N : p_marked) {
  1333. HashMap<Node *, CachedNode>::Iterator I = node_cache.get(N);
  1334. if (I) {
  1335. node_cache.mark_dirty(N);
  1336. node_cache.mark_children_dirty(N, true);
  1337. }
  1338. }
  1339. }
  1340. void SceneTreeEditor::set_marked(const HashSet<Node *> &p_marked, bool p_selectable, bool p_children_selectable) {
  1341. _update_if_clean();
  1342. _update_marking_list(marked);
  1343. _update_marking_list(p_marked);
  1344. marked = p_marked;
  1345. marked_selectable = p_selectable;
  1346. marked_children_selectable = p_children_selectable;
  1347. _update_tree();
  1348. }
  1349. void SceneTreeEditor::set_marked(Node *p_marked, bool p_selectable, bool p_children_selectable) {
  1350. HashSet<Node *> s;
  1351. if (p_marked) {
  1352. s.insert(p_marked);
  1353. }
  1354. set_marked(s, p_selectable, p_children_selectable);
  1355. }
  1356. void SceneTreeEditor::set_filter(const String &p_filter) {
  1357. filter = p_filter;
  1358. _update_filter(nullptr, true);
  1359. }
  1360. String SceneTreeEditor::get_filter() const {
  1361. return filter;
  1362. }
  1363. String SceneTreeEditor::get_filter_term_warning() {
  1364. return filter_term_warning;
  1365. }
  1366. void SceneTreeEditor::set_show_all_nodes(bool p_show_all_nodes) {
  1367. show_all_nodes = p_show_all_nodes;
  1368. _update_filter(nullptr, true);
  1369. }
  1370. void SceneTreeEditor::set_as_scene_tree_dock() {
  1371. is_scene_tree_dock = true;
  1372. }
  1373. void SceneTreeEditor::set_display_foreign_nodes(bool p_display) {
  1374. display_foreign = p_display;
  1375. _update_tree();
  1376. }
  1377. void SceneTreeEditor::set_valid_types(const Vector<StringName> &p_valid) {
  1378. valid_types = p_valid;
  1379. node_cache.force_update = true;
  1380. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  1381. tree_dirty = true;
  1382. }
  1383. void SceneTreeEditor::set_editor_selection(EditorSelection *p_selection) {
  1384. editor_selection = p_selection;
  1385. tree->set_select_mode(Tree::SELECT_MULTI);
  1386. tree->set_cursor_can_exit_tree(false);
  1387. editor_selection->connect("selection_changed", callable_mp(this, &SceneTreeEditor::_selection_changed));
  1388. }
  1389. void SceneTreeEditor::_update_selection(TreeItem *item) {
  1390. ERR_FAIL_NULL(item);
  1391. NodePath np = item->get_metadata(0);
  1392. if (!has_node(np)) {
  1393. return;
  1394. }
  1395. Node *n = get_node(np);
  1396. if (!n) {
  1397. return;
  1398. }
  1399. if (editor_selection->is_selected(n)) {
  1400. if (!item->is_selected(0)) {
  1401. item->select(0);
  1402. }
  1403. } else {
  1404. if (item->is_selected(0)) {
  1405. TreeItem *previous_cursor_item = tree->get_selected();
  1406. item->deselect(0);
  1407. if (previous_cursor_item) {
  1408. previous_cursor_item->set_as_cursor(0);
  1409. }
  1410. }
  1411. }
  1412. TreeItem *c = item->get_first_child();
  1413. while (c) {
  1414. _update_selection(c);
  1415. c = c->get_next();
  1416. }
  1417. }
  1418. void SceneTreeEditor::_selection_changed() {
  1419. if (!editor_selection) {
  1420. return;
  1421. }
  1422. TreeItem *root = tree->get_root();
  1423. if (!root) {
  1424. return;
  1425. }
  1426. _update_selection(root);
  1427. }
  1428. void SceneTreeEditor::_cell_collapsed(Object *p_obj) {
  1429. if (updating_tree) {
  1430. return;
  1431. }
  1432. if (!can_rename) {
  1433. return;
  1434. }
  1435. TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
  1436. if (!ti) {
  1437. return;
  1438. }
  1439. bool collapsed = ti->is_collapsed();
  1440. NodePath np = ti->get_metadata(0);
  1441. Node *n = get_node(np);
  1442. ERR_FAIL_NULL(n);
  1443. n->set_display_folded(collapsed);
  1444. }
  1445. Variant SceneTreeEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1446. if (!can_rename) {
  1447. return Variant(); // Not editable tree.
  1448. }
  1449. if (tree->get_button_id_at_position(p_point) != -1) {
  1450. return Variant(); // Dragging from button.
  1451. }
  1452. Vector<Node *> selected_nodes;
  1453. Vector<Ref<Texture2D>> icons;
  1454. TreeItem *next = tree->get_next_selected(nullptr);
  1455. while (next) {
  1456. NodePath np = next->get_metadata(0);
  1457. Node *n = get_node(np);
  1458. if (n) {
  1459. selected_nodes.push_back(n);
  1460. icons.push_back(next->get_icon(0));
  1461. }
  1462. next = tree->get_next_selected(next);
  1463. }
  1464. if (selected_nodes.is_empty()) {
  1465. return Variant();
  1466. }
  1467. VBoxContainer *vb = memnew(VBoxContainer);
  1468. Array objs;
  1469. int list_max = 10;
  1470. float opacity_step = 1.0f / list_max;
  1471. float opacity_item = 1.0f;
  1472. for (int i = 0; i < selected_nodes.size(); i++) {
  1473. if (i < list_max) {
  1474. HBoxContainer *hb = memnew(HBoxContainer);
  1475. TextureRect *tf = memnew(TextureRect);
  1476. int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  1477. tf->set_custom_minimum_size(Size2(icon_size, icon_size));
  1478. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1479. tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1480. tf->set_texture(icons[i]);
  1481. hb->add_child(tf);
  1482. Label *label = memnew(Label(selected_nodes[i]->get_name()));
  1483. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1484. hb->add_child(label);
  1485. vb->add_child(hb);
  1486. hb->set_modulate(Color(1, 1, 1, opacity_item));
  1487. opacity_item -= opacity_step;
  1488. }
  1489. NodePath p = selected_nodes[i]->get_path();
  1490. objs.push_back(p);
  1491. }
  1492. set_drag_preview(vb);
  1493. Dictionary drag_data;
  1494. drag_data["type"] = "nodes";
  1495. drag_data["nodes"] = objs;
  1496. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1497. emit_signal(SNAME("nodes_dragged"));
  1498. return drag_data;
  1499. }
  1500. bool SceneTreeEditor::_is_script_type(const StringName &p_type) const {
  1501. return (script_types->find(p_type));
  1502. }
  1503. bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1504. if (!can_rename) {
  1505. return false; // Not editable tree.
  1506. }
  1507. Dictionary d = p_data;
  1508. if (!d.has("type")) {
  1509. return false;
  1510. }
  1511. TreeItem *item = tree->get_item_at_position(p_point);
  1512. if (!item) {
  1513. return false;
  1514. }
  1515. int section = tree->get_drop_section_at_position(p_point);
  1516. if (section < -1 || (section == -1 && !item->get_parent())) {
  1517. return false;
  1518. }
  1519. if (String(d["type"]) == "files") {
  1520. Vector<String> files = d["files"];
  1521. if (files.size() == 0) {
  1522. return false; // TODO Weird?
  1523. }
  1524. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(files[0]))) {
  1525. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1526. return true;
  1527. }
  1528. bool scene_drop = true;
  1529. bool audio_drop = true;
  1530. for (int i = 0; i < files.size(); i++) {
  1531. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[i]);
  1532. if (ftype != "PackedScene") {
  1533. scene_drop = false;
  1534. }
  1535. if (audio_drop && !ClassDB::is_parent_class(ftype, "AudioStream")) {
  1536. audio_drop = false;
  1537. }
  1538. }
  1539. if (scene_drop) {
  1540. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1541. return true;
  1542. }
  1543. if (audio_drop) {
  1544. if (files.size() > 1) {
  1545. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  1546. } else {
  1547. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1548. }
  1549. return true;
  1550. }
  1551. if (files.size() > 1) {
  1552. return false;
  1553. }
  1554. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1555. return true;
  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. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1563. return true;
  1564. }
  1565. }
  1566. }
  1567. if (filter.is_empty() && String(d["type"]) == "nodes") {
  1568. Array nodes = d["nodes"];
  1569. for (int i = 0; i < nodes.size(); i++) {
  1570. Node *n = get_node(nodes[i]);
  1571. // Nodes from an instantiated scene can't be rearranged.
  1572. if (n && n->get_owner() && n->get_owner() != get_scene_node() && !n->get_owner()->get_scene_file_path().is_empty()) {
  1573. return false;
  1574. }
  1575. }
  1576. return true;
  1577. }
  1578. return false;
  1579. }
  1580. void SceneTreeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1581. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  1582. return;
  1583. }
  1584. TreeItem *item = tree->get_item_at_position(p_point);
  1585. if (!item) {
  1586. return;
  1587. }
  1588. int section = tree->get_drop_section_at_position(p_point);
  1589. if (section < -1) {
  1590. return;
  1591. }
  1592. NodePath np = item->get_metadata(0);
  1593. Node *n = get_node(np);
  1594. if (!n) {
  1595. return;
  1596. }
  1597. Dictionary d = p_data;
  1598. if (String(d["type"]) == "nodes") {
  1599. Array nodes = d["nodes"];
  1600. emit_signal(SNAME("nodes_rearranged"), nodes, np, section);
  1601. }
  1602. if (String(d["type"]) == "files") {
  1603. Vector<String> files = d["files"];
  1604. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]);
  1605. if (_is_script_type(ftype)) {
  1606. emit_signal(SNAME("script_dropped"), files[0], np);
  1607. } else {
  1608. emit_signal(SNAME("files_dropped"), files, np, section);
  1609. }
  1610. }
  1611. if (String(d["type"]) == "script_list_element") {
  1612. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  1613. if (se) {
  1614. String sp = se->get_edited_resource()->get_path();
  1615. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  1616. emit_signal(SNAME("script_dropped"), sp, np);
  1617. }
  1618. }
  1619. }
  1620. }
  1621. void SceneTreeEditor::_empty_clicked(const Vector2 &p_pos, MouseButton p_button) {
  1622. if (p_button != MouseButton::RIGHT) {
  1623. return;
  1624. }
  1625. _rmb_select(p_pos);
  1626. }
  1627. void SceneTreeEditor::_rmb_select(const Vector2 &p_pos, MouseButton p_button) {
  1628. if (p_button != MouseButton::RIGHT) {
  1629. return;
  1630. }
  1631. emit_signal(SNAME("rmb_pressed"), tree->get_screen_position() + p_pos);
  1632. }
  1633. void SceneTreeEditor::update_warning() {
  1634. _warning_changed(nullptr);
  1635. }
  1636. void SceneTreeEditor::_warning_changed(Node *p_for_node) {
  1637. node_cache.mark_dirty(p_for_node);
  1638. // Should use a timer.
  1639. update_timer->start();
  1640. }
  1641. void SceneTreeEditor::set_auto_expand_selected(bool p_auto, bool p_update_settings) {
  1642. if (p_update_settings) {
  1643. EditorSettings::get_singleton()->set("docks/scene_tree/auto_expand_to_selected", p_auto);
  1644. }
  1645. auto_expand_selected = p_auto;
  1646. }
  1647. void SceneTreeEditor::set_hide_filtered_out_parents(bool p_hide, bool p_update_settings) {
  1648. if (p_hide == hide_filtered_out_parents) {
  1649. return;
  1650. }
  1651. if (p_update_settings) {
  1652. EditorSettings::get_singleton()->set("docks/scene_tree/hide_filtered_out_parents", p_hide);
  1653. }
  1654. hide_filtered_out_parents = p_hide;
  1655. if (hide_filtered_out_parents) {
  1656. _update_filter();
  1657. } else {
  1658. node_cache.force_update = true;
  1659. _update_tree();
  1660. }
  1661. }
  1662. void SceneTreeEditor::set_connect_to_script_mode(bool p_enable) {
  1663. connect_to_script_mode = p_enable;
  1664. _update_tree();
  1665. }
  1666. void SceneTreeEditor::set_connecting_signal(bool p_enable) {
  1667. connecting_signal = p_enable;
  1668. _update_tree();
  1669. }
  1670. void SceneTreeEditor::set_update_when_invisible(bool p_enable) {
  1671. update_when_invisible = p_enable;
  1672. _update_tree();
  1673. }
  1674. void SceneTreeEditor::_bind_methods() {
  1675. ClassDB::bind_method(D_METHOD("_update_tree"), &SceneTreeEditor::_update_tree, DEFVAL(false)); // Still used by UndoRedo.
  1676. ClassDB::bind_method(D_METHOD("update_tree"), &SceneTreeEditor::update_tree);
  1677. ADD_SIGNAL(MethodInfo("node_selected"));
  1678. ADD_SIGNAL(MethodInfo("node_renamed"));
  1679. ADD_SIGNAL(MethodInfo("node_prerename"));
  1680. ADD_SIGNAL(MethodInfo("node_changed"));
  1681. ADD_SIGNAL(MethodInfo("nodes_dragged"));
  1682. ADD_SIGNAL(MethodInfo("nodes_rearranged", PropertyInfo(Variant::ARRAY, "paths"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1683. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1684. ADD_SIGNAL(MethodInfo("script_dropped", PropertyInfo(Variant::STRING, "file"), PropertyInfo(Variant::NODE_PATH, "to_path")));
  1685. ADD_SIGNAL(MethodInfo("rmb_pressed", PropertyInfo(Variant::VECTOR2, "position")));
  1686. ADD_SIGNAL(MethodInfo("open"));
  1687. ADD_SIGNAL(MethodInfo("open_script"));
  1688. }
  1689. SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_open_instance) :
  1690. node_cache(this) {
  1691. selected = nullptr;
  1692. can_rename = p_can_rename;
  1693. can_open_instance = p_can_open_instance;
  1694. editor_selection = nullptr;
  1695. if (p_label) {
  1696. Label *label = memnew(Label);
  1697. label->set_theme_type_variation("HeaderSmall");
  1698. label->set_position(Point2(10, 0));
  1699. label->set_text(TTR("Scene Tree (Nodes):"));
  1700. add_child(label);
  1701. }
  1702. tree = memnew(Tree);
  1703. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1704. tree->set_anchor(SIDE_RIGHT, ANCHOR_END);
  1705. tree->set_anchor(SIDE_BOTTOM, ANCHOR_END);
  1706. tree->set_begin(Point2(0, p_label ? 18 : 0));
  1707. tree->set_end(Point2(0, 0));
  1708. tree->set_allow_reselect(true);
  1709. tree->add_theme_constant_override("button_margin", 0);
  1710. add_child(tree);
  1711. SET_DRAG_FORWARDING_GCD(tree, SceneTreeEditor);
  1712. if (p_can_rename) {
  1713. tree->set_allow_rmb_select(true);
  1714. tree->connect("item_mouse_selected", callable_mp(this, &SceneTreeEditor::_rmb_select));
  1715. tree->connect("empty_clicked", callable_mp(this, &SceneTreeEditor::_empty_clicked));
  1716. }
  1717. tree->connect("cell_selected", callable_mp(this, &SceneTreeEditor::_selected_changed));
  1718. tree->connect("item_edited", callable_mp(this, &SceneTreeEditor::_edited));
  1719. tree->connect("multi_selected", callable_mp(this, &SceneTreeEditor::_cell_multi_selected));
  1720. tree->connect("button_clicked", callable_mp(this, &SceneTreeEditor::_cell_button_pressed));
  1721. tree->connect("nothing_selected", callable_mp(this, &SceneTreeEditor::_deselect_items));
  1722. error = memnew(AcceptDialog);
  1723. add_child(error);
  1724. warning = memnew(AcceptDialog);
  1725. add_child(warning);
  1726. warning->set_title(TTR("Node Configuration Warning!"));
  1727. warning->set_flag(Window::FLAG_POPUP, true);
  1728. last_hash = 0;
  1729. blocked = 0;
  1730. update_timer = memnew(Timer);
  1731. update_timer->connect("timeout", callable_mp(this, &SceneTreeEditor::_update_tree).bind(false));
  1732. update_timer->set_one_shot(true);
  1733. update_timer->set_wait_time(0.5);
  1734. add_child(update_timer);
  1735. update_node_tooltip_delay = memnew(Timer);
  1736. update_node_tooltip_delay->set_wait_time(0.5);
  1737. update_node_tooltip_delay->set_one_shot(true);
  1738. add_child(update_node_tooltip_delay);
  1739. revoke_dialog = memnew(ConfirmationDialog);
  1740. revoke_dialog->set_ok_button_text(TTR("Revoke"));
  1741. add_child(revoke_dialog);
  1742. revoke_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SceneTreeEditor::_update_ask_before_revoking_unique_name));
  1743. VBoxContainer *vb = memnew(VBoxContainer);
  1744. revoke_dialog->add_child(vb);
  1745. revoke_dialog_label = memnew(Label);
  1746. vb->add_child(revoke_dialog_label);
  1747. ask_before_revoke_checkbox = memnew(CheckBox(TTR("Don't Ask Again")));
  1748. 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."));
  1749. vb->add_child(ask_before_revoke_checkbox);
  1750. script_types = memnew(List<StringName>);
  1751. ClassDB::get_inheriters_from_class("Script", script_types);
  1752. }
  1753. SceneTreeEditor::~SceneTreeEditor() {
  1754. memdelete(script_types);
  1755. }
  1756. /******** DIALOG *********/
  1757. void SceneTreeDialog::popup_scenetree_dialog(Node *p_selected_node, Node *p_marked_node, bool p_marked_node_selectable, bool p_marked_node_children_selectable) {
  1758. get_scene_tree()->set_marked(p_marked_node, p_marked_node_selectable, p_marked_node_children_selectable);
  1759. get_scene_tree()->set_selected(p_selected_node);
  1760. popup_centered_clamped(Size2(350, 700) * EDSCALE);
  1761. }
  1762. void SceneTreeDialog::_show_all_nodes_changed(bool p_button_pressed) {
  1763. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_all_nodes_for_node_selection", p_button_pressed);
  1764. tree->set_show_all_nodes(p_button_pressed);
  1765. }
  1766. void SceneTreeDialog::set_valid_types(const Vector<StringName> &p_valid) {
  1767. if (allowed_types_hbox) {
  1768. allowed_types_hbox->queue_free();
  1769. allowed_types_hbox = nullptr;
  1770. valid_type_icons.clear();
  1771. }
  1772. tree->set_valid_types(p_valid);
  1773. if (p_valid.is_empty()) {
  1774. return;
  1775. }
  1776. allowed_types_hbox = memnew(HBoxContainer);
  1777. content->add_child(allowed_types_hbox);
  1778. content->move_child(allowed_types_hbox, 0);
  1779. {
  1780. Label *label = memnew(Label);
  1781. allowed_types_hbox->add_child(label);
  1782. label->set_text(TTR("Allowed:"));
  1783. }
  1784. HFlowContainer *hflow = memnew(HFlowContainer);
  1785. allowed_types_hbox->add_child(hflow);
  1786. hflow->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1787. for (const StringName &type : p_valid) {
  1788. HBoxContainer *hb = memnew(HBoxContainer);
  1789. hflow->add_child(hb);
  1790. // Attempt to get the correct name and icon for script path types.
  1791. String name = type;
  1792. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(type);
  1793. // If we can't find a global class icon, try to find one for the script.
  1794. if (icon.is_null() && ResourceLoader::exists(type, "Script")) {
  1795. Ref<Script> node_script = ResourceLoader::load(type);
  1796. if (node_script.is_valid()) {
  1797. name = name.get_file();
  1798. icon = EditorNode::get_singleton()->get_object_icon(node_script.ptr());
  1799. }
  1800. }
  1801. TextureRect *trect = memnew(TextureRect);
  1802. hb->add_child(trect);
  1803. trect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1804. trect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1805. trect->set_meta("icon", icon);
  1806. valid_type_icons.push_back(trect);
  1807. Label *label = memnew(Label);
  1808. hb->add_child(label);
  1809. label->set_text(name);
  1810. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1811. }
  1812. show_all_nodes->show();
  1813. if (is_inside_tree()) {
  1814. _update_valid_type_icons();
  1815. }
  1816. }
  1817. void SceneTreeDialog::_notification(int p_what) {
  1818. switch (p_what) {
  1819. case NOTIFICATION_VISIBILITY_CHANGED: {
  1820. if (is_visible()) {
  1821. tree->update_tree();
  1822. // Select the search bar by default.
  1823. callable_mp((Control *)filter, &Control::grab_focus).call_deferred();
  1824. }
  1825. } break;
  1826. case NOTIFICATION_ENTER_TREE: {
  1827. connect(SceneStringName(confirmed), callable_mp(this, &SceneTreeDialog::_select));
  1828. } break;
  1829. case NOTIFICATION_THEME_CHANGED: {
  1830. _update_valid_type_icons();
  1831. } break;
  1832. case NOTIFICATION_EXIT_TREE: {
  1833. disconnect(SceneStringName(confirmed), callable_mp(this, &SceneTreeDialog::_select));
  1834. } break;
  1835. }
  1836. }
  1837. void SceneTreeDialog::_update_valid_type_icons() {
  1838. filter->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  1839. for (TextureRect *trect : valid_type_icons) {
  1840. trect->set_custom_minimum_size(Vector2(get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)), 0));
  1841. trect->set_texture(trect->get_meta("icon"));
  1842. }
  1843. }
  1844. void SceneTreeDialog::_cancel() {
  1845. hide();
  1846. }
  1847. void SceneTreeDialog::_select() {
  1848. if (tree->get_selected()) {
  1849. // The signal may cause another dialog to be displayed, so be sure to hide this one first.
  1850. hide();
  1851. emit_signal(SNAME("selected"), tree->get_selected()->get_path());
  1852. }
  1853. }
  1854. void SceneTreeDialog::_selected_changed() {
  1855. get_ok_button()->set_disabled(!tree->get_selected());
  1856. }
  1857. void SceneTreeDialog::_filter_changed(const String &p_filter) {
  1858. tree->set_filter(p_filter);
  1859. }
  1860. void SceneTreeDialog::_on_filter_gui_input(const Ref<InputEvent> &p_event) {
  1861. // Redirect navigational key events to the tree.
  1862. Ref<InputEventKey> key = p_event;
  1863. if (key.is_valid()) {
  1864. 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")) {
  1865. tree->get_scene_tree()->gui_input(key);
  1866. filter->accept_event();
  1867. }
  1868. }
  1869. }
  1870. void SceneTreeDialog::_bind_methods() {
  1871. ClassDB::bind_method("_cancel", &SceneTreeDialog::_cancel);
  1872. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::NODE_PATH, "path")));
  1873. }
  1874. SceneTreeDialog::SceneTreeDialog() {
  1875. set_title(TTR("Select a Node"));
  1876. content = memnew(VBoxContainer);
  1877. add_child(content);
  1878. HBoxContainer *filter_hbc = memnew(HBoxContainer);
  1879. content->add_child(filter_hbc);
  1880. filter = memnew(LineEdit);
  1881. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1882. filter->set_placeholder(TTR("Filter Nodes"));
  1883. filter->set_clear_button_enabled(true);
  1884. filter->add_theme_constant_override("minimum_character_width", 0);
  1885. filter->connect(SceneStringName(text_changed), callable_mp(this, &SceneTreeDialog::_filter_changed));
  1886. filter->connect(SceneStringName(gui_input), callable_mp(this, &SceneTreeDialog::_on_filter_gui_input));
  1887. register_text_enter(filter);
  1888. filter_hbc->add_child(filter);
  1889. // Add 'Show All' button to HBoxContainer next to the filter, visible only when valid_types is defined.
  1890. show_all_nodes = memnew(CheckButton);
  1891. show_all_nodes->set_text(TTR("Show All"));
  1892. show_all_nodes->connect(SceneStringName(toggled), callable_mp(this, &SceneTreeDialog::_show_all_nodes_changed));
  1893. show_all_nodes->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);
  1894. show_all_nodes->hide();
  1895. filter_hbc->add_child(show_all_nodes);
  1896. tree = memnew(SceneTreeEditor(false, false, true));
  1897. tree->set_update_when_invisible(false);
  1898. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1899. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &SceneTreeDialog::_select));
  1900. // Initialize button state, must be done after the tree has been created to update its 'show_all_nodes' flag.
  1901. // This is also done before adding the tree to the content to avoid triggering unnecessary tree filtering.
  1902. show_all_nodes->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_all_nodes_for_node_selection", false));
  1903. content->add_child(tree);
  1904. // Disable the OK button when no node is selected.
  1905. get_ok_button()->set_disabled(!tree->get_selected());
  1906. tree->connect("node_selected", callable_mp(this, &SceneTreeDialog::_selected_changed));
  1907. }
  1908. SceneTreeDialog::~SceneTreeDialog() {
  1909. }
  1910. /******** CACHE *********/
  1911. HashMap<Node *, SceneTreeEditor::CachedNode>::Iterator SceneTreeEditor::NodeCache::add(Node *p_node, TreeItem *p_item) {
  1912. if (!p_node) {
  1913. return HashMap<Node *, CachedNode>::Iterator();
  1914. }
  1915. return cache.insert(p_node, CachedNode(p_node, p_item));
  1916. }
  1917. HashMap<Node *, SceneTreeEditor::CachedNode>::Iterator SceneTreeEditor::NodeCache::get(Node *p_node, bool p_deleted_ok) {
  1918. if (!p_node) {
  1919. return HashMap<Node *, CachedNode>::Iterator();
  1920. }
  1921. HashMap<Node *, CachedNode>::Iterator I = cache.find(p_node);
  1922. if (I) {
  1923. if (I->value.delete_serial != UINT16_MAX) {
  1924. // Don't give us a node marked for deletion.
  1925. if (!p_deleted_ok) {
  1926. return HashMap<Node *, CachedNode>::Iterator();
  1927. }
  1928. to_delete.erase(&I->value);
  1929. I->value.delete_serial = UINT16_MAX;
  1930. // If we were resurrected from near-death we might have been renamed.
  1931. // Make sure that we are updated properly.
  1932. mark_dirty(p_node);
  1933. mark_children_dirty(p_node, true);
  1934. }
  1935. }
  1936. return I;
  1937. }
  1938. void SceneTreeEditor::NodeCache::remove(Node *p_node, bool p_recursive) {
  1939. if (!p_node) {
  1940. return;
  1941. }
  1942. if (p_node == editor->selected) {
  1943. editor->selected = nullptr;
  1944. }
  1945. editor->marked.erase(p_node);
  1946. HashMap<Node *, CachedNode>::Iterator I = cache.find(p_node);
  1947. if (I) {
  1948. if (p_recursive) {
  1949. int cc = p_node->get_child_count(false);
  1950. for (int i = 0; i < cc; i++) {
  1951. remove(p_node->get_child(i, false), p_recursive);
  1952. }
  1953. }
  1954. if (current_scene_node != p_node) {
  1955. // Do not remove from the Tree control here. See delete_pending below.
  1956. I->value.item->deselect(0);
  1957. I->value.delete_serial = delete_serial;
  1958. I->value.index = -1;
  1959. I->value.cache_iterator = I;
  1960. to_delete.insert(&I->value);
  1961. } else {
  1962. // If it is the root node, we leave the TreeItem and reuse it later.
  1963. cache.remove(I);
  1964. }
  1965. }
  1966. }
  1967. void SceneTreeEditor::NodeCache::mark_dirty(Node *p_node, bool p_parents) {
  1968. Node *node = p_node;
  1969. while (node) {
  1970. HashMap<Node *, CachedNode>::Iterator I = cache.find(node);
  1971. if (I) {
  1972. I->value.dirty = true;
  1973. }
  1974. if (!p_parents) {
  1975. break;
  1976. }
  1977. node = node->get_parent();
  1978. }
  1979. }
  1980. void SceneTreeEditor::NodeCache::mark_children_dirty(Node *p_node, bool p_recursive) {
  1981. if (!p_node) {
  1982. return;
  1983. }
  1984. int cc = p_node->get_child_count(false);
  1985. for (int i = 0; i < cc; i++) {
  1986. Node *c = p_node->get_child(i, false);
  1987. HashMap<Node *, CachedNode>::Iterator IC = cache.find(c);
  1988. if (IC) {
  1989. IC->value.dirty = true;
  1990. if (p_recursive) {
  1991. mark_children_dirty(c, p_recursive);
  1992. }
  1993. }
  1994. }
  1995. }
  1996. void SceneTreeEditor::NodeCache::delete_pending() {
  1997. HashSet<CachedNode *>::Iterator I = to_delete.begin();
  1998. while (I) {
  1999. // We want to keep TreeItems around just long enough for a Node removal,
  2000. // and immediate reinsertion. This is what happens with moves and
  2001. // type changes.
  2002. if (Math::abs((*I)->delete_serial - delete_serial) >= 2) {
  2003. memdelete((*I)->item);
  2004. cache.remove((*I)->cache_iterator);
  2005. to_delete.remove(I);
  2006. } else if (!(*I)->removed) {
  2007. // We don't remove from the tree until now because if the node got
  2008. // deleted from a @tool script the SceneTreeEditor might have had it
  2009. // marked or selected before the node was removed. If we immediately
  2010. // remove from the Tree control then we end up trying to scroll to an
  2011. // Item without a parent.
  2012. //
  2013. // We might already be removed (and thus not have a parent) by rapid
  2014. // undo/redo.
  2015. if (!(*I)->removed) {
  2016. TreeItem *parent = (*I)->item->get_parent();
  2017. parent->remove_child((*I)->item);
  2018. }
  2019. (*I)->removed = true;
  2020. }
  2021. ++I;
  2022. }
  2023. ++delete_serial;
  2024. }
  2025. void SceneTreeEditor::NodeCache::clear() {
  2026. for (CachedNode *E : to_delete) {
  2027. // Only removed entries won't be automatically cleaned up by Tree::clear().
  2028. if (E->removed) {
  2029. memdelete(E->item);
  2030. }
  2031. }
  2032. cache.clear();
  2033. to_delete.clear();
  2034. }