scene_tree_dock.cpp 130 KB

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