scene_tree_dock.cpp 97 KB

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