scene_tree_dock.cpp 108 KB

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