scene_tree_dock.cpp 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556
  1. /*************************************************************************/
  2. /* scene_tree_dock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_dock.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/object/message_queue.h"
  35. #include "core/os/keyboard.h"
  36. #include "editor/debugger/editor_debugger_node.h"
  37. #include "editor/editor_feature_profile.h"
  38. #include "editor/editor_file_dialog.h"
  39. #include "editor/editor_node.h"
  40. #include "editor/editor_scale.h"
  41. #include "editor/editor_settings.h"
  42. #include "editor/multi_node_edit.h"
  43. #include "editor/plugins/animation_player_editor_plugin.h"
  44. #include "editor/plugins/canvas_item_editor_plugin.h"
  45. #include "editor/plugins/node_3d_editor_plugin.h"
  46. #include "editor/plugins/script_editor_plugin.h"
  47. #include "editor/shader_create_dialog.h"
  48. #include "scene/main/window.h"
  49. #include "scene/property_utils.h"
  50. #include "scene/resources/packed_scene.h"
  51. #include "servers/display_server.h"
  52. #include "servers/rendering_server.h"
  53. #include "modules/modules_enabled.gen.h" // For regex.
  54. #ifdef MODULE_REGEX_ENABLED
  55. #include "editor/rename_dialog.h"
  56. #endif // MODULE_REGEX_ENABLED
  57. void SceneTreeDock::_nodes_drag_begin() {
  58. pending_click_select = nullptr;
  59. }
  60. void SceneTreeDock::_quick_open() {
  61. instantiate_scenes(quick_open->get_selected_files(), scene_tree->get_selected());
  62. }
  63. void SceneTreeDock::input(const Ref<InputEvent> &p_event) {
  64. ERR_FAIL_COND(p_event.is_null());
  65. Ref<InputEventMouseButton> mb = p_event;
  66. if (mb.is_valid() && (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT)) {
  67. if (mb->is_pressed() && scene_tree->get_rect().has_point(mb->get_position())) {
  68. tree_clicked = true;
  69. } else if (!mb->is_pressed()) {
  70. tree_clicked = false;
  71. }
  72. if (!mb->is_pressed() && pending_click_select) {
  73. _push_item(pending_click_select);
  74. pending_click_select = nullptr;
  75. }
  76. }
  77. }
  78. void SceneTreeDock::shortcut_input(const Ref<InputEvent> &p_event) {
  79. ERR_FAIL_COND(p_event.is_null());
  80. if (get_viewport()->gui_get_focus_owner() && get_viewport()->gui_get_focus_owner()->is_text_field()) {
  81. return;
  82. }
  83. if (!p_event->is_pressed() || p_event->is_echo()) {
  84. return;
  85. }
  86. if (ED_IS_SHORTCUT("scene_tree/rename", p_event)) {
  87. _tool_selected(TOOL_RENAME);
  88. #ifdef MODULE_REGEX_ENABLED
  89. } else if (ED_IS_SHORTCUT("scene_tree/batch_rename", p_event)) {
  90. _tool_selected(TOOL_BATCH_RENAME);
  91. #endif // MODULE_REGEX_ENABLED
  92. } else if (ED_IS_SHORTCUT("scene_tree/add_child_node", p_event)) {
  93. _tool_selected(TOOL_NEW);
  94. } else if (ED_IS_SHORTCUT("scene_tree/instance_scene", p_event)) {
  95. _tool_selected(TOOL_INSTANTIATE);
  96. } else if (ED_IS_SHORTCUT("scene_tree/expand_collapse_all", p_event)) {
  97. _tool_selected(TOOL_EXPAND_COLLAPSE);
  98. } else if (ED_IS_SHORTCUT("scene_tree/cut_node", p_event)) {
  99. _tool_selected(TOOL_CUT);
  100. } else if (ED_IS_SHORTCUT("scene_tree/copy_node", p_event)) {
  101. _tool_selected(TOOL_COPY);
  102. } else if (ED_IS_SHORTCUT("scene_tree/paste_node", p_event)) {
  103. _tool_selected(TOOL_PASTE);
  104. } else if (ED_IS_SHORTCUT("scene_tree/change_node_type", p_event)) {
  105. _tool_selected(TOOL_REPLACE);
  106. } else if (ED_IS_SHORTCUT("scene_tree/duplicate", p_event)) {
  107. _tool_selected(TOOL_DUPLICATE);
  108. } else if (ED_IS_SHORTCUT("scene_tree/attach_script", p_event)) {
  109. _tool_selected(TOOL_ATTACH_SCRIPT);
  110. } else if (ED_IS_SHORTCUT("scene_tree/detach_script", p_event)) {
  111. _tool_selected(TOOL_DETACH_SCRIPT);
  112. } else if (ED_IS_SHORTCUT("scene_tree/move_up", p_event)) {
  113. _tool_selected(TOOL_MOVE_UP);
  114. } else if (ED_IS_SHORTCUT("scene_tree/move_down", p_event)) {
  115. _tool_selected(TOOL_MOVE_DOWN);
  116. } else if (ED_IS_SHORTCUT("scene_tree/reparent", p_event)) {
  117. _tool_selected(TOOL_REPARENT);
  118. } else if (ED_IS_SHORTCUT("scene_tree/save_branch_as_scene", p_event)) {
  119. _tool_selected(TOOL_NEW_SCENE_FROM);
  120. } else if (ED_IS_SHORTCUT("scene_tree/delete_no_confirm", p_event)) {
  121. _tool_selected(TOOL_ERASE, true);
  122. } else if (ED_IS_SHORTCUT("scene_tree/copy_node_path", p_event)) {
  123. _tool_selected(TOOL_COPY_NODE_PATH);
  124. } else if (ED_IS_SHORTCUT("scene_tree/delete", p_event)) {
  125. _tool_selected(TOOL_ERASE);
  126. } else {
  127. return;
  128. }
  129. // Tool selection was successful, accept the event to stop propagation.
  130. accept_event();
  131. }
  132. void SceneTreeDock::instantiate(const String &p_file) {
  133. Vector<String> scenes;
  134. scenes.push_back(p_file);
  135. instantiate_scenes(scenes, scene_tree->get_selected());
  136. }
  137. void SceneTreeDock::instantiate_scenes(const Vector<String> &p_files, Node *p_parent) {
  138. Node *parent = p_parent;
  139. if (!parent) {
  140. parent = scene_tree->get_selected();
  141. }
  142. if (!parent) {
  143. parent = edited_scene;
  144. }
  145. if (!parent) {
  146. if (p_files.size() == 1) {
  147. accept->set_text(TTR("No parent to instantiate a child at."));
  148. } else {
  149. accept->set_text(TTR("No parent to instantiate the scenes at."));
  150. }
  151. accept->popup_centered();
  152. return;
  153. };
  154. _perform_instantiate_scenes(p_files, parent, -1);
  155. }
  156. void SceneTreeDock::_perform_instantiate_scenes(const Vector<String> &p_files, Node *parent, int p_pos) {
  157. ERR_FAIL_COND(!parent);
  158. Vector<Node *> instances;
  159. bool error = false;
  160. for (int i = 0; i < p_files.size(); i++) {
  161. Ref<PackedScene> sdata = ResourceLoader::load(p_files[i]);
  162. if (!sdata.is_valid()) {
  163. current_option = -1;
  164. accept->set_text(vformat(TTR("Error loading scene from %s"), p_files[i]));
  165. accept->popup_centered();
  166. error = true;
  167. break;
  168. }
  169. Node *instantiated_scene = sdata->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  170. if (!instantiated_scene) {
  171. current_option = -1;
  172. accept->set_text(vformat(TTR("Error instancing scene from %s"), p_files[i]));
  173. accept->popup_centered();
  174. error = true;
  175. break;
  176. }
  177. if (!edited_scene->get_scene_file_path().is_empty()) {
  178. if (_cyclical_dependency_exists(edited_scene->get_scene_file_path(), instantiated_scene)) {
  179. accept->set_text(vformat(TTR("Cannot instance the scene '%s' because the current scene exists within one of its nodes."), p_files[i]));
  180. accept->popup_centered();
  181. error = true;
  182. break;
  183. }
  184. }
  185. instantiated_scene->set_scene_file_path(ProjectSettings::get_singleton()->localize_path(p_files[i]));
  186. instances.push_back(instantiated_scene);
  187. }
  188. if (error) {
  189. for (int i = 0; i < instances.size(); i++) {
  190. memdelete(instances[i]);
  191. }
  192. return;
  193. }
  194. editor_data->get_undo_redo().create_action(TTR("Instance Scene(s)"));
  195. for (int i = 0; i < instances.size(); i++) {
  196. Node *instantiated_scene = instances[i];
  197. editor_data->get_undo_redo().add_do_method(parent, "add_child", instantiated_scene, true);
  198. if (p_pos >= 0) {
  199. editor_data->get_undo_redo().add_do_method(parent, "move_child", instantiated_scene, p_pos + i);
  200. }
  201. editor_data->get_undo_redo().add_do_method(instantiated_scene, "set_owner", edited_scene);
  202. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  203. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", instantiated_scene);
  204. editor_data->get_undo_redo().add_do_reference(instantiated_scene);
  205. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instantiated_scene);
  206. String new_name = parent->validate_child_name(instantiated_scene);
  207. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  208. editor_data->get_undo_redo().add_do_method(ed, "live_debug_instance_node", edited_scene->get_path_to(parent), p_files[i], new_name);
  209. editor_data->get_undo_redo().add_undo_method(ed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)).plus_file(new_name)));
  210. }
  211. editor_data->get_undo_redo().commit_action();
  212. _push_item(instances[instances.size() - 1]);
  213. for (int i = 0; i < instances.size(); i++) {
  214. emit_signal(SNAME("node_created"), instances[i]);
  215. }
  216. }
  217. void SceneTreeDock::_replace_with_branch_scene(const String &p_file, Node *base) {
  218. Ref<PackedScene> sdata = ResourceLoader::load(p_file);
  219. if (!sdata.is_valid()) {
  220. accept->set_text(vformat(TTR("Error loading scene from %s"), p_file));
  221. accept->popup_centered();
  222. return;
  223. }
  224. Node *instantiated_scene = sdata->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  225. if (!instantiated_scene) {
  226. accept->set_text(vformat(TTR("Error instancing scene from %s"), p_file));
  227. accept->popup_centered();
  228. return;
  229. }
  230. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  231. undo_redo->create_action(TTR("Replace with Branch Scene"));
  232. Node *parent = base->get_parent();
  233. int pos = base->get_index();
  234. undo_redo->add_do_method(parent, "remove_child", base);
  235. undo_redo->add_undo_method(parent, "remove_child", instantiated_scene);
  236. undo_redo->add_do_method(parent, "add_child", instantiated_scene, true);
  237. undo_redo->add_undo_method(parent, "add_child", base, true);
  238. undo_redo->add_do_method(parent, "move_child", instantiated_scene, pos);
  239. undo_redo->add_undo_method(parent, "move_child", base, pos);
  240. List<Node *> owned;
  241. base->get_owned_by(base->get_owner(), &owned);
  242. Array owners;
  243. for (Node *F : owned) {
  244. owners.push_back(F);
  245. }
  246. undo_redo->add_do_method(instantiated_scene, "set_owner", edited_scene);
  247. undo_redo->add_undo_method(this, "_set_owners", edited_scene, owners);
  248. undo_redo->add_do_method(editor_selection, "clear");
  249. undo_redo->add_undo_method(editor_selection, "clear");
  250. undo_redo->add_do_method(editor_selection, "add_node", instantiated_scene);
  251. undo_redo->add_undo_method(editor_selection, "add_node", base);
  252. undo_redo->add_do_property(scene_tree, "set_selected", instantiated_scene);
  253. undo_redo->add_undo_property(scene_tree, "set_selected", base);
  254. undo_redo->add_do_reference(instantiated_scene);
  255. undo_redo->add_undo_reference(base);
  256. undo_redo->commit_action();
  257. }
  258. bool SceneTreeDock::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  259. int childCount = p_desired_node->get_child_count();
  260. if (_track_inherit(p_target_scene_path, p_desired_node)) {
  261. return true;
  262. }
  263. for (int i = 0; i < childCount; i++) {
  264. Node *child = p_desired_node->get_child(i);
  265. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. bool SceneTreeDock::_track_inherit(const String &p_target_scene_path, Node *p_desired_node) {
  272. Node *p = p_desired_node;
  273. bool result = false;
  274. Vector<Node *> instances;
  275. while (true) {
  276. if (p->get_scene_file_path() == p_target_scene_path) {
  277. result = true;
  278. break;
  279. }
  280. Ref<SceneState> ss = p->get_scene_inherited_state();
  281. if (ss.is_valid()) {
  282. String path = ss->get_path();
  283. Ref<PackedScene> data = ResourceLoader::load(path);
  284. if (data.is_valid()) {
  285. p = data->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  286. if (!p) {
  287. continue;
  288. }
  289. instances.push_back(p);
  290. } else {
  291. break;
  292. }
  293. } else {
  294. break;
  295. }
  296. }
  297. for (int i = 0; i < instances.size(); i++) {
  298. memdelete(instances[i]);
  299. }
  300. return result;
  301. }
  302. void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
  303. current_option = p_tool;
  304. switch (p_tool) {
  305. #ifdef MODULE_REGEX_ENABLED
  306. case TOOL_BATCH_RENAME: {
  307. if (!profile_allow_editing) {
  308. break;
  309. }
  310. if (editor_selection->get_selected_node_list().size() > 1) {
  311. rename_dialog->popup_centered();
  312. }
  313. } break;
  314. #endif // MODULE_REGEX_ENABLED
  315. case TOOL_RENAME: {
  316. if (!profile_allow_editing) {
  317. break;
  318. }
  319. Tree *tree = scene_tree->get_scene_tree();
  320. if (tree->is_anything_selected()) {
  321. tree->grab_focus();
  322. tree->edit_selected();
  323. }
  324. } break;
  325. case TOOL_REPARENT_TO_NEW_NODE:
  326. if (!_validate_no_foreign()) {
  327. break;
  328. }
  329. [[fallthrough]];
  330. case TOOL_NEW: {
  331. if (!profile_allow_editing) {
  332. break;
  333. }
  334. if (reset_create_dialog && !p_confirm_override) {
  335. create_dialog->set_base_type("Node");
  336. reset_create_dialog = false;
  337. }
  338. // Prefer nodes that inherit from the current scene root.
  339. Node *current_edited_scene_root = EditorNode::get_singleton()->get_edited_scene();
  340. if (current_edited_scene_root) {
  341. String root_class = current_edited_scene_root->get_class_name();
  342. static Vector<String> preferred_types;
  343. if (preferred_types.is_empty()) {
  344. preferred_types.push_back("Control");
  345. preferred_types.push_back("Node2D");
  346. preferred_types.push_back("Node3D");
  347. }
  348. for (int i = 0; i < preferred_types.size(); i++) {
  349. if (ClassDB::is_parent_class(root_class, preferred_types[i])) {
  350. create_dialog->set_preferred_search_result_type(preferred_types[i]);
  351. break;
  352. }
  353. }
  354. }
  355. create_dialog->popup_create(true);
  356. if (!p_confirm_override) {
  357. emit_signal(SNAME("add_node_used"));
  358. }
  359. } break;
  360. case TOOL_INSTANTIATE: {
  361. if (!profile_allow_editing) {
  362. break;
  363. }
  364. Node *scene = edited_scene;
  365. if (!scene) {
  366. EditorNode::get_singleton()->new_inherited_scene();
  367. break;
  368. }
  369. quick_open->popup_dialog("PackedScene", true);
  370. quick_open->set_title(TTR("Instantiate Child Scene"));
  371. if (!p_confirm_override) {
  372. emit_signal(SNAME("add_node_used"));
  373. }
  374. } break;
  375. case TOOL_EXPAND_COLLAPSE: {
  376. Tree *tree = scene_tree->get_scene_tree();
  377. TreeItem *selected_item = tree->get_selected();
  378. if (!selected_item) {
  379. selected_item = tree->get_root();
  380. if (!selected_item) {
  381. break;
  382. }
  383. }
  384. bool collapsed = _is_collapsed_recursive(selected_item);
  385. _set_collapsed_recursive(selected_item, !collapsed);
  386. tree->ensure_cursor_is_visible();
  387. } break;
  388. case TOOL_CUT:
  389. case TOOL_COPY: {
  390. if (!edited_scene || (p_tool == TOOL_CUT && !_validate_no_foreign())) {
  391. break;
  392. }
  393. List<Node *> selection = editor_selection->get_selected_node_list();
  394. if (selection.size() == 0) {
  395. break;
  396. }
  397. bool was_empty = false;
  398. if (!node_clipboard.is_empty()) {
  399. _clear_clipboard();
  400. } else {
  401. was_empty = true;
  402. }
  403. clipboard_source_scene = EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path();
  404. selection.sort_custom<Node::Comparator>();
  405. for (Node *node : selection) {
  406. HashMap<const Node *, Node *> duplimap;
  407. Node *dup = node->duplicate_from_editor(duplimap);
  408. ERR_CONTINUE(!dup);
  409. node_clipboard.push_back(dup);
  410. }
  411. if (p_tool == TOOL_CUT) {
  412. _delete_confirm(true);
  413. }
  414. if (was_empty) {
  415. _update_create_root_dialog();
  416. }
  417. } break;
  418. case TOOL_PASTE: {
  419. paste_nodes();
  420. } break;
  421. case TOOL_REPLACE: {
  422. if (!profile_allow_editing) {
  423. break;
  424. }
  425. if (!_validate_no_foreign()) {
  426. break;
  427. }
  428. if (!_validate_no_instance()) {
  429. break;
  430. }
  431. if (reset_create_dialog) {
  432. create_dialog->set_base_type("Node");
  433. reset_create_dialog = false;
  434. }
  435. Node *selected = scene_tree->get_selected();
  436. if (!selected && !editor_selection->get_selected_node_list().is_empty()) {
  437. selected = editor_selection->get_selected_node_list().front()->get();
  438. }
  439. if (selected) {
  440. create_dialog->popup_create(false, true, selected->get_class());
  441. }
  442. } break;
  443. case TOOL_EXTEND_SCRIPT: {
  444. attach_script_to_selected(true);
  445. } break;
  446. case TOOL_ATTACH_SCRIPT: {
  447. attach_script_to_selected(false);
  448. } break;
  449. case TOOL_DETACH_SCRIPT: {
  450. if (!profile_allow_script_editing) {
  451. break;
  452. }
  453. Array selection = editor_selection->get_selected_nodes();
  454. if (selection.is_empty()) {
  455. return;
  456. }
  457. editor_data->get_undo_redo().create_action(TTR("Detach Script"));
  458. editor_data->get_undo_redo().add_do_method(EditorNode::get_singleton(), "push_item", (Script *)nullptr);
  459. for (int i = 0; i < selection.size(); i++) {
  460. Node *n = Object::cast_to<Node>(selection[i]);
  461. Ref<Script> existing = n->get_script();
  462. Ref<Script> empty = EditorNode::get_singleton()->get_object_custom_type_base(n);
  463. if (existing != empty) {
  464. editor_data->get_undo_redo().add_do_method(n, "set_script", empty);
  465. editor_data->get_undo_redo().add_undo_method(n, "set_script", existing);
  466. }
  467. }
  468. editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
  469. editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
  470. editor_data->get_undo_redo().commit_action();
  471. } break;
  472. case TOOL_MOVE_UP:
  473. case TOOL_MOVE_DOWN: {
  474. if (!profile_allow_editing) {
  475. break;
  476. }
  477. if (!scene_tree->get_selected()) {
  478. break;
  479. }
  480. if (scene_tree->get_selected() == edited_scene) {
  481. current_option = -1;
  482. accept->set_text(TTR("This operation can't be done on the tree root."));
  483. accept->popup_centered();
  484. break;
  485. }
  486. if (!_validate_no_foreign()) {
  487. break;
  488. }
  489. bool MOVING_DOWN = (p_tool == TOOL_MOVE_DOWN);
  490. bool MOVING_UP = !MOVING_DOWN;
  491. Node *common_parent = scene_tree->get_selected()->get_parent();
  492. List<Node *> selection = editor_selection->get_selected_node_list();
  493. selection.sort_custom<Node::Comparator>(); // sort by index
  494. if (MOVING_DOWN) {
  495. selection.reverse();
  496. }
  497. int lowest_id = common_parent->get_child_count() - 1;
  498. int highest_id = 0;
  499. for (Node *E : selection) {
  500. int index = E->get_index();
  501. if (index > highest_id) {
  502. highest_id = index;
  503. }
  504. if (index < lowest_id) {
  505. lowest_id = index;
  506. }
  507. if (E->get_parent() != common_parent) {
  508. common_parent = nullptr;
  509. }
  510. }
  511. if (!common_parent || (MOVING_DOWN && highest_id >= common_parent->get_child_count() - MOVING_DOWN) || (MOVING_UP && lowest_id == 0)) {
  512. break; // one or more nodes can not be moved
  513. }
  514. if (selection.size() == 1) {
  515. editor_data->get_undo_redo().create_action(TTR("Move Node In Parent"));
  516. }
  517. if (selection.size() > 1) {
  518. editor_data->get_undo_redo().create_action(TTR("Move Nodes In Parent"));
  519. }
  520. for (int i = 0; i < selection.size(); i++) {
  521. Node *top_node = selection[i];
  522. Node *bottom_node = selection[selection.size() - 1 - i];
  523. ERR_FAIL_COND(!top_node->get_parent());
  524. ERR_FAIL_COND(!bottom_node->get_parent());
  525. int bottom_node_pos = bottom_node->get_index();
  526. int top_node_pos_next = top_node->get_index() + (MOVING_DOWN ? 1 : -1);
  527. editor_data->get_undo_redo().add_do_method(top_node->get_parent(), "move_child", top_node, top_node_pos_next);
  528. editor_data->get_undo_redo().add_undo_method(bottom_node->get_parent(), "move_child", bottom_node, bottom_node_pos);
  529. }
  530. editor_data->get_undo_redo().commit_action();
  531. } break;
  532. case TOOL_DUPLICATE: {
  533. if (!profile_allow_editing) {
  534. break;
  535. }
  536. if (!edited_scene) {
  537. break;
  538. }
  539. if (editor_selection->is_selected(edited_scene)) {
  540. current_option = -1;
  541. accept->set_text(TTR("This operation can't be done on the tree root."));
  542. accept->popup_centered();
  543. break;
  544. }
  545. if (!_validate_no_foreign()) {
  546. break;
  547. }
  548. List<Node *> selection = editor_selection->get_selected_node_list();
  549. if (selection.size() == 0) {
  550. break;
  551. }
  552. editor_data->get_undo_redo().create_action(TTR("Duplicate Node(s)"));
  553. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  554. Node *dupsingle = nullptr;
  555. selection.sort_custom<Node::Comparator>();
  556. Node *add_below_node = selection.back()->get();
  557. for (Node *node : selection) {
  558. Node *parent = node->get_parent();
  559. List<Node *> owned;
  560. node->get_owned_by(node->get_owner(), &owned);
  561. HashMap<const Node *, Node *> duplimap;
  562. Node *dup = node->duplicate_from_editor(duplimap);
  563. ERR_CONTINUE(!dup);
  564. if (selection.size() == 1) {
  565. dupsingle = dup;
  566. }
  567. dup->set_name(parent->validate_child_name(dup));
  568. editor_data->get_undo_redo().add_do_method(add_below_node, "add_sibling", dup, true);
  569. for (Node *F : owned) {
  570. if (!duplimap.has(F)) {
  571. continue;
  572. }
  573. Node *d = duplimap[F];
  574. editor_data->get_undo_redo().add_do_method(d, "set_owner", node->get_owner());
  575. }
  576. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", dup);
  577. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", dup);
  578. editor_data->get_undo_redo().add_do_reference(dup);
  579. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  580. editor_data->get_undo_redo().add_do_method(ed, "live_debug_duplicate_node", edited_scene->get_path_to(node), dup->get_name());
  581. editor_data->get_undo_redo().add_undo_method(ed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)).plus_file(dup->get_name())));
  582. add_below_node = dup;
  583. }
  584. editor_data->get_undo_redo().commit_action();
  585. if (dupsingle) {
  586. _push_item(dupsingle);
  587. }
  588. } break;
  589. case TOOL_REPARENT: {
  590. if (!profile_allow_editing) {
  591. break;
  592. }
  593. if (!scene_tree->get_selected()) {
  594. break;
  595. }
  596. if (editor_selection->is_selected(edited_scene)) {
  597. current_option = -1;
  598. accept->set_text(TTR("This operation can't be done on the tree root."));
  599. accept->popup_centered();
  600. break;
  601. }
  602. if (!_validate_no_foreign()) {
  603. break;
  604. }
  605. List<Node *> nodes = editor_selection->get_selected_node_list();
  606. HashSet<Node *> nodeset;
  607. for (Node *E : nodes) {
  608. nodeset.insert(E);
  609. }
  610. reparent_dialog->set_current(nodeset);
  611. reparent_dialog->popup_centered_clamped(Size2(350, 700) * EDSCALE);
  612. } break;
  613. case TOOL_MAKE_ROOT: {
  614. if (!profile_allow_editing) {
  615. break;
  616. }
  617. List<Node *> nodes = editor_selection->get_selected_node_list();
  618. ERR_FAIL_COND(nodes.size() != 1);
  619. Node *node = nodes.front()->get();
  620. Node *root = get_tree()->get_edited_scene_root();
  621. if (node == root) {
  622. return;
  623. }
  624. //check that from node to root, all owners are right
  625. if (root->get_scene_inherited_state().is_valid()) {
  626. accept->set_text(TTR("Can't reparent nodes in inherited scenes, order of nodes can't change."));
  627. accept->popup_centered();
  628. return;
  629. }
  630. if (node->get_owner() != root) {
  631. accept->set_text(TTR("Node must belong to the edited scene to become root."));
  632. accept->popup_centered();
  633. return;
  634. }
  635. if (!node->get_scene_file_path().is_empty()) {
  636. accept->set_text(TTR("Instantiated scenes can't become root"));
  637. accept->popup_centered();
  638. return;
  639. }
  640. editor_data->get_undo_redo().create_action(TTR("Make node as Root"));
  641. editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
  642. editor_data->get_undo_redo().add_do_method(EditorNode::get_singleton(), "set_edited_scene", node);
  643. editor_data->get_undo_redo().add_do_method(node, "add_child", root, true);
  644. editor_data->get_undo_redo().add_do_method(node, "set_scene_file_path", root->get_scene_file_path());
  645. editor_data->get_undo_redo().add_do_method(root, "set_scene_file_path", String());
  646. editor_data->get_undo_redo().add_do_method(node, "set_owner", (Object *)nullptr);
  647. editor_data->get_undo_redo().add_do_method(root, "set_owner", node);
  648. _node_replace_owner(root, root, node, MODE_DO);
  649. editor_data->get_undo_redo().add_undo_method(root, "set_scene_file_path", root->get_scene_file_path());
  650. editor_data->get_undo_redo().add_undo_method(node, "set_scene_file_path", String());
  651. editor_data->get_undo_redo().add_undo_method(node, "remove_child", root);
  652. editor_data->get_undo_redo().add_undo_method(EditorNode::get_singleton(), "set_edited_scene", root);
  653. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node, true);
  654. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "move_child", node, node->get_index());
  655. editor_data->get_undo_redo().add_undo_method(root, "set_owner", (Object *)nullptr);
  656. editor_data->get_undo_redo().add_undo_method(node, "set_owner", root);
  657. _node_replace_owner(root, root, root, MODE_UNDO);
  658. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  659. editor_data->get_undo_redo().add_undo_method(scene_tree, "update_tree");
  660. editor_data->get_undo_redo().commit_action();
  661. } break;
  662. case TOOL_MULTI_EDIT: {
  663. if (!profile_allow_editing) {
  664. break;
  665. }
  666. Node *root = EditorNode::get_singleton()->get_edited_scene();
  667. if (!root) {
  668. break;
  669. }
  670. Ref<MultiNodeEdit> mne = memnew(MultiNodeEdit);
  671. for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
  672. mne->add_node(root->get_path_to(E.key));
  673. }
  674. _push_item(mne.ptr());
  675. } break;
  676. case TOOL_ERASE: {
  677. if (!profile_allow_editing) {
  678. break;
  679. }
  680. List<Node *> remove_list = editor_selection->get_selected_node_list();
  681. if (remove_list.is_empty()) {
  682. return;
  683. }
  684. if (!_validate_no_foreign()) {
  685. break;
  686. }
  687. if (p_confirm_override) {
  688. _delete_confirm();
  689. } else {
  690. String msg;
  691. if (remove_list.size() > 1) {
  692. bool any_children = false;
  693. for (int i = 0; !any_children && i < remove_list.size(); i++) {
  694. any_children = remove_list[i]->get_child_count() > 0;
  695. }
  696. msg = vformat(any_children ? TTR("Delete %d nodes and any children?") : TTR("Delete %d nodes?"), remove_list.size());
  697. } else {
  698. Node *node = remove_list[0];
  699. if (node == editor_data->get_edited_scene_root()) {
  700. msg = vformat(TTR("Delete the root node \"%s\"?"), node->get_name());
  701. } else if (node->get_scene_file_path().is_empty() && node->get_child_count() > 0) {
  702. // Display this message only for non-instantiated scenes
  703. msg = vformat(TTR("Delete node \"%s\" and its children?"), node->get_name());
  704. } else {
  705. msg = vformat(TTR("Delete node \"%s\"?"), node->get_name());
  706. }
  707. }
  708. delete_dialog->set_text(msg);
  709. // Resize the dialog to its minimum size.
  710. // This prevents the dialog from being too wide after displaying
  711. // a deletion confirmation for a node with a long name.
  712. delete_dialog->reset_size();
  713. delete_dialog->popup_centered();
  714. }
  715. } break;
  716. case TOOL_NEW_SCENE_FROM: {
  717. if (!profile_allow_editing) {
  718. break;
  719. }
  720. Node *scene = editor_data->get_edited_scene_root();
  721. if (!scene) {
  722. accept->set_text(TTR("Saving the branch as a scene requires having a scene open in the editor."));
  723. accept->popup_centered();
  724. break;
  725. }
  726. List<Node *> selection = editor_selection->get_selected_node_list();
  727. if (selection.size() != 1) {
  728. accept->set_text(vformat(TTR("Saving the branch as a scene requires selecting only one node, but you have selected %d nodes."), selection.size()));
  729. accept->popup_centered();
  730. break;
  731. }
  732. Node *tocopy = selection.front()->get();
  733. if (tocopy == scene) {
  734. accept->set_text(TTR("Can't save the root node branch as an instantiated scene.\nTo create an editable copy of the current scene, duplicate it using the FileSystem dock context menu\nor create an inherited scene using Scene > New Inherited Scene... instead."));
  735. accept->popup_centered();
  736. break;
  737. }
  738. if (tocopy != editor_data->get_edited_scene_root() && !tocopy->get_scene_file_path().is_empty()) {
  739. accept->set_text(TTR("Can't save the branch of an already instantiated scene.\nTo create a variation of a scene, you can make an inherited scene based on the instantiated scene using Scene > New Inherited Scene... instead."));
  740. accept->popup_centered();
  741. break;
  742. }
  743. if (tocopy->get_owner() != scene) {
  744. accept->set_text(TTR("Can't save a branch which is a child of an already instantiated scene.\nTo save this branch into its own scene, open the original scene, right click on this branch, and select \"Save Branch as Scene\"."));
  745. accept->popup_centered();
  746. break;
  747. }
  748. if (scene->get_scene_inherited_state().is_valid() && scene->get_scene_inherited_state()->find_node_by_path(scene->get_path_to(tocopy)) >= 0) {
  749. accept->set_text(TTR("Can't save a branch which is part of an inherited scene.\nTo save this branch into its own scene, open the original scene, right click on this branch, and select \"Save Branch as Scene\"."));
  750. accept->popup_centered();
  751. break;
  752. }
  753. new_scene_from_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  754. List<String> extensions;
  755. Ref<PackedScene> sd = memnew(PackedScene);
  756. ResourceSaver::get_recognized_extensions(sd, &extensions);
  757. new_scene_from_dialog->clear_filters();
  758. for (int i = 0; i < extensions.size(); i++) {
  759. new_scene_from_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  760. }
  761. String existing;
  762. if (extensions.size()) {
  763. String root_name(tocopy->get_name());
  764. existing = root_name + "." + extensions.front()->get().to_lower();
  765. }
  766. new_scene_from_dialog->set_current_path(existing);
  767. new_scene_from_dialog->set_title(TTR("Save New Scene As..."));
  768. new_scene_from_dialog->popup_file_dialog();
  769. } break;
  770. case TOOL_COPY_NODE_PATH: {
  771. List<Node *> selection = editor_selection->get_selected_node_list();
  772. List<Node *>::Element *e = selection.front();
  773. if (e) {
  774. Node *node = e->get();
  775. if (node) {
  776. Node *root = EditorNode::get_singleton()->get_edited_scene();
  777. NodePath path = root->get_path().rel_path_to(node->get_path());
  778. DisplayServer::get_singleton()->clipboard_set(path);
  779. }
  780. }
  781. } break;
  782. case TOOL_OPEN_DOCUMENTATION: {
  783. List<Node *> selection = editor_selection->get_selected_node_list();
  784. for (int i = 0; i < selection.size(); i++) {
  785. ScriptEditor::get_singleton()->goto_help("class_name:" + selection[i]->get_class());
  786. }
  787. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  788. } break;
  789. case TOOL_AUTO_EXPAND: {
  790. scene_tree->set_auto_expand_selected(!EditorSettings::get_singleton()->get("docks/scene_tree/auto_expand_to_selected"), true);
  791. } break;
  792. case TOOL_SCENE_EDITABLE_CHILDREN: {
  793. if (!profile_allow_editing) {
  794. break;
  795. }
  796. List<Node *> selection = editor_selection->get_selected_node_list();
  797. List<Node *>::Element *e = selection.front();
  798. if (e) {
  799. Node *node = e->get();
  800. if (node) {
  801. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node);
  802. if (editable) {
  803. editable_instance_remove_dialog->set_text(TTR("Disabling \"editable_instance\" will cause all properties of the node to be reverted to their default."));
  804. editable_instance_remove_dialog->popup_centered();
  805. break;
  806. }
  807. _toggle_editable_children(node);
  808. }
  809. }
  810. } break;
  811. case TOOL_SCENE_USE_PLACEHOLDER: {
  812. if (!profile_allow_editing) {
  813. break;
  814. }
  815. List<Node *> selection = editor_selection->get_selected_node_list();
  816. List<Node *>::Element *e = selection.front();
  817. if (e) {
  818. Node *node = e->get();
  819. if (node) {
  820. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node);
  821. bool placeholder = node->get_scene_instance_load_placeholder();
  822. // Fire confirmation dialog when children are editable.
  823. if (editable && !placeholder) {
  824. placeholder_editable_instance_remove_dialog->set_text(TTR("Enabling \"Load As Placeholder\" will disable \"Editable Children\" and cause all properties of the node to be reverted to their default."));
  825. placeholder_editable_instance_remove_dialog->popup_centered();
  826. break;
  827. }
  828. placeholder = !placeholder;
  829. if (placeholder) {
  830. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(node, false);
  831. }
  832. node->set_scene_instance_load_placeholder(placeholder);
  833. scene_tree->update_tree();
  834. }
  835. }
  836. } break;
  837. case TOOL_SCENE_MAKE_LOCAL: {
  838. if (!profile_allow_editing) {
  839. break;
  840. }
  841. List<Node *> selection = editor_selection->get_selected_node_list();
  842. List<Node *>::Element *e = selection.front();
  843. if (e) {
  844. Node *node = e->get();
  845. if (node) {
  846. Node *root = EditorNode::get_singleton()->get_edited_scene();
  847. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  848. if (!root) {
  849. break;
  850. }
  851. ERR_FAIL_COND(node->get_scene_file_path().is_empty());
  852. undo_redo->create_action(TTR("Make Local"));
  853. undo_redo->add_do_method(node, "set_scene_file_path", "");
  854. undo_redo->add_undo_method(node, "set_scene_file_path", node->get_scene_file_path());
  855. _node_replace_owner(node, node, root);
  856. undo_redo->add_do_method(scene_tree, "update_tree");
  857. undo_redo->add_undo_method(scene_tree, "update_tree");
  858. undo_redo->commit_action();
  859. }
  860. }
  861. } break;
  862. case TOOL_SCENE_OPEN: {
  863. List<Node *> selection = editor_selection->get_selected_node_list();
  864. List<Node *>::Element *e = selection.front();
  865. if (e) {
  866. Node *node = e->get();
  867. if (node) {
  868. scene_tree->emit_signal(SNAME("open"), node->get_scene_file_path());
  869. }
  870. }
  871. } break;
  872. case TOOL_SCENE_CLEAR_INHERITANCE: {
  873. if (!profile_allow_editing) {
  874. break;
  875. }
  876. clear_inherit_confirm->popup_centered();
  877. } break;
  878. case TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM: {
  879. if (!profile_allow_editing) {
  880. break;
  881. }
  882. List<Node *> selection = editor_selection->get_selected_node_list();
  883. List<Node *>::Element *e = selection.front();
  884. if (e) {
  885. Node *node = e->get();
  886. if (node) {
  887. node->set_scene_inherited_state(Ref<SceneState>());
  888. scene_tree->update_tree();
  889. InspectorDock::get_inspector_singleton()->update_tree();
  890. }
  891. }
  892. } break;
  893. case TOOL_SCENE_OPEN_INHERITED: {
  894. List<Node *> selection = editor_selection->get_selected_node_list();
  895. List<Node *>::Element *e = selection.front();
  896. if (e) {
  897. Node *node = e->get();
  898. if (node && node->get_scene_inherited_state().is_valid()) {
  899. scene_tree->emit_signal(SNAME("open"), node->get_scene_inherited_state()->get_path());
  900. }
  901. }
  902. } break;
  903. case TOOL_TOGGLE_SCENE_UNIQUE_NAME: {
  904. List<Node *> selection = editor_selection->get_selected_node_list();
  905. List<Node *>::Element *e = selection.front();
  906. if (e) {
  907. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  908. Node *node = e->get();
  909. bool enabled = node->is_unique_name_in_owner();
  910. if (!enabled && get_tree()->get_edited_scene_root()->get_node_or_null(UNIQUE_NODE_PREFIX + String(node->get_name())) != nullptr) {
  911. accept->set_text(TTR("Another node already uses this unique name in the scene."));
  912. accept->popup_centered();
  913. return;
  914. }
  915. if (!enabled) {
  916. undo_redo->create_action(TTR("Enable Scene Unique Name"));
  917. } else {
  918. undo_redo->create_action(TTR("Disable Scene Unique Name"));
  919. }
  920. undo_redo->add_do_method(node, "set_unique_name_in_owner", !enabled);
  921. undo_redo->add_undo_method(node, "set_unique_name_in_owner", enabled);
  922. undo_redo->commit_action();
  923. }
  924. } break;
  925. case TOOL_CREATE_2D_SCENE:
  926. case TOOL_CREATE_3D_SCENE:
  927. case TOOL_CREATE_USER_INTERFACE:
  928. case TOOL_CREATE_FAVORITE: {
  929. Node *new_node = nullptr;
  930. if (TOOL_CREATE_FAVORITE == p_tool) {
  931. String name = selected_favorite_root.get_slicec(' ', 0);
  932. if (ScriptServer::is_global_class(name)) {
  933. new_node = Object::cast_to<Node>(ClassDB::instantiate(ScriptServer::get_global_class_native_base(name)));
  934. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(name), "Script");
  935. if (new_node && script.is_valid()) {
  936. new_node->set_script(script);
  937. new_node->set_name(name);
  938. }
  939. } else {
  940. new_node = Object::cast_to<Node>(ClassDB::instantiate(selected_favorite_root));
  941. }
  942. if (!new_node) {
  943. new_node = memnew(Node);
  944. ERR_PRINT("Creating root from favorite '" + selected_favorite_root + "' failed. Creating 'Node' instead.");
  945. }
  946. } else {
  947. switch (p_tool) {
  948. case TOOL_CREATE_2D_SCENE:
  949. new_node = memnew(Node2D);
  950. break;
  951. case TOOL_CREATE_3D_SCENE:
  952. new_node = memnew(Node3D);
  953. break;
  954. case TOOL_CREATE_USER_INTERFACE: {
  955. Control *node = memnew(Control);
  956. node->set_anchors_and_offsets_preset(PRESET_WIDE); //more useful for resizable UIs.
  957. new_node = node;
  958. } break;
  959. }
  960. }
  961. add_root_node(new_node);
  962. EditorNode::get_singleton()->edit_node(new_node);
  963. editor_selection->clear();
  964. editor_selection->add_node(new_node);
  965. scene_tree->get_scene_tree()->grab_focus();
  966. } break;
  967. default: {
  968. if (p_tool >= EDIT_SUBRESOURCE_BASE) {
  969. int idx = p_tool - EDIT_SUBRESOURCE_BASE;
  970. ERR_FAIL_INDEX(idx, subresources.size());
  971. Object *obj = ObjectDB::get_instance(subresources[idx]);
  972. ERR_FAIL_COND(!obj);
  973. _push_item(obj);
  974. }
  975. }
  976. }
  977. }
  978. void SceneTreeDock::_property_selected(int p_idx) {
  979. ERR_FAIL_NULL(property_drop_node);
  980. _perform_property_drop(property_drop_node, menu_properties->get_item_metadata(p_idx), ResourceLoader::load(resource_drop_path));
  981. property_drop_node = nullptr;
  982. }
  983. void SceneTreeDock::_perform_property_drop(Node *p_node, String p_property, Ref<Resource> p_res) {
  984. editor_data->get_undo_redo().create_action(vformat(TTR("Set %s"), p_property));
  985. editor_data->get_undo_redo().add_do_property(p_node, p_property, p_res);
  986. editor_data->get_undo_redo().add_undo_property(p_node, p_property, p_node->get(p_property));
  987. editor_data->get_undo_redo().commit_action();
  988. }
  989. void SceneTreeDock::add_root_node(Node *p_node) {
  990. editor_data->get_undo_redo().create_action(TTR("New Scene Root"));
  991. editor_data->get_undo_redo().add_do_method(EditorNode::get_singleton(), "set_edited_scene", p_node);
  992. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  993. editor_data->get_undo_redo().add_do_reference(p_node);
  994. editor_data->get_undo_redo().add_undo_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);
  995. editor_data->get_undo_redo().commit_action();
  996. }
  997. void SceneTreeDock::_node_collapsed(Object *p_obj) {
  998. TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
  999. if (!ti) {
  1000. return;
  1001. }
  1002. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  1003. _set_collapsed_recursive(ti, ti->is_collapsed());
  1004. }
  1005. }
  1006. void SceneTreeDock::_notification(int p_what) {
  1007. switch (p_what) {
  1008. case NOTIFICATION_READY: {
  1009. if (!first_enter) {
  1010. break;
  1011. }
  1012. first_enter = false;
  1013. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &SceneTreeDock::_feature_profile_changed));
  1014. CanvasItemEditorPlugin *canvas_item_plugin = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  1015. if (canvas_item_plugin) {
  1016. canvas_item_plugin->get_canvas_item_editor()->connect("item_lock_status_changed", Callable(scene_tree, "_update_tree"));
  1017. canvas_item_plugin->get_canvas_item_editor()->connect("item_group_status_changed", Callable(scene_tree, "_update_tree"));
  1018. scene_tree->connect("node_changed", callable_mp((CanvasItem *)canvas_item_plugin->get_canvas_item_editor()->get_viewport_control(), &CanvasItem::update));
  1019. }
  1020. Node3DEditorPlugin *spatial_editor_plugin = Object::cast_to<Node3DEditorPlugin>(editor_data->get_editor("3D"));
  1021. spatial_editor_plugin->get_spatial_editor()->connect("item_lock_status_changed", Callable(scene_tree, "_update_tree"));
  1022. spatial_editor_plugin->get_spatial_editor()->connect("item_group_status_changed", Callable(scene_tree, "_update_tree"));
  1023. button_add->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  1024. button_instance->set_icon(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")));
  1025. button_create_script->set_icon(get_theme_icon(SNAME("ScriptCreate"), SNAME("EditorIcons")));
  1026. button_detach_script->set_icon(get_theme_icon(SNAME("ScriptRemove"), SNAME("EditorIcons")));
  1027. button_tree_menu->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  1028. filter->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  1029. filter->set_clear_button_enabled(true);
  1030. // create_root_dialog
  1031. HBoxContainer *top_row = memnew(HBoxContainer);
  1032. top_row->set_name("NodeShortcutsTopRow");
  1033. top_row->set_h_size_flags(SIZE_EXPAND_FILL);
  1034. Label *l = memnew(Label(TTR("Create Root Node:")));
  1035. l->set_theme_type_variation("HeaderSmall");
  1036. top_row->add_child(l);
  1037. top_row->add_spacer();
  1038. Button *node_shortcuts_toggle = memnew(Button);
  1039. node_shortcuts_toggle->set_flat(true);
  1040. node_shortcuts_toggle->set_name("NodeShortcutsToggle");
  1041. node_shortcuts_toggle->set_icon(get_theme_icon(SNAME("Favorites"), SNAME("EditorIcons")));
  1042. node_shortcuts_toggle->set_toggle_mode(true);
  1043. node_shortcuts_toggle->set_tooltip(TTR("Switch to Favorite Nodes"));
  1044. node_shortcuts_toggle->set_pressed(EDITOR_GET("_use_favorites_root_selection"));
  1045. node_shortcuts_toggle->set_anchors_and_offsets_preset(Control::PRESET_CENTER_RIGHT);
  1046. node_shortcuts_toggle->connect("pressed", callable_mp(this, &SceneTreeDock::_update_create_root_dialog));
  1047. top_row->add_child(node_shortcuts_toggle);
  1048. create_root_dialog->add_child(top_row);
  1049. ScrollContainer *scroll_container = memnew(ScrollContainer);
  1050. scroll_container->set_name("NodeShortcutsScrollContainer");
  1051. create_root_dialog->add_child(scroll_container);
  1052. scroll_container->set_v_size_flags(SIZE_EXPAND_FILL);
  1053. scroll_container->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1054. VBoxContainer *node_shortcuts = memnew(VBoxContainer);
  1055. node_shortcuts->set_name("NodeShortcuts");
  1056. scroll_container->add_child(node_shortcuts);
  1057. node_shortcuts->set_h_size_flags(SIZE_EXPAND_FILL);
  1058. VBoxContainer *beginner_node_shortcuts = memnew(VBoxContainer);
  1059. beginner_node_shortcuts->set_name("BeginnerNodeShortcuts");
  1060. node_shortcuts->add_child(beginner_node_shortcuts);
  1061. button_2d = memnew(Button);
  1062. beginner_node_shortcuts->add_child(button_2d);
  1063. button_2d->set_text(TTR("2D Scene"));
  1064. button_2d->set_icon(get_theme_icon(SNAME("Node2D"), SNAME("EditorIcons")));
  1065. button_2d->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(TOOL_CREATE_2D_SCENE, false));
  1066. button_3d = memnew(Button);
  1067. beginner_node_shortcuts->add_child(button_3d);
  1068. button_3d->set_text(TTR("3D Scene"));
  1069. button_3d->set_icon(get_theme_icon(SNAME("Node3D"), SNAME("EditorIcons")));
  1070. button_3d->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(TOOL_CREATE_3D_SCENE, false));
  1071. button_ui = memnew(Button);
  1072. beginner_node_shortcuts->add_child(button_ui);
  1073. button_ui->set_text(TTR("User Interface"));
  1074. button_ui->set_icon(get_theme_icon(SNAME("Control"), SNAME("EditorIcons")));
  1075. button_ui->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(TOOL_CREATE_USER_INTERFACE, false));
  1076. VBoxContainer *favorite_node_shortcuts = memnew(VBoxContainer);
  1077. favorite_node_shortcuts->set_name("FavoriteNodeShortcuts");
  1078. node_shortcuts->add_child(favorite_node_shortcuts);
  1079. button_custom = memnew(Button);
  1080. node_shortcuts->add_child(button_custom);
  1081. button_custom->set_text(TTR("Other Node"));
  1082. button_custom->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  1083. button_custom->connect("pressed", callable_bind(callable_mp(this, &SceneTreeDock::_tool_selected), TOOL_NEW, false));
  1084. button_clipboard = memnew(Button);
  1085. node_shortcuts->add_child(button_clipboard);
  1086. button_clipboard->set_text(TTR("Paste From Clipboard"));
  1087. button_clipboard->set_icon(get_theme_icon(SNAME("ActionPaste"), SNAME("EditorIcons")));
  1088. button_clipboard->connect("pressed", callable_bind(callable_mp(this, &SceneTreeDock::_tool_selected), TOOL_PASTE, false));
  1089. _update_create_root_dialog();
  1090. } break;
  1091. case NOTIFICATION_ENTER_TREE: {
  1092. clear_inherit_confirm->connect("confirmed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM, false));
  1093. scene_tree->set_auto_expand_selected(EditorSettings::get_singleton()->get("docks/scene_tree/auto_expand_to_selected"), false);
  1094. } break;
  1095. case NOTIFICATION_EXIT_TREE: {
  1096. clear_inherit_confirm->disconnect("confirmed", callable_mp(this, &SceneTreeDock::_tool_selected));
  1097. } break;
  1098. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1099. scene_tree->set_auto_expand_selected(EditorSettings::get_singleton()->get("docks/scene_tree/auto_expand_to_selected"), false);
  1100. button_add->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  1101. button_instance->set_icon(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")));
  1102. button_create_script->set_icon(get_theme_icon(SNAME("ScriptCreate"), SNAME("EditorIcons")));
  1103. button_detach_script->set_icon(get_theme_icon(SNAME("ScriptRemove"), SNAME("EditorIcons")));
  1104. button_tree_menu->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  1105. button_2d->set_icon(get_theme_icon(SNAME("Node2D"), SNAME("EditorIcons")));
  1106. button_3d->set_icon(get_theme_icon(SNAME("Node3D"), SNAME("EditorIcons")));
  1107. button_ui->set_icon(get_theme_icon(SNAME("Control"), SNAME("EditorIcons")));
  1108. button_custom->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  1109. button_clipboard->set_icon(get_theme_icon(SNAME("ActionPaste"), SNAME("EditorIcons")));
  1110. filter->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  1111. filter->set_clear_button_enabled(true);
  1112. } break;
  1113. case NOTIFICATION_PROCESS: {
  1114. bool show_create_root = bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) && get_tree()->get_edited_scene_root() == nullptr;
  1115. if (show_create_root != create_root_dialog->is_visible_in_tree() && !remote_tree->is_visible()) {
  1116. if (show_create_root) {
  1117. create_root_dialog->show();
  1118. scene_tree->hide();
  1119. } else {
  1120. create_root_dialog->hide();
  1121. scene_tree->show();
  1122. }
  1123. }
  1124. } break;
  1125. }
  1126. }
  1127. void SceneTreeDock::_node_replace_owner(Node *p_base, Node *p_node, Node *p_root, ReplaceOwnerMode p_mode) {
  1128. if (p_node->get_owner() == p_base && p_node != p_root) {
  1129. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  1130. switch (p_mode) {
  1131. case MODE_BIDI: {
  1132. bool is_unique = p_node->is_unique_name_in_owner() && p_base->get_node_or_null(UNIQUE_NODE_PREFIX + String(p_node->get_name())) != nullptr;
  1133. if (is_unique) {
  1134. // Will create a unique name conflict. Disable before setting owner.
  1135. undo_redo->add_do_method(p_node, "set_unique_name_in_owner", false);
  1136. }
  1137. undo_redo->add_do_method(p_node, "set_owner", p_root);
  1138. undo_redo->add_undo_method(p_node, "set_owner", p_base);
  1139. if (is_unique) {
  1140. // Will create a unique name conflict. Enable after setting owner.
  1141. undo_redo->add_undo_method(p_node, "set_unique_name_in_owner", true);
  1142. }
  1143. } break;
  1144. case MODE_DO: {
  1145. undo_redo->add_do_method(p_node, "set_owner", p_root);
  1146. } break;
  1147. case MODE_UNDO: {
  1148. undo_redo->add_undo_method(p_node, "set_owner", p_root);
  1149. } break;
  1150. }
  1151. }
  1152. for (int i = 0; i < p_node->get_child_count(); i++) {
  1153. _node_replace_owner(p_base, p_node->get_child(i), p_root, p_mode);
  1154. }
  1155. }
  1156. void SceneTreeDock::_load_request(const String &p_path) {
  1157. EditorNode::get_singleton()->open_request(p_path);
  1158. }
  1159. void SceneTreeDock::_script_open_request(const Ref<Script> &p_script) {
  1160. EditorNode::get_singleton()->edit_resource(p_script);
  1161. }
  1162. void SceneTreeDock::_push_item(Object *p_object) {
  1163. EditorNode::get_singleton()->push_item(p_object);
  1164. }
  1165. void SceneTreeDock::_handle_select(Node *p_node) {
  1166. if (tree_clicked) {
  1167. pending_click_select = p_node;
  1168. } else {
  1169. _push_item(p_node);
  1170. }
  1171. }
  1172. void SceneTreeDock::_node_selected() {
  1173. Node *node = scene_tree->get_selected();
  1174. if (!node) {
  1175. return;
  1176. }
  1177. _handle_select(node);
  1178. }
  1179. void SceneTreeDock::_node_renamed() {
  1180. _node_selected();
  1181. }
  1182. void SceneTreeDock::_set_owners(Node *p_owner, const Array &p_nodes) {
  1183. for (int i = 0; i < p_nodes.size(); i++) {
  1184. Node *n = Object::cast_to<Node>(p_nodes[i]);
  1185. if (!n) {
  1186. continue;
  1187. }
  1188. n->set_owner(p_owner);
  1189. }
  1190. }
  1191. void SceneTreeDock::_fill_path_renames(Vector<StringName> base_path, Vector<StringName> new_base_path, Node *p_node, HashMap<Node *, NodePath> *p_renames) {
  1192. base_path.push_back(p_node->get_name());
  1193. if (new_base_path.size()) {
  1194. new_base_path.push_back(p_node->get_name());
  1195. }
  1196. NodePath new_path;
  1197. if (new_base_path.size()) {
  1198. new_path = NodePath(new_base_path, true);
  1199. }
  1200. p_renames->insert(p_node, new_path);
  1201. for (int i = 0; i < p_node->get_child_count(); i++) {
  1202. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), p_renames);
  1203. }
  1204. }
  1205. void SceneTreeDock::fill_path_renames(Node *p_node, Node *p_new_parent, HashMap<Node *, NodePath> *p_renames) {
  1206. Vector<StringName> base_path;
  1207. Node *n = p_node->get_parent();
  1208. while (n) {
  1209. base_path.push_back(n->get_name());
  1210. n = n->get_parent();
  1211. }
  1212. base_path.reverse();
  1213. Vector<StringName> new_base_path;
  1214. if (p_new_parent) {
  1215. n = p_new_parent;
  1216. while (n) {
  1217. new_base_path.push_back(n->get_name());
  1218. n = n->get_parent();
  1219. }
  1220. new_base_path.reverse();
  1221. }
  1222. _fill_path_renames(base_path, new_base_path, p_node, p_renames);
  1223. }
  1224. bool SceneTreeDock::_update_node_path(Node *p_root_node, NodePath &r_node_path, HashMap<Node *, NodePath> *p_renames) const {
  1225. Node *target_node = p_root_node->get_node_or_null(r_node_path);
  1226. ERR_FAIL_NULL_V_MSG(target_node, false, "Found invalid node path '" + String(r_node_path) + "' on node '" + String(scene_root->get_path_to(p_root_node)) + "'");
  1227. // Try to find the target node in modified node paths.
  1228. HashMap<Node *, NodePath>::Iterator found_node_path = p_renames->find(target_node);
  1229. if (found_node_path) {
  1230. HashMap<Node *, NodePath>::Iterator found_root_path = p_renames->find(p_root_node);
  1231. NodePath root_path_new = found_root_path ? found_root_path->value : p_root_node->get_path();
  1232. r_node_path = root_path_new.rel_path_to(found_node_path->value);
  1233. return true;
  1234. }
  1235. // Update the path if the base node has changed and has not been deleted.
  1236. HashMap<Node *, NodePath>::Iterator found_root_path = p_renames->find(p_root_node);
  1237. if (found_root_path) {
  1238. NodePath root_path_new = found_root_path->value;
  1239. if (!root_path_new.is_empty()) {
  1240. NodePath old_abs_path = NodePath(String(p_root_node->get_path()).plus_file(r_node_path));
  1241. old_abs_path.simplify();
  1242. r_node_path = root_path_new.rel_path_to(old_abs_path);
  1243. }
  1244. return true;
  1245. }
  1246. return false;
  1247. }
  1248. bool SceneTreeDock::_check_node_path_recursive(Node *p_root_node, Variant &r_variant, HashMap<Node *, NodePath> *p_renames) const {
  1249. switch (r_variant.get_type()) {
  1250. case Variant::NODE_PATH: {
  1251. NodePath node_path = r_variant;
  1252. if (!node_path.is_empty() && _update_node_path(p_root_node, node_path, p_renames)) {
  1253. r_variant = node_path;
  1254. return true;
  1255. }
  1256. } break;
  1257. case Variant::ARRAY: {
  1258. Array a = r_variant;
  1259. bool updated = false;
  1260. for (int i = 0; i < a.size(); i++) {
  1261. Variant value = a[i];
  1262. if (_check_node_path_recursive(p_root_node, value, p_renames)) {
  1263. if (!updated) {
  1264. a = a.duplicate(); // Need to duplicate for undo-redo to work.
  1265. updated = true;
  1266. }
  1267. a[i] = value;
  1268. }
  1269. }
  1270. if (updated) {
  1271. r_variant = a;
  1272. return true;
  1273. }
  1274. } break;
  1275. case Variant::DICTIONARY: {
  1276. Dictionary d = r_variant;
  1277. bool updated = false;
  1278. for (int i = 0; i < d.size(); i++) {
  1279. Variant value = d.get_value_at_index(i);
  1280. if (_check_node_path_recursive(p_root_node, value, p_renames)) {
  1281. if (!updated) {
  1282. d = d.duplicate(); // Need to duplicate for undo-redo to work.
  1283. updated = true;
  1284. }
  1285. d[d.get_key_at_index(i)] = value;
  1286. }
  1287. }
  1288. if (updated) {
  1289. r_variant = d;
  1290. return true;
  1291. }
  1292. } break;
  1293. default: {
  1294. }
  1295. }
  1296. return false;
  1297. }
  1298. void SceneTreeDock::perform_node_renames(Node *p_base, HashMap<Node *, NodePath> *p_renames, HashMap<Ref<Animation>, HashSet<int>> *r_rem_anims) {
  1299. HashMap<Ref<Animation>, HashSet<int>> rem_anims;
  1300. if (!r_rem_anims) {
  1301. r_rem_anims = &rem_anims;
  1302. }
  1303. if (!p_base) {
  1304. p_base = edited_scene;
  1305. }
  1306. if (!p_base) {
  1307. return;
  1308. }
  1309. // No renaming if base node is deleted.
  1310. HashMap<Node *, NodePath>::Iterator found_base_path = p_renames->find(p_base);
  1311. if (found_base_path && found_base_path->value.is_empty()) {
  1312. return;
  1313. }
  1314. // Renaming node paths used in node properties.
  1315. List<PropertyInfo> properties;
  1316. p_base->get_property_list(&properties);
  1317. for (const PropertyInfo &E : properties) {
  1318. if (!(E.usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR))) {
  1319. continue;
  1320. }
  1321. String propertyname = E.name;
  1322. Variant old_variant = p_base->get(propertyname);
  1323. Variant updated_variant = old_variant;
  1324. if (_check_node_path_recursive(p_base, updated_variant, p_renames)) {
  1325. editor_data->get_undo_redo().add_do_property(p_base, propertyname, updated_variant);
  1326. editor_data->get_undo_redo().add_undo_property(p_base, propertyname, old_variant);
  1327. p_base->set(propertyname, updated_variant);
  1328. }
  1329. }
  1330. bool autorename_animation_tracks = bool(EDITOR_GET("editors/animation/autorename_animation_tracks"));
  1331. if (autorename_animation_tracks && Object::cast_to<AnimationPlayer>(p_base)) {
  1332. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_base);
  1333. List<StringName> anims;
  1334. ap->get_animation_list(&anims);
  1335. Node *root = ap->get_node(ap->get_root());
  1336. if (root) {
  1337. HashMap<Node *, NodePath>::Iterator found_root_path = p_renames->find(root);
  1338. NodePath new_root_path = found_root_path ? found_root_path->value : root->get_path();
  1339. if (!new_root_path.is_empty()) { // No renaming if root node is deleted.
  1340. for (const StringName &E : anims) {
  1341. Ref<Animation> anim = ap->get_animation(E);
  1342. if (!r_rem_anims->has(anim)) {
  1343. r_rem_anims->insert(anim, HashSet<int>());
  1344. HashSet<int> &ran = r_rem_anims->find(anim)->value;
  1345. for (int i = 0; i < anim->get_track_count(); i++) {
  1346. ran.insert(i);
  1347. }
  1348. }
  1349. HashSet<int> &ran = r_rem_anims->find(anim)->value;
  1350. if (anim.is_null()) {
  1351. continue;
  1352. }
  1353. for (int i = 0; i < anim->get_track_count(); i++) {
  1354. NodePath track_np = anim->track_get_path(i);
  1355. Node *n = root->get_node_or_null(track_np);
  1356. if (!n) {
  1357. continue;
  1358. }
  1359. if (!ran.has(i)) {
  1360. continue; //channel was removed
  1361. }
  1362. HashMap<Node *, NodePath>::Iterator found_path = p_renames->find(n);
  1363. if (found_path) {
  1364. if (found_path->value == NodePath()) {
  1365. //will be erased
  1366. int idx = 0;
  1367. HashSet<int>::Iterator EI = ran.begin();
  1368. ERR_FAIL_COND(!EI); //bug
  1369. while (*EI != i) {
  1370. idx++;
  1371. ++EI;
  1372. ERR_FAIL_COND(!EI); //another bug
  1373. }
  1374. editor_data->get_undo_redo().add_do_method(anim.ptr(), "remove_track", idx);
  1375. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "add_track", anim->track_get_type(i), idx);
  1376. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", idx, track_np);
  1377. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_interpolation_type", idx, anim->track_get_interpolation_type(i));
  1378. for (int j = 0; j < anim->track_get_key_count(i); j++) {
  1379. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_insert_key", idx, anim->track_get_key_time(i, j), anim->track_get_key_value(i, j), anim->track_get_key_transition(i, j));
  1380. }
  1381. ran.erase(i); //byebye channel
  1382. } else {
  1383. //will be renamed
  1384. NodePath rel_path = new_root_path.rel_path_to(found_path->value);
  1385. NodePath new_path = NodePath(rel_path.get_names(), track_np.get_subnames(), false);
  1386. if (new_path == track_np) {
  1387. continue; //bleh
  1388. }
  1389. editor_data->get_undo_redo().add_do_method(anim.ptr(), "track_set_path", i, new_path);
  1390. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", i, track_np);
  1391. }
  1392. }
  1393. }
  1394. }
  1395. }
  1396. }
  1397. }
  1398. for (int i = 0; i < p_base->get_child_count(); i++) {
  1399. perform_node_renames(p_base->get_child(i), p_renames, r_rem_anims);
  1400. }
  1401. }
  1402. void SceneTreeDock::_node_prerenamed(Node *p_node, const String &p_new_name) {
  1403. HashMap<Node *, NodePath> path_renames;
  1404. Vector<StringName> base_path;
  1405. Node *n = p_node->get_parent();
  1406. while (n) {
  1407. base_path.push_back(n->get_name());
  1408. n = n->get_parent();
  1409. }
  1410. base_path.reverse();
  1411. Vector<StringName> new_base_path = base_path;
  1412. base_path.push_back(p_node->get_name());
  1413. new_base_path.push_back(p_new_name);
  1414. NodePath new_path(new_base_path, true);
  1415. path_renames[p_node] = new_path;
  1416. for (int i = 0; i < p_node->get_child_count(); i++) {
  1417. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), &path_renames);
  1418. }
  1419. perform_node_renames(nullptr, &path_renames);
  1420. }
  1421. bool SceneTreeDock::_validate_no_foreign() {
  1422. List<Node *> selection = editor_selection->get_selected_node_list();
  1423. for (Node *E : selection) {
  1424. if (E != edited_scene && E->get_owner() != edited_scene) {
  1425. accept->set_text(TTR("Can't operate on nodes from a foreign scene!"));
  1426. accept->popup_centered();
  1427. return false;
  1428. }
  1429. // When edited_scene inherits from another one the root Node will be the parent Scene,
  1430. // we don't want to consider that Node a foreign one otherwise we would not be able to
  1431. // delete it.
  1432. if (edited_scene->get_scene_inherited_state().is_valid() && edited_scene == E) {
  1433. continue;
  1434. }
  1435. if (edited_scene->get_scene_inherited_state().is_valid() && edited_scene->get_scene_inherited_state()->find_node_by_path(edited_scene->get_path_to(E)) >= 0) {
  1436. accept->set_text(TTR("Can't operate on nodes the current scene inherits from!"));
  1437. accept->popup_centered();
  1438. return false;
  1439. }
  1440. }
  1441. return true;
  1442. }
  1443. bool SceneTreeDock::_validate_no_instance() {
  1444. List<Node *> selection = editor_selection->get_selected_node_list();
  1445. for (Node *E : selection) {
  1446. if (E != edited_scene && !E->get_scene_file_path().is_empty()) {
  1447. accept->set_text(TTR("This operation can't be done on instantiated scenes."));
  1448. accept->popup_centered();
  1449. return false;
  1450. }
  1451. }
  1452. return true;
  1453. }
  1454. void SceneTreeDock::_node_reparent(NodePath p_path, bool p_keep_global_xform) {
  1455. Node *new_parent = scene_root->get_node(p_path);
  1456. ERR_FAIL_COND(!new_parent);
  1457. List<Node *> selection = editor_selection->get_selected_node_list();
  1458. if (selection.is_empty()) {
  1459. return; // Nothing to reparent.
  1460. }
  1461. Vector<Node *> nodes;
  1462. for (Node *E : selection) {
  1463. nodes.push_back(E);
  1464. }
  1465. _do_reparent(new_parent, -1, nodes, p_keep_global_xform);
  1466. }
  1467. void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, Vector<Node *> p_nodes, bool p_keep_global_xform) {
  1468. Node *new_parent = p_new_parent;
  1469. ERR_FAIL_COND(!new_parent);
  1470. if (p_nodes.size() == 0) {
  1471. return; // Nothing to reparent.
  1472. }
  1473. p_nodes.sort_custom<Node::Comparator>(); //Makes result reliable.
  1474. bool no_change = true;
  1475. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1476. if (p_nodes[ni] == p_new_parent) {
  1477. return; // Attempt to reparent to itself.
  1478. }
  1479. if (p_nodes[ni]->get_parent() != p_new_parent || p_position_in_parent + ni != p_nodes[ni]->get_index()) {
  1480. no_change = false;
  1481. }
  1482. }
  1483. if (no_change) {
  1484. return; // Position and parent didn't change.
  1485. }
  1486. Node *validate = new_parent;
  1487. while (validate) {
  1488. ERR_FAIL_COND_MSG(p_nodes.has(validate), "Selection changed at some point. Can't reparent.");
  1489. validate = validate->get_parent();
  1490. }
  1491. // Sort by tree order, so re-adding is easy.
  1492. p_nodes.sort_custom<Node::Comparator>();
  1493. editor_data->get_undo_redo().create_action(TTR("Reparent Node"));
  1494. HashMap<Node *, NodePath> path_renames;
  1495. Vector<StringName> former_names;
  1496. int inc = 0;
  1497. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1498. // No undo implemented for this yet.
  1499. Node *node = p_nodes[ni];
  1500. fill_path_renames(node, new_parent, &path_renames);
  1501. former_names.push_back(node->get_name());
  1502. List<Node *> owned;
  1503. node->get_owned_by(node->get_owner(), &owned);
  1504. Array owners;
  1505. for (Node *E : owned) {
  1506. owners.push_back(E);
  1507. }
  1508. if (new_parent == node->get_parent() && node->get_index() < p_position_in_parent + ni) {
  1509. inc--; // If the child will generate a gap when moved, adjust.
  1510. }
  1511. editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
  1512. editor_data->get_undo_redo().add_do_method(new_parent, "add_child", node, true);
  1513. if (p_position_in_parent >= 0) {
  1514. editor_data->get_undo_redo().add_do_method(new_parent, "move_child", node, p_position_in_parent + inc);
  1515. }
  1516. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  1517. String old_name = former_names[ni];
  1518. String new_name = new_parent->validate_child_name(node);
  1519. // Name was modified, fix the path renames.
  1520. if (old_name.casecmp_to(new_name) != 0) {
  1521. // Fix the to name to have the new name.
  1522. HashMap<Node *, NodePath>::Iterator found_path = path_renames.find(node);
  1523. if (found_path) {
  1524. NodePath old_new_name = found_path->value;
  1525. Vector<StringName> unfixed_new_names = old_new_name.get_names();
  1526. Vector<StringName> fixed_new_names;
  1527. // Get last name and replace with fixed new name.
  1528. for (int a = 0; a < (unfixed_new_names.size() - 1); a++) {
  1529. fixed_new_names.push_back(unfixed_new_names[a]);
  1530. }
  1531. fixed_new_names.push_back(new_name);
  1532. NodePath fixed_node_path = NodePath(fixed_new_names, true);
  1533. path_renames[node] = fixed_node_path;
  1534. } else {
  1535. ERR_PRINT("Internal error. Can't find renamed path for node '" + node->get_path() + "'");
  1536. }
  1537. }
  1538. editor_data->get_undo_redo().add_do_method(ed, "live_debug_reparent_node", edited_scene->get_path_to(node), edited_scene->get_path_to(new_parent), new_name, p_position_in_parent + inc);
  1539. editor_data->get_undo_redo().add_undo_method(ed, "live_debug_reparent_node", NodePath(String(edited_scene->get_path_to(new_parent)).plus_file(new_name)), edited_scene->get_path_to(node->get_parent()), node->get_name(), node->get_index());
  1540. if (p_keep_global_xform) {
  1541. if (Object::cast_to<Node2D>(node)) {
  1542. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Node2D>(node)->get_global_transform());
  1543. }
  1544. if (Object::cast_to<Node3D>(node)) {
  1545. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Node3D>(node)->get_global_transform());
  1546. }
  1547. if (Object::cast_to<Control>(node)) {
  1548. editor_data->get_undo_redo().add_do_method(node, "set_global_position", Object::cast_to<Control>(node)->get_global_position());
  1549. }
  1550. }
  1551. editor_data->get_undo_redo().add_do_method(this, "_set_owners", edited_scene, owners);
  1552. if (AnimationPlayerEditor::get_singleton()->get_track_editor()->get_root() == node) {
  1553. editor_data->get_undo_redo().add_do_method(AnimationPlayerEditor::get_singleton()->get_track_editor(), "set_root", node);
  1554. }
  1555. editor_data->get_undo_redo().add_undo_method(new_parent, "remove_child", node);
  1556. editor_data->get_undo_redo().add_undo_method(node, "set_name", former_names[ni]);
  1557. inc++;
  1558. }
  1559. // Add and move in a second step (so old order is preserved).
  1560. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1561. Node *node = p_nodes[ni];
  1562. List<Node *> owned;
  1563. node->get_owned_by(node->get_owner(), &owned);
  1564. Array owners;
  1565. for (Node *E : owned) {
  1566. owners.push_back(E);
  1567. }
  1568. int child_pos = node->get_index();
  1569. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node, true);
  1570. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "move_child", node, child_pos);
  1571. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  1572. if (AnimationPlayerEditor::get_singleton()->get_track_editor()->get_root() == node) {
  1573. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::get_singleton()->get_track_editor(), "set_root", node);
  1574. }
  1575. if (p_keep_global_xform) {
  1576. if (Object::cast_to<Node2D>(node)) {
  1577. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Node2D>(node)->get_transform());
  1578. }
  1579. if (Object::cast_to<Node3D>(node)) {
  1580. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Node3D>(node)->get_transform());
  1581. }
  1582. if (Object::cast_to<Control>(node)) {
  1583. editor_data->get_undo_redo().add_undo_method(node, "set_position", Object::cast_to<Control>(node)->get_position());
  1584. }
  1585. }
  1586. }
  1587. perform_node_renames(nullptr, &path_renames);
  1588. editor_data->get_undo_redo().commit_action();
  1589. }
  1590. bool SceneTreeDock::_is_collapsed_recursive(TreeItem *p_item) const {
  1591. bool is_branch_collapsed = false;
  1592. List<TreeItem *> needs_check;
  1593. needs_check.push_back(p_item);
  1594. while (!needs_check.is_empty()) {
  1595. TreeItem *item = needs_check.back()->get();
  1596. needs_check.pop_back();
  1597. TreeItem *child = item->get_first_child();
  1598. is_branch_collapsed = item->is_collapsed() && child;
  1599. if (is_branch_collapsed) {
  1600. break;
  1601. }
  1602. while (child) {
  1603. needs_check.push_back(child);
  1604. child = child->get_next();
  1605. }
  1606. }
  1607. return is_branch_collapsed;
  1608. }
  1609. void SceneTreeDock::_set_collapsed_recursive(TreeItem *p_item, bool p_collapsed) {
  1610. List<TreeItem *> to_collapse;
  1611. to_collapse.push_back(p_item);
  1612. while (!to_collapse.is_empty()) {
  1613. TreeItem *item = to_collapse.back()->get();
  1614. to_collapse.pop_back();
  1615. item->set_collapsed(p_collapsed);
  1616. TreeItem *child = item->get_first_child();
  1617. while (child) {
  1618. to_collapse.push_back(child);
  1619. child = child->get_next();
  1620. }
  1621. }
  1622. }
  1623. void SceneTreeDock::_script_created(Ref<Script> p_script) {
  1624. List<Node *> selected = editor_selection->get_selected_node_list();
  1625. if (selected.is_empty()) {
  1626. return;
  1627. }
  1628. editor_data->get_undo_redo().create_action(TTR("Attach Script"));
  1629. for (Node *E : selected) {
  1630. Ref<Script> existing = E->get_script();
  1631. editor_data->get_undo_redo().add_do_method(E, "set_script", p_script);
  1632. editor_data->get_undo_redo().add_undo_method(E, "set_script", existing);
  1633. editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
  1634. editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
  1635. }
  1636. editor_data->get_undo_redo().commit_action();
  1637. _push_item(p_script.operator->());
  1638. _update_script_button();
  1639. }
  1640. void SceneTreeDock::_shader_created(Ref<Shader> p_shader) {
  1641. if (selected_shader_material.is_null()) {
  1642. return;
  1643. }
  1644. Ref<Shader> existing = selected_shader_material->get_shader();
  1645. editor_data->get_undo_redo().create_action(TTR("Set Shader"));
  1646. editor_data->get_undo_redo().add_do_method(selected_shader_material.ptr(), "set_shader", p_shader);
  1647. editor_data->get_undo_redo().add_undo_method(selected_shader_material.ptr(), "set_shader", existing);
  1648. editor_data->get_undo_redo().commit_action();
  1649. }
  1650. void SceneTreeDock::_script_creation_closed() {
  1651. script_create_dialog->disconnect("script_created", callable_mp(this, &SceneTreeDock::_script_created));
  1652. script_create_dialog->disconnect("confirmed", callable_mp(this, &SceneTreeDock::_script_creation_closed));
  1653. script_create_dialog->disconnect("cancelled", callable_mp(this, &SceneTreeDock::_script_creation_closed));
  1654. }
  1655. void SceneTreeDock::_shader_creation_closed() {
  1656. shader_create_dialog->disconnect("shader_created", callable_mp(this, &SceneTreeDock::_shader_created));
  1657. shader_create_dialog->disconnect("confirmed", callable_mp(this, &SceneTreeDock::_shader_creation_closed));
  1658. shader_create_dialog->disconnect("cancelled", callable_mp(this, &SceneTreeDock::_shader_creation_closed));
  1659. }
  1660. void SceneTreeDock::_toggle_editable_children_from_selection() {
  1661. List<Node *> selection = editor_selection->get_selected_node_list();
  1662. List<Node *>::Element *e = selection.front();
  1663. if (e) {
  1664. _toggle_editable_children(e->get());
  1665. }
  1666. }
  1667. void SceneTreeDock::_toggle_placeholder_from_selection() {
  1668. List<Node *> selection = editor_selection->get_selected_node_list();
  1669. List<Node *>::Element *e = selection.front();
  1670. if (e) {
  1671. Node *node = e->get();
  1672. if (node) {
  1673. _toggle_editable_children(node);
  1674. bool placeholder = node->get_scene_instance_load_placeholder();
  1675. placeholder = !placeholder;
  1676. node->set_scene_instance_load_placeholder(placeholder);
  1677. scene_tree->update_tree();
  1678. }
  1679. }
  1680. }
  1681. void SceneTreeDock::_toggle_editable_children(Node *p_node) {
  1682. if (p_node) {
  1683. bool editable = !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node);
  1684. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(p_node, editable);
  1685. if (editable) {
  1686. p_node->set_scene_instance_load_placeholder(false);
  1687. }
  1688. Node3DEditor::get_singleton()->update_all_gizmos(p_node);
  1689. scene_tree->update_tree();
  1690. }
  1691. }
  1692. void SceneTreeDock::_delete_confirm(bool p_cut) {
  1693. List<Node *> remove_list = editor_selection->get_selected_node_list();
  1694. if (remove_list.is_empty()) {
  1695. return;
  1696. }
  1697. EditorNode::get_singleton()->get_editor_plugins_over()->make_visible(false);
  1698. if (p_cut) {
  1699. editor_data->get_undo_redo().create_action(TTR("Cut Node(s)"));
  1700. } else {
  1701. editor_data->get_undo_redo().create_action(TTR("Remove Node(s)"));
  1702. }
  1703. bool entire_scene = false;
  1704. for (const Node *E : remove_list) {
  1705. if (E == edited_scene) {
  1706. entire_scene = true;
  1707. }
  1708. }
  1709. if (entire_scene) {
  1710. editor_data->get_undo_redo().add_do_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);
  1711. editor_data->get_undo_redo().add_undo_method(EditorNode::get_singleton(), "set_edited_scene", edited_scene);
  1712. editor_data->get_undo_redo().add_undo_method(edited_scene, "set_owner", edited_scene->get_owner());
  1713. editor_data->get_undo_redo().add_undo_method(scene_tree, "update_tree");
  1714. editor_data->get_undo_redo().add_undo_reference(edited_scene);
  1715. } else {
  1716. remove_list.sort_custom<Node::Comparator>(); //sort nodes to keep positions
  1717. HashMap<Node *, NodePath> path_renames;
  1718. //delete from animation
  1719. for (Node *n : remove_list) {
  1720. if (!n->is_inside_tree() || !n->get_parent()) {
  1721. continue;
  1722. }
  1723. fill_path_renames(n, nullptr, &path_renames);
  1724. }
  1725. perform_node_renames(nullptr, &path_renames);
  1726. //delete for read
  1727. for (Node *n : remove_list) {
  1728. if (!n->is_inside_tree() || !n->get_parent()) {
  1729. continue;
  1730. }
  1731. List<Node *> owned;
  1732. n->get_owned_by(n->get_owner(), &owned);
  1733. Array owners;
  1734. for (Node *F : owned) {
  1735. owners.push_back(F);
  1736. }
  1737. editor_data->get_undo_redo().add_do_method(n->get_parent(), "remove_child", n);
  1738. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "add_child", n, true);
  1739. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "move_child", n, n->get_index());
  1740. if (AnimationPlayerEditor::get_singleton()->get_track_editor()->get_root() == n) {
  1741. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::get_singleton()->get_track_editor(), "set_root", n);
  1742. }
  1743. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  1744. editor_data->get_undo_redo().add_undo_reference(n);
  1745. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  1746. editor_data->get_undo_redo().add_do_method(ed, "live_debug_remove_and_keep_node", edited_scene->get_path_to(n), n->get_instance_id());
  1747. editor_data->get_undo_redo().add_undo_method(ed, "live_debug_restore_node", n->get_instance_id(), edited_scene->get_path_to(n->get_parent()), n->get_index());
  1748. }
  1749. }
  1750. editor_data->get_undo_redo().commit_action();
  1751. // hack, force 2d editor viewport to refresh after deletion
  1752. if (CanvasItemEditor *editor = CanvasItemEditor::get_singleton()) {
  1753. editor->get_viewport_control()->update();
  1754. }
  1755. _push_item(nullptr);
  1756. // Fixes the EditorSelectionHistory from still offering deleted notes
  1757. EditorSelectionHistory *editor_history = EditorNode::get_singleton()->get_editor_selection_history();
  1758. editor_history->cleanup_history();
  1759. InspectorDock::get_singleton()->call("_prepare_history");
  1760. }
  1761. void SceneTreeDock::_update_script_button() {
  1762. if (!profile_allow_script_editing) {
  1763. button_create_script->hide();
  1764. button_detach_script->hide();
  1765. } else if (editor_selection->get_selection().size() == 0) {
  1766. button_create_script->hide();
  1767. button_detach_script->hide();
  1768. } else if (editor_selection->get_selection().size() == 1) {
  1769. Node *n = editor_selection->get_selected_node_list()[0];
  1770. if (n->get_script().is_null()) {
  1771. button_create_script->show();
  1772. button_detach_script->hide();
  1773. } else {
  1774. button_create_script->hide();
  1775. button_detach_script->show();
  1776. }
  1777. } else {
  1778. button_create_script->hide();
  1779. Array selection = editor_selection->get_selected_nodes();
  1780. for (int i = 0; i < selection.size(); i++) {
  1781. Node *n = Object::cast_to<Node>(selection[i]);
  1782. if (!n->get_script().is_null()) {
  1783. button_detach_script->show();
  1784. return;
  1785. }
  1786. }
  1787. button_detach_script->hide();
  1788. }
  1789. }
  1790. void SceneTreeDock::_selection_changed() {
  1791. int selection_size = editor_selection->get_selection().size();
  1792. if (selection_size > 1) {
  1793. //automatically turn on multi-edit
  1794. _tool_selected(TOOL_MULTI_EDIT);
  1795. } else if (selection_size == 1) {
  1796. _handle_select(editor_selection->get_selection().begin()->key);
  1797. } else if (selection_size == 0) {
  1798. _push_item(nullptr);
  1799. }
  1800. _update_script_button();
  1801. }
  1802. void SceneTreeDock::_do_create(Node *p_parent) {
  1803. Variant c = create_dialog->instance_selected();
  1804. ERR_FAIL_COND(!c);
  1805. Node *child = Object::cast_to<Node>(c);
  1806. ERR_FAIL_COND(!child);
  1807. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  1808. if (edited_scene) {
  1809. editor_data->get_undo_redo().add_do_method(p_parent, "add_child", child, true);
  1810. editor_data->get_undo_redo().add_do_method(child, "set_owner", edited_scene);
  1811. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  1812. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", child);
  1813. editor_data->get_undo_redo().add_do_reference(child);
  1814. editor_data->get_undo_redo().add_undo_method(p_parent, "remove_child", child);
  1815. String new_name = p_parent->validate_child_name(child);
  1816. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  1817. editor_data->get_undo_redo().add_do_method(ed, "live_debug_create_node", edited_scene->get_path_to(p_parent), child->get_class(), new_name);
  1818. editor_data->get_undo_redo().add_undo_method(ed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(p_parent)).plus_file(new_name)));
  1819. } else {
  1820. editor_data->get_undo_redo().add_do_method(EditorNode::get_singleton(), "set_edited_scene", child);
  1821. editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
  1822. editor_data->get_undo_redo().add_do_reference(child);
  1823. editor_data->get_undo_redo().add_undo_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);
  1824. }
  1825. editor_data->get_undo_redo().commit_action();
  1826. _push_item(c);
  1827. editor_selection->clear();
  1828. editor_selection->add_node(child);
  1829. if (Object::cast_to<Control>(c)) {
  1830. //make editor more comfortable, so some controls don't appear super shrunk
  1831. Control *ct = Object::cast_to<Control>(c);
  1832. Size2 ms = ct->get_minimum_size();
  1833. if (ms.width < 4) {
  1834. ms.width = 40;
  1835. }
  1836. if (ms.height < 4) {
  1837. ms.height = 40;
  1838. }
  1839. if (ct->is_layout_rtl()) {
  1840. ct->set_position(ct->get_position() - Vector2(ms.x, 0));
  1841. }
  1842. ct->set_size(ms);
  1843. }
  1844. emit_signal(SNAME("node_created"), c);
  1845. }
  1846. void SceneTreeDock::_create() {
  1847. if (current_option == TOOL_NEW) {
  1848. Node *parent = nullptr;
  1849. if (edited_scene) {
  1850. // If root exists in edited scene
  1851. parent = scene_tree->get_selected();
  1852. if (!parent) {
  1853. parent = edited_scene;
  1854. }
  1855. } else {
  1856. // If no root exist in edited scene
  1857. parent = scene_root;
  1858. ERR_FAIL_COND(!parent);
  1859. }
  1860. _do_create(parent);
  1861. } else if (current_option == TOOL_REPLACE) {
  1862. List<Node *> selection = editor_selection->get_selected_node_list();
  1863. ERR_FAIL_COND(selection.size() <= 0);
  1864. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  1865. ur->create_action(TTR("Change type of node(s)"));
  1866. for (Node *n : selection) {
  1867. ERR_FAIL_COND(!n);
  1868. Variant c = create_dialog->instance_selected();
  1869. ERR_FAIL_COND(!c);
  1870. Node *newnode = Object::cast_to<Node>(c);
  1871. ERR_FAIL_COND(!newnode);
  1872. ur->add_do_method(this, "replace_node", n, newnode, true, false);
  1873. ur->add_do_reference(newnode);
  1874. ur->add_undo_method(this, "replace_node", newnode, n, false, false);
  1875. ur->add_undo_reference(n);
  1876. }
  1877. ur->commit_action();
  1878. } else if (current_option == TOOL_REPARENT_TO_NEW_NODE) {
  1879. List<Node *> selection = editor_selection->get_selected_node_list();
  1880. ERR_FAIL_COND(selection.size() <= 0);
  1881. // Find top level node in selection
  1882. bool only_one_top_node = true;
  1883. Node *first = selection.front()->get();
  1884. ERR_FAIL_COND(!first);
  1885. int smaller_path_to_top = first->get_path_to(scene_root).get_name_count();
  1886. Node *top_node = first;
  1887. for (List<Node *>::Element *E = selection.front()->next(); E; E = E->next()) {
  1888. Node *n = E->get();
  1889. ERR_FAIL_COND(!n);
  1890. int path_length = n->get_path_to(scene_root).get_name_count();
  1891. if (top_node != n) {
  1892. if (smaller_path_to_top > path_length) {
  1893. top_node = n;
  1894. smaller_path_to_top = path_length;
  1895. only_one_top_node = true;
  1896. } else if (smaller_path_to_top == path_length) {
  1897. if (only_one_top_node && top_node->get_parent() != n->get_parent()) {
  1898. only_one_top_node = false;
  1899. }
  1900. }
  1901. }
  1902. }
  1903. Node *parent = nullptr;
  1904. if (only_one_top_node) {
  1905. parent = top_node->get_parent();
  1906. } else {
  1907. parent = top_node->get_parent()->get_parent();
  1908. }
  1909. _do_create(parent);
  1910. Vector<Node *> nodes;
  1911. for (Node *E : selection) {
  1912. nodes.push_back(E);
  1913. }
  1914. // This works because editor_selection was cleared and populated with last created node in _do_create()
  1915. Node *last_created = editor_selection->get_selected_node_list().front()->get();
  1916. _do_reparent(last_created, -1, nodes, true);
  1917. }
  1918. scene_tree->get_scene_tree()->grab_focus();
  1919. }
  1920. void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node, bool p_keep_properties, bool p_remove_old) {
  1921. Node *n = p_node;
  1922. Node *newnode = p_by_node;
  1923. if (p_keep_properties) {
  1924. Node *default_oldnode = Object::cast_to<Node>(ClassDB::instantiate(n->get_class()));
  1925. List<PropertyInfo> pinfo;
  1926. n->get_property_list(&pinfo);
  1927. for (const PropertyInfo &E : pinfo) {
  1928. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1929. continue;
  1930. }
  1931. if (default_oldnode->get(E.name) != n->get(E.name)) {
  1932. newnode->set(E.name, n->get(E.name));
  1933. }
  1934. }
  1935. memdelete(default_oldnode);
  1936. }
  1937. _push_item(nullptr);
  1938. //reconnect signals
  1939. List<MethodInfo> sl;
  1940. n->get_signal_list(&sl);
  1941. for (const MethodInfo &E : sl) {
  1942. List<Object::Connection> cl;
  1943. n->get_signal_connection_list(E.name, &cl);
  1944. for (const Object::Connection &c : cl) {
  1945. if (!(c.flags & Object::CONNECT_PERSIST)) {
  1946. continue;
  1947. }
  1948. newnode->connect(c.signal.get_name(), c.callable, c.binds, Object::CONNECT_PERSIST);
  1949. }
  1950. }
  1951. String newname = n->get_name();
  1952. List<Node *> to_erase;
  1953. for (int i = 0; i < n->get_child_count(); i++) {
  1954. if (n->get_child(i)->get_owner() == nullptr && n->is_owned_by_parent()) {
  1955. to_erase.push_back(n->get_child(i));
  1956. }
  1957. }
  1958. n->replace_by(newnode, true);
  1959. if (n == edited_scene) {
  1960. edited_scene = newnode;
  1961. EditorNode::get_singleton()->set_edited_scene(newnode);
  1962. }
  1963. //small hack to make collisionshapes and other kind of nodes to work
  1964. for (int i = 0; i < newnode->get_child_count(); i++) {
  1965. Node *c = newnode->get_child(i);
  1966. c->call("set_transform", c->call("get_transform"));
  1967. }
  1968. //p_remove_old was added to support undo
  1969. if (p_remove_old) {
  1970. editor_data->get_undo_redo().clear_history();
  1971. }
  1972. newnode->set_name(newname);
  1973. _push_item(newnode);
  1974. if (p_remove_old) {
  1975. memdelete(n);
  1976. while (to_erase.front()) {
  1977. memdelete(to_erase.front()->get());
  1978. to_erase.pop_front();
  1979. }
  1980. }
  1981. }
  1982. void SceneTreeDock::set_edited_scene(Node *p_scene) {
  1983. edited_scene = p_scene;
  1984. }
  1985. void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected) {
  1986. scene_tree->set_selected(p_node, p_emit_selected);
  1987. }
  1988. void SceneTreeDock::_new_scene_from(String p_file) {
  1989. List<Node *> selection = editor_selection->get_selected_node_list();
  1990. if (selection.size() != 1) {
  1991. accept->set_text(TTR("This operation requires a single selected node."));
  1992. accept->popup_centered();
  1993. return;
  1994. }
  1995. if (EditorNode::get_singleton()->is_scene_open(p_file)) {
  1996. accept->set_text(TTR("Can't overwrite scene that is still open!"));
  1997. accept->popup_centered();
  1998. return;
  1999. }
  2000. Node *base = selection.front()->get();
  2001. HashMap<const Node *, Node *> duplimap;
  2002. Node *copy = base->duplicate_from_editor(duplimap);
  2003. if (copy) {
  2004. for (int i = 0; i < copy->get_child_count(); i++) {
  2005. _set_node_owner_recursive(copy->get_child(i), copy);
  2006. }
  2007. Ref<PackedScene> sdata = memnew(PackedScene);
  2008. Error err = sdata->pack(copy);
  2009. memdelete(copy);
  2010. if (err != OK) {
  2011. accept->set_text(TTR("Couldn't save new scene. Likely dependencies (instances) couldn't be satisfied."));
  2012. accept->popup_centered();
  2013. return;
  2014. }
  2015. int flg = 0;
  2016. if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources")) {
  2017. flg |= ResourceSaver::FLAG_COMPRESS;
  2018. }
  2019. err = ResourceSaver::save(p_file, sdata, flg);
  2020. if (err != OK) {
  2021. accept->set_text(TTR("Error saving scene."));
  2022. accept->popup_centered();
  2023. return;
  2024. }
  2025. _replace_with_branch_scene(p_file, base);
  2026. } else {
  2027. accept->set_text(TTR("Error duplicating scene to save it."));
  2028. accept->popup_centered();
  2029. return;
  2030. }
  2031. }
  2032. void SceneTreeDock::_set_node_owner_recursive(Node *p_node, Node *p_owner) {
  2033. if (!p_node->get_owner()) {
  2034. p_node->set_owner(p_owner);
  2035. }
  2036. for (int i = 0; i < p_node->get_child_count(); i++) {
  2037. _set_node_owner_recursive(p_node->get_child(i), p_owner);
  2038. }
  2039. }
  2040. static bool _is_node_visible(Node *p_node) {
  2041. if (!p_node->get_owner()) {
  2042. return false;
  2043. }
  2044. if (p_node->get_owner() != EditorNode::get_singleton()->get_edited_scene() && !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node->get_owner())) {
  2045. return false;
  2046. }
  2047. return true;
  2048. }
  2049. static bool _has_visible_children(Node *p_node) {
  2050. bool collapsed = p_node->is_displayed_folded();
  2051. if (collapsed) {
  2052. return false;
  2053. }
  2054. for (int i = 0; i < p_node->get_child_count(); i++) {
  2055. Node *child = p_node->get_child(i);
  2056. if (!_is_node_visible(child)) {
  2057. continue;
  2058. }
  2059. return true;
  2060. }
  2061. return false;
  2062. }
  2063. void SceneTreeDock::_normalize_drop(Node *&to_node, int &to_pos, int p_type) {
  2064. to_pos = -1;
  2065. if (p_type == -1) {
  2066. //drop at above selected node
  2067. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  2068. to_node = nullptr;
  2069. ERR_FAIL_MSG("Cannot perform drop above the root node!");
  2070. }
  2071. to_pos = to_node->get_index();
  2072. to_node = to_node->get_parent();
  2073. } else if (p_type == 1) {
  2074. //drop at below selected node
  2075. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  2076. //if at lower sibling of root node
  2077. to_pos = 0; //just insert at beginning of root node
  2078. return;
  2079. }
  2080. Node *lower_sibling = nullptr;
  2081. if (_has_visible_children(to_node)) {
  2082. to_pos = 0;
  2083. } else {
  2084. for (int i = to_node->get_index() + 1; i < to_node->get_parent()->get_child_count(); i++) {
  2085. Node *c = to_node->get_parent()->get_child(i);
  2086. if (_is_node_visible(c)) {
  2087. lower_sibling = c;
  2088. break;
  2089. }
  2090. }
  2091. if (lower_sibling) {
  2092. to_pos = lower_sibling->get_index();
  2093. }
  2094. to_node = to_node->get_parent();
  2095. }
  2096. }
  2097. }
  2098. void SceneTreeDock::_files_dropped(Vector<String> p_files, NodePath p_to, int p_type) {
  2099. Node *node = get_node(p_to);
  2100. ERR_FAIL_COND(!node);
  2101. if (scene_tree->get_scene_tree()->get_drop_mode_flags() & Tree::DROP_MODE_INBETWEEN) {
  2102. // Dropped PackedScene, instance it.
  2103. int to_pos = -1;
  2104. _normalize_drop(node, to_pos, p_type);
  2105. _perform_instantiate_scenes(p_files, node, to_pos);
  2106. } else {
  2107. String res_path = p_files[0];
  2108. StringName res_type = EditorFileSystem::get_singleton()->get_file_type(res_path);
  2109. List<String> valid_properties;
  2110. List<PropertyInfo> pinfo;
  2111. node->get_property_list(&pinfo);
  2112. for (const PropertyInfo &p : pinfo) {
  2113. if (!(p.usage & PROPERTY_USAGE_EDITOR) || !(p.usage & PROPERTY_USAGE_STORAGE) || p.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  2114. continue;
  2115. }
  2116. Vector<String> valid_types = p.hint_string.split(",");
  2117. for (const String &prop_type : valid_types) {
  2118. if (res_type == prop_type || ClassDB::is_parent_class(res_type, prop_type) || EditorNode::get_editor_data().script_class_is_parent(res_type, prop_type)) {
  2119. valid_properties.push_back(p.name);
  2120. break;
  2121. }
  2122. }
  2123. }
  2124. if (valid_properties.size() > 1) {
  2125. property_drop_node = node;
  2126. resource_drop_path = res_path;
  2127. const EditorPropertyNameProcessor::Style style = InspectorDock::get_singleton()->get_property_name_style();
  2128. menu_properties->clear();
  2129. for (const String &p : valid_properties) {
  2130. menu_properties->add_item(EditorPropertyNameProcessor::get_singleton()->process_name(p, style));
  2131. menu_properties->set_item_metadata(-1, p);
  2132. }
  2133. menu_properties->reset_size();
  2134. menu_properties->set_position(get_screen_position() + get_local_mouse_position());
  2135. menu_properties->popup();
  2136. } else if (!valid_properties.is_empty()) {
  2137. _perform_property_drop(node, valid_properties[0], ResourceLoader::load(res_path));
  2138. }
  2139. }
  2140. }
  2141. void SceneTreeDock::_script_dropped(String p_file, NodePath p_to) {
  2142. Ref<Script> scr = ResourceLoader::load(p_file);
  2143. ERR_FAIL_COND(!scr.is_valid());
  2144. Node *n = get_node(p_to);
  2145. if (n) {
  2146. editor_data->get_undo_redo().create_action(TTR("Attach Script"));
  2147. editor_data->get_undo_redo().add_do_method(n, "set_script", scr);
  2148. editor_data->get_undo_redo().add_undo_method(n, "set_script", n->get_script());
  2149. editor_data->get_undo_redo().add_do_method(this, "_update_script_button");
  2150. editor_data->get_undo_redo().add_undo_method(this, "_update_script_button");
  2151. editor_data->get_undo_redo().commit_action();
  2152. }
  2153. }
  2154. void SceneTreeDock::_nodes_dragged(Array p_nodes, NodePath p_to, int p_type) {
  2155. if (!_validate_no_foreign()) {
  2156. return;
  2157. }
  2158. List<Node *> selection = editor_selection->get_selected_node_list();
  2159. if (selection.is_empty()) {
  2160. return; //nothing to reparent
  2161. }
  2162. Node *to_node = get_node(p_to);
  2163. if (!to_node) {
  2164. return;
  2165. }
  2166. Vector<Node *> nodes;
  2167. for (Node *E : selection) {
  2168. nodes.push_back(E);
  2169. }
  2170. int to_pos = -1;
  2171. _normalize_drop(to_node, to_pos, p_type);
  2172. _do_reparent(to_node, to_pos, nodes, !Input::get_singleton()->is_key_pressed(Key::SHIFT));
  2173. }
  2174. void SceneTreeDock::_add_children_to_popup(Object *p_obj, int p_depth) {
  2175. if (p_depth > 8) {
  2176. return;
  2177. }
  2178. List<PropertyInfo> pinfo;
  2179. p_obj->get_property_list(&pinfo);
  2180. for (const PropertyInfo &E : pinfo) {
  2181. if (!(E.usage & PROPERTY_USAGE_EDITOR)) {
  2182. continue;
  2183. }
  2184. if (E.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  2185. continue;
  2186. }
  2187. Variant value = p_obj->get(E.name);
  2188. if (value.get_type() != Variant::OBJECT) {
  2189. continue;
  2190. }
  2191. Object *obj = value;
  2192. if (!obj) {
  2193. continue;
  2194. }
  2195. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  2196. if (menu->get_item_count() == 0) {
  2197. menu->add_submenu_item(TTR("Sub-Resources"), "Sub-Resources");
  2198. }
  2199. int index = menu_subresources->get_item_count();
  2200. menu_subresources->add_icon_item(icon, E.name.capitalize(), EDIT_SUBRESOURCE_BASE + subresources.size());
  2201. menu_subresources->set_item_h_offset(index, p_depth * 10 * EDSCALE);
  2202. subresources.push_back(obj->get_instance_id());
  2203. _add_children_to_popup(obj, p_depth + 1);
  2204. }
  2205. }
  2206. void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
  2207. if (!EditorNode::get_singleton()->get_edited_scene()) {
  2208. menu->clear();
  2209. if (profile_allow_editing) {
  2210. menu->add_icon_shortcut(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  2211. menu->add_icon_shortcut(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANTIATE);
  2212. }
  2213. menu->reset_size();
  2214. menu->set_position(get_screen_position() + p_menu_pos);
  2215. menu->popup();
  2216. return;
  2217. }
  2218. List<Node *> selection = editor_selection->get_selected_node_list();
  2219. List<Node *> full_selection = editor_selection->get_full_selected_node_list(); // Above method only returns nodes with common parent.
  2220. if (selection.size() == 0) {
  2221. return;
  2222. }
  2223. menu->clear();
  2224. Ref<Script> existing_script;
  2225. bool existing_script_removable = true;
  2226. if (selection.size() == 1) {
  2227. Node *selected = selection[0];
  2228. if (profile_allow_editing) {
  2229. subresources.clear();
  2230. menu_subresources->clear();
  2231. menu_subresources->reset_size();
  2232. _add_children_to_popup(selection.front()->get(), 0);
  2233. if (menu->get_item_count() > 0) {
  2234. menu->add_separator();
  2235. }
  2236. menu->add_icon_shortcut(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  2237. menu->add_icon_shortcut(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANTIATE);
  2238. }
  2239. menu->add_icon_shortcut(get_theme_icon(SNAME("Collapse"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/expand_collapse_all"), TOOL_EXPAND_COLLAPSE);
  2240. menu->add_separator();
  2241. existing_script = selected->get_script();
  2242. if (EditorNode::get_singleton()->get_object_custom_type_base(selected) == existing_script) {
  2243. existing_script_removable = false;
  2244. }
  2245. }
  2246. if (profile_allow_editing) {
  2247. menu->add_icon_shortcut(get_theme_icon(SNAME("ActionCut"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/cut_node"), TOOL_CUT);
  2248. menu->add_icon_shortcut(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/copy_node"), TOOL_COPY);
  2249. if (selection.size() == 1 && !node_clipboard.is_empty()) {
  2250. menu->add_icon_shortcut(get_theme_icon(SNAME("ActionPaste"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/paste_node"), TOOL_PASTE);
  2251. }
  2252. menu->add_separator();
  2253. }
  2254. if (profile_allow_script_editing) {
  2255. bool add_separator = false;
  2256. if (full_selection.size() == 1) {
  2257. add_separator = true;
  2258. menu->add_icon_shortcut(get_theme_icon(SNAME("ScriptCreate"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/attach_script"), TOOL_ATTACH_SCRIPT);
  2259. if (existing_script.is_valid()) {
  2260. menu->add_icon_shortcut(get_theme_icon(SNAME("ScriptExtend"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/extend_script"), TOOL_EXTEND_SCRIPT);
  2261. }
  2262. }
  2263. if (existing_script.is_valid() && existing_script_removable) {
  2264. add_separator = true;
  2265. menu->add_icon_shortcut(get_theme_icon(SNAME("ScriptRemove"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
  2266. } else if (full_selection.size() > 1) {
  2267. bool script_exists = false;
  2268. for (Node *E : full_selection) {
  2269. if (!E->get_script().is_null()) {
  2270. script_exists = true;
  2271. break;
  2272. }
  2273. }
  2274. if (script_exists) {
  2275. add_separator = true;
  2276. menu->add_icon_shortcut(get_theme_icon(SNAME("ScriptRemove"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
  2277. }
  2278. }
  2279. if (add_separator && profile_allow_editing) {
  2280. menu->add_separator();
  2281. }
  2282. }
  2283. if (profile_allow_editing) {
  2284. bool add_separator = false;
  2285. if (full_selection.size() == 1) {
  2286. add_separator = true;
  2287. menu->add_icon_shortcut(get_theme_icon(SNAME("Rename"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/rename"), TOOL_RENAME);
  2288. }
  2289. bool can_replace = true;
  2290. for (Node *E : selection) {
  2291. if (E != edited_scene && (E->get_owner() != edited_scene || !E->get_scene_file_path().is_empty())) {
  2292. can_replace = false;
  2293. break;
  2294. }
  2295. }
  2296. if (can_replace) {
  2297. add_separator = true;
  2298. menu->add_icon_shortcut(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/change_node_type"), TOOL_REPLACE);
  2299. }
  2300. if (scene_tree->get_selected() != edited_scene) {
  2301. if (add_separator) {
  2302. menu->add_separator();
  2303. }
  2304. menu->add_icon_shortcut(get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/move_up"), TOOL_MOVE_UP);
  2305. menu->add_icon_shortcut(get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/move_down"), TOOL_MOVE_DOWN);
  2306. menu->add_icon_shortcut(get_theme_icon(SNAME("Duplicate"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/duplicate"), TOOL_DUPLICATE);
  2307. menu->add_icon_shortcut(get_theme_icon(SNAME("Reparent"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/reparent"), TOOL_REPARENT);
  2308. menu->add_icon_shortcut(get_theme_icon(SNAME("ReparentToNewNode"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/reparent_to_new_node"), TOOL_REPARENT_TO_NEW_NODE);
  2309. if (selection.size() == 1) {
  2310. menu->add_icon_shortcut(get_theme_icon(SNAME("NewRoot"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/make_root"), TOOL_MAKE_ROOT);
  2311. }
  2312. }
  2313. }
  2314. if (selection.size() == 1) {
  2315. if (profile_allow_editing) {
  2316. menu->add_separator();
  2317. menu->add_icon_shortcut(get_theme_icon(SNAME("CreateNewSceneFrom"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/save_branch_as_scene"), TOOL_NEW_SCENE_FROM);
  2318. }
  2319. if (full_selection.size() == 1) {
  2320. menu->add_separator();
  2321. menu->add_icon_shortcut(get_theme_icon(SNAME("CopyNodePath"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/copy_node_path"), TOOL_COPY_NODE_PATH);
  2322. }
  2323. if (selection[0]->get_owner() == EditorNode::get_singleton()->get_edited_scene()) {
  2324. // Only for nodes owned by the edited scene root.
  2325. menu->add_separator();
  2326. menu->add_icon_check_item(get_theme_icon(SNAME("SceneUniqueName"), SNAME("EditorIcons")), TTR("Access as Scene Unique Name"), TOOL_TOGGLE_SCENE_UNIQUE_NAME);
  2327. menu->set_item_checked(menu->get_item_index(TOOL_TOGGLE_SCENE_UNIQUE_NAME), selection[0]->is_unique_name_in_owner());
  2328. }
  2329. bool is_external = (!selection[0]->get_scene_file_path().is_empty());
  2330. if (is_external) {
  2331. bool is_inherited = selection[0]->get_scene_inherited_state() != nullptr;
  2332. bool is_top_level = selection[0]->get_owner() == nullptr;
  2333. if (is_inherited && is_top_level) {
  2334. menu->add_separator();
  2335. if (profile_allow_editing) {
  2336. menu->add_item(TTR("Clear Inheritance"), TOOL_SCENE_CLEAR_INHERITANCE);
  2337. }
  2338. menu->add_icon_item(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")), TTR("Open in Editor"), TOOL_SCENE_OPEN_INHERITED);
  2339. } else if (!is_top_level) {
  2340. menu->add_separator();
  2341. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(selection[0]);
  2342. bool placeholder = selection[0]->get_scene_instance_load_placeholder();
  2343. if (profile_allow_editing) {
  2344. menu->add_check_item(TTR("Editable Children"), TOOL_SCENE_EDITABLE_CHILDREN);
  2345. menu->add_check_item(TTR("Load As Placeholder"), TOOL_SCENE_USE_PLACEHOLDER);
  2346. menu->add_item(TTR("Make Local"), TOOL_SCENE_MAKE_LOCAL);
  2347. }
  2348. menu->add_icon_item(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")), TTR("Open in Editor"), TOOL_SCENE_OPEN);
  2349. if (profile_allow_editing) {
  2350. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Editable Children")), editable);
  2351. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Load As Placeholder")), placeholder);
  2352. }
  2353. }
  2354. }
  2355. }
  2356. #ifdef MODULE_REGEX_ENABLED
  2357. if (profile_allow_editing && selection.size() > 1) {
  2358. //this is not a commonly used action, it makes no sense for it to be where it was nor always present.
  2359. menu->add_separator();
  2360. menu->add_icon_shortcut(get_theme_icon(SNAME("Rename"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/batch_rename"), TOOL_BATCH_RENAME);
  2361. }
  2362. #endif // MODULE_REGEX_ENABLED
  2363. menu->add_separator();
  2364. menu->add_icon_item(get_theme_icon(SNAME("Help"), SNAME("EditorIcons")), TTR("Open Documentation"), TOOL_OPEN_DOCUMENTATION);
  2365. if (profile_allow_editing) {
  2366. menu->add_separator();
  2367. menu->add_icon_shortcut(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), ED_SHORTCUT("scene_tree/delete", TTR("Delete Node(s)"), Key::KEY_DELETE), TOOL_ERASE);
  2368. }
  2369. menu->reset_size();
  2370. menu->set_position(p_menu_pos);
  2371. menu->popup();
  2372. }
  2373. void SceneTreeDock::_update_tree_menu() {
  2374. PopupMenu *tree_menu = button_tree_menu->get_popup();
  2375. tree_menu->set_item_checked(tree_menu->get_item_idx_from_text(TTR("Auto Expand to Selected")), EditorSettings::get_singleton()->get("docks/scene_tree/auto_expand_to_selected"));
  2376. }
  2377. void SceneTreeDock::_filter_changed(const String &p_filter) {
  2378. scene_tree->set_filter(p_filter);
  2379. }
  2380. String SceneTreeDock::get_filter() {
  2381. return filter->get_text();
  2382. }
  2383. void SceneTreeDock::set_filter(const String &p_filter) {
  2384. filter->set_text(p_filter);
  2385. scene_tree->set_filter(p_filter);
  2386. }
  2387. void SceneTreeDock::save_branch_to_file(String p_directory) {
  2388. new_scene_from_dialog->set_current_dir(p_directory);
  2389. _tool_selected(TOOL_NEW_SCENE_FROM);
  2390. }
  2391. void SceneTreeDock::_focus_node() {
  2392. Node *node = scene_tree->get_selected();
  2393. ERR_FAIL_COND(!node);
  2394. if (node->is_class("CanvasItem")) {
  2395. CanvasItemEditorPlugin *editor = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  2396. editor->get_canvas_item_editor()->focus_selection();
  2397. } else {
  2398. Node3DEditorPlugin *editor = Object::cast_to<Node3DEditorPlugin>(editor_data->get_editor("3D"));
  2399. editor->get_spatial_editor()->get_editor_viewport(0)->focus_selection();
  2400. }
  2401. }
  2402. void SceneTreeDock::attach_script_to_selected(bool p_extend) {
  2403. if (ScriptServer::get_language_count() == 0) {
  2404. EditorNode::get_singleton()->show_warning(TTR("Cannot attach a script: there are no languages registered.\nThis is probably because this editor was built with all language modules disabled."));
  2405. return;
  2406. }
  2407. if (!profile_allow_script_editing) {
  2408. return;
  2409. }
  2410. List<Node *> selection = editor_selection->get_selected_node_list();
  2411. if (selection.is_empty()) {
  2412. return;
  2413. }
  2414. Node *selected = scene_tree->get_selected();
  2415. if (!selected) {
  2416. selected = selection.front()->get();
  2417. }
  2418. Ref<Script> existing = selected->get_script();
  2419. String path = selected->get_scene_file_path();
  2420. if (path.is_empty()) {
  2421. String root_path = editor_data->get_edited_scene_root()->get_scene_file_path();
  2422. if (root_path.is_empty()) {
  2423. path = String("res://").plus_file(selected->get_name());
  2424. } else {
  2425. path = root_path.get_base_dir().plus_file(selected->get_name());
  2426. }
  2427. }
  2428. String inherits = selected->get_class();
  2429. if (p_extend && existing.is_valid()) {
  2430. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2431. ScriptLanguage *l = ScriptServer::get_language(i);
  2432. if (l->get_type() == existing->get_class()) {
  2433. String name = l->get_global_class_name(existing->get_path());
  2434. if (ScriptServer::is_global_class(name) && EDITOR_GET("interface/editors/derive_script_globals_by_name").operator bool()) {
  2435. inherits = name;
  2436. } else if (l->can_inherit_from_file()) {
  2437. inherits = "\"" + existing->get_path() + "\"";
  2438. }
  2439. break;
  2440. }
  2441. }
  2442. }
  2443. script_create_dialog->connect("script_created", callable_mp(this, &SceneTreeDock::_script_created));
  2444. script_create_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_script_creation_closed));
  2445. script_create_dialog->connect("cancelled", callable_mp(this, &SceneTreeDock::_script_creation_closed));
  2446. script_create_dialog->set_inheritance_base_type("Node");
  2447. script_create_dialog->config(inherits, path);
  2448. script_create_dialog->popup_centered();
  2449. }
  2450. void SceneTreeDock::open_script_dialog(Node *p_for_node, bool p_extend) {
  2451. scene_tree->set_selected(p_for_node, false);
  2452. if (p_extend) {
  2453. _tool_selected(TOOL_EXTEND_SCRIPT);
  2454. } else {
  2455. _tool_selected(TOOL_ATTACH_SCRIPT);
  2456. }
  2457. }
  2458. void SceneTreeDock::attach_shader_to_selected(int p_preferred_mode) {
  2459. if (selected_shader_material.is_null()) {
  2460. return;
  2461. }
  2462. String path = selected_shader_material->get_path();
  2463. if (path.is_empty()) {
  2464. String root_path;
  2465. if (editor_data->get_edited_scene_root()) {
  2466. root_path = editor_data->get_edited_scene_root()->get_scene_file_path();
  2467. }
  2468. String shader_name;
  2469. if (selected_shader_material->get_name().is_empty()) {
  2470. shader_name = root_path.get_file();
  2471. } else {
  2472. shader_name = selected_shader_material->get_name();
  2473. }
  2474. if (root_path.is_empty()) {
  2475. path = String("res://").plus_file(shader_name);
  2476. } else {
  2477. path = root_path.get_base_dir().plus_file(shader_name);
  2478. }
  2479. }
  2480. shader_create_dialog->connect("shader_created", callable_mp(this, &SceneTreeDock::_shader_created));
  2481. shader_create_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_shader_creation_closed));
  2482. shader_create_dialog->connect("cancelled", callable_mp(this, &SceneTreeDock::_shader_creation_closed));
  2483. shader_create_dialog->config(path, true, true, -1, p_preferred_mode);
  2484. shader_create_dialog->popup_centered();
  2485. }
  2486. void SceneTreeDock::open_shader_dialog(const Ref<ShaderMaterial> &p_for_material, int p_preferred_mode) {
  2487. selected_shader_material = p_for_material;
  2488. attach_shader_to_selected(p_preferred_mode);
  2489. }
  2490. void SceneTreeDock::open_add_child_dialog() {
  2491. create_dialog->set_base_type("CanvasItem");
  2492. _tool_selected(TOOL_NEW, true);
  2493. reset_create_dialog = true;
  2494. }
  2495. void SceneTreeDock::open_instance_child_dialog() {
  2496. _tool_selected(TOOL_INSTANTIATE, true);
  2497. }
  2498. List<Node *> SceneTreeDock::paste_nodes() {
  2499. List<Node *> pasted_nodes;
  2500. if (node_clipboard.is_empty()) {
  2501. return pasted_nodes;
  2502. }
  2503. bool has_cycle = false;
  2504. if (edited_scene && !edited_scene->get_scene_file_path().is_empty()) {
  2505. for (Node *E : node_clipboard) {
  2506. if (edited_scene->get_scene_file_path() == E->get_scene_file_path()) {
  2507. has_cycle = true;
  2508. break;
  2509. }
  2510. }
  2511. }
  2512. if (has_cycle) {
  2513. current_option = -1;
  2514. accept->set_text(TTR("Can't paste root node into the same scene."));
  2515. accept->popup_centered();
  2516. return pasted_nodes;
  2517. }
  2518. Node *paste_parent = edited_scene;
  2519. List<Node *> selection = editor_selection->get_selected_node_list();
  2520. if (selection.size() > 0) {
  2521. paste_parent = selection.back()->get();
  2522. }
  2523. Node *owner = nullptr;
  2524. if (paste_parent) {
  2525. owner = paste_parent->get_owner();
  2526. }
  2527. if (!owner) {
  2528. owner = paste_parent;
  2529. }
  2530. UndoRedo &ur = editor_data->get_undo_redo();
  2531. ur.create_action(TTR("Paste Node(s)"));
  2532. ur.add_do_method(editor_selection, "clear");
  2533. HashMap<Ref<Resource>, Ref<Resource>> resource_remap;
  2534. String target_scene;
  2535. if (edited_scene) {
  2536. target_scene = edited_scene->get_scene_file_path();
  2537. }
  2538. if (target_scene != clipboard_source_scene) {
  2539. if (!clipboard_resource_remap.has(target_scene)) {
  2540. HashMap<Ref<Resource>, Ref<Resource>> remap;
  2541. for (Node *E : node_clipboard) {
  2542. _create_remap_for_node(E, remap);
  2543. }
  2544. clipboard_resource_remap[target_scene] = remap;
  2545. }
  2546. resource_remap = clipboard_resource_remap[target_scene];
  2547. }
  2548. for (Node *node : node_clipboard) {
  2549. HashMap<const Node *, Node *> duplimap;
  2550. Node *dup = node->duplicate_from_editor(duplimap, resource_remap);
  2551. ERR_CONTINUE(!dup);
  2552. pasted_nodes.push_back(dup);
  2553. if (!paste_parent) {
  2554. paste_parent = dup;
  2555. owner = dup;
  2556. ur.add_do_method(EditorNode::get_singleton(), "set_edited_scene", dup);
  2557. } else {
  2558. ur.add_do_method(paste_parent, "add_child", dup, true);
  2559. }
  2560. for (KeyValue<const Node *, Node *> &E2 : duplimap) {
  2561. Node *d = E2.value;
  2562. if (d != dup) {
  2563. ur.add_do_method(d, "set_owner", owner);
  2564. }
  2565. }
  2566. if (dup != owner) {
  2567. ur.add_do_method(dup, "set_owner", owner);
  2568. }
  2569. ur.add_do_method(editor_selection, "add_node", dup);
  2570. if (dup == paste_parent) {
  2571. ur.add_undo_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);
  2572. } else {
  2573. ur.add_undo_method(paste_parent, "remove_child", dup);
  2574. }
  2575. ur.add_do_reference(dup);
  2576. if (node_clipboard.size() == 1) {
  2577. ur.add_do_method(EditorNode::get_singleton(), "push_item", dup);
  2578. }
  2579. }
  2580. ur.commit_action();
  2581. return pasted_nodes;
  2582. }
  2583. List<Node *> SceneTreeDock::get_node_clipboard() const {
  2584. return node_clipboard;
  2585. }
  2586. void SceneTreeDock::add_remote_tree_editor(Control *p_remote) {
  2587. ERR_FAIL_COND(remote_tree != nullptr);
  2588. add_child(p_remote);
  2589. remote_tree = p_remote;
  2590. remote_tree->hide();
  2591. }
  2592. void SceneTreeDock::show_remote_tree() {
  2593. _remote_tree_selected();
  2594. }
  2595. void SceneTreeDock::hide_remote_tree() {
  2596. _local_tree_selected();
  2597. }
  2598. void SceneTreeDock::show_tab_buttons() {
  2599. button_hb->show();
  2600. }
  2601. void SceneTreeDock::hide_tab_buttons() {
  2602. button_hb->hide();
  2603. }
  2604. void SceneTreeDock::_remote_tree_selected() {
  2605. scene_tree->hide();
  2606. create_root_dialog->hide();
  2607. if (remote_tree) {
  2608. remote_tree->show();
  2609. }
  2610. edit_remote->set_pressed(true);
  2611. edit_local->set_pressed(false);
  2612. emit_signal(SNAME("remote_tree_selected"));
  2613. }
  2614. void SceneTreeDock::_local_tree_selected() {
  2615. if (!bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) || get_tree()->get_edited_scene_root() != nullptr) {
  2616. scene_tree->show();
  2617. }
  2618. if (remote_tree) {
  2619. remote_tree->hide();
  2620. }
  2621. edit_remote->set_pressed(false);
  2622. edit_local->set_pressed(true);
  2623. }
  2624. void SceneTreeDock::_update_create_root_dialog() {
  2625. BaseButton *toggle = Object::cast_to<BaseButton>(create_root_dialog->get_node(String("NodeShortcutsTopRow/NodeShortcutsToggle")));
  2626. Node *node_shortcuts = create_root_dialog->get_node(String("NodeShortcutsScrollContainer/NodeShortcuts"));
  2627. if (!toggle || !node_shortcuts) {
  2628. return;
  2629. }
  2630. Control *beginner_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("BeginnerNodeShortcuts")));
  2631. Control *favorite_nodes = Object::cast_to<Control>(node_shortcuts->get_node(String("FavoriteNodeShortcuts")));
  2632. if (!beginner_nodes || !favorite_nodes) {
  2633. return;
  2634. }
  2635. EditorSettings::get_singleton()->set_setting("_use_favorites_root_selection", toggle->is_pressed());
  2636. EditorSettings::get_singleton()->save();
  2637. if (toggle->is_pressed()) {
  2638. for (int i = 0; i < favorite_nodes->get_child_count(); i++) {
  2639. favorite_nodes->get_child(i)->queue_delete();
  2640. }
  2641. Ref<FileAccess> f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("favorites.Node"), FileAccess::READ);
  2642. if (f.is_valid()) {
  2643. while (!f->eof_reached()) {
  2644. String l = f->get_line().strip_edges();
  2645. if (!l.is_empty()) {
  2646. Button *button = memnew(Button);
  2647. favorite_nodes->add_child(button);
  2648. button->set_text(l);
  2649. button->set_clip_text(true);
  2650. String name = l.get_slicec(' ', 0);
  2651. if (ScriptServer::is_global_class(name)) {
  2652. name = ScriptServer::get_global_class_native_base(name);
  2653. }
  2654. button->set_icon(EditorNode::get_singleton()->get_class_icon(name));
  2655. button->connect("pressed", callable_mp(this, &SceneTreeDock::_favorite_root_selected), make_binds(l));
  2656. }
  2657. }
  2658. }
  2659. if (!favorite_nodes->is_visible_in_tree()) {
  2660. favorite_nodes->show();
  2661. beginner_nodes->hide();
  2662. }
  2663. } else {
  2664. if (!beginner_nodes->is_visible_in_tree()) {
  2665. beginner_nodes->show();
  2666. favorite_nodes->hide();
  2667. }
  2668. button_clipboard->set_visible(!node_clipboard.is_empty());
  2669. }
  2670. }
  2671. void SceneTreeDock::_favorite_root_selected(const String &p_class) {
  2672. selected_favorite_root = p_class;
  2673. _tool_selected(TOOL_CREATE_FAVORITE);
  2674. }
  2675. void SceneTreeDock::_feature_profile_changed() {
  2676. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  2677. if (profile.is_valid()) {
  2678. profile_allow_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCENE_TREE);
  2679. profile_allow_script_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT);
  2680. bool profile_allow_3d = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D);
  2681. button_3d->set_visible(profile_allow_3d);
  2682. button_add->set_visible(profile_allow_editing);
  2683. button_instance->set_visible(profile_allow_editing);
  2684. scene_tree->set_can_rename(profile_allow_editing);
  2685. } else {
  2686. button_3d->set_visible(true);
  2687. button_add->set_visible(true);
  2688. button_instance->set_visible(true);
  2689. scene_tree->set_can_rename(true);
  2690. profile_allow_editing = true;
  2691. profile_allow_script_editing = true;
  2692. }
  2693. _update_script_button();
  2694. }
  2695. void SceneTreeDock::_clear_clipboard() {
  2696. for (Node *E : node_clipboard) {
  2697. memdelete(E);
  2698. }
  2699. node_clipboard.clear();
  2700. clipboard_resource_remap.clear();
  2701. }
  2702. void SceneTreeDock::_create_remap_for_node(Node *p_node, HashMap<Ref<Resource>, Ref<Resource>> &r_remap) {
  2703. List<PropertyInfo> props;
  2704. p_node->get_property_list(&props);
  2705. Vector<SceneState::PackState> states_stack;
  2706. bool states_stack_ready = false;
  2707. for (const PropertyInfo &E : props) {
  2708. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  2709. continue;
  2710. }
  2711. Variant v = p_node->get(E.name);
  2712. if (v.is_ref_counted()) {
  2713. Ref<Resource> res = v;
  2714. if (res.is_valid()) {
  2715. if (!states_stack_ready) {
  2716. states_stack = PropertyUtils::get_node_states_stack(p_node);
  2717. states_stack_ready = true;
  2718. }
  2719. bool is_valid_default = false;
  2720. Variant orig = PropertyUtils::get_property_default_value(p_node, E.name, &is_valid_default, &states_stack);
  2721. if (is_valid_default && !PropertyUtils::is_property_value_different(v, orig)) {
  2722. continue;
  2723. }
  2724. if (res->is_built_in() && !r_remap.has(res)) {
  2725. _create_remap_for_resource(res, r_remap);
  2726. }
  2727. }
  2728. }
  2729. }
  2730. for (int i = 0; i < p_node->get_child_count(); i++) {
  2731. _create_remap_for_node(p_node->get_child(i), r_remap);
  2732. }
  2733. }
  2734. void SceneTreeDock::_create_remap_for_resource(Ref<Resource> p_resource, HashMap<Ref<Resource>, Ref<Resource>> &r_remap) {
  2735. r_remap[p_resource] = p_resource->duplicate();
  2736. List<PropertyInfo> props;
  2737. p_resource->get_property_list(&props);
  2738. for (const PropertyInfo &E : props) {
  2739. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  2740. continue;
  2741. }
  2742. Variant v = p_resource->get(E.name);
  2743. if (v.is_ref_counted()) {
  2744. Ref<Resource> res = v;
  2745. if (res.is_valid()) {
  2746. if (res->is_built_in() && !r_remap.has(res)) {
  2747. _create_remap_for_resource(res, r_remap);
  2748. }
  2749. }
  2750. }
  2751. }
  2752. }
  2753. void SceneTreeDock::_bind_methods() {
  2754. ClassDB::bind_method(D_METHOD("_set_owners"), &SceneTreeDock::_set_owners);
  2755. ClassDB::bind_method(D_METHOD("_update_script_button"), &SceneTreeDock::_update_script_button);
  2756. ClassDB::bind_method(D_METHOD("instantiate"), &SceneTreeDock::instantiate);
  2757. ClassDB::bind_method(D_METHOD("get_tree_editor"), &SceneTreeDock::get_tree_editor);
  2758. ClassDB::bind_method(D_METHOD("replace_node"), &SceneTreeDock::replace_node);
  2759. ADD_SIGNAL(MethodInfo("remote_tree_selected"));
  2760. ADD_SIGNAL(MethodInfo("add_node_used"));
  2761. ADD_SIGNAL(MethodInfo("node_created", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2762. }
  2763. SceneTreeDock *SceneTreeDock::singleton = nullptr;
  2764. void SceneTreeDock::_update_configuration_warning() {
  2765. if (singleton) {
  2766. MessageQueue::get_singleton()->push_callable(callable_mp(singleton->scene_tree, &SceneTreeEditor::update_warning));
  2767. }
  2768. }
  2769. SceneTreeDock::SceneTreeDock(Node *p_scene_root, EditorSelection *p_editor_selection, EditorData &p_editor_data) {
  2770. singleton = this;
  2771. set_name("Scene");
  2772. edited_scene = nullptr;
  2773. editor_data = &p_editor_data;
  2774. editor_selection = p_editor_selection;
  2775. scene_root = p_scene_root;
  2776. VBoxContainer *vbc = this;
  2777. HBoxContainer *filter_hbc = memnew(HBoxContainer);
  2778. filter_hbc->add_theme_constant_override("separate", 0);
  2779. ED_SHORTCUT("scene_tree/rename", TTR("Rename"), Key::F2);
  2780. ED_SHORTCUT_OVERRIDE("scene_tree/rename", "macos", Key::ENTER);
  2781. ED_SHORTCUT("scene_tree/batch_rename", TTR("Batch Rename"), KeyModifierMask::SHIFT | Key::F2);
  2782. ED_SHORTCUT_OVERRIDE("scene_tree/batch_rename", "macos", KeyModifierMask::SHIFT | Key::ENTER);
  2783. ED_SHORTCUT("scene_tree/add_child_node", TTR("Add Child Node"), KeyModifierMask::CMD | Key::A);
  2784. ED_SHORTCUT("scene_tree/instance_scene", TTR("Instantiate Child Scene"), KeyModifierMask::CMD | KeyModifierMask::SHIFT | Key::A);
  2785. ED_SHORTCUT("scene_tree/expand_collapse_all", TTR("Expand/Collapse Branch"));
  2786. ED_SHORTCUT("scene_tree/cut_node", TTR("Cut"), KeyModifierMask::CMD | Key::X);
  2787. ED_SHORTCUT("scene_tree/copy_node", TTR("Copy"), KeyModifierMask::CMD | Key::C);
  2788. ED_SHORTCUT("scene_tree/paste_node", TTR("Paste"), KeyModifierMask::CMD | Key::V);
  2789. ED_SHORTCUT("scene_tree/change_node_type", TTR("Change Type"));
  2790. ED_SHORTCUT("scene_tree/attach_script", TTR("Attach Script"));
  2791. ED_SHORTCUT("scene_tree/extend_script", TTR("Extend Script"));
  2792. ED_SHORTCUT("scene_tree/detach_script", TTR("Detach Script"));
  2793. ED_SHORTCUT("scene_tree/move_up", TTR("Move Up"), KeyModifierMask::CMD | Key::UP);
  2794. ED_SHORTCUT("scene_tree/move_down", TTR("Move Down"), KeyModifierMask::CMD | Key::DOWN);
  2795. ED_SHORTCUT("scene_tree/duplicate", TTR("Duplicate"), KeyModifierMask::CMD | Key::D);
  2796. ED_SHORTCUT("scene_tree/reparent", TTR("Reparent"));
  2797. ED_SHORTCUT("scene_tree/reparent_to_new_node", TTR("Reparent to New Node"));
  2798. ED_SHORTCUT("scene_tree/make_root", TTR("Make Scene Root"));
  2799. ED_SHORTCUT("scene_tree/save_branch_as_scene", TTR("Save Branch as Scene"));
  2800. ED_SHORTCUT("scene_tree/copy_node_path", TTR("Copy Node Path"), KeyModifierMask::CMD | KeyModifierMask::SHIFT | Key::C);
  2801. ED_SHORTCUT("scene_tree/delete_no_confirm", TTR("Delete (No Confirm)"), KeyModifierMask::SHIFT | Key::KEY_DELETE);
  2802. ED_SHORTCUT("scene_tree/delete", TTR("Delete"), Key::KEY_DELETE);
  2803. button_add = memnew(Button);
  2804. button_add->set_flat(true);
  2805. button_add->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(TOOL_NEW, false));
  2806. button_add->set_tooltip(TTR("Add/Create a New Node."));
  2807. button_add->set_shortcut(ED_GET_SHORTCUT("scene_tree/add_child_node"));
  2808. filter_hbc->add_child(button_add);
  2809. button_instance = memnew(Button);
  2810. button_instance->set_flat(true);
  2811. button_instance->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(TOOL_INSTANTIATE, false));
  2812. button_instance->set_tooltip(TTR("Instantiate a scene file as a Node. Creates an inherited scene if no root node exists."));
  2813. button_instance->set_shortcut(ED_GET_SHORTCUT("scene_tree/instance_scene"));
  2814. filter_hbc->add_child(button_instance);
  2815. vbc->add_child(filter_hbc);
  2816. filter = memnew(LineEdit);
  2817. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2818. filter->set_placeholder(TTR("Filter Nodes"));
  2819. filter_hbc->add_child(filter);
  2820. filter->add_theme_constant_override("minimum_character_width", 0);
  2821. filter->connect("text_changed", callable_mp(this, &SceneTreeDock::_filter_changed));
  2822. button_create_script = memnew(Button);
  2823. button_create_script->set_flat(true);
  2824. button_create_script->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(TOOL_ATTACH_SCRIPT, false));
  2825. button_create_script->set_tooltip(TTR("Attach a new or existing script to the selected node."));
  2826. button_create_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/attach_script"));
  2827. filter_hbc->add_child(button_create_script);
  2828. button_create_script->hide();
  2829. button_detach_script = memnew(Button);
  2830. button_detach_script->set_flat(true);
  2831. button_detach_script->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(TOOL_DETACH_SCRIPT, false));
  2832. button_detach_script->set_tooltip(TTR("Detach the script from the selected node."));
  2833. button_detach_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/detach_script"));
  2834. filter_hbc->add_child(button_detach_script);
  2835. button_detach_script->hide();
  2836. button_tree_menu = memnew(MenuButton);
  2837. button_tree_menu->set_flat(true);
  2838. button_tree_menu->connect("about_to_popup", callable_mp(this, &SceneTreeDock::_update_tree_menu));
  2839. filter_hbc->add_child(button_tree_menu);
  2840. PopupMenu *tree_menu = button_tree_menu->get_popup();
  2841. tree_menu->add_check_item(TTR("Auto Expand to Selected"), TOOL_AUTO_EXPAND);
  2842. tree_menu->connect("id_pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(false));
  2843. button_hb = memnew(HBoxContainer);
  2844. vbc->add_child(button_hb);
  2845. edit_remote = memnew(Button);
  2846. edit_remote->set_flat(true);
  2847. button_hb->add_child(edit_remote);
  2848. edit_remote->set_h_size_flags(SIZE_EXPAND_FILL);
  2849. edit_remote->set_text(TTR("Remote"));
  2850. edit_remote->set_toggle_mode(true);
  2851. edit_remote->set_tooltip(TTR("If selected, the Remote scene tree dock will cause the project to stutter every time it updates.\nSwitch back to the Local scene tree dock to improve performance."));
  2852. edit_remote->connect("pressed", callable_mp(this, &SceneTreeDock::_remote_tree_selected));
  2853. edit_local = memnew(Button);
  2854. edit_local->set_flat(true);
  2855. button_hb->add_child(edit_local);
  2856. edit_local->set_h_size_flags(SIZE_EXPAND_FILL);
  2857. edit_local->set_text(TTR("Local"));
  2858. edit_local->set_toggle_mode(true);
  2859. edit_local->set_pressed(true);
  2860. edit_local->connect("pressed", callable_mp(this, &SceneTreeDock::_local_tree_selected));
  2861. remote_tree = nullptr;
  2862. button_hb->hide();
  2863. create_root_dialog = memnew(VBoxContainer);
  2864. vbc->add_child(create_root_dialog);
  2865. create_root_dialog->set_v_size_flags(SIZE_EXPAND_FILL);
  2866. create_root_dialog->hide();
  2867. scene_tree = memnew(SceneTreeEditor(false, true, true));
  2868. vbc->add_child(scene_tree);
  2869. scene_tree->set_v_size_flags(SIZE_EXPAND | SIZE_FILL);
  2870. scene_tree->connect("rmb_pressed", callable_mp(this, &SceneTreeDock::_tree_rmb));
  2871. scene_tree->connect("node_selected", callable_mp(this, &SceneTreeDock::_node_selected), varray(), CONNECT_DEFERRED);
  2872. scene_tree->connect("node_renamed", callable_mp(this, &SceneTreeDock::_node_renamed), varray(), CONNECT_DEFERRED);
  2873. scene_tree->connect("node_prerename", callable_mp(this, &SceneTreeDock::_node_prerenamed));
  2874. scene_tree->connect("open", callable_mp(this, &SceneTreeDock::_load_request));
  2875. scene_tree->connect("open_script", callable_mp(this, &SceneTreeDock::_script_open_request));
  2876. scene_tree->connect("nodes_rearranged", callable_mp(this, &SceneTreeDock::_nodes_dragged));
  2877. scene_tree->connect("files_dropped", callable_mp(this, &SceneTreeDock::_files_dropped));
  2878. scene_tree->connect("script_dropped", callable_mp(this, &SceneTreeDock::_script_dropped));
  2879. scene_tree->connect("nodes_dragged", callable_mp(this, &SceneTreeDock::_nodes_drag_begin));
  2880. scene_tree->get_scene_tree()->connect("item_double_clicked", callable_mp(this, &SceneTreeDock::_focus_node));
  2881. scene_tree->get_scene_tree()->connect("item_collapsed", callable_mp(this, &SceneTreeDock::_node_collapsed));
  2882. editor_selection->connect("selection_changed", callable_mp(this, &SceneTreeDock::_selection_changed));
  2883. scene_tree->set_undo_redo(&editor_data->get_undo_redo());
  2884. scene_tree->set_editor_selection(editor_selection);
  2885. create_dialog = memnew(CreateDialog);
  2886. create_dialog->set_base_type("Node");
  2887. add_child(create_dialog);
  2888. create_dialog->connect("create", callable_mp(this, &SceneTreeDock::_create));
  2889. create_dialog->connect("favorites_updated", callable_mp(this, &SceneTreeDock::_update_create_root_dialog));
  2890. #ifdef MODULE_REGEX_ENABLED
  2891. rename_dialog = memnew(RenameDialog(scene_tree, &editor_data->get_undo_redo()));
  2892. add_child(rename_dialog);
  2893. #endif // MODULE_REGEX_ENABLED
  2894. script_create_dialog = memnew(ScriptCreateDialog);
  2895. script_create_dialog->set_inheritance_base_type("Node");
  2896. add_child(script_create_dialog);
  2897. shader_create_dialog = memnew(ShaderCreateDialog);
  2898. add_child(shader_create_dialog);
  2899. reparent_dialog = memnew(ReparentDialog);
  2900. add_child(reparent_dialog);
  2901. reparent_dialog->connect("reparent", callable_mp(this, &SceneTreeDock::_node_reparent));
  2902. accept = memnew(AcceptDialog);
  2903. add_child(accept);
  2904. quick_open = memnew(EditorQuickOpen);
  2905. add_child(quick_open);
  2906. quick_open->connect("quick_open", callable_mp(this, &SceneTreeDock::_quick_open));
  2907. set_process_shortcut_input(true);
  2908. delete_dialog = memnew(ConfirmationDialog);
  2909. add_child(delete_dialog);
  2910. delete_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_delete_confirm), varray(false));
  2911. editable_instance_remove_dialog = memnew(ConfirmationDialog);
  2912. add_child(editable_instance_remove_dialog);
  2913. editable_instance_remove_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_toggle_editable_children_from_selection));
  2914. placeholder_editable_instance_remove_dialog = memnew(ConfirmationDialog);
  2915. add_child(placeholder_editable_instance_remove_dialog);
  2916. placeholder_editable_instance_remove_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_toggle_placeholder_from_selection));
  2917. new_scene_from_dialog = memnew(EditorFileDialog);
  2918. new_scene_from_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  2919. add_child(new_scene_from_dialog);
  2920. new_scene_from_dialog->connect("file_selected", callable_mp(this, &SceneTreeDock::_new_scene_from));
  2921. menu = memnew(PopupMenu);
  2922. add_child(menu);
  2923. menu->connect("id_pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(false));
  2924. menu_subresources = memnew(PopupMenu);
  2925. menu_subresources->set_name("Sub-Resources");
  2926. menu_subresources->connect("id_pressed", callable_mp(this, &SceneTreeDock::_tool_selected), make_binds(false));
  2927. menu->add_child(menu_subresources);
  2928. menu_properties = memnew(PopupMenu);
  2929. add_child(menu_properties);
  2930. menu_properties->connect("id_pressed", callable_mp(this, &SceneTreeDock::_property_selected));
  2931. clear_inherit_confirm = memnew(ConfirmationDialog);
  2932. clear_inherit_confirm->set_text(TTR("Clear Inheritance? (No Undo!)"));
  2933. clear_inherit_confirm->get_ok_button()->set_text(TTR("Clear"));
  2934. add_child(clear_inherit_confirm);
  2935. set_process_input(true);
  2936. set_process(true);
  2937. EDITOR_DEF("interface/editors/show_scene_tree_root_selection", true);
  2938. EDITOR_DEF("interface/editors/derive_script_globals_by_name", true);
  2939. EDITOR_DEF("_use_favorites_root_selection", false);
  2940. Resource::_update_configuration_warning = _update_configuration_warning;
  2941. }
  2942. SceneTreeDock::~SceneTreeDock() {
  2943. singleton = nullptr;
  2944. if (!node_clipboard.is_empty()) {
  2945. _clear_clipboard();
  2946. }
  2947. }