scene_tree_dock.cpp 125 KB

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