scene_tree_dock.cpp 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894
  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. PopupMenu *filter_menu = filter->get_menu();
  1178. filter_menu->set_item_icon(filter_menu->get_item_idx_from_text(TTR("Filters")), get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  1179. filter_menu->set_item_icon(filter_menu->get_item_index(FILTER_BY_TYPE), get_theme_icon(SNAME("Node"), SNAME("EditorIcons")));
  1180. filter_menu->set_item_icon(filter_menu->get_item_index(FILTER_BY_GROUP), get_theme_icon(SNAME("Groups"), SNAME("EditorIcons")));
  1181. // These buttons are created on READY, because reasons...
  1182. if (button_2d) {
  1183. button_2d->set_icon(get_theme_icon(SNAME("Node2D"), SNAME("EditorIcons")));
  1184. }
  1185. if (button_3d) {
  1186. button_3d->set_icon(get_theme_icon(SNAME("Node3D"), SNAME("EditorIcons")));
  1187. }
  1188. if (button_ui) {
  1189. button_ui->set_icon(get_theme_icon(SNAME("Control"), SNAME("EditorIcons")));
  1190. }
  1191. if (button_custom) {
  1192. button_custom->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  1193. }
  1194. if (button_clipboard) {
  1195. button_clipboard->set_icon(get_theme_icon(SNAME("ActionPaste"), SNAME("EditorIcons")));
  1196. }
  1197. menu_subresources->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), SNAME("Editor")));
  1198. } break;
  1199. case NOTIFICATION_PROCESS: {
  1200. bool show_create_root = bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) && get_tree()->get_edited_scene_root() == nullptr;
  1201. if (show_create_root != create_root_dialog->is_visible_in_tree() && !remote_tree->is_visible()) {
  1202. if (show_create_root) {
  1203. create_root_dialog->show();
  1204. scene_tree->hide();
  1205. } else {
  1206. create_root_dialog->hide();
  1207. scene_tree->show();
  1208. }
  1209. }
  1210. } break;
  1211. }
  1212. }
  1213. void SceneTreeDock::_node_replace_owner(Node *p_base, Node *p_node, Node *p_root, ReplaceOwnerMode p_mode) {
  1214. if (p_node->get_owner() == p_base && p_node != p_root) {
  1215. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1216. switch (p_mode) {
  1217. case MODE_BIDI: {
  1218. 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;
  1219. if (disable_unique) {
  1220. // Will create a unique name conflict. Disable before setting owner.
  1221. undo_redo->add_do_method(p_node, "set_unique_name_in_owner", false);
  1222. }
  1223. undo_redo->add_do_method(p_node, "set_owner", p_root);
  1224. undo_redo->add_undo_method(p_node, "set_owner", p_base);
  1225. if (disable_unique) {
  1226. // Will create a unique name conflict. Enable after setting owner.
  1227. undo_redo->add_undo_method(p_node, "set_unique_name_in_owner", true);
  1228. }
  1229. } break;
  1230. case MODE_DO: {
  1231. undo_redo->add_do_method(p_node, "set_owner", p_root);
  1232. } break;
  1233. case MODE_UNDO: {
  1234. undo_redo->add_undo_method(p_node, "set_owner", p_root);
  1235. } break;
  1236. }
  1237. }
  1238. for (int i = 0; i < p_node->get_child_count(); i++) {
  1239. _node_replace_owner(p_base, p_node->get_child(i), p_root, p_mode);
  1240. }
  1241. }
  1242. void SceneTreeDock::_load_request(const String &p_path) {
  1243. EditorNode::get_singleton()->open_request(p_path);
  1244. _local_tree_selected();
  1245. }
  1246. void SceneTreeDock::_script_open_request(const Ref<Script> &p_script) {
  1247. EditorNode::get_singleton()->edit_resource(p_script);
  1248. }
  1249. void SceneTreeDock::_push_item(Object *p_object) {
  1250. Node *node = Object::cast_to<Node>(p_object);
  1251. if (node || !p_object) {
  1252. // Assume that null object is a Node.
  1253. EditorNode::get_singleton()->push_node_item(node);
  1254. } else {
  1255. EditorNode::get_singleton()->push_item(p_object);
  1256. }
  1257. if (p_object == nullptr) {
  1258. EditorNode::get_singleton()->hide_unused_editors(this);
  1259. }
  1260. }
  1261. void SceneTreeDock::_handle_select(Node *p_node) {
  1262. if (tree_clicked) {
  1263. pending_click_select = p_node;
  1264. } else {
  1265. _push_item(p_node);
  1266. }
  1267. }
  1268. void SceneTreeDock::_node_selected() {
  1269. Node *node = scene_tree->get_selected();
  1270. if (!node) {
  1271. return;
  1272. }
  1273. _handle_select(node);
  1274. }
  1275. void SceneTreeDock::_node_renamed() {
  1276. _node_selected();
  1277. }
  1278. void SceneTreeDock::_set_owners(Node *p_owner, const Array &p_nodes) {
  1279. for (int i = 0; i < p_nodes.size(); i++) {
  1280. Node *n = Object::cast_to<Node>(p_nodes[i]);
  1281. if (!n) {
  1282. continue;
  1283. }
  1284. n->set_owner(p_owner);
  1285. }
  1286. }
  1287. void SceneTreeDock::_fill_path_renames(Vector<StringName> base_path, Vector<StringName> new_base_path, Node *p_node, HashMap<Node *, NodePath> *p_renames) {
  1288. base_path.push_back(p_node->get_name());
  1289. NodePath new_path;
  1290. if (!new_base_path.is_empty()) {
  1291. new_base_path.push_back(p_node->get_name());
  1292. new_path = NodePath(new_base_path, true);
  1293. }
  1294. p_renames->insert(p_node, new_path);
  1295. for (int i = 0; i < p_node->get_child_count(); i++) {
  1296. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), p_renames);
  1297. }
  1298. }
  1299. bool SceneTreeDock::_has_tracks_to_delete(Node *p_node, List<Node *> &p_to_delete) const {
  1300. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  1301. if (ap) {
  1302. Node *root = ap->get_node(ap->get_root());
  1303. if (root && !p_to_delete.find(root)) {
  1304. List<StringName> anims;
  1305. ap->get_animation_list(&anims);
  1306. for (const StringName &E : anims) {
  1307. Ref<Animation> anim = ap->get_animation(E);
  1308. if (anim.is_null()) {
  1309. continue;
  1310. }
  1311. for (int i = 0; i < anim->get_track_count(); i++) {
  1312. NodePath track_np = anim->track_get_path(i);
  1313. Node *n = root->get_node_or_null(track_np);
  1314. if (n) {
  1315. for (const Node *F : p_to_delete) {
  1316. if (F == n || F->is_ancestor_of(n)) {
  1317. return true;
  1318. }
  1319. }
  1320. }
  1321. }
  1322. }
  1323. }
  1324. }
  1325. for (int i = 0; i < p_node->get_child_count(); i++) {
  1326. if (_has_tracks_to_delete(p_node->get_child(i), p_to_delete)) {
  1327. return true;
  1328. }
  1329. }
  1330. return false;
  1331. }
  1332. void SceneTreeDock::fill_path_renames(Node *p_node, Node *p_new_parent, HashMap<Node *, NodePath> *p_renames) {
  1333. Vector<StringName> base_path;
  1334. Node *n = p_node->get_parent();
  1335. while (n) {
  1336. base_path.push_back(n->get_name());
  1337. n = n->get_parent();
  1338. }
  1339. base_path.reverse();
  1340. Vector<StringName> new_base_path;
  1341. if (p_new_parent) {
  1342. n = p_new_parent;
  1343. while (n) {
  1344. new_base_path.push_back(n->get_name());
  1345. n = n->get_parent();
  1346. }
  1347. new_base_path.reverse();
  1348. }
  1349. _fill_path_renames(base_path, new_base_path, p_node, p_renames);
  1350. }
  1351. bool SceneTreeDock::_update_node_path(Node *p_root_node, NodePath &r_node_path, HashMap<Node *, NodePath> *p_renames) const {
  1352. Node *target_node = p_root_node->get_node_or_null(r_node_path);
  1353. 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)) + "'");
  1354. // Try to find the target node in modified node paths.
  1355. HashMap<Node *, NodePath>::Iterator found_node_path = p_renames->find(target_node);
  1356. if (found_node_path) {
  1357. HashMap<Node *, NodePath>::Iterator found_root_path = p_renames->find(p_root_node);
  1358. NodePath root_path_new = found_root_path ? found_root_path->value : p_root_node->get_path();
  1359. r_node_path = root_path_new.rel_path_to(found_node_path->value);
  1360. return true;
  1361. }
  1362. // Update the path if the base node has changed and has not been deleted.
  1363. HashMap<Node *, NodePath>::Iterator found_root_path = p_renames->find(p_root_node);
  1364. if (found_root_path) {
  1365. NodePath root_path_new = found_root_path->value;
  1366. if (!root_path_new.is_empty()) {
  1367. NodePath old_abs_path = NodePath(String(p_root_node->get_path()).path_join(r_node_path));
  1368. old_abs_path.simplify();
  1369. r_node_path = root_path_new.rel_path_to(old_abs_path);
  1370. }
  1371. return true;
  1372. }
  1373. return false;
  1374. }
  1375. bool SceneTreeDock::_check_node_path_recursive(Node *p_root_node, Variant &r_variant, HashMap<Node *, NodePath> *p_renames) const {
  1376. switch (r_variant.get_type()) {
  1377. case Variant::NODE_PATH: {
  1378. NodePath node_path = r_variant;
  1379. if (!node_path.is_empty() && _update_node_path(p_root_node, node_path, p_renames)) {
  1380. r_variant = node_path;
  1381. return true;
  1382. }
  1383. } break;
  1384. case Variant::ARRAY: {
  1385. Array a = r_variant;
  1386. bool updated = false;
  1387. for (int i = 0; i < a.size(); i++) {
  1388. Variant value = a[i];
  1389. if (_check_node_path_recursive(p_root_node, value, p_renames)) {
  1390. if (!updated) {
  1391. a = a.duplicate(); // Need to duplicate for undo-redo to work.
  1392. updated = true;
  1393. }
  1394. a[i] = value;
  1395. }
  1396. }
  1397. if (updated) {
  1398. r_variant = a;
  1399. return true;
  1400. }
  1401. } break;
  1402. case Variant::DICTIONARY: {
  1403. Dictionary d = r_variant;
  1404. bool updated = false;
  1405. for (int i = 0; i < d.size(); i++) {
  1406. Variant value = d.get_value_at_index(i);
  1407. if (_check_node_path_recursive(p_root_node, value, p_renames)) {
  1408. if (!updated) {
  1409. d = d.duplicate(); // Need to duplicate for undo-redo to work.
  1410. updated = true;
  1411. }
  1412. d[d.get_key_at_index(i)] = value;
  1413. }
  1414. }
  1415. if (updated) {
  1416. r_variant = d;
  1417. return true;
  1418. }
  1419. } break;
  1420. default: {
  1421. }
  1422. }
  1423. return false;
  1424. }
  1425. void SceneTreeDock::perform_node_renames(Node *p_base, HashMap<Node *, NodePath> *p_renames, HashMap<Ref<Animation>, HashSet<int>> *r_rem_anims) {
  1426. HashMap<Ref<Animation>, HashSet<int>> rem_anims;
  1427. if (!r_rem_anims) {
  1428. r_rem_anims = &rem_anims;
  1429. }
  1430. if (!p_base) {
  1431. p_base = edited_scene;
  1432. }
  1433. if (!p_base) {
  1434. return;
  1435. }
  1436. // No renaming if base node is deleted.
  1437. HashMap<Node *, NodePath>::Iterator found_base_path = p_renames->find(p_base);
  1438. if (found_base_path && found_base_path->value.is_empty()) {
  1439. return;
  1440. }
  1441. // Renaming node paths used in node properties.
  1442. List<PropertyInfo> properties;
  1443. p_base->get_property_list(&properties);
  1444. for (const PropertyInfo &E : properties) {
  1445. if (!(E.usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR))) {
  1446. continue;
  1447. }
  1448. String propertyname = E.name;
  1449. Variant old_variant = p_base->get(propertyname);
  1450. Variant updated_variant = old_variant;
  1451. if (_check_node_path_recursive(p_base, updated_variant, p_renames)) {
  1452. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1453. undo_redo->add_do_property(p_base, propertyname, updated_variant);
  1454. undo_redo->add_undo_property(p_base, propertyname, old_variant);
  1455. p_base->set(propertyname, updated_variant);
  1456. }
  1457. }
  1458. bool autorename_animation_tracks = bool(EDITOR_GET("editors/animation/autorename_animation_tracks"));
  1459. if (autorename_animation_tracks && Object::cast_to<AnimationPlayer>(p_base)) {
  1460. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_base);
  1461. List<StringName> anims;
  1462. ap->get_animation_list(&anims);
  1463. Node *root = ap->get_node(ap->get_root());
  1464. if (root) {
  1465. HashMap<Node *, NodePath>::Iterator found_root_path = p_renames->find(root);
  1466. NodePath new_root_path = found_root_path ? found_root_path->value : root->get_path();
  1467. if (!new_root_path.is_empty()) { // No renaming if root node is deleted.
  1468. for (const StringName &E : anims) {
  1469. Ref<Animation> anim = ap->get_animation(E);
  1470. if (!r_rem_anims->has(anim)) {
  1471. r_rem_anims->insert(anim, HashSet<int>());
  1472. HashSet<int> &ran = r_rem_anims->find(anim)->value;
  1473. for (int i = 0; i < anim->get_track_count(); i++) {
  1474. ran.insert(i);
  1475. }
  1476. }
  1477. HashSet<int> &ran = r_rem_anims->find(anim)->value;
  1478. if (anim.is_null()) {
  1479. continue;
  1480. }
  1481. int tracks_removed = 0;
  1482. for (int i = 0; i < anim->get_track_count(); i++) {
  1483. NodePath track_np = anim->track_get_path(i);
  1484. Node *n = root->get_node_or_null(track_np);
  1485. if (!n) {
  1486. continue;
  1487. }
  1488. if (!ran.has(i)) {
  1489. continue; //channel was removed
  1490. }
  1491. HashMap<Node *, NodePath>::Iterator found_path = p_renames->find(n);
  1492. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1493. if (found_path) {
  1494. if (found_path->value.is_empty()) {
  1495. //will be erased
  1496. int idx = i - tracks_removed;
  1497. tracks_removed++;
  1498. undo_redo->add_do_method(anim.ptr(), "remove_track", idx);
  1499. undo_redo->add_undo_method(anim.ptr(), "add_track", anim->track_get_type(i), idx);
  1500. undo_redo->add_undo_method(anim.ptr(), "track_set_path", idx, track_np);
  1501. undo_redo->add_undo_method(anim.ptr(), "track_set_interpolation_type", idx, anim->track_get_interpolation_type(i));
  1502. for (int j = 0; j < anim->track_get_key_count(i); j++) {
  1503. 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));
  1504. }
  1505. ran.erase(i); //byebye channel
  1506. } else {
  1507. //will be renamed
  1508. NodePath rel_path = new_root_path.rel_path_to(found_path->value);
  1509. NodePath new_path = NodePath(rel_path.get_names(), track_np.get_subnames(), false);
  1510. if (new_path == track_np) {
  1511. continue; //bleh
  1512. }
  1513. undo_redo->add_do_method(anim.ptr(), "track_set_path", i, new_path);
  1514. undo_redo->add_undo_method(anim.ptr(), "track_set_path", i, track_np);
  1515. }
  1516. }
  1517. }
  1518. }
  1519. }
  1520. }
  1521. }
  1522. for (int i = 0; i < p_base->get_child_count(); i++) {
  1523. perform_node_renames(p_base->get_child(i), p_renames, r_rem_anims);
  1524. }
  1525. }
  1526. void SceneTreeDock::_node_prerenamed(Node *p_node, const String &p_new_name) {
  1527. HashMap<Node *, NodePath> path_renames;
  1528. Vector<StringName> base_path;
  1529. Node *n = p_node->get_parent();
  1530. while (n) {
  1531. base_path.push_back(n->get_name());
  1532. n = n->get_parent();
  1533. }
  1534. base_path.reverse();
  1535. Vector<StringName> new_base_path = base_path;
  1536. base_path.push_back(p_node->get_name());
  1537. new_base_path.push_back(p_new_name);
  1538. NodePath new_path(new_base_path, true);
  1539. path_renames[p_node] = new_path;
  1540. for (int i = 0; i < p_node->get_child_count(); i++) {
  1541. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), &path_renames);
  1542. }
  1543. perform_node_renames(nullptr, &path_renames);
  1544. }
  1545. bool SceneTreeDock::_validate_no_foreign() {
  1546. List<Node *> selection = editor_selection->get_selected_node_list();
  1547. for (Node *E : selection) {
  1548. if (E != edited_scene && E->get_owner() != edited_scene) {
  1549. accept->set_text(TTR("Can't operate on nodes from a foreign scene!"));
  1550. accept->popup_centered();
  1551. return false;
  1552. }
  1553. // When edited_scene inherits from another one the root Node will be the parent Scene,
  1554. // we don't want to consider that Node a foreign one otherwise we would not be able to
  1555. // delete it.
  1556. if (edited_scene->get_scene_inherited_state().is_valid() && edited_scene == E) {
  1557. continue;
  1558. }
  1559. 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) {
  1560. accept->set_text(TTR("Can't operate on nodes the current scene inherits from!"));
  1561. accept->popup_centered();
  1562. return false;
  1563. }
  1564. }
  1565. return true;
  1566. }
  1567. bool SceneTreeDock::_validate_no_instance() {
  1568. List<Node *> selection = editor_selection->get_selected_node_list();
  1569. for (Node *E : selection) {
  1570. if (E != edited_scene && !E->get_scene_file_path().is_empty()) {
  1571. accept->set_text(TTR("This operation can't be done on instantiated scenes."));
  1572. accept->popup_centered();
  1573. return false;
  1574. }
  1575. }
  1576. return true;
  1577. }
  1578. void SceneTreeDock::_node_reparent(NodePath p_path, bool p_keep_global_xform) {
  1579. Node *new_parent = scene_root->get_node(p_path);
  1580. ERR_FAIL_COND(!new_parent);
  1581. List<Node *> selection = editor_selection->get_selected_node_list();
  1582. if (selection.is_empty()) {
  1583. return; // Nothing to reparent.
  1584. }
  1585. Vector<Node *> nodes;
  1586. for (Node *E : selection) {
  1587. nodes.push_back(E);
  1588. }
  1589. _do_reparent(new_parent, -1, nodes, p_keep_global_xform);
  1590. }
  1591. void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, Vector<Node *> p_nodes, bool p_keep_global_xform) {
  1592. Node *new_parent = p_new_parent;
  1593. ERR_FAIL_COND(!new_parent);
  1594. if (p_nodes.size() == 0) {
  1595. return; // Nothing to reparent.
  1596. }
  1597. p_nodes.sort_custom<Node::Comparator>(); //Makes result reliable.
  1598. bool no_change = true;
  1599. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1600. if (p_nodes[ni] == p_new_parent) {
  1601. return; // Attempt to reparent to itself.
  1602. }
  1603. if (p_nodes[ni]->get_parent() != p_new_parent || p_position_in_parent + ni != p_nodes[ni]->get_index()) {
  1604. no_change = false;
  1605. }
  1606. }
  1607. if (no_change) {
  1608. return; // Position and parent didn't change.
  1609. }
  1610. Node *validate = new_parent;
  1611. while (validate) {
  1612. ERR_FAIL_COND_MSG(p_nodes.has(validate), "Selection changed at some point. Can't reparent.");
  1613. validate = validate->get_parent();
  1614. }
  1615. // Sort by tree order, so re-adding is easy.
  1616. p_nodes.sort_custom<Node::Comparator>();
  1617. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1618. undo_redo->create_action(TTR("Reparent Node"), UndoRedo::MERGE_DISABLE, p_nodes[0]);
  1619. HashMap<Node *, NodePath> path_renames;
  1620. Vector<StringName> former_names;
  1621. int inc = 0;
  1622. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1623. // No undo implemented for this yet.
  1624. Node *node = p_nodes[ni];
  1625. fill_path_renames(node, new_parent, &path_renames);
  1626. former_names.push_back(node->get_name());
  1627. List<Node *> owned;
  1628. node->get_owned_by(node->get_owner(), &owned);
  1629. Array owners;
  1630. for (Node *E : owned) {
  1631. owners.push_back(E);
  1632. }
  1633. bool same_parent = new_parent == node->get_parent();
  1634. if (same_parent && node->get_index() < p_position_in_parent + ni) {
  1635. inc--; // If the child will generate a gap when moved, adjust.
  1636. }
  1637. if (!same_parent) {
  1638. undo_redo->add_do_method(node->get_parent(), "remove_child", node);
  1639. undo_redo->add_do_method(new_parent, "add_child", node, true);
  1640. }
  1641. int new_position_in_parent = p_position_in_parent == -1 ? -1 : p_position_in_parent + inc;
  1642. if (new_position_in_parent >= 0 || same_parent) {
  1643. undo_redo->add_do_method(new_parent, "move_child", node, new_position_in_parent);
  1644. }
  1645. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  1646. String old_name = former_names[ni];
  1647. String new_name = new_parent->validate_child_name(node);
  1648. // Name was modified, fix the path renames.
  1649. if (old_name.casecmp_to(new_name) != 0) {
  1650. // Fix the to name to have the new name.
  1651. HashMap<Node *, NodePath>::Iterator found_path = path_renames.find(node);
  1652. if (found_path) {
  1653. NodePath old_new_name = found_path->value;
  1654. Vector<StringName> unfixed_new_names = old_new_name.get_names();
  1655. Vector<StringName> fixed_new_names;
  1656. // Get last name and replace with fixed new name.
  1657. for (int a = 0; a < (unfixed_new_names.size() - 1); a++) {
  1658. fixed_new_names.push_back(unfixed_new_names[a]);
  1659. }
  1660. fixed_new_names.push_back(new_name);
  1661. NodePath fixed_node_path = NodePath(fixed_new_names, true);
  1662. path_renames[node] = fixed_node_path;
  1663. } else {
  1664. ERR_PRINT("Internal error. Can't find renamed path for node '" + node->get_path() + "'");
  1665. }
  1666. }
  1667. 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);
  1668. 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());
  1669. if (p_keep_global_xform) {
  1670. if (Object::cast_to<Node2D>(node)) {
  1671. undo_redo->add_do_method(node, "set_global_transform", Object::cast_to<Node2D>(node)->get_global_transform());
  1672. }
  1673. if (Object::cast_to<Node3D>(node)) {
  1674. undo_redo->add_do_method(node, "set_global_transform", Object::cast_to<Node3D>(node)->get_global_transform());
  1675. }
  1676. if (Object::cast_to<Control>(node)) {
  1677. undo_redo->add_do_method(node, "set_global_position", Object::cast_to<Control>(node)->get_global_position());
  1678. }
  1679. }
  1680. undo_redo->add_do_method(this, "_set_owners", edited_scene, owners);
  1681. if (AnimationPlayerEditor::get_singleton()->get_track_editor()->get_root() == node) {
  1682. undo_redo->add_do_method(AnimationPlayerEditor::get_singleton()->get_track_editor(), "set_root", node);
  1683. }
  1684. undo_redo->add_undo_method(new_parent, "remove_child", node);
  1685. undo_redo->add_undo_method(node, "set_name", former_names[ni]);
  1686. inc++;
  1687. }
  1688. // Add and move in a second step (so old order is preserved).
  1689. for (int ni = 0; ni < p_nodes.size(); ni++) {
  1690. Node *node = p_nodes[ni];
  1691. List<Node *> owned;
  1692. node->get_owned_by(node->get_owner(), &owned);
  1693. Array owners;
  1694. for (Node *E : owned) {
  1695. owners.push_back(E);
  1696. }
  1697. int child_pos = node->get_index();
  1698. undo_redo->add_undo_method(node->get_parent(), "add_child", node, true);
  1699. undo_redo->add_undo_method(node->get_parent(), "move_child", node, child_pos);
  1700. undo_redo->add_undo_method(this, "_set_owners", edited_scene, owners);
  1701. if (AnimationPlayerEditor::get_singleton()->get_track_editor()->get_root() == node) {
  1702. undo_redo->add_undo_method(AnimationPlayerEditor::get_singleton()->get_track_editor(), "set_root", node);
  1703. }
  1704. if (p_keep_global_xform) {
  1705. if (Object::cast_to<Node2D>(node)) {
  1706. undo_redo->add_undo_method(node, "set_transform", Object::cast_to<Node2D>(node)->get_transform());
  1707. }
  1708. if (Object::cast_to<Node3D>(node)) {
  1709. undo_redo->add_undo_method(node, "set_transform", Object::cast_to<Node3D>(node)->get_transform());
  1710. }
  1711. if (Object::cast_to<Control>(node)) {
  1712. undo_redo->add_undo_method(node, "set_position", Object::cast_to<Control>(node)->get_position());
  1713. }
  1714. }
  1715. }
  1716. perform_node_renames(nullptr, &path_renames);
  1717. undo_redo->commit_action();
  1718. }
  1719. void SceneTreeDock::_script_created(Ref<Script> p_script) {
  1720. List<Node *> selected = editor_selection->get_selected_node_list();
  1721. if (selected.is_empty()) {
  1722. return;
  1723. }
  1724. if (p_script->is_built_in()) {
  1725. p_script->set_path(edited_scene->get_scene_file_path() + "::");
  1726. }
  1727. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1728. undo_redo->create_action(TTR("Attach Script"), UndoRedo::MERGE_DISABLE, selected.front()->get());
  1729. for (Node *E : selected) {
  1730. Ref<Script> existing = E->get_script();
  1731. undo_redo->add_do_method(InspectorDock::get_singleton(), "store_script_properties", E);
  1732. undo_redo->add_undo_method(InspectorDock::get_singleton(), "store_script_properties", E);
  1733. undo_redo->add_do_method(E, "set_script", p_script);
  1734. undo_redo->add_undo_method(E, "set_script", existing);
  1735. undo_redo->add_do_method(InspectorDock::get_singleton(), "apply_script_properties", E);
  1736. undo_redo->add_undo_method(InspectorDock::get_singleton(), "apply_script_properties", E);
  1737. undo_redo->add_do_method(this, "_update_script_button");
  1738. undo_redo->add_undo_method(this, "_update_script_button");
  1739. }
  1740. undo_redo->commit_action();
  1741. _push_item(p_script.ptr());
  1742. _update_script_button();
  1743. }
  1744. void SceneTreeDock::_shader_created(Ref<Shader> p_shader) {
  1745. if (selected_shader_material.is_null()) {
  1746. return;
  1747. }
  1748. Ref<Shader> existing = selected_shader_material->get_shader();
  1749. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1750. undo_redo->create_action(TTR("Set Shader"));
  1751. undo_redo->add_do_method(selected_shader_material.ptr(), "set_shader", p_shader);
  1752. undo_redo->add_undo_method(selected_shader_material.ptr(), "set_shader", existing);
  1753. undo_redo->commit_action();
  1754. }
  1755. void SceneTreeDock::_script_creation_closed() {
  1756. script_create_dialog->disconnect("script_created", callable_mp(this, &SceneTreeDock::_script_created));
  1757. script_create_dialog->disconnect("confirmed", callable_mp(this, &SceneTreeDock::_script_creation_closed));
  1758. script_create_dialog->disconnect("canceled", callable_mp(this, &SceneTreeDock::_script_creation_closed));
  1759. }
  1760. void SceneTreeDock::_shader_creation_closed() {
  1761. shader_create_dialog->disconnect("shader_created", callable_mp(this, &SceneTreeDock::_shader_created));
  1762. shader_create_dialog->disconnect("confirmed", callable_mp(this, &SceneTreeDock::_shader_creation_closed));
  1763. shader_create_dialog->disconnect("canceled", callable_mp(this, &SceneTreeDock::_shader_creation_closed));
  1764. }
  1765. void SceneTreeDock::_toggle_editable_children_from_selection() {
  1766. List<Node *> selection = editor_selection->get_selected_node_list();
  1767. List<Node *>::Element *e = selection.front();
  1768. if (e) {
  1769. _toggle_editable_children(e->get());
  1770. }
  1771. }
  1772. void SceneTreeDock::_toggle_placeholder_from_selection() {
  1773. List<Node *> selection = editor_selection->get_selected_node_list();
  1774. List<Node *>::Element *e = selection.front();
  1775. if (e) {
  1776. Node *node = e->get();
  1777. if (node) {
  1778. _toggle_editable_children(node);
  1779. bool placeholder = node->get_scene_instance_load_placeholder();
  1780. placeholder = !placeholder;
  1781. node->set_scene_instance_load_placeholder(placeholder);
  1782. scene_tree->update_tree();
  1783. }
  1784. }
  1785. }
  1786. void SceneTreeDock::_toggle_editable_children(Node *p_node) {
  1787. if (p_node) {
  1788. bool editable = !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node);
  1789. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(p_node, editable);
  1790. if (editable) {
  1791. p_node->set_scene_instance_load_placeholder(false);
  1792. }
  1793. Node3DEditor::get_singleton()->update_all_gizmos(p_node);
  1794. scene_tree->update_tree();
  1795. }
  1796. }
  1797. void SceneTreeDock::_delete_confirm(bool p_cut) {
  1798. List<Node *> remove_list = editor_selection->get_selected_node_list();
  1799. if (remove_list.is_empty()) {
  1800. return;
  1801. }
  1802. bool entire_scene = false;
  1803. for (const Node *E : remove_list) {
  1804. if (E == edited_scene) {
  1805. entire_scene = true;
  1806. break;
  1807. }
  1808. }
  1809. EditorNode::get_singleton()->hide_unused_editors(this);
  1810. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1811. undo_redo->create_action(p_cut ? TTR("Cut Node(s)") : TTR("Remove Node(s)"), UndoRedo::MERGE_DISABLE, remove_list.front()->get());
  1812. if (entire_scene) {
  1813. undo_redo->add_do_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);
  1814. undo_redo->add_undo_method(EditorNode::get_singleton(), "set_edited_scene", edited_scene);
  1815. undo_redo->add_undo_method(edited_scene, "set_owner", edited_scene->get_owner());
  1816. undo_redo->add_undo_method(scene_tree, "update_tree");
  1817. undo_redo->add_undo_reference(edited_scene);
  1818. } else {
  1819. if (delete_tracks_checkbox->is_pressed() || p_cut) {
  1820. remove_list.sort_custom<Node::Comparator>(); // Sort nodes to keep positions.
  1821. HashMap<Node *, NodePath> path_renames;
  1822. //delete from animation
  1823. for (Node *n : remove_list) {
  1824. if (!n->is_inside_tree() || !n->get_parent()) {
  1825. continue;
  1826. }
  1827. fill_path_renames(n, nullptr, &path_renames);
  1828. }
  1829. perform_node_renames(nullptr, &path_renames);
  1830. }
  1831. //delete for read
  1832. for (Node *n : remove_list) {
  1833. if (!n->is_inside_tree() || !n->get_parent()) {
  1834. continue;
  1835. }
  1836. List<Node *> owned;
  1837. n->get_owned_by(n->get_owner(), &owned);
  1838. Array owners;
  1839. for (Node *F : owned) {
  1840. owners.push_back(F);
  1841. }
  1842. undo_redo->add_do_method(n->get_parent(), "remove_child", n);
  1843. undo_redo->add_undo_method(n->get_parent(), "add_child", n, true);
  1844. undo_redo->add_undo_method(n->get_parent(), "move_child", n, n->get_index());
  1845. if (AnimationPlayerEditor::get_singleton()->get_track_editor()->get_root() == n) {
  1846. undo_redo->add_undo_method(AnimationPlayerEditor::get_singleton()->get_track_editor(), "set_root", n);
  1847. }
  1848. undo_redo->add_undo_method(this, "_set_owners", edited_scene, owners);
  1849. undo_redo->add_undo_reference(n);
  1850. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  1851. undo_redo->add_do_method(ed, "live_debug_remove_and_keep_node", edited_scene->get_path_to(n), n->get_instance_id());
  1852. 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());
  1853. }
  1854. }
  1855. undo_redo->commit_action();
  1856. // hack, force 2d editor viewport to refresh after deletion
  1857. if (CanvasItemEditor *editor = CanvasItemEditor::get_singleton()) {
  1858. editor->get_viewport_control()->queue_redraw();
  1859. }
  1860. _push_item(nullptr);
  1861. // Fixes the EditorSelectionHistory from still offering deleted notes
  1862. EditorSelectionHistory *editor_history = EditorNode::get_singleton()->get_editor_selection_history();
  1863. editor_history->cleanup_history();
  1864. InspectorDock::get_singleton()->call("_prepare_history");
  1865. }
  1866. void SceneTreeDock::_update_script_button() {
  1867. if (!profile_allow_script_editing) {
  1868. button_create_script->hide();
  1869. button_detach_script->hide();
  1870. } else if (editor_selection->get_selection().size() == 0) {
  1871. button_create_script->hide();
  1872. button_detach_script->hide();
  1873. } else if (editor_selection->get_selection().size() == 1) {
  1874. Node *n = editor_selection->get_selected_node_list()[0];
  1875. if (n->get_script().is_null()) {
  1876. button_create_script->show();
  1877. button_detach_script->hide();
  1878. } else {
  1879. button_create_script->hide();
  1880. button_detach_script->show();
  1881. }
  1882. } else {
  1883. button_create_script->hide();
  1884. Array selection = editor_selection->get_selected_nodes();
  1885. for (int i = 0; i < selection.size(); i++) {
  1886. Node *n = Object::cast_to<Node>(selection[i]);
  1887. if (!n->get_script().is_null()) {
  1888. button_detach_script->show();
  1889. return;
  1890. }
  1891. }
  1892. button_detach_script->hide();
  1893. }
  1894. }
  1895. void SceneTreeDock::_selection_changed() {
  1896. int selection_size = editor_selection->get_selection().size();
  1897. if (selection_size > 1) {
  1898. //automatically turn on multi-edit
  1899. _tool_selected(TOOL_MULTI_EDIT);
  1900. } else if (selection_size == 1) {
  1901. _handle_select(editor_selection->get_selection().begin()->key);
  1902. } else if (selection_size == 0) {
  1903. _push_item(nullptr);
  1904. }
  1905. _update_script_button();
  1906. }
  1907. void SceneTreeDock::_do_create(Node *p_parent) {
  1908. Variant c = create_dialog->instantiate_selected();
  1909. Node *child = Object::cast_to<Node>(c);
  1910. ERR_FAIL_COND(!child);
  1911. String new_name = p_parent->validate_child_name(child);
  1912. if (GLOBAL_GET("editor/naming/node_name_casing").operator int() != NAME_CASING_PASCAL_CASE) {
  1913. new_name = adjust_name_casing(new_name);
  1914. }
  1915. child->set_name(new_name);
  1916. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1917. undo_redo->create_action_for_history(TTR("Create Node"), editor_data->get_current_edited_scene_history_id());
  1918. if (edited_scene) {
  1919. undo_redo->add_do_method(p_parent, "add_child", child, true);
  1920. undo_redo->add_do_method(child, "set_owner", edited_scene);
  1921. undo_redo->add_do_method(editor_selection, "clear");
  1922. undo_redo->add_do_method(editor_selection, "add_node", child);
  1923. undo_redo->add_do_reference(child);
  1924. undo_redo->add_undo_method(p_parent, "remove_child", child);
  1925. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  1926. undo_redo->add_do_method(ed, "live_debug_create_node", edited_scene->get_path_to(p_parent), child->get_class(), new_name);
  1927. undo_redo->add_undo_method(ed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(p_parent)).path_join(new_name)));
  1928. } else {
  1929. undo_redo->add_do_method(EditorNode::get_singleton(), "set_edited_scene", child);
  1930. undo_redo->add_do_method(scene_tree, "update_tree");
  1931. undo_redo->add_do_reference(child);
  1932. undo_redo->add_undo_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);
  1933. }
  1934. undo_redo->commit_action();
  1935. _push_item(c);
  1936. editor_selection->clear();
  1937. editor_selection->add_node(child);
  1938. if (Object::cast_to<Control>(c)) {
  1939. //make editor more comfortable, so some controls don't appear super shrunk
  1940. Control *ct = Object::cast_to<Control>(c);
  1941. Size2 ms = ct->get_minimum_size();
  1942. if (ms.width < 4) {
  1943. ms.width = 40;
  1944. }
  1945. if (ms.height < 4) {
  1946. ms.height = 40;
  1947. }
  1948. if (ct->is_layout_rtl()) {
  1949. ct->set_position(ct->get_position() - Vector2(ms.x, 0));
  1950. }
  1951. ct->set_size(ms);
  1952. }
  1953. emit_signal(SNAME("node_created"), c);
  1954. }
  1955. void SceneTreeDock::_create() {
  1956. if (current_option == TOOL_NEW) {
  1957. Node *parent = nullptr;
  1958. if (edited_scene) {
  1959. // If root exists in edited scene
  1960. parent = scene_tree->get_selected();
  1961. if (!parent) {
  1962. parent = edited_scene;
  1963. }
  1964. } else {
  1965. // If no root exist in edited scene
  1966. parent = scene_root;
  1967. ERR_FAIL_COND(!parent);
  1968. }
  1969. _do_create(parent);
  1970. } else if (current_option == TOOL_REPLACE) {
  1971. List<Node *> selection = editor_selection->get_selected_node_list();
  1972. ERR_FAIL_COND(selection.size() <= 0);
  1973. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1974. ur->create_action(TTR("Change type of node(s)"), UndoRedo::MERGE_DISABLE, selection.front()->get());
  1975. for (Node *n : selection) {
  1976. ERR_FAIL_COND(!n);
  1977. Variant c = create_dialog->instantiate_selected();
  1978. ERR_FAIL_COND(!c);
  1979. Node *newnode = Object::cast_to<Node>(c);
  1980. ERR_FAIL_COND(!newnode);
  1981. ur->add_do_method(this, "replace_node", n, newnode, true, false);
  1982. ur->add_do_reference(newnode);
  1983. ur->add_undo_method(this, "replace_node", newnode, n, false, false);
  1984. ur->add_undo_reference(n);
  1985. }
  1986. ur->commit_action();
  1987. } else if (current_option == TOOL_REPARENT_TO_NEW_NODE) {
  1988. List<Node *> selection = editor_selection->get_selected_node_list();
  1989. ERR_FAIL_COND(selection.size() <= 0);
  1990. // Find top level node in selection
  1991. bool only_one_top_node = true;
  1992. Node *first = selection.front()->get();
  1993. ERR_FAIL_COND(!first);
  1994. int smaller_path_to_top = first->get_path_to(scene_root).get_name_count();
  1995. Node *top_node = first;
  1996. for (List<Node *>::Element *E = selection.front()->next(); E; E = E->next()) {
  1997. Node *n = E->get();
  1998. ERR_FAIL_COND(!n);
  1999. int path_length = n->get_path_to(scene_root).get_name_count();
  2000. if (top_node != n) {
  2001. if (smaller_path_to_top > path_length) {
  2002. top_node = n;
  2003. smaller_path_to_top = path_length;
  2004. only_one_top_node = true;
  2005. } else if (smaller_path_to_top == path_length) {
  2006. if (only_one_top_node && top_node->get_parent() != n->get_parent()) {
  2007. only_one_top_node = false;
  2008. }
  2009. }
  2010. }
  2011. }
  2012. Node *parent = nullptr;
  2013. if (only_one_top_node) {
  2014. parent = top_node->get_parent();
  2015. } else {
  2016. parent = top_node->get_parent()->get_parent();
  2017. }
  2018. _do_create(parent);
  2019. Vector<Node *> nodes;
  2020. for (Node *E : selection) {
  2021. nodes.push_back(E);
  2022. }
  2023. // This works because editor_selection was cleared and populated with last created node in _do_create()
  2024. Node *last_created = editor_selection->get_selected_node_list().front()->get();
  2025. _do_reparent(last_created, -1, nodes, true);
  2026. }
  2027. scene_tree->get_scene_tree()->grab_focus();
  2028. }
  2029. void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node, bool p_keep_properties, bool p_remove_old) {
  2030. Node *n = p_node;
  2031. Node *newnode = p_by_node;
  2032. if (p_keep_properties) {
  2033. Node *default_oldnode = Object::cast_to<Node>(ClassDB::instantiate(n->get_class()));
  2034. List<PropertyInfo> pinfo;
  2035. n->get_property_list(&pinfo);
  2036. for (const PropertyInfo &E : pinfo) {
  2037. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  2038. continue;
  2039. }
  2040. if (default_oldnode->get(E.name) != n->get(E.name)) {
  2041. newnode->set(E.name, n->get(E.name));
  2042. }
  2043. }
  2044. memdelete(default_oldnode);
  2045. }
  2046. _push_item(nullptr);
  2047. //reconnect signals
  2048. List<MethodInfo> sl;
  2049. n->get_signal_list(&sl);
  2050. for (const MethodInfo &E : sl) {
  2051. List<Object::Connection> cl;
  2052. n->get_signal_connection_list(E.name, &cl);
  2053. for (const Object::Connection &c : cl) {
  2054. if (!(c.flags & Object::CONNECT_PERSIST)) {
  2055. continue;
  2056. }
  2057. newnode->connect(c.signal.get_name(), c.callable, Object::CONNECT_PERSIST);
  2058. }
  2059. }
  2060. String newname = n->get_name();
  2061. List<Node *> to_erase;
  2062. for (int i = 0; i < n->get_child_count(); i++) {
  2063. if (n->get_child(i)->get_owner() == nullptr && n->is_owned_by_parent()) {
  2064. to_erase.push_back(n->get_child(i));
  2065. }
  2066. }
  2067. n->replace_by(newnode, true);
  2068. //small hack to make collisionshapes and other kind of nodes to work
  2069. for (int i = 0; i < newnode->get_child_count(); i++) {
  2070. Node *c = newnode->get_child(i);
  2071. c->call("set_transform", c->call("get_transform"));
  2072. }
  2073. //p_remove_old was added to support undo
  2074. if (p_remove_old) {
  2075. EditorUndoRedoManager::get_singleton()->clear_history();
  2076. }
  2077. newnode->set_name(newname);
  2078. _push_item(newnode);
  2079. if (p_remove_old) {
  2080. memdelete(n);
  2081. while (to_erase.front()) {
  2082. memdelete(to_erase.front()->get());
  2083. to_erase.pop_front();
  2084. }
  2085. }
  2086. }
  2087. void SceneTreeDock::set_edited_scene(Node *p_scene) {
  2088. edited_scene = p_scene;
  2089. }
  2090. void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected) {
  2091. scene_tree->set_selected(p_node, p_emit_selected);
  2092. }
  2093. void SceneTreeDock::_new_scene_from(String p_file) {
  2094. List<Node *> selection = editor_selection->get_selected_node_list();
  2095. if (selection.size() != 1) {
  2096. accept->set_text(TTR("This operation requires a single selected node."));
  2097. accept->popup_centered();
  2098. return;
  2099. }
  2100. if (EditorNode::get_singleton()->is_scene_open(p_file)) {
  2101. accept->set_text(TTR("Can't overwrite scene that is still open!"));
  2102. accept->popup_centered();
  2103. return;
  2104. }
  2105. Node *base = selection.front()->get();
  2106. HashMap<const Node *, Node *> duplimap;
  2107. Node *copy = base->duplicate_from_editor(duplimap);
  2108. if (copy) {
  2109. // Handle Unique Nodes.
  2110. for (int i = 0; i < copy->get_child_count(false); i++) {
  2111. _set_node_owner_recursive(copy->get_child(i, false), copy);
  2112. }
  2113. // Root node cannot ever be unique name in its own Scene!
  2114. copy->set_unique_name_in_owner(false);
  2115. Ref<PackedScene> sdata = memnew(PackedScene);
  2116. Error err = sdata->pack(copy);
  2117. memdelete(copy);
  2118. if (err != OK) {
  2119. accept->set_text(TTR("Couldn't save new scene. Likely dependencies (instances) couldn't be satisfied."));
  2120. accept->popup_centered();
  2121. return;
  2122. }
  2123. int flg = 0;
  2124. if (EDITOR_GET("filesystem/on_save/compress_binary_resources")) {
  2125. flg |= ResourceSaver::FLAG_COMPRESS;
  2126. }
  2127. err = ResourceSaver::save(sdata, p_file, flg);
  2128. if (err != OK) {
  2129. accept->set_text(TTR("Error saving scene."));
  2130. accept->popup_centered();
  2131. return;
  2132. }
  2133. _replace_with_branch_scene(p_file, base);
  2134. } else {
  2135. accept->set_text(TTR("Error duplicating scene to save it."));
  2136. accept->popup_centered();
  2137. return;
  2138. }
  2139. }
  2140. void SceneTreeDock::_set_node_owner_recursive(Node *p_node, Node *p_owner) {
  2141. if (!p_node->get_owner()) {
  2142. p_node->set_owner(p_owner);
  2143. }
  2144. for (int i = 0; i < p_node->get_child_count(false); i++) {
  2145. _set_node_owner_recursive(p_node->get_child(i, false), p_owner);
  2146. }
  2147. }
  2148. static bool _is_node_visible(Node *p_node) {
  2149. if (!p_node->get_owner()) {
  2150. return false;
  2151. }
  2152. if (p_node->get_owner() != EditorNode::get_singleton()->get_edited_scene() && !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node->get_owner())) {
  2153. return false;
  2154. }
  2155. return true;
  2156. }
  2157. static bool _has_visible_children(Node *p_node) {
  2158. bool collapsed = p_node->is_displayed_folded();
  2159. if (collapsed) {
  2160. return false;
  2161. }
  2162. for (int i = 0; i < p_node->get_child_count(); i++) {
  2163. Node *child = p_node->get_child(i);
  2164. if (!_is_node_visible(child)) {
  2165. continue;
  2166. }
  2167. return true;
  2168. }
  2169. return false;
  2170. }
  2171. void SceneTreeDock::_normalize_drop(Node *&to_node, int &to_pos, int p_type) {
  2172. to_pos = -1;
  2173. if (p_type == -1) {
  2174. //drop at above selected node
  2175. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  2176. to_node = nullptr;
  2177. ERR_FAIL_MSG("Cannot perform drop above the root node!");
  2178. }
  2179. to_pos = to_node->get_index();
  2180. to_node = to_node->get_parent();
  2181. } else if (p_type == 1) {
  2182. //drop at below selected node
  2183. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  2184. //if at lower sibling of root node
  2185. to_pos = 0; //just insert at beginning of root node
  2186. return;
  2187. }
  2188. Node *lower_sibling = nullptr;
  2189. if (_has_visible_children(to_node)) {
  2190. to_pos = 0;
  2191. } else {
  2192. for (int i = to_node->get_index() + 1; i < to_node->get_parent()->get_child_count(); i++) {
  2193. Node *c = to_node->get_parent()->get_child(i);
  2194. if (_is_node_visible(c)) {
  2195. lower_sibling = c;
  2196. break;
  2197. }
  2198. }
  2199. if (lower_sibling) {
  2200. to_pos = lower_sibling->get_index();
  2201. }
  2202. to_node = to_node->get_parent();
  2203. }
  2204. }
  2205. }
  2206. void SceneTreeDock::_files_dropped(Vector<String> p_files, NodePath p_to, int p_type) {
  2207. Node *node = get_node(p_to);
  2208. ERR_FAIL_COND(!node);
  2209. if (scene_tree->get_scene_tree()->get_drop_mode_flags() & Tree::DROP_MODE_INBETWEEN) {
  2210. // Dropped PackedScene, instance it.
  2211. int to_pos = -1;
  2212. _normalize_drop(node, to_pos, p_type);
  2213. _perform_instantiate_scenes(p_files, node, to_pos);
  2214. } else {
  2215. String res_path = p_files[0];
  2216. StringName res_type = EditorFileSystem::get_singleton()->get_file_type(res_path);
  2217. List<String> valid_properties;
  2218. List<PropertyInfo> pinfo;
  2219. node->get_property_list(&pinfo);
  2220. for (const PropertyInfo &p : pinfo) {
  2221. if (!(p.usage & PROPERTY_USAGE_EDITOR) || !(p.usage & PROPERTY_USAGE_STORAGE) || p.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  2222. continue;
  2223. }
  2224. Vector<String> valid_types = p.hint_string.split(",");
  2225. for (const String &prop_type : valid_types) {
  2226. 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)) {
  2227. valid_properties.push_back(p.name);
  2228. break;
  2229. }
  2230. }
  2231. }
  2232. if (valid_properties.size() > 1) {
  2233. property_drop_node = node;
  2234. resource_drop_path = res_path;
  2235. const EditorPropertyNameProcessor::Style style = InspectorDock::get_singleton()->get_property_name_style();
  2236. menu_properties->clear();
  2237. for (const String &p : valid_properties) {
  2238. menu_properties->add_item(EditorPropertyNameProcessor::get_singleton()->process_name(p, style));
  2239. menu_properties->set_item_metadata(-1, p);
  2240. }
  2241. menu_properties->reset_size();
  2242. menu_properties->set_position(get_screen_position() + get_local_mouse_position());
  2243. menu_properties->popup();
  2244. } else if (!valid_properties.is_empty()) {
  2245. _perform_property_drop(node, valid_properties[0], ResourceLoader::load(res_path));
  2246. }
  2247. }
  2248. }
  2249. void SceneTreeDock::_script_dropped(String p_file, NodePath p_to) {
  2250. Ref<Script> scr = ResourceLoader::load(p_file);
  2251. ERR_FAIL_COND(!scr.is_valid());
  2252. Node *n = get_node(p_to);
  2253. if (!n) {
  2254. return;
  2255. }
  2256. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2257. if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  2258. Object *obj = ClassDB::instantiate(scr->get_instance_base_type());
  2259. ERR_FAIL_NULL(obj);
  2260. Node *new_node = Object::cast_to<Node>(obj);
  2261. if (!new_node) {
  2262. if (!obj->is_ref_counted()) {
  2263. memdelete(obj);
  2264. }
  2265. ERR_FAIL_MSG("Script does not extend Node-derived type.");
  2266. }
  2267. new_node->set_name(Node::adjust_name_casing(p_file.get_file().get_basename()));
  2268. new_node->set_script(scr);
  2269. undo_redo->create_action(TTR("Instantiate Script"));
  2270. undo_redo->add_do_method(n, "add_child", new_node, true);
  2271. undo_redo->add_do_method(new_node, "set_owner", edited_scene);
  2272. undo_redo->add_do_method(editor_selection, "clear");
  2273. undo_redo->add_do_method(editor_selection, "add_node", new_node);
  2274. undo_redo->add_do_reference(new_node);
  2275. undo_redo->add_undo_method(n, "remove_child", new_node);
  2276. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  2277. undo_redo->add_do_method(ed, "live_debug_create_node", edited_scene->get_path_to(n), new_node->get_class(), new_node->get_name());
  2278. undo_redo->add_undo_method(ed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(n)).path_join(new_node->get_name())));
  2279. undo_redo->commit_action();
  2280. } else {
  2281. undo_redo->create_action(TTR("Attach Script"), UndoRedo::MERGE_DISABLE, n);
  2282. undo_redo->add_do_method(InspectorDock::get_singleton(), "store_script_properties", n);
  2283. undo_redo->add_undo_method(InspectorDock::get_singleton(), "store_script_properties", n);
  2284. undo_redo->add_do_method(n, "set_script", scr);
  2285. undo_redo->add_undo_method(n, "set_script", n->get_script());
  2286. undo_redo->add_do_method(InspectorDock::get_singleton(), "apply_script_properties", n);
  2287. undo_redo->add_undo_method(InspectorDock::get_singleton(), "apply_script_properties", n);
  2288. undo_redo->add_do_method(this, "_update_script_button");
  2289. undo_redo->add_undo_method(this, "_update_script_button");
  2290. undo_redo->commit_action();
  2291. }
  2292. }
  2293. void SceneTreeDock::_nodes_dragged(Array p_nodes, NodePath p_to, int p_type) {
  2294. if (!_validate_no_foreign()) {
  2295. return;
  2296. }
  2297. List<Node *> selection = editor_selection->get_selected_node_list();
  2298. if (selection.is_empty()) {
  2299. return; //nothing to reparent
  2300. }
  2301. Node *to_node = get_node(p_to);
  2302. if (!to_node) {
  2303. return;
  2304. }
  2305. Vector<Node *> nodes;
  2306. for (Node *E : selection) {
  2307. nodes.push_back(E);
  2308. }
  2309. int to_pos = -1;
  2310. _normalize_drop(to_node, to_pos, p_type);
  2311. _do_reparent(to_node, to_pos, nodes, !Input::get_singleton()->is_key_pressed(Key::SHIFT));
  2312. }
  2313. void SceneTreeDock::_add_children_to_popup(Object *p_obj, int p_depth) {
  2314. if (p_depth > 8) {
  2315. return;
  2316. }
  2317. List<PropertyInfo> pinfo;
  2318. p_obj->get_property_list(&pinfo);
  2319. for (const PropertyInfo &E : pinfo) {
  2320. if (!(E.usage & PROPERTY_USAGE_EDITOR)) {
  2321. continue;
  2322. }
  2323. if (E.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  2324. continue;
  2325. }
  2326. Variant value = p_obj->get(E.name);
  2327. if (value.get_type() != Variant::OBJECT) {
  2328. continue;
  2329. }
  2330. Object *obj = value;
  2331. if (!obj) {
  2332. continue;
  2333. }
  2334. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj);
  2335. if (menu->get_item_count() == 0) {
  2336. menu->add_submenu_item(TTR("Sub-Resources"), "Sub-Resources");
  2337. }
  2338. menu_subresources->add_icon_item(icon, E.name.capitalize(), EDIT_SUBRESOURCE_BASE + subresources.size());
  2339. menu_subresources->set_item_indent(-1, p_depth);
  2340. subresources.push_back(obj->get_instance_id());
  2341. _add_children_to_popup(obj, p_depth + 1);
  2342. }
  2343. }
  2344. void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
  2345. if (!EditorNode::get_singleton()->get_edited_scene()) {
  2346. menu->clear();
  2347. if (profile_allow_editing) {
  2348. menu->add_icon_shortcut(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  2349. menu->add_icon_shortcut(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/instantiate_scene"), TOOL_INSTANTIATE);
  2350. }
  2351. menu->reset_size();
  2352. menu->set_position(get_screen_position() + p_menu_pos);
  2353. menu->popup();
  2354. return;
  2355. }
  2356. List<Node *> selection = editor_selection->get_selected_node_list();
  2357. List<Node *> full_selection = editor_selection->get_full_selected_node_list(); // Above method only returns nodes with common parent.
  2358. if (selection.size() == 0) {
  2359. return;
  2360. }
  2361. menu->clear();
  2362. Ref<Script> existing_script;
  2363. bool existing_script_removable = true;
  2364. if (selection.size() == 1) {
  2365. Node *selected = selection[0];
  2366. if (profile_allow_editing) {
  2367. subresources.clear();
  2368. menu_subresources->clear();
  2369. menu_subresources->reset_size();
  2370. _add_children_to_popup(selection.front()->get(), 0);
  2371. if (menu->get_item_count() > 0) {
  2372. menu->add_separator();
  2373. }
  2374. menu->add_icon_shortcut(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  2375. menu->add_icon_shortcut(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/instantiate_scene"), TOOL_INSTANTIATE);
  2376. }
  2377. menu->add_icon_shortcut(get_theme_icon(SNAME("Collapse"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/expand_collapse_all"), TOOL_EXPAND_COLLAPSE);
  2378. menu->add_separator();
  2379. existing_script = selected->get_script();
  2380. if (EditorNode::get_singleton()->get_object_custom_type_base(selected) == existing_script) {
  2381. existing_script_removable = false;
  2382. }
  2383. }
  2384. if (profile_allow_editing) {
  2385. menu->add_icon_shortcut(get_theme_icon(SNAME("ActionCut"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/cut_node"), TOOL_CUT);
  2386. menu->add_icon_shortcut(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/copy_node"), TOOL_COPY);
  2387. if (selection.size() == 1 && !node_clipboard.is_empty()) {
  2388. menu->add_icon_shortcut(get_theme_icon(SNAME("ActionPaste"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/paste_node"), TOOL_PASTE);
  2389. }
  2390. menu->add_separator();
  2391. }
  2392. if (profile_allow_script_editing) {
  2393. bool add_separator = false;
  2394. if (full_selection.size() == 1) {
  2395. add_separator = true;
  2396. menu->add_icon_shortcut(get_theme_icon(SNAME("ScriptCreate"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/attach_script"), TOOL_ATTACH_SCRIPT);
  2397. if (existing_script.is_valid()) {
  2398. menu->add_icon_shortcut(get_theme_icon(SNAME("ScriptExtend"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/extend_script"), TOOL_EXTEND_SCRIPT);
  2399. }
  2400. }
  2401. if (existing_script.is_valid() && existing_script_removable) {
  2402. add_separator = true;
  2403. menu->add_icon_shortcut(get_theme_icon(SNAME("ScriptRemove"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
  2404. } else if (full_selection.size() > 1) {
  2405. bool script_exists = false;
  2406. for (Node *E : full_selection) {
  2407. if (!E->get_script().is_null()) {
  2408. script_exists = true;
  2409. break;
  2410. }
  2411. }
  2412. if (script_exists) {
  2413. add_separator = true;
  2414. menu->add_icon_shortcut(get_theme_icon(SNAME("ScriptRemove"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/detach_script"), TOOL_DETACH_SCRIPT);
  2415. }
  2416. }
  2417. if (add_separator && profile_allow_editing) {
  2418. menu->add_separator();
  2419. }
  2420. }
  2421. if (profile_allow_editing) {
  2422. bool add_separator = false;
  2423. if (full_selection.size() == 1) {
  2424. add_separator = true;
  2425. menu->add_icon_shortcut(get_theme_icon(SNAME("Rename"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/rename"), TOOL_RENAME);
  2426. }
  2427. bool can_replace = true;
  2428. for (Node *E : selection) {
  2429. if (E != edited_scene && (E->get_owner() != edited_scene || !E->get_scene_file_path().is_empty())) {
  2430. can_replace = false;
  2431. break;
  2432. }
  2433. }
  2434. if (can_replace) {
  2435. add_separator = true;
  2436. menu->add_icon_shortcut(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/change_node_type"), TOOL_REPLACE);
  2437. }
  2438. if (scene_tree->get_selected() != edited_scene) {
  2439. if (add_separator) {
  2440. menu->add_separator();
  2441. }
  2442. menu->add_icon_shortcut(get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/move_up"), TOOL_MOVE_UP);
  2443. menu->add_icon_shortcut(get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/move_down"), TOOL_MOVE_DOWN);
  2444. menu->add_icon_shortcut(get_theme_icon(SNAME("Duplicate"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/duplicate"), TOOL_DUPLICATE);
  2445. menu->add_icon_shortcut(get_theme_icon(SNAME("Reparent"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/reparent"), TOOL_REPARENT);
  2446. 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);
  2447. if (selection.size() == 1) {
  2448. menu->add_icon_shortcut(get_theme_icon(SNAME("NewRoot"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/make_root"), TOOL_MAKE_ROOT);
  2449. }
  2450. }
  2451. }
  2452. if (selection.size() == 1) {
  2453. if (profile_allow_editing) {
  2454. menu->add_separator();
  2455. menu->add_icon_shortcut(get_theme_icon(SNAME("CreateNewSceneFrom"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/save_branch_as_scene"), TOOL_NEW_SCENE_FROM);
  2456. }
  2457. if (full_selection.size() == 1) {
  2458. menu->add_separator();
  2459. menu->add_icon_shortcut(get_theme_icon(SNAME("CopyNodePath"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/copy_node_path"), TOOL_COPY_NODE_PATH);
  2460. }
  2461. }
  2462. if (profile_allow_editing) {
  2463. // Allow multi-toggling scene unique names but only if all selected nodes are owned by the edited scene root.
  2464. bool all_owned = true;
  2465. for (Node *node : full_selection) {
  2466. if (node->get_owner() != EditorNode::get_singleton()->get_edited_scene()) {
  2467. all_owned = false;
  2468. break;
  2469. }
  2470. }
  2471. if (all_owned) {
  2472. // Group "toggle_unique_name" with "copy_node_path", if it is available.
  2473. if (menu->get_item_index(TOOL_COPY_NODE_PATH) == -1) {
  2474. menu->add_separator();
  2475. }
  2476. Node *node = full_selection[0];
  2477. menu->add_icon_shortcut(get_theme_icon(SNAME("SceneUniqueName"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/toggle_unique_name"), TOOL_TOGGLE_SCENE_UNIQUE_NAME);
  2478. 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"));
  2479. }
  2480. }
  2481. if (selection.size() == 1) {
  2482. bool is_external = (!selection[0]->get_scene_file_path().is_empty());
  2483. if (is_external) {
  2484. bool is_inherited = selection[0]->get_scene_inherited_state() != nullptr;
  2485. bool is_top_level = selection[0]->get_owner() == nullptr;
  2486. if (is_inherited && is_top_level) {
  2487. menu->add_separator();
  2488. if (profile_allow_editing) {
  2489. menu->add_item(TTR("Clear Inheritance"), TOOL_SCENE_CLEAR_INHERITANCE);
  2490. }
  2491. menu->add_icon_item(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")), TTR("Open in Editor"), TOOL_SCENE_OPEN_INHERITED);
  2492. } else if (!is_top_level) {
  2493. menu->add_separator();
  2494. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(selection[0]);
  2495. bool placeholder = selection[0]->get_scene_instance_load_placeholder();
  2496. if (profile_allow_editing) {
  2497. menu->add_check_item(TTR("Editable Children"), TOOL_SCENE_EDITABLE_CHILDREN);
  2498. menu->add_check_item(TTR("Load As Placeholder"), TOOL_SCENE_USE_PLACEHOLDER);
  2499. menu->add_item(TTR("Make Local"), TOOL_SCENE_MAKE_LOCAL);
  2500. }
  2501. menu->add_icon_item(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")), TTR("Open in Editor"), TOOL_SCENE_OPEN);
  2502. if (profile_allow_editing) {
  2503. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Editable Children")), editable);
  2504. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Load As Placeholder")), placeholder);
  2505. }
  2506. }
  2507. }
  2508. }
  2509. #ifdef MODULE_REGEX_ENABLED
  2510. if (profile_allow_editing && selection.size() > 1) {
  2511. //this is not a commonly used action, it makes no sense for it to be where it was nor always present.
  2512. menu->add_separator();
  2513. menu->add_icon_shortcut(get_theme_icon(SNAME("Rename"), SNAME("EditorIcons")), ED_GET_SHORTCUT("scene_tree/batch_rename"), TOOL_BATCH_RENAME);
  2514. }
  2515. #endif // MODULE_REGEX_ENABLED
  2516. menu->add_separator();
  2517. menu->add_icon_item(get_theme_icon(SNAME("Help"), SNAME("EditorIcons")), TTR("Open Documentation"), TOOL_OPEN_DOCUMENTATION);
  2518. if (profile_allow_editing) {
  2519. menu->add_separator();
  2520. 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);
  2521. }
  2522. menu->reset_size();
  2523. menu->set_position(p_menu_pos);
  2524. menu->popup();
  2525. }
  2526. void SceneTreeDock::_update_tree_menu() {
  2527. PopupMenu *tree_menu = button_tree_menu->get_popup();
  2528. tree_menu->clear();
  2529. _append_filter_options_to(tree_menu);
  2530. tree_menu->add_separator();
  2531. tree_menu->add_check_item(TTR("Auto Expand to Selected"), TOOL_AUTO_EXPAND);
  2532. tree_menu->set_item_checked(tree_menu->get_item_index(TOOL_AUTO_EXPAND), EDITOR_GET("docks/scene_tree/auto_expand_to_selected"));
  2533. PopupMenu *resource_list = memnew(PopupMenu);
  2534. resource_list->set_name("AllResources");
  2535. resource_list->connect("about_to_popup", callable_mp(this, &SceneTreeDock::_list_all_subresources).bind(resource_list));
  2536. resource_list->connect("index_pressed", callable_mp(this, &SceneTreeDock::_edit_subresource).bind(resource_list));
  2537. tree_menu->add_child(resource_list);
  2538. tree_menu->add_submenu_item(TTR("All Scene Sub-Resources"), "AllResources");
  2539. }
  2540. void SceneTreeDock::_filter_changed(const String &p_filter) {
  2541. scene_tree->set_filter(p_filter);
  2542. String warning = scene_tree->get_filter_term_warning();
  2543. if (!warning.is_empty()) {
  2544. filter->add_theme_icon_override(SNAME("clear"), get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons")));
  2545. filter->set_tooltip_text(warning);
  2546. } else {
  2547. filter->remove_theme_icon_override(SNAME("clear"));
  2548. filter->set_tooltip_text("");
  2549. }
  2550. }
  2551. void SceneTreeDock::_filter_gui_input(const Ref<InputEvent> &p_event) {
  2552. Ref<InputEventMouseButton> mb = p_event;
  2553. if (mb.is_null()) {
  2554. return;
  2555. }
  2556. if (mb->is_pressed() && mb->get_button_index() == MouseButton::MIDDLE) {
  2557. filter_quick_menu->clear();
  2558. _append_filter_options_to(filter_quick_menu, false);
  2559. filter_quick_menu->set_position(get_screen_position() + get_local_mouse_position());
  2560. filter_quick_menu->reset_size();
  2561. filter_quick_menu->popup();
  2562. filter_quick_menu->grab_focus();
  2563. accept_event();
  2564. }
  2565. }
  2566. void SceneTreeDock::_filter_option_selected(int p_option) {
  2567. String filter_parameter;
  2568. switch (p_option) {
  2569. case FILTER_BY_TYPE: {
  2570. filter_parameter = "type";
  2571. } break;
  2572. case FILTER_BY_GROUP: {
  2573. filter_parameter = "group";
  2574. } break;
  2575. }
  2576. if (!filter_parameter.is_empty()) {
  2577. set_filter((get_filter() + " " + filter_parameter + ":").strip_edges());
  2578. filter->set_caret_column(filter->get_text().length());
  2579. filter->grab_focus();
  2580. }
  2581. }
  2582. void SceneTreeDock::_append_filter_options_to(PopupMenu *p_menu, bool p_include_separator) {
  2583. if (p_include_separator) {
  2584. p_menu->add_separator();
  2585. p_menu->set_item_text(-1, TTR("Filters"));
  2586. p_menu->set_item_indent(-1, -2);
  2587. }
  2588. p_menu->add_item(TTR("Filter by Type"), FILTER_BY_TYPE);
  2589. p_menu->add_item(TTR("Filter by Group"), FILTER_BY_GROUP);
  2590. p_menu->set_item_tooltip(p_menu->get_item_index(FILTER_BY_TYPE), TTR("Selects all Nodes of the given type."));
  2591. 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."));
  2592. }
  2593. String SceneTreeDock::get_filter() {
  2594. return filter->get_text();
  2595. }
  2596. void SceneTreeDock::set_filter(const String &p_filter) {
  2597. filter->set_text(p_filter);
  2598. scene_tree->set_filter(p_filter);
  2599. }
  2600. void SceneTreeDock::save_branch_to_file(String p_directory) {
  2601. new_scene_from_dialog->set_current_dir(p_directory);
  2602. _tool_selected(TOOL_NEW_SCENE_FROM);
  2603. }
  2604. void SceneTreeDock::_focus_node() {
  2605. Node *node = scene_tree->get_selected();
  2606. ERR_FAIL_COND(!node);
  2607. if (node->is_class("CanvasItem")) {
  2608. CanvasItemEditorPlugin *editor = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  2609. editor->get_canvas_item_editor()->focus_selection();
  2610. } else {
  2611. Node3DEditorPlugin *editor = Object::cast_to<Node3DEditorPlugin>(editor_data->get_editor("3D"));
  2612. editor->get_spatial_editor()->get_editor_viewport(0)->focus_selection();
  2613. }
  2614. }
  2615. void SceneTreeDock::attach_script_to_selected(bool p_extend) {
  2616. if (ScriptServer::get_language_count() == 0) {
  2617. 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."));
  2618. return;
  2619. }
  2620. if (!profile_allow_script_editing) {
  2621. return;
  2622. }
  2623. List<Node *> selection = editor_selection->get_selected_node_list();
  2624. if (selection.is_empty()) {
  2625. return;
  2626. }
  2627. Node *selected = scene_tree->get_selected();
  2628. if (!selected) {
  2629. selected = selection.front()->get();
  2630. }
  2631. Ref<Script> existing = selected->get_script();
  2632. String path = selected->get_scene_file_path();
  2633. if (path.is_empty()) {
  2634. String root_path = editor_data->get_edited_scene_root()->get_scene_file_path();
  2635. if (root_path.is_empty()) {
  2636. path = String("res://").path_join(selected->get_name());
  2637. } else {
  2638. path = root_path.get_base_dir().path_join(selected->get_name());
  2639. }
  2640. }
  2641. String inherits = selected->get_class();
  2642. if (p_extend && existing.is_valid()) {
  2643. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2644. ScriptLanguage *l = ScriptServer::get_language(i);
  2645. if (l->get_type() == existing->get_class()) {
  2646. String name = l->get_global_class_name(existing->get_path());
  2647. if (ScriptServer::is_global_class(name) && EDITOR_GET("interface/editors/derive_script_globals_by_name").operator bool()) {
  2648. inherits = name;
  2649. } else if (l->can_inherit_from_file()) {
  2650. inherits = "\"" + existing->get_path() + "\"";
  2651. }
  2652. break;
  2653. }
  2654. }
  2655. }
  2656. script_create_dialog->connect("script_created", callable_mp(this, &SceneTreeDock::_script_created));
  2657. script_create_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_script_creation_closed));
  2658. script_create_dialog->connect("canceled", callable_mp(this, &SceneTreeDock::_script_creation_closed));
  2659. script_create_dialog->set_inheritance_base_type("Node");
  2660. script_create_dialog->config(inherits, path);
  2661. script_create_dialog->popup_centered();
  2662. }
  2663. void SceneTreeDock::open_script_dialog(Node *p_for_node, bool p_extend) {
  2664. scene_tree->set_selected(p_for_node, false);
  2665. if (p_extend) {
  2666. _tool_selected(TOOL_EXTEND_SCRIPT);
  2667. } else {
  2668. _tool_selected(TOOL_ATTACH_SCRIPT);
  2669. }
  2670. }
  2671. void SceneTreeDock::attach_shader_to_selected(int p_preferred_mode) {
  2672. if (selected_shader_material.is_null()) {
  2673. return;
  2674. }
  2675. String path = selected_shader_material->get_path();
  2676. if (path.is_empty()) {
  2677. String root_path;
  2678. if (editor_data->get_edited_scene_root()) {
  2679. root_path = editor_data->get_edited_scene_root()->get_scene_file_path();
  2680. }
  2681. String shader_name;
  2682. if (selected_shader_material->get_name().is_empty()) {
  2683. shader_name = root_path.get_file();
  2684. } else {
  2685. shader_name = selected_shader_material->get_name();
  2686. }
  2687. if (root_path.is_empty()) {
  2688. path = String("res://").path_join(shader_name);
  2689. } else {
  2690. path = root_path.get_base_dir().path_join(shader_name);
  2691. }
  2692. }
  2693. shader_create_dialog->connect("shader_created", callable_mp(this, &SceneTreeDock::_shader_created));
  2694. shader_create_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_shader_creation_closed));
  2695. shader_create_dialog->connect("canceled", callable_mp(this, &SceneTreeDock::_shader_creation_closed));
  2696. shader_create_dialog->config(path, true, true, -1, p_preferred_mode);
  2697. shader_create_dialog->popup_centered();
  2698. }
  2699. void SceneTreeDock::open_shader_dialog(const Ref<ShaderMaterial> &p_for_material, int p_preferred_mode) {
  2700. selected_shader_material = p_for_material;
  2701. attach_shader_to_selected(p_preferred_mode);
  2702. }
  2703. void SceneTreeDock::open_add_child_dialog() {
  2704. create_dialog->set_base_type("CanvasItem");
  2705. _tool_selected(TOOL_NEW, true);
  2706. reset_create_dialog = true;
  2707. }
  2708. void SceneTreeDock::open_instance_child_dialog() {
  2709. _tool_selected(TOOL_INSTANTIATE, true);
  2710. }
  2711. List<Node *> SceneTreeDock::paste_nodes() {
  2712. List<Node *> pasted_nodes;
  2713. if (node_clipboard.is_empty()) {
  2714. return pasted_nodes;
  2715. }
  2716. bool has_cycle = false;
  2717. if (edited_scene && !edited_scene->get_scene_file_path().is_empty()) {
  2718. for (Node *E : node_clipboard) {
  2719. if (edited_scene->get_scene_file_path() == E->get_scene_file_path()) {
  2720. has_cycle = true;
  2721. break;
  2722. }
  2723. }
  2724. }
  2725. if (has_cycle) {
  2726. current_option = -1;
  2727. accept->set_text(TTR("Can't paste root node into the same scene."));
  2728. accept->popup_centered();
  2729. return pasted_nodes;
  2730. }
  2731. Node *paste_parent = edited_scene;
  2732. List<Node *> selection = editor_selection->get_selected_node_list();
  2733. if (selection.size() > 0) {
  2734. paste_parent = selection.back()->get();
  2735. }
  2736. Node *owner = nullptr;
  2737. if (paste_parent) {
  2738. owner = paste_parent->get_owner();
  2739. }
  2740. if (!owner) {
  2741. owner = paste_parent;
  2742. }
  2743. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2744. ur->create_action(TTR("Paste Node(s)"), UndoRedo::MERGE_DISABLE, EditorNode::get_singleton()->get_edited_scene());
  2745. ur->add_do_method(editor_selection, "clear");
  2746. HashMap<Ref<Resource>, Ref<Resource>> resource_remap;
  2747. String target_scene;
  2748. if (edited_scene) {
  2749. target_scene = edited_scene->get_scene_file_path();
  2750. }
  2751. if (target_scene != clipboard_source_scene) {
  2752. if (!clipboard_resource_remap.has(target_scene)) {
  2753. HashMap<Ref<Resource>, Ref<Resource>> remap;
  2754. for (Node *E : node_clipboard) {
  2755. _create_remap_for_node(E, remap);
  2756. }
  2757. clipboard_resource_remap[target_scene] = remap;
  2758. }
  2759. resource_remap = clipboard_resource_remap[target_scene];
  2760. }
  2761. for (Node *node : node_clipboard) {
  2762. HashMap<const Node *, Node *> duplimap;
  2763. Node *dup = node->duplicate_from_editor(duplimap, resource_remap);
  2764. ERR_CONTINUE(!dup);
  2765. pasted_nodes.push_back(dup);
  2766. if (!paste_parent) {
  2767. paste_parent = dup;
  2768. owner = dup;
  2769. dup->set_scene_file_path(String()); // Make sure the scene path is empty, to avoid accidental references.
  2770. ur->add_do_method(EditorNode::get_singleton(), "set_edited_scene", dup);
  2771. } else {
  2772. ur->add_do_method(paste_parent, "add_child", dup, true);
  2773. }
  2774. for (KeyValue<const Node *, Node *> &E2 : duplimap) {
  2775. Node *d = E2.value;
  2776. // When copying, all nodes that should have an owner assigned here were given node as an owner.
  2777. if (d != dup && E2.key->get_owner() == node) {
  2778. ur->add_do_method(d, "set_owner", owner);
  2779. }
  2780. }
  2781. if (dup != owner) {
  2782. ur->add_do_method(dup, "set_owner", owner);
  2783. }
  2784. ur->add_do_method(editor_selection, "add_node", dup);
  2785. if (dup == paste_parent) {
  2786. ur->add_undo_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);
  2787. } else {
  2788. ur->add_undo_method(paste_parent, "remove_child", dup);
  2789. }
  2790. ur->add_do_reference(dup);
  2791. if (node_clipboard.size() == 1) {
  2792. ur->add_do_method(EditorNode::get_singleton(), "push_item", dup);
  2793. }
  2794. }
  2795. ur->commit_action();
  2796. return pasted_nodes;
  2797. }
  2798. List<Node *> SceneTreeDock::get_node_clipboard() const {
  2799. return node_clipboard;
  2800. }
  2801. void SceneTreeDock::add_remote_tree_editor(Control *p_remote) {
  2802. ERR_FAIL_COND(remote_tree != nullptr);
  2803. add_child(p_remote);
  2804. remote_tree = p_remote;
  2805. remote_tree->hide();
  2806. remote_tree->connect("open", callable_mp(this, &SceneTreeDock::_load_request));
  2807. }
  2808. void SceneTreeDock::show_remote_tree() {
  2809. _remote_tree_selected();
  2810. }
  2811. void SceneTreeDock::hide_remote_tree() {
  2812. _local_tree_selected();
  2813. }
  2814. void SceneTreeDock::show_tab_buttons() {
  2815. button_hb->show();
  2816. }
  2817. void SceneTreeDock::hide_tab_buttons() {
  2818. button_hb->hide();
  2819. }
  2820. void SceneTreeDock::_remote_tree_selected() {
  2821. scene_tree->hide();
  2822. create_root_dialog->hide();
  2823. if (remote_tree) {
  2824. remote_tree->show();
  2825. }
  2826. edit_remote->set_pressed(true);
  2827. edit_local->set_pressed(false);
  2828. emit_signal(SNAME("remote_tree_selected"));
  2829. }
  2830. void SceneTreeDock::_local_tree_selected() {
  2831. if (!bool(EDITOR_GET("interface/editors/show_scene_tree_root_selection")) || get_tree()->get_edited_scene_root() != nullptr) {
  2832. scene_tree->show();
  2833. }
  2834. if (remote_tree) {
  2835. remote_tree->hide();
  2836. }
  2837. edit_remote->set_pressed(false);
  2838. edit_local->set_pressed(true);
  2839. }
  2840. void SceneTreeDock::_update_create_root_dialog() {
  2841. EditorSettings::get_singleton()->set_setting("_use_favorites_root_selection", node_shortcuts_toggle->is_pressed());
  2842. EditorSettings::get_singleton()->save();
  2843. if (node_shortcuts_toggle->is_pressed()) {
  2844. for (int i = 0; i < favorite_node_shortcuts->get_child_count(); i++) {
  2845. favorite_node_shortcuts->get_child(i)->queue_free();
  2846. }
  2847. Ref<FileAccess> f = FileAccess::open(EditorPaths::get_singleton()->get_project_settings_dir().path_join("favorites.Node"), FileAccess::READ);
  2848. if (f.is_valid()) {
  2849. while (!f->eof_reached()) {
  2850. String l = f->get_line().strip_edges();
  2851. if (!l.is_empty()) {
  2852. Button *button = memnew(Button);
  2853. favorite_node_shortcuts->add_child(button);
  2854. button->set_text(l);
  2855. button->set_clip_text(true);
  2856. String name = l.get_slicec(' ', 0);
  2857. if (ScriptServer::is_global_class(name)) {
  2858. name = ScriptServer::get_global_class_native_base(name);
  2859. }
  2860. button->set_icon(EditorNode::get_singleton()->get_class_icon(name));
  2861. button->connect("pressed", callable_mp(this, &SceneTreeDock::_favorite_root_selected).bind(l));
  2862. }
  2863. }
  2864. }
  2865. if (!favorite_node_shortcuts->is_visible_in_tree()) {
  2866. favorite_node_shortcuts->show();
  2867. beginner_node_shortcuts->hide();
  2868. }
  2869. } else {
  2870. if (!beginner_node_shortcuts->is_visible_in_tree()) {
  2871. beginner_node_shortcuts->show();
  2872. favorite_node_shortcuts->hide();
  2873. }
  2874. button_clipboard->set_visible(!node_clipboard.is_empty());
  2875. }
  2876. }
  2877. void SceneTreeDock::_favorite_root_selected(const String &p_class) {
  2878. selected_favorite_root = p_class;
  2879. _tool_selected(TOOL_CREATE_FAVORITE);
  2880. }
  2881. void SceneTreeDock::_feature_profile_changed() {
  2882. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  2883. if (profile.is_valid()) {
  2884. profile_allow_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCENE_TREE);
  2885. profile_allow_script_editing = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT);
  2886. bool profile_allow_3d = !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D);
  2887. button_3d->set_visible(profile_allow_3d);
  2888. button_add->set_visible(profile_allow_editing);
  2889. button_instance->set_visible(profile_allow_editing);
  2890. scene_tree->set_can_rename(profile_allow_editing);
  2891. } else {
  2892. button_3d->set_visible(true);
  2893. button_add->set_visible(true);
  2894. button_instance->set_visible(true);
  2895. scene_tree->set_can_rename(true);
  2896. profile_allow_editing = true;
  2897. profile_allow_script_editing = true;
  2898. }
  2899. _update_script_button();
  2900. }
  2901. void SceneTreeDock::_clear_clipboard() {
  2902. for (Node *E : node_clipboard) {
  2903. memdelete(E);
  2904. }
  2905. node_clipboard.clear();
  2906. clipboard_resource_remap.clear();
  2907. }
  2908. void SceneTreeDock::_create_remap_for_node(Node *p_node, HashMap<Ref<Resource>, Ref<Resource>> &r_remap) {
  2909. List<PropertyInfo> props;
  2910. p_node->get_property_list(&props);
  2911. Vector<SceneState::PackState> states_stack;
  2912. bool states_stack_ready = false;
  2913. for (const PropertyInfo &E : props) {
  2914. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  2915. continue;
  2916. }
  2917. Variant v = p_node->get(E.name);
  2918. if (v.is_ref_counted()) {
  2919. Ref<Resource> res = v;
  2920. if (res.is_valid()) {
  2921. if (!states_stack_ready) {
  2922. states_stack = PropertyUtils::get_node_states_stack(p_node);
  2923. states_stack_ready = true;
  2924. }
  2925. bool is_valid_default = false;
  2926. Variant orig = PropertyUtils::get_property_default_value(p_node, E.name, &is_valid_default, &states_stack);
  2927. if (is_valid_default && !PropertyUtils::is_property_value_different(v, orig)) {
  2928. continue;
  2929. }
  2930. if (res->is_built_in() && !r_remap.has(res)) {
  2931. _create_remap_for_resource(res, r_remap);
  2932. }
  2933. }
  2934. }
  2935. }
  2936. for (int i = 0; i < p_node->get_child_count(); i++) {
  2937. _create_remap_for_node(p_node->get_child(i), r_remap);
  2938. }
  2939. }
  2940. void SceneTreeDock::_create_remap_for_resource(Ref<Resource> p_resource, HashMap<Ref<Resource>, Ref<Resource>> &r_remap) {
  2941. r_remap[p_resource] = p_resource->duplicate();
  2942. List<PropertyInfo> props;
  2943. p_resource->get_property_list(&props);
  2944. for (const PropertyInfo &E : props) {
  2945. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  2946. continue;
  2947. }
  2948. Variant v = p_resource->get(E.name);
  2949. if (v.is_ref_counted()) {
  2950. Ref<Resource> res = v;
  2951. if (res.is_valid()) {
  2952. if (res->is_built_in() && !r_remap.has(res)) {
  2953. _create_remap_for_resource(res, r_remap);
  2954. }
  2955. }
  2956. }
  2957. }
  2958. }
  2959. void SceneTreeDock::_list_all_subresources(PopupMenu *p_menu) {
  2960. p_menu->clear();
  2961. List<Pair<Ref<Resource>, Node *>> all_resources;
  2962. if (edited_scene) {
  2963. _gather_resources(edited_scene, all_resources);
  2964. }
  2965. HashMap<String, List<Pair<Ref<Resource>, Node *>>> resources_by_type;
  2966. HashMap<Ref<Resource>, int> unique_resources;
  2967. for (const Pair<Ref<Resource>, Node *> &pair : all_resources) {
  2968. if (!unique_resources.has(pair.first)) {
  2969. resources_by_type[pair.first->get_class()].push_back(pair);
  2970. }
  2971. unique_resources[pair.first]++;
  2972. }
  2973. for (KeyValue<String, List<Pair<Ref<Resource>, Node *>>> kv : resources_by_type) {
  2974. p_menu->add_icon_item(EditorNode::get_singleton()->get_class_icon(kv.key), kv.key);
  2975. p_menu->set_item_as_separator(-1, true);
  2976. for (const Pair<Ref<Resource>, Node *> &pair : kv.value) {
  2977. String display_text;
  2978. if (pair.first->get_name().is_empty()) {
  2979. display_text = vformat(TTR("<Unnamed> at %s"), pair.second->get_name());
  2980. } else {
  2981. display_text = pair.first->get_name();
  2982. }
  2983. if (unique_resources[pair.first] > 1) {
  2984. display_text += " " + vformat(TTR("(used %d times)"), unique_resources[pair.first]);
  2985. }
  2986. p_menu->add_item(display_text);
  2987. p_menu->set_item_metadata(-1, pair.first->get_instance_id());
  2988. }
  2989. }
  2990. if (resources_by_type.is_empty()) {
  2991. p_menu->add_item(TTR("None"));
  2992. p_menu->set_item_disabled(-1, true);
  2993. }
  2994. p_menu->reset_size();
  2995. }
  2996. void SceneTreeDock::_gather_resources(Node *p_node, List<Pair<Ref<Resource>, Node *>> &r_resources) {
  2997. if (p_node != edited_scene && p_node->get_owner() != edited_scene) {
  2998. return;
  2999. }
  3000. List<PropertyInfo> pinfo;
  3001. p_node->get_property_list(&pinfo);
  3002. for (const PropertyInfo &E : pinfo) {
  3003. if (!(E.usage & PROPERTY_USAGE_EDITOR)) {
  3004. continue;
  3005. }
  3006. if (E.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  3007. continue;
  3008. }
  3009. Variant value = p_node->get(E.name);
  3010. if (value.get_type() != Variant::OBJECT) {
  3011. continue;
  3012. }
  3013. Ref<Resource> res = value;
  3014. if (res.is_null()) {
  3015. continue;
  3016. }
  3017. if (!res->is_built_in() || res->get_path().get_slice("::", 0) != edited_scene->get_scene_file_path()) {
  3018. // Ignore external and foreign resources.
  3019. continue;
  3020. }
  3021. const Pair<Ref<Resource>, Node *> pair(res, p_node);
  3022. r_resources.push_back(pair);
  3023. }
  3024. for (int i = 0; i < p_node->get_child_count(); i++) {
  3025. _gather_resources(p_node->get_child(i), r_resources);
  3026. }
  3027. }
  3028. void SceneTreeDock::_edit_subresource(int p_idx, const PopupMenu *p_from_menu) {
  3029. const ObjectID &id = p_from_menu->get_item_metadata(p_idx);
  3030. Object *obj = ObjectDB::get_instance(id);
  3031. ERR_FAIL_COND(!obj);
  3032. _push_item(obj);
  3033. }
  3034. void SceneTreeDock::_bind_methods() {
  3035. ClassDB::bind_method(D_METHOD("_set_owners"), &SceneTreeDock::_set_owners);
  3036. ClassDB::bind_method(D_METHOD("_update_script_button"), &SceneTreeDock::_update_script_button);
  3037. ClassDB::bind_method(D_METHOD("instantiate"), &SceneTreeDock::instantiate);
  3038. ClassDB::bind_method(D_METHOD("get_tree_editor"), &SceneTreeDock::get_tree_editor);
  3039. ClassDB::bind_method(D_METHOD("replace_node"), &SceneTreeDock::replace_node);
  3040. ADD_SIGNAL(MethodInfo("remote_tree_selected"));
  3041. ADD_SIGNAL(MethodInfo("add_node_used"));
  3042. ADD_SIGNAL(MethodInfo("node_created", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  3043. }
  3044. SceneTreeDock *SceneTreeDock::singleton = nullptr;
  3045. void SceneTreeDock::_update_configuration_warning() {
  3046. if (singleton) {
  3047. MessageQueue::get_singleton()->push_callable(callable_mp(singleton->scene_tree, &SceneTreeEditor::update_warning));
  3048. }
  3049. }
  3050. SceneTreeDock::SceneTreeDock(Node *p_scene_root, EditorSelection *p_editor_selection, EditorData &p_editor_data) {
  3051. singleton = this;
  3052. set_name("Scene");
  3053. edited_scene = nullptr;
  3054. editor_data = &p_editor_data;
  3055. editor_selection = p_editor_selection;
  3056. scene_root = p_scene_root;
  3057. VBoxContainer *vbc = this;
  3058. HBoxContainer *filter_hbc = memnew(HBoxContainer);
  3059. filter_hbc->add_theme_constant_override("separate", 0);
  3060. ED_SHORTCUT("scene_tree/rename", TTR("Rename"), Key::F2);
  3061. ED_SHORTCUT_OVERRIDE("scene_tree/rename", "macos", Key::ENTER);
  3062. ED_SHORTCUT("scene_tree/batch_rename", TTR("Batch Rename"), KeyModifierMask::SHIFT | Key::F2);
  3063. ED_SHORTCUT_OVERRIDE("scene_tree/batch_rename", "macos", KeyModifierMask::SHIFT | Key::ENTER);
  3064. ED_SHORTCUT("scene_tree/add_child_node", TTR("Add Child Node"), KeyModifierMask::CMD_OR_CTRL | Key::A);
  3065. ED_SHORTCUT("scene_tree/instantiate_scene", TTR("Instantiate Child Scene"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::A);
  3066. ED_SHORTCUT("scene_tree/expand_collapse_all", TTR("Expand/Collapse Branch"));
  3067. ED_SHORTCUT("scene_tree/cut_node", TTR("Cut"), KeyModifierMask::CMD_OR_CTRL | Key::X);
  3068. ED_SHORTCUT("scene_tree/copy_node", TTR("Copy"), KeyModifierMask::CMD_OR_CTRL | Key::C);
  3069. ED_SHORTCUT("scene_tree/paste_node", TTR("Paste"), KeyModifierMask::CMD_OR_CTRL | Key::V);
  3070. ED_SHORTCUT("scene_tree/change_node_type", TTR("Change Type"));
  3071. ED_SHORTCUT("scene_tree/attach_script", TTR("Attach Script"));
  3072. ED_SHORTCUT("scene_tree/extend_script", TTR("Extend Script"));
  3073. ED_SHORTCUT("scene_tree/detach_script", TTR("Detach Script"));
  3074. ED_SHORTCUT("scene_tree/move_up", TTR("Move Up"), KeyModifierMask::CMD_OR_CTRL | Key::UP);
  3075. ED_SHORTCUT("scene_tree/move_down", TTR("Move Down"), KeyModifierMask::CMD_OR_CTRL | Key::DOWN);
  3076. ED_SHORTCUT("scene_tree/duplicate", TTR("Duplicate"), KeyModifierMask::CMD_OR_CTRL | Key::D);
  3077. ED_SHORTCUT("scene_tree/reparent", TTR("Reparent"));
  3078. ED_SHORTCUT("scene_tree/reparent_to_new_node", TTR("Reparent to New Node"));
  3079. ED_SHORTCUT("scene_tree/make_root", TTR("Make Scene Root"));
  3080. ED_SHORTCUT("scene_tree/save_branch_as_scene", TTR("Save Branch as Scene"));
  3081. ED_SHORTCUT("scene_tree/copy_node_path", TTR("Copy Node Path"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::C);
  3082. ED_SHORTCUT("scene_tree/toggle_unique_name", TTR("Toggle Access as Unique Name"));
  3083. ED_SHORTCUT("scene_tree/delete_no_confirm", TTR("Delete (No Confirm)"), KeyModifierMask::SHIFT | Key::KEY_DELETE);
  3084. ED_SHORTCUT("scene_tree/delete", TTR("Delete"), Key::KEY_DELETE);
  3085. button_add = memnew(Button);
  3086. button_add->set_flat(true);
  3087. button_add->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected).bind(TOOL_NEW, false));
  3088. button_add->set_tooltip_text(TTR("Add/Create a New Node."));
  3089. button_add->set_shortcut(ED_GET_SHORTCUT("scene_tree/add_child_node"));
  3090. filter_hbc->add_child(button_add);
  3091. button_instance = memnew(Button);
  3092. button_instance->set_flat(true);
  3093. button_instance->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected).bind(TOOL_INSTANTIATE, false));
  3094. button_instance->set_tooltip_text(TTR("Instantiate a scene file as a Node. Creates an inherited scene if no root node exists."));
  3095. button_instance->set_shortcut(ED_GET_SHORTCUT("scene_tree/instantiate_scene"));
  3096. filter_hbc->add_child(button_instance);
  3097. vbc->add_child(filter_hbc);
  3098. // The "Filter Nodes" text input above the Scene Tree Editor.
  3099. filter = memnew(LineEdit);
  3100. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  3101. filter->set_placeholder(TTR("Filter Nodes"));
  3102. filter_hbc->add_child(filter);
  3103. filter->add_theme_constant_override("minimum_character_width", 0);
  3104. filter->connect("text_changed", callable_mp(this, &SceneTreeDock::_filter_changed));
  3105. filter->connect("gui_input", callable_mp(this, &SceneTreeDock::_filter_gui_input));
  3106. filter->get_menu()->connect("id_pressed", callable_mp(this, &SceneTreeDock::_filter_option_selected));
  3107. _append_filter_options_to(filter->get_menu());
  3108. filter_quick_menu = memnew(PopupMenu);
  3109. filter_quick_menu->connect("id_pressed", callable_mp(this, &SceneTreeDock::_filter_option_selected));
  3110. filter->add_child(filter_quick_menu);
  3111. button_create_script = memnew(Button);
  3112. button_create_script->set_flat(true);
  3113. button_create_script->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected).bind(TOOL_ATTACH_SCRIPT, false));
  3114. button_create_script->set_tooltip_text(TTR("Attach a new or existing script to the selected node."));
  3115. button_create_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/attach_script"));
  3116. filter_hbc->add_child(button_create_script);
  3117. button_create_script->hide();
  3118. button_detach_script = memnew(Button);
  3119. button_detach_script->set_flat(true);
  3120. button_detach_script->connect("pressed", callable_mp(this, &SceneTreeDock::_tool_selected).bind(TOOL_DETACH_SCRIPT, false));
  3121. button_detach_script->set_tooltip_text(TTR("Detach the script from the selected node."));
  3122. button_detach_script->set_shortcut(ED_GET_SHORTCUT("scene_tree/detach_script"));
  3123. filter_hbc->add_child(button_detach_script);
  3124. button_detach_script->hide();
  3125. button_tree_menu = memnew(MenuButton);
  3126. button_tree_menu->set_flat(true);
  3127. button_tree_menu->set_tooltip_text(TTR("Extra scene options."));
  3128. button_tree_menu->connect("about_to_popup", callable_mp(this, &SceneTreeDock::_update_tree_menu));
  3129. filter_hbc->add_child(button_tree_menu);
  3130. PopupMenu *tree_menu = button_tree_menu->get_popup();
  3131. tree_menu->connect("id_pressed", callable_mp(this, &SceneTreeDock::_tool_selected).bind(false));
  3132. button_hb = memnew(HBoxContainer);
  3133. vbc->add_child(button_hb);
  3134. edit_remote = memnew(Button);
  3135. edit_remote->set_flat(true);
  3136. button_hb->add_child(edit_remote);
  3137. edit_remote->set_h_size_flags(SIZE_EXPAND_FILL);
  3138. edit_remote->set_text(TTR("Remote"));
  3139. edit_remote->set_toggle_mode(true);
  3140. 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."));
  3141. edit_remote->connect("pressed", callable_mp(this, &SceneTreeDock::_remote_tree_selected));
  3142. edit_local = memnew(Button);
  3143. edit_local->set_flat(true);
  3144. button_hb->add_child(edit_local);
  3145. edit_local->set_h_size_flags(SIZE_EXPAND_FILL);
  3146. edit_local->set_text(TTR("Local"));
  3147. edit_local->set_toggle_mode(true);
  3148. edit_local->set_pressed(true);
  3149. edit_local->connect("pressed", callable_mp(this, &SceneTreeDock::_local_tree_selected));
  3150. remote_tree = nullptr;
  3151. button_hb->hide();
  3152. create_root_dialog = memnew(VBoxContainer);
  3153. vbc->add_child(create_root_dialog);
  3154. create_root_dialog->set_v_size_flags(SIZE_EXPAND_FILL);
  3155. create_root_dialog->hide();
  3156. scene_tree = memnew(SceneTreeEditor(false, true, true));
  3157. vbc->add_child(scene_tree);
  3158. scene_tree->set_v_size_flags(SIZE_EXPAND | SIZE_FILL);
  3159. scene_tree->connect("rmb_pressed", callable_mp(this, &SceneTreeDock::_tree_rmb));
  3160. scene_tree->connect("node_selected", callable_mp(this, &SceneTreeDock::_node_selected), CONNECT_DEFERRED);
  3161. scene_tree->connect("node_renamed", callable_mp(this, &SceneTreeDock::_node_renamed), CONNECT_DEFERRED);
  3162. scene_tree->connect("node_prerename", callable_mp(this, &SceneTreeDock::_node_prerenamed));
  3163. scene_tree->connect("open", callable_mp(this, &SceneTreeDock::_load_request));
  3164. scene_tree->connect("open_script", callable_mp(this, &SceneTreeDock::_script_open_request));
  3165. scene_tree->connect("nodes_rearranged", callable_mp(this, &SceneTreeDock::_nodes_dragged));
  3166. scene_tree->connect("files_dropped", callable_mp(this, &SceneTreeDock::_files_dropped));
  3167. scene_tree->connect("script_dropped", callable_mp(this, &SceneTreeDock::_script_dropped));
  3168. scene_tree->connect("nodes_dragged", callable_mp(this, &SceneTreeDock::_nodes_drag_begin));
  3169. scene_tree->get_scene_tree()->connect("item_icon_double_clicked", callable_mp(this, &SceneTreeDock::_focus_node));
  3170. editor_selection->connect("selection_changed", callable_mp(this, &SceneTreeDock::_selection_changed));
  3171. scene_tree->set_as_scene_tree_dock();
  3172. scene_tree->set_editor_selection(editor_selection);
  3173. create_dialog = memnew(CreateDialog);
  3174. create_dialog->set_base_type("Node");
  3175. add_child(create_dialog);
  3176. create_dialog->connect("create", callable_mp(this, &SceneTreeDock::_create));
  3177. create_dialog->connect("favorites_updated", callable_mp(this, &SceneTreeDock::_update_create_root_dialog));
  3178. #ifdef MODULE_REGEX_ENABLED
  3179. rename_dialog = memnew(RenameDialog(scene_tree));
  3180. add_child(rename_dialog);
  3181. #endif // MODULE_REGEX_ENABLED
  3182. script_create_dialog = memnew(ScriptCreateDialog);
  3183. script_create_dialog->set_inheritance_base_type("Node");
  3184. add_child(script_create_dialog);
  3185. shader_create_dialog = memnew(ShaderCreateDialog);
  3186. add_child(shader_create_dialog);
  3187. reparent_dialog = memnew(ReparentDialog);
  3188. add_child(reparent_dialog);
  3189. reparent_dialog->connect("reparent", callable_mp(this, &SceneTreeDock::_node_reparent));
  3190. accept = memnew(AcceptDialog);
  3191. add_child(accept);
  3192. quick_open = memnew(EditorQuickOpen);
  3193. add_child(quick_open);
  3194. quick_open->connect("quick_open", callable_mp(this, &SceneTreeDock::_quick_open));
  3195. set_process_shortcut_input(true);
  3196. delete_dialog = memnew(ConfirmationDialog);
  3197. add_child(delete_dialog);
  3198. delete_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_delete_confirm).bind(false));
  3199. VBoxContainer *vb = memnew(VBoxContainer);
  3200. delete_dialog->add_child(vb);
  3201. delete_dialog_label = memnew(Label);
  3202. vb->add_child(delete_dialog_label);
  3203. delete_tracks_checkbox = memnew(CheckBox(TTR("Delete Related Animation Tracks")));
  3204. delete_tracks_checkbox->set_pressed(true);
  3205. vb->add_child(delete_tracks_checkbox);
  3206. editable_instance_remove_dialog = memnew(ConfirmationDialog);
  3207. add_child(editable_instance_remove_dialog);
  3208. editable_instance_remove_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_toggle_editable_children_from_selection));
  3209. placeholder_editable_instance_remove_dialog = memnew(ConfirmationDialog);
  3210. add_child(placeholder_editable_instance_remove_dialog);
  3211. placeholder_editable_instance_remove_dialog->connect("confirmed", callable_mp(this, &SceneTreeDock::_toggle_placeholder_from_selection));
  3212. new_scene_from_dialog = memnew(EditorFileDialog);
  3213. new_scene_from_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  3214. add_child(new_scene_from_dialog);
  3215. new_scene_from_dialog->connect("file_selected", callable_mp(this, &SceneTreeDock::_new_scene_from));
  3216. menu = memnew(PopupMenu);
  3217. add_child(menu);
  3218. menu->connect("id_pressed", callable_mp(this, &SceneTreeDock::_tool_selected).bind(false));
  3219. menu_subresources = memnew(PopupMenu);
  3220. menu_subresources->set_name("Sub-Resources");
  3221. menu_subresources->connect("id_pressed", callable_mp(this, &SceneTreeDock::_tool_selected).bind(false));
  3222. menu->add_child(menu_subresources);
  3223. menu_properties = memnew(PopupMenu);
  3224. add_child(menu_properties);
  3225. menu_properties->connect("id_pressed", callable_mp(this, &SceneTreeDock::_property_selected));
  3226. clear_inherit_confirm = memnew(ConfirmationDialog);
  3227. clear_inherit_confirm->set_text(TTR("Clear Inheritance? (No Undo!)"));
  3228. clear_inherit_confirm->set_ok_button_text(TTR("Clear"));
  3229. add_child(clear_inherit_confirm);
  3230. set_process_input(true);
  3231. set_process(true);
  3232. EDITOR_DEF("interface/editors/show_scene_tree_root_selection", true);
  3233. EDITOR_DEF("interface/editors/derive_script_globals_by_name", true);
  3234. EDITOR_DEF("docks/scene_tree/ask_before_deleting_related_animation_tracks", true);
  3235. EDITOR_DEF("_use_favorites_root_selection", false);
  3236. Resource::_update_configuration_warning = _update_configuration_warning;
  3237. }
  3238. SceneTreeDock::~SceneTreeDock() {
  3239. singleton = nullptr;
  3240. if (!node_clipboard.is_empty()) {
  3241. _clear_clipboard();
  3242. }
  3243. }