scene_tree_dock.cpp 96 KB

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