scene_tree_dock.cpp 84 KB

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