scene_tree_dock.cpp 123 KB

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