scene_tree_dock.cpp 100 KB

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