scene_tree_dock.cpp 128 KB

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