scene_tree_dock.cpp 113 KB

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