scene_tree_dock.cpp 107 KB

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