scene_tree_dock.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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/animation_editor.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/multi_node_edit.h"
  38. #include "editor/plugins/animation_player_editor_plugin.h"
  39. #include "editor/plugins/canvas_item_editor_plugin.h"
  40. #include "editor/plugins/script_editor_plugin.h"
  41. #include "editor/plugins/spatial_editor_plugin.h"
  42. #include "editor/script_editor_debugger.h"
  43. #include "scene/main/viewport.h"
  44. #include "scene/resources/packed_scene.h"
  45. void SceneTreeDock::_nodes_drag_begin() {
  46. if (restore_script_editor_on_drag) {
  47. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  48. restore_script_editor_on_drag = false;
  49. }
  50. }
  51. void SceneTreeDock::_input(Ref<InputEvent> p_event) {
  52. Ref<InputEventMouseButton> mb = p_event;
  53. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  54. restore_script_editor_on_drag = false; //lost chance
  55. }
  56. }
  57. void SceneTreeDock::_unhandled_key_input(Ref<InputEvent> p_event) {
  58. if (get_viewport()->get_modal_stack_top())
  59. return; //ignore because of modal window
  60. if (!p_event->is_pressed() || p_event->is_echo())
  61. return;
  62. if (ED_IS_SHORTCUT("scene_tree/add_child_node", p_event)) {
  63. _tool_selected(TOOL_NEW);
  64. } else if (ED_IS_SHORTCUT("scene_tree/instance_scene", p_event)) {
  65. _tool_selected(TOOL_INSTANCE);
  66. } else if (ED_IS_SHORTCUT("scene_tree/change_node_type", p_event)) {
  67. _tool_selected(TOOL_REPLACE);
  68. } else if (ED_IS_SHORTCUT("scene_tree/duplicate", p_event)) {
  69. _tool_selected(TOOL_DUPLICATE);
  70. } else if (ED_IS_SHORTCUT("scene_tree/attach_script", p_event)) {
  71. _tool_selected(TOOL_ATTACH_SCRIPT);
  72. } else if (ED_IS_SHORTCUT("scene_tree/clear_script", p_event)) {
  73. _tool_selected(TOOL_CLEAR_SCRIPT);
  74. } else if (ED_IS_SHORTCUT("scene_tree/move_up", p_event)) {
  75. _tool_selected(TOOL_MOVE_UP);
  76. } else if (ED_IS_SHORTCUT("scene_tree/move_down", p_event)) {
  77. _tool_selected(TOOL_MOVE_DOWN);
  78. } else if (ED_IS_SHORTCUT("scene_tree/reparent", p_event)) {
  79. _tool_selected(TOOL_REPARENT);
  80. } else if (ED_IS_SHORTCUT("scene_tree/merge_from_scene", p_event)) {
  81. _tool_selected(TOOL_MERGE_FROM_SCENE);
  82. } else if (ED_IS_SHORTCUT("scene_tree/save_branch_as_scene", p_event)) {
  83. _tool_selected(TOOL_NEW_SCENE_FROM);
  84. } else if (ED_IS_SHORTCUT("scene_tree/delete_no_confirm", p_event)) {
  85. _tool_selected(TOOL_ERASE, true);
  86. } else if (ED_IS_SHORTCUT("scene_tree/copy_node_path", p_event)) {
  87. _tool_selected(TOOL_COPY_NODE_PATH);
  88. } else if (ED_IS_SHORTCUT("scene_tree/delete", p_event)) {
  89. _tool_selected(TOOL_ERASE);
  90. }
  91. }
  92. void SceneTreeDock::instance(const String &p_file) {
  93. Node *parent = scene_tree->get_selected();
  94. if (!parent || !edited_scene) {
  95. current_option = -1;
  96. accept->get_ok()->set_text(TTR("OK :("));
  97. accept->set_text(TTR("No parent to instance a child at."));
  98. accept->popup_centered_minsize();
  99. return;
  100. };
  101. ERR_FAIL_COND(!parent);
  102. Vector<String> scenes;
  103. scenes.push_back(p_file);
  104. _perform_instance_scenes(scenes, parent, -1);
  105. }
  106. void SceneTreeDock::instance_scenes(const Vector<String> &p_files, Node *p_parent) {
  107. Node *parent = p_parent;
  108. if (!parent) {
  109. parent = scene_tree->get_selected();
  110. }
  111. if (!parent || !edited_scene) {
  112. accept->get_ok()->set_text(TTR("OK"));
  113. accept->set_text(TTR("No parent to instance the scenes at."));
  114. accept->popup_centered_minsize();
  115. return;
  116. };
  117. _perform_instance_scenes(p_files, parent, -1);
  118. }
  119. void SceneTreeDock::_perform_instance_scenes(const Vector<String> &p_files, Node *parent, int p_pos) {
  120. ERR_FAIL_COND(!parent);
  121. Vector<Node *> instances;
  122. bool error = false;
  123. for (int i = 0; i < p_files.size(); i++) {
  124. Ref<PackedScene> sdata = ResourceLoader::load(p_files[i]);
  125. if (!sdata.is_valid()) {
  126. current_option = -1;
  127. accept->get_ok()->set_text(TTR("Ugh"));
  128. accept->set_text(vformat(TTR("Error loading scene from %s"), p_files[i]));
  129. accept->popup_centered_minsize();
  130. error = true;
  131. break;
  132. }
  133. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  134. if (!instanced_scene) {
  135. current_option = -1;
  136. accept->get_ok()->set_text(TTR("Ugh"));
  137. accept->set_text(vformat(TTR("Error instancing scene from %s"), p_files[i]));
  138. accept->popup_centered_minsize();
  139. error = true;
  140. break;
  141. }
  142. if (edited_scene->get_filename() != "") {
  143. if (_cyclical_dependency_exists(edited_scene->get_filename(), instanced_scene)) {
  144. accept->get_ok()->set_text(TTR("Ok"));
  145. accept->set_text(vformat(TTR("Cannot instance the scene '%s' because the current scene exists within one of its nodes."), p_files[i]));
  146. accept->popup_centered_minsize();
  147. error = true;
  148. break;
  149. }
  150. }
  151. instanced_scene->set_filename(ProjectSettings::get_singleton()->localize_path(p_files[i]));
  152. instances.push_back(instanced_scene);
  153. }
  154. if (error) {
  155. for (int i = 0; i < instances.size(); i++) {
  156. memdelete(instances[i]);
  157. }
  158. return;
  159. }
  160. editor_data->get_undo_redo().create_action(TTR("Instance Scene(s)"));
  161. for (int i = 0; i < instances.size(); i++) {
  162. Node *instanced_scene = instances[i];
  163. editor_data->get_undo_redo().add_do_method(parent, "add_child", instanced_scene);
  164. if (p_pos >= 0) {
  165. editor_data->get_undo_redo().add_do_method(parent, "move_child", instanced_scene, p_pos + i);
  166. }
  167. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_owner", edited_scene);
  168. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  169. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", instanced_scene);
  170. editor_data->get_undo_redo().add_do_reference(instanced_scene);
  171. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instanced_scene);
  172. String new_name = parent->validate_child_name(instanced_scene);
  173. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  174. editor_data->get_undo_redo().add_do_method(sed, "live_debug_instance_node", edited_scene->get_path_to(parent), p_files[i], new_name);
  175. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)) + "/" + new_name));
  176. }
  177. editor_data->get_undo_redo().commit_action();
  178. }
  179. void SceneTreeDock::_replace_with_branch_scene(const String &p_file, Node *base) {
  180. Ref<PackedScene> sdata = ResourceLoader::load(p_file);
  181. if (!sdata.is_valid()) {
  182. accept->get_ok()->set_text(TTR("Ugh"));
  183. accept->set_text(vformat(TTR("Error loading scene from %s"), p_file));
  184. accept->popup_centered_minsize();
  185. return;
  186. }
  187. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  188. if (!instanced_scene) {
  189. accept->get_ok()->set_text(TTR("Ugh"));
  190. accept->set_text(vformat(TTR("Error instancing scene from %s"), p_file));
  191. accept->popup_centered_minsize();
  192. return;
  193. }
  194. Node *parent = base->get_parent();
  195. int pos = base->get_index();
  196. memdelete(base);
  197. parent->add_child(instanced_scene);
  198. parent->move_child(instanced_scene, pos);
  199. instanced_scene->set_owner(edited_scene);
  200. editor_selection->clear();
  201. editor_selection->add_node(instanced_scene);
  202. scene_tree->set_selected(instanced_scene);
  203. }
  204. bool SceneTreeDock::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  205. int childCount = p_desired_node->get_child_count();
  206. if (p_desired_node->get_filename() == p_target_scene_path) {
  207. return true;
  208. }
  209. for (int i = 0; i < childCount; i++) {
  210. Node *child = p_desired_node->get_child(i);
  211. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217. void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
  218. current_option = p_tool;
  219. switch (p_tool) {
  220. case TOOL_NEW: {
  221. String preferred = "";
  222. Node *current_edited_scene_root = EditorNode::get_singleton()->get_edited_scene();
  223. if (current_edited_scene_root) {
  224. if (ClassDB::is_parent_class(current_edited_scene_root->get_class_name(), "Node2D"))
  225. preferred = "Node2D";
  226. else if (ClassDB::is_parent_class(current_edited_scene_root->get_class_name(), "Spatial"))
  227. preferred = "Spatial";
  228. }
  229. create_dialog->set_preferred_search_result_type(preferred);
  230. create_dialog->popup_create(true);
  231. } break;
  232. case TOOL_INSTANCE: {
  233. Node *scene = edited_scene;
  234. if (!scene) {
  235. EditorNode::get_singleton()->new_inherited_scene();
  236. }
  237. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  238. List<String> extensions;
  239. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  240. file->clear_filters();
  241. for (int i = 0; i < extensions.size(); i++) {
  242. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  243. }
  244. file->popup_centered_ratio();
  245. } break;
  246. case TOOL_REPLACE: {
  247. create_dialog->popup_create(false);
  248. } break;
  249. case TOOL_ATTACH_SCRIPT: {
  250. Node *selected = scene_tree->get_selected();
  251. if (!selected)
  252. break;
  253. Ref<Script> existing = selected->get_script();
  254. if (existing.is_valid())
  255. editor->push_item(existing.ptr());
  256. else {
  257. String path = selected->get_filename();
  258. if (path == "") {
  259. String root_path = editor_data->get_edited_scene_root()->get_filename();
  260. if (root_path == "") {
  261. path = "res://" + selected->get_name();
  262. } else {
  263. path = root_path.get_base_dir() + "/" + selected->get_name();
  264. }
  265. }
  266. script_create_dialog->config(selected->get_class(), path);
  267. script_create_dialog->popup_centered();
  268. }
  269. } break;
  270. case TOOL_CLEAR_SCRIPT: {
  271. Node *selected = scene_tree->get_selected();
  272. if (!selected)
  273. break;
  274. Ref<Script> existing = selected->get_script();
  275. if (existing.is_valid()) {
  276. const RefPtr empty;
  277. selected->set_script(empty);
  278. button_create_script->show();
  279. button_clear_script->hide();
  280. }
  281. } break;
  282. case TOOL_MOVE_UP:
  283. case TOOL_MOVE_DOWN: {
  284. if (!scene_tree->get_selected())
  285. break;
  286. if (scene_tree->get_selected() == edited_scene) {
  287. current_option = -1;
  288. accept->get_ok()->set_text(TTR("I see.."));
  289. accept->set_text(TTR("This operation can't be done on the tree root."));
  290. accept->popup_centered_minsize();
  291. break;
  292. }
  293. if (!_validate_no_foreign())
  294. break;
  295. bool MOVING_DOWN = (p_tool == TOOL_MOVE_DOWN);
  296. bool MOVING_UP = !MOVING_DOWN;
  297. Node *common_parent = scene_tree->get_selected()->get_parent();
  298. List<Node *> selection = editor_selection->get_selected_node_list();
  299. selection.sort_custom<Node::Comparator>(); // sort by index
  300. if (MOVING_DOWN)
  301. selection.invert();
  302. int lowest_id = common_parent->get_child_count() - 1;
  303. int highest_id = 0;
  304. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  305. int index = E->get()->get_index();
  306. if (index > highest_id) highest_id = index;
  307. if (index < lowest_id) lowest_id = index;
  308. if (E->get()->get_parent() != common_parent)
  309. common_parent = NULL;
  310. }
  311. if (!common_parent || (MOVING_DOWN && highest_id >= common_parent->get_child_count() - MOVING_DOWN) || (MOVING_UP && lowest_id == 0))
  312. break; // one or more nodes can not be moved
  313. if (selection.size() == 1) editor_data->get_undo_redo().create_action(TTR("Move Node In Parent"));
  314. if (selection.size() > 1) editor_data->get_undo_redo().create_action(TTR("Move Nodes In Parent"));
  315. for (int i = 0; i < selection.size(); i++) {
  316. Node *top_node = selection[i];
  317. Node *bottom_node = selection[selection.size() - 1 - i];
  318. ERR_FAIL_COND(!top_node->get_parent());
  319. ERR_FAIL_COND(!bottom_node->get_parent());
  320. int bottom_node_pos = bottom_node->get_index();
  321. int top_node_pos_next = top_node->get_index() + (MOVING_DOWN ? 1 : -1);
  322. editor_data->get_undo_redo().add_do_method(top_node->get_parent(), "move_child", top_node, top_node_pos_next);
  323. editor_data->get_undo_redo().add_undo_method(bottom_node->get_parent(), "move_child", bottom_node, bottom_node_pos);
  324. }
  325. editor_data->get_undo_redo().commit_action();
  326. } break;
  327. case TOOL_DUPLICATE: {
  328. if (!edited_scene)
  329. break;
  330. if (editor_selection->is_selected(edited_scene)) {
  331. current_option = -1;
  332. //accept->get_cancel()->hide();
  333. accept->get_ok()->set_text(TTR("I see.."));
  334. accept->set_text(TTR("This operation can't be done on the tree root."));
  335. accept->popup_centered_minsize();
  336. break;
  337. }
  338. if (!_validate_no_foreign())
  339. break;
  340. List<Node *> selection = editor_selection->get_selected_node_list();
  341. if (selection.size() == 0)
  342. break;
  343. editor_data->get_undo_redo().create_action(TTR("Duplicate Node(s)"));
  344. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  345. Node *dupsingle = NULL;
  346. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  347. Node *node = E->get();
  348. Node *parent = node->get_parent();
  349. List<Node *> owned;
  350. node->get_owned_by(node->get_owner(), &owned);
  351. Map<Node *, Node *> duplimap;
  352. Node *dup = _duplicate(node, duplimap);
  353. ERR_CONTINUE(!dup);
  354. if (selection.size() == 1)
  355. dupsingle = dup;
  356. dup->set_name(parent->validate_child_name(dup));
  357. editor_data->get_undo_redo().add_do_method(parent, "add_child_below_node", node, dup);
  358. for (List<Node *>::Element *F = owned.front(); F; F = F->next()) {
  359. if (!duplimap.has(F->get())) {
  360. continue;
  361. }
  362. Node *d = duplimap[F->get()];
  363. editor_data->get_undo_redo().add_do_method(d, "set_owner", node->get_owner());
  364. }
  365. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", dup);
  366. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", dup);
  367. editor_data->get_undo_redo().add_do_reference(dup);
  368. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  369. editor_data->get_undo_redo().add_do_method(sed, "live_debug_duplicate_node", edited_scene->get_path_to(node), dup->get_name());
  370. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)) + "/" + dup->get_name()));
  371. }
  372. editor_data->get_undo_redo().commit_action();
  373. if (dupsingle)
  374. editor->push_item(dupsingle);
  375. } break;
  376. case TOOL_REPARENT: {
  377. if (!scene_tree->get_selected())
  378. break;
  379. if (editor_selection->is_selected(edited_scene)) {
  380. current_option = -1;
  381. accept->get_ok()->set_text(TTR("I see.."));
  382. accept->set_text(TTR("This operation can't be done on the tree root."));
  383. accept->popup_centered_minsize();
  384. break;
  385. }
  386. if (!_validate_no_foreign())
  387. break;
  388. List<Node *> nodes = editor_selection->get_selected_node_list();
  389. Set<Node *> nodeset;
  390. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  391. nodeset.insert(E->get());
  392. }
  393. reparent_dialog->popup_centered_ratio();
  394. reparent_dialog->set_current(nodeset);
  395. } break;
  396. case TOOL_MULTI_EDIT: {
  397. Node *root = EditorNode::get_singleton()->get_edited_scene();
  398. if (!root)
  399. break;
  400. Ref<MultiNodeEdit> mne = memnew(MultiNodeEdit);
  401. for (const Map<Node *, Object *>::Element *E = EditorNode::get_singleton()->get_editor_selection()->get_selection().front(); E; E = E->next()) {
  402. mne->add_node(root->get_path_to(E->key()));
  403. }
  404. EditorNode::get_singleton()->push_item(mne.ptr());
  405. } break;
  406. case TOOL_ERASE: {
  407. List<Node *> remove_list = editor_selection->get_selected_node_list();
  408. if (remove_list.empty())
  409. return;
  410. if (!_validate_no_foreign())
  411. break;
  412. if (p_confirm_override) {
  413. _delete_confirm();
  414. } else {
  415. delete_dialog->set_text(TTR("Delete Node(s)?"));
  416. delete_dialog->popup_centered_minsize();
  417. }
  418. } break;
  419. case TOOL_MERGE_FROM_SCENE: {
  420. EditorNode::get_singleton()->merge_from_scene();
  421. } break;
  422. case TOOL_NEW_SCENE_FROM: {
  423. Node *scene = editor_data->get_edited_scene_root();
  424. if (!scene) {
  425. accept->get_ok()->set_text(TTR("I see.."));
  426. accept->set_text(TTR("This operation can't be done without a scene."));
  427. accept->popup_centered_minsize();
  428. break;
  429. }
  430. List<Node *> selection = editor_selection->get_selected_node_list();
  431. if (selection.size() != 1) {
  432. accept->get_ok()->set_text(TTR("I see.."));
  433. accept->set_text(TTR("This operation requires a single selected node."));
  434. accept->popup_centered_minsize();
  435. break;
  436. }
  437. Node *tocopy = selection.front()->get();
  438. if (tocopy == scene) {
  439. accept->get_ok()->set_text(TTR("I see.."));
  440. accept->set_text(TTR("Can not perform with the root node."));
  441. accept->popup_centered_minsize();
  442. break;
  443. }
  444. if (tocopy != editor_data->get_edited_scene_root() && tocopy->get_filename() != "") {
  445. accept->get_ok()->set_text(TTR("I see.."));
  446. accept->set_text(TTR("This operation can't be done on instanced scenes."));
  447. accept->popup_centered_minsize();
  448. break;
  449. }
  450. new_scene_from_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  451. List<String> extensions;
  452. Ref<PackedScene> sd = memnew(PackedScene);
  453. ResourceSaver::get_recognized_extensions(sd, &extensions);
  454. new_scene_from_dialog->clear_filters();
  455. for (int i = 0; i < extensions.size(); i++) {
  456. new_scene_from_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  457. }
  458. String existing;
  459. if (extensions.size()) {
  460. String root_name(tocopy->get_name());
  461. existing = root_name + "." + extensions.front()->get().to_lower();
  462. }
  463. new_scene_from_dialog->set_current_path(existing);
  464. new_scene_from_dialog->popup_centered_ratio();
  465. new_scene_from_dialog->set_title(TTR("Save New Scene As.."));
  466. } break;
  467. case TOOL_COPY_NODE_PATH: {
  468. List<Node *> selection = editor_selection->get_selected_node_list();
  469. List<Node *>::Element *e = selection.front();
  470. if (e) {
  471. Node *node = e->get();
  472. if (node) {
  473. Node *root = EditorNode::get_singleton()->get_edited_scene();
  474. NodePath path = root->get_path().rel_path_to(node->get_path());
  475. OS::get_singleton()->set_clipboard(path);
  476. }
  477. }
  478. } break;
  479. case TOOL_SCENE_EDITABLE_CHILDREN: {
  480. List<Node *> selection = editor_selection->get_selected_node_list();
  481. List<Node *>::Element *e = selection.front();
  482. if (e) {
  483. Node *node = e->get();
  484. if (node) {
  485. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(node);
  486. int editable_item_idx = menu->get_item_idx_from_text(TTR("Editable Children"));
  487. int placeholder_item_idx = menu->get_item_idx_from_text(TTR("Load As Placeholder"));
  488. editable = !editable;
  489. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(node, editable);
  490. menu->set_item_checked(editable_item_idx, editable);
  491. if (editable) {
  492. node->set_scene_instance_load_placeholder(false);
  493. menu->set_item_checked(placeholder_item_idx, false);
  494. }
  495. scene_tree->update_tree();
  496. }
  497. }
  498. } break;
  499. case TOOL_SCENE_USE_PLACEHOLDER: {
  500. List<Node *> selection = editor_selection->get_selected_node_list();
  501. List<Node *>::Element *e = selection.front();
  502. if (e) {
  503. Node *node = e->get();
  504. if (node) {
  505. bool placeholder = node->get_scene_instance_load_placeholder();
  506. placeholder = !placeholder;
  507. int editable_item_idx = menu->get_item_idx_from_text(TTR("Editable Children"));
  508. int placeholder_item_idx = menu->get_item_idx_from_text(TTR("Load As Placeholder"));
  509. if (placeholder)
  510. EditorNode::get_singleton()->get_edited_scene()->set_editable_instance(node, false);
  511. node->set_scene_instance_load_placeholder(placeholder);
  512. menu->set_item_checked(editable_item_idx, false);
  513. menu->set_item_checked(placeholder_item_idx, placeholder);
  514. scene_tree->update_tree();
  515. }
  516. }
  517. } break;
  518. case TOOL_SCENE_CLEAR_INSTANCING: {
  519. List<Node *> selection = editor_selection->get_selected_node_list();
  520. List<Node *>::Element *e = selection.front();
  521. if (e) {
  522. Node *node = e->get();
  523. if (node) {
  524. Node *root = EditorNode::get_singleton()->get_edited_scene();
  525. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  526. if (!root)
  527. break;
  528. ERR_FAIL_COND(node->get_filename() == String());
  529. undo_redo->create_action(TTR("Discard Instancing"));
  530. undo_redo->add_do_method(node, "set_filename", "");
  531. undo_redo->add_undo_method(node, "set_filename", node->get_filename());
  532. _node_replace_owner(node, node, root);
  533. undo_redo->add_do_method(scene_tree, "update_tree");
  534. undo_redo->add_undo_method(scene_tree, "update_tree");
  535. undo_redo->commit_action();
  536. }
  537. }
  538. } break;
  539. case TOOL_SCENE_OPEN: {
  540. List<Node *> selection = editor_selection->get_selected_node_list();
  541. List<Node *>::Element *e = selection.front();
  542. if (e) {
  543. Node *node = e->get();
  544. if (node) {
  545. scene_tree->emit_signal("open", node->get_filename());
  546. }
  547. }
  548. } break;
  549. case TOOL_SCENE_CLEAR_INHERITANCE: {
  550. clear_inherit_confirm->popup_centered_minsize();
  551. } break;
  552. case TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM: {
  553. List<Node *> selection = editor_selection->get_selected_node_list();
  554. List<Node *>::Element *e = selection.front();
  555. if (e) {
  556. Node *node = e->get();
  557. if (node) {
  558. node->set_scene_inherited_state(Ref<SceneState>());
  559. scene_tree->update_tree();
  560. EditorNode::get_singleton()->get_property_editor()->update_tree();
  561. }
  562. }
  563. } break;
  564. case TOOL_SCENE_OPEN_INHERITED: {
  565. List<Node *> selection = editor_selection->get_selected_node_list();
  566. List<Node *>::Element *e = selection.front();
  567. if (e) {
  568. Node *node = e->get();
  569. if (node) {
  570. if (node && node->get_scene_inherited_state().is_valid()) {
  571. scene_tree->emit_signal("open", node->get_scene_inherited_state()->get_path());
  572. }
  573. }
  574. }
  575. } break;
  576. default: {
  577. if (p_tool >= EDIT_SUBRESOURCE_BASE) {
  578. int idx = p_tool - EDIT_SUBRESOURCE_BASE;
  579. ERR_FAIL_INDEX(idx, subresources.size());
  580. Object *obj = ObjectDB::get_instance(subresources[idx]);
  581. ERR_FAIL_COND(!obj);
  582. editor->push_item(obj);
  583. }
  584. }
  585. }
  586. }
  587. void SceneTreeDock::_notification(int p_what) {
  588. switch (p_what) {
  589. case NOTIFICATION_READY: {
  590. if (!first_enter)
  591. break;
  592. first_enter = false;
  593. CanvasItemEditorPlugin *canvas_item_plugin = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  594. if (canvas_item_plugin) {
  595. canvas_item_plugin->get_canvas_item_editor()->connect("item_lock_status_changed", scene_tree, "_update_tree");
  596. canvas_item_plugin->get_canvas_item_editor()->connect("item_group_status_changed", scene_tree, "_update_tree");
  597. scene_tree->connect("node_changed", canvas_item_plugin->get_canvas_item_editor()->get_viewport_control(), "update");
  598. }
  599. button_add->set_icon(get_icon("Add", "EditorIcons"));
  600. button_instance->set_icon(get_icon("Instance", "EditorIcons"));
  601. button_create_script->set_icon(get_icon("ScriptCreate", "EditorIcons"));
  602. button_clear_script->set_icon(get_icon("ScriptRemove", "EditorIcons"));
  603. filter->add_icon_override("right_icon", get_icon("Search", "EditorIcons"));
  604. EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", this, "_selection_changed");
  605. } break;
  606. case NOTIFICATION_ENTER_TREE: {
  607. clear_inherit_confirm->connect("confirmed", this, "_tool_selected", varray(TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM));
  608. } break;
  609. case NOTIFICATION_EXIT_TREE: {
  610. clear_inherit_confirm->disconnect("confirmed", this, "_tool_selected");
  611. } break;
  612. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  613. button_add->set_icon(get_icon("Add", "EditorIcons"));
  614. button_instance->set_icon(get_icon("Instance", "EditorIcons"));
  615. button_create_script->set_icon(get_icon("ScriptCreate", "EditorIcons"));
  616. button_clear_script->set_icon(get_icon("ScriptRemove", "EditorIcons"));
  617. filter->add_icon_override("right_icon", get_icon("Search", "EditorIcons"));
  618. } break;
  619. }
  620. }
  621. void SceneTreeDock::_node_replace_owner(Node *p_base, Node *p_node, Node *p_root) {
  622. if (p_base != p_node) {
  623. if (p_node->get_owner() == p_base) {
  624. UndoRedo *undo_redo = &editor_data->get_undo_redo();
  625. undo_redo->add_do_method(p_node, "set_owner", p_root);
  626. undo_redo->add_undo_method(p_node, "set_owner", p_base);
  627. }
  628. }
  629. for (int i = 0; i < p_node->get_child_count(); i++) {
  630. _node_replace_owner(p_base, p_node->get_child(i), p_root);
  631. }
  632. }
  633. void SceneTreeDock::_load_request(const String &p_path) {
  634. editor->open_request(p_path);
  635. }
  636. void SceneTreeDock::_script_open_request(const Ref<Script> &p_script) {
  637. editor->edit_resource(p_script);
  638. }
  639. void SceneTreeDock::_node_selected() {
  640. Node *node = scene_tree->get_selected();
  641. if (!node) {
  642. editor->push_item(NULL);
  643. return;
  644. }
  645. if (ScriptEditor::get_singleton()->is_visible_in_tree()) {
  646. restore_script_editor_on_drag = true;
  647. }
  648. editor->push_item(node);
  649. }
  650. void SceneTreeDock::_node_renamed() {
  651. _node_selected();
  652. }
  653. Node *SceneTreeDock::_duplicate(Node *p_node, Map<Node *, Node *> &duplimap) {
  654. Node *node = NULL;
  655. if (p_node->get_filename() != "") { //an instance
  656. Ref<PackedScene> sd = ResourceLoader::load(p_node->get_filename());
  657. ERR_FAIL_COND_V(!sd.is_valid(), NULL);
  658. node = sd->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  659. ERR_FAIL_COND_V(!node, NULL);
  660. node->set_scene_instance_load_placeholder(p_node->get_scene_instance_load_placeholder());
  661. } else {
  662. Object *obj = ClassDB::instance(p_node->get_class());
  663. ERR_FAIL_COND_V(!obj, NULL);
  664. node = Object::cast_to<Node>(obj);
  665. if (!node)
  666. memdelete(obj);
  667. ERR_FAIL_COND_V(!node, NULL);
  668. }
  669. List<PropertyInfo> plist;
  670. p_node->get_property_list(&plist);
  671. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  672. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  673. continue;
  674. String name = E->get().name;
  675. Variant value = p_node->get(name);
  676. // Duplicate dictionaries and arrays, mainly needed for __meta__
  677. if (value.get_type() == Variant::DICTIONARY) {
  678. value = Dictionary(value).copy();
  679. } else if (value.get_type() == Variant::ARRAY) {
  680. value = Array(value).duplicate();
  681. }
  682. node->set(name, value);
  683. }
  684. List<Node::GroupInfo> group_info;
  685. p_node->get_groups(&group_info);
  686. for (List<Node::GroupInfo>::Element *E = group_info.front(); E; E = E->next()) {
  687. if (E->get().persistent)
  688. node->add_to_group(E->get().name, true);
  689. }
  690. node->set_name(p_node->get_name());
  691. duplimap[p_node] = node;
  692. for (int i = 0; i < p_node->get_child_count(); i++) {
  693. Node *child = p_node->get_child(i);
  694. if (p_node->get_owner() != child->get_owner())
  695. continue; //don't bother with not in-scene nodes.
  696. Node *dup = _duplicate(child, duplimap);
  697. if (!dup) {
  698. memdelete(node);
  699. return NULL;
  700. }
  701. node->add_child(dup);
  702. }
  703. return node;
  704. }
  705. void SceneTreeDock::_set_owners(Node *p_owner, const Array &p_nodes) {
  706. for (int i = 0; i < p_nodes.size(); i++) {
  707. Node *n = Object::cast_to<Node>(p_nodes[i]);
  708. if (!n)
  709. continue;
  710. n->set_owner(p_owner);
  711. }
  712. }
  713. void SceneTreeDock::_fill_path_renames(Vector<StringName> base_path, Vector<StringName> new_base_path, Node *p_node, List<Pair<NodePath, NodePath> > *p_renames) {
  714. base_path.push_back(p_node->get_name());
  715. if (new_base_path.size())
  716. new_base_path.push_back(p_node->get_name());
  717. NodePath from(base_path, true);
  718. NodePath to;
  719. if (new_base_path.size())
  720. to = NodePath(new_base_path, true);
  721. Pair<NodePath, NodePath> npp;
  722. npp.first = from;
  723. npp.second = to;
  724. p_renames->push_back(npp);
  725. for (int i = 0; i < p_node->get_child_count(); i++) {
  726. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), p_renames);
  727. }
  728. }
  729. void SceneTreeDock::fill_path_renames(Node *p_node, Node *p_new_parent, List<Pair<NodePath, NodePath> > *p_renames) {
  730. if (!bool(EDITOR_DEF("editors/animation/autorename_animation_tracks", true)))
  731. return;
  732. Vector<StringName> base_path;
  733. Node *n = p_node->get_parent();
  734. while (n) {
  735. base_path.push_back(n->get_name());
  736. n = n->get_parent();
  737. }
  738. base_path.invert();
  739. Vector<StringName> new_base_path;
  740. if (p_new_parent) {
  741. n = p_new_parent;
  742. while (n) {
  743. new_base_path.push_back(n->get_name());
  744. n = n->get_parent();
  745. }
  746. new_base_path.invert();
  747. }
  748. _fill_path_renames(base_path, new_base_path, p_node, p_renames);
  749. }
  750. void SceneTreeDock::perform_node_renames(Node *p_base, List<Pair<NodePath, NodePath> > *p_renames, Map<Ref<Animation>, Set<int> > *r_rem_anims) {
  751. Map<Ref<Animation>, Set<int> > rem_anims;
  752. if (!r_rem_anims)
  753. r_rem_anims = &rem_anims;
  754. if (!bool(EDITOR_DEF("editors/animation/autorename_animation_tracks", true)))
  755. return;
  756. if (!p_base) {
  757. p_base = edited_scene;
  758. }
  759. if (!p_base)
  760. return;
  761. if (Object::cast_to<AnimationPlayer>(p_base)) {
  762. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_base);
  763. List<StringName> anims;
  764. ap->get_animation_list(&anims);
  765. Node *root = ap->get_node(ap->get_root());
  766. if (root) {
  767. NodePath root_path = root->get_path();
  768. NodePath new_root_path = root_path;
  769. for (List<Pair<NodePath, NodePath> >::Element *E = p_renames->front(); E; E = E->next()) {
  770. if (E->get().first == root_path) {
  771. new_root_path = E->get().second;
  772. break;
  773. }
  774. }
  775. if (new_root_path != NodePath()) {
  776. //will not be erased
  777. for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
  778. Ref<Animation> anim = ap->get_animation(E->get());
  779. if (!r_rem_anims->has(anim)) {
  780. r_rem_anims->insert(anim, Set<int>());
  781. Set<int> &ran = r_rem_anims->find(anim)->get();
  782. for (int i = 0; i < anim->get_track_count(); i++)
  783. ran.insert(i);
  784. }
  785. Set<int> &ran = r_rem_anims->find(anim)->get();
  786. if (anim.is_null())
  787. continue;
  788. for (int i = 0; i < anim->get_track_count(); i++) {
  789. NodePath track_np = anim->track_get_path(i);
  790. Node *n = root->get_node(track_np);
  791. if (!n) {
  792. continue;
  793. }
  794. NodePath old_np = n->get_path();
  795. if (!ran.has(i))
  796. continue; //channel was removed
  797. for (List<Pair<NodePath, NodePath> >::Element *E = p_renames->front(); E; E = E->next()) {
  798. if (E->get().first == old_np) {
  799. if (E->get().second == NodePath()) {
  800. //will be erased
  801. int idx = 0;
  802. Set<int>::Element *EI = ran.front();
  803. ERR_FAIL_COND(!EI); //bug
  804. while (EI->get() != i) {
  805. idx++;
  806. EI = EI->next();
  807. ERR_FAIL_COND(!EI); //another bug
  808. }
  809. editor_data->get_undo_redo().add_do_method(anim.ptr(), "remove_track", idx);
  810. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "add_track", anim->track_get_type(i), idx);
  811. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", idx, track_np);
  812. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_interpolation_type", idx, anim->track_get_interpolation_type(i));
  813. for (int j = 0; j < anim->track_get_key_count(i); j++) {
  814. 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));
  815. }
  816. ran.erase(i); //byebye channel
  817. } else {
  818. //will be renamed
  819. NodePath rel_path = new_root_path.rel_path_to(E->get().second);
  820. NodePath new_path = NodePath(rel_path.get_names(), track_np.get_subnames(), false, track_np.get_property());
  821. if (new_path == track_np)
  822. continue; //bleh
  823. editor_data->get_undo_redo().add_do_method(anim.ptr(), "track_set_path", i, new_path);
  824. editor_data->get_undo_redo().add_undo_method(anim.ptr(), "track_set_path", i, track_np);
  825. }
  826. }
  827. }
  828. }
  829. }
  830. }
  831. }
  832. }
  833. for (int i = 0; i < p_base->get_child_count(); i++)
  834. perform_node_renames(p_base->get_child(i), p_renames, r_rem_anims);
  835. }
  836. void SceneTreeDock::_node_prerenamed(Node *p_node, const String &p_new_name) {
  837. List<Pair<NodePath, NodePath> > path_renames;
  838. Vector<StringName> base_path;
  839. Node *n = p_node->get_parent();
  840. while (n) {
  841. base_path.push_back(n->get_name());
  842. n = n->get_parent();
  843. }
  844. base_path.invert();
  845. Vector<StringName> new_base_path = base_path;
  846. base_path.push_back(p_node->get_name());
  847. new_base_path.push_back(p_new_name);
  848. Pair<NodePath, NodePath> npp;
  849. npp.first = NodePath(base_path, true);
  850. npp.second = NodePath(new_base_path, true);
  851. path_renames.push_back(npp);
  852. for (int i = 0; i < p_node->get_child_count(); i++)
  853. _fill_path_renames(base_path, new_base_path, p_node->get_child(i), &path_renames);
  854. perform_node_renames(NULL, &path_renames);
  855. }
  856. bool SceneTreeDock::_validate_no_foreign() {
  857. List<Node *> selection = editor_selection->get_selected_node_list();
  858. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  859. if (E->get() != edited_scene && E->get()->get_owner() != edited_scene) {
  860. accept->get_ok()->set_text(TTR("Makes Sense!"));
  861. accept->set_text(TTR("Can't operate on nodes from a foreign scene!"));
  862. accept->popup_centered_minsize();
  863. return false;
  864. }
  865. 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) {
  866. accept->get_ok()->set_text(TTR("Makes Sense!"));
  867. accept->set_text(TTR("Can't operate on nodes the current scene inherits from!"));
  868. accept->popup_centered_minsize();
  869. return false;
  870. }
  871. }
  872. return true;
  873. }
  874. void SceneTreeDock::_node_reparent(NodePath p_path, bool p_keep_global_xform) {
  875. Node *new_parent = scene_root->get_node(p_path);
  876. ERR_FAIL_COND(!new_parent);
  877. List<Node *> selection = editor_selection->get_selected_node_list();
  878. if (selection.empty())
  879. return; //nothing to reparent
  880. Vector<Node *> nodes;
  881. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  882. nodes.push_back(E->get());
  883. }
  884. _do_reparent(new_parent, -1, nodes, p_keep_global_xform);
  885. }
  886. void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, Vector<Node *> p_nodes, bool p_keep_global_xform) {
  887. Node *new_parent = p_new_parent;
  888. ERR_FAIL_COND(!new_parent);
  889. Node *validate = new_parent;
  890. while (validate) {
  891. if (p_nodes.find(validate) != -1) {
  892. ERR_EXPLAIN("Selection changed at some point.. can't reparent");
  893. ERR_FAIL();
  894. return;
  895. }
  896. validate = validate->get_parent();
  897. }
  898. //ok all valid
  899. if (p_nodes.size() == 0)
  900. return; //nothing to reparent
  901. //sort by tree order, so re-adding is easy
  902. p_nodes.sort_custom<Node::Comparator>();
  903. editor_data->get_undo_redo().create_action(TTR("Reparent Node"));
  904. List<Pair<NodePath, NodePath> > path_renames;
  905. Vector<StringName> former_names;
  906. int inc = 0;
  907. for (int ni = 0; ni < p_nodes.size(); ni++) {
  908. //no undo for now, sorry
  909. Node *node = p_nodes[ni];
  910. fill_path_renames(node, new_parent, &path_renames);
  911. former_names.push_back(node->get_name());
  912. List<Node *> owned;
  913. node->get_owned_by(node->get_owner(), &owned);
  914. Array owners;
  915. for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
  916. owners.push_back(E->get());
  917. }
  918. if (new_parent == node->get_parent() && node->get_index() < p_position_in_parent + ni) {
  919. //if child will generate a gap when moved, adjust
  920. inc--;
  921. }
  922. editor_data->get_undo_redo().add_do_method(node->get_parent(), "remove_child", node);
  923. editor_data->get_undo_redo().add_do_method(new_parent, "add_child", node);
  924. if (p_position_in_parent >= 0)
  925. editor_data->get_undo_redo().add_do_method(new_parent, "move_child", node, p_position_in_parent + inc);
  926. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  927. String new_name = new_parent->validate_child_name(node);
  928. 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, -1);
  929. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_reparent_node", NodePath(String(edited_scene->get_path_to(new_parent)) + "/" + new_name), edited_scene->get_path_to(node->get_parent()), node->get_name(), node->get_index());
  930. if (p_keep_global_xform) {
  931. if (Object::cast_to<Node2D>(node))
  932. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Node2D>(node)->get_global_transform());
  933. if (Object::cast_to<Spatial>(node))
  934. editor_data->get_undo_redo().add_do_method(node, "set_global_transform", Object::cast_to<Spatial>(node)->get_global_transform());
  935. if (Object::cast_to<Control>(node))
  936. editor_data->get_undo_redo().add_do_method(node, "set_global_position", Object::cast_to<Control>(node)->get_global_position());
  937. }
  938. editor_data->get_undo_redo().add_do_method(this, "_set_owners", edited_scene, owners);
  939. if (AnimationPlayerEditor::singleton->get_key_editor()->get_root() == node)
  940. editor_data->get_undo_redo().add_do_method(AnimationPlayerEditor::singleton->get_key_editor(), "set_root", node);
  941. editor_data->get_undo_redo().add_undo_method(new_parent, "remove_child", node);
  942. editor_data->get_undo_redo().add_undo_method(node, "set_name", former_names[ni]);
  943. inc++;
  944. }
  945. //add and move in a second step.. (so old order is preserved)
  946. for (int ni = 0; ni < p_nodes.size(); ni++) {
  947. Node *node = p_nodes[ni];
  948. List<Node *> owned;
  949. node->get_owned_by(node->get_owner(), &owned);
  950. Array owners;
  951. for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
  952. owners.push_back(E->get());
  953. }
  954. int child_pos = node->get_position_in_parent();
  955. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "add_child", node);
  956. editor_data->get_undo_redo().add_undo_method(node->get_parent(), "move_child", node, child_pos);
  957. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  958. if (AnimationPlayerEditor::singleton->get_key_editor()->get_root() == node)
  959. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_key_editor(), "set_root", node);
  960. if (p_keep_global_xform) {
  961. if (Object::cast_to<Node2D>(node))
  962. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Node2D>(node)->get_transform());
  963. if (Object::cast_to<Spatial>(node))
  964. editor_data->get_undo_redo().add_undo_method(node, "set_transform", Object::cast_to<Spatial>(node)->get_transform());
  965. if (Object::cast_to<Control>(node))
  966. editor_data->get_undo_redo().add_undo_method(node, "set_position", Object::cast_to<Control>(node)->get_position());
  967. }
  968. }
  969. perform_node_renames(NULL, &path_renames);
  970. editor_data->get_undo_redo().commit_action();
  971. }
  972. void SceneTreeDock::_script_created(Ref<Script> p_script) {
  973. Node *selected = scene_tree->get_selected();
  974. if (!selected)
  975. return;
  976. selected->set_script(p_script.get_ref_ptr());
  977. editor->push_item(p_script.operator->());
  978. button_create_script->hide();
  979. button_clear_script->show();
  980. }
  981. void SceneTreeDock::_delete_confirm() {
  982. List<Node *> remove_list = editor_selection->get_selected_node_list();
  983. if (remove_list.empty())
  984. return;
  985. editor->get_editor_plugins_over()->make_visible(false);
  986. editor_data->get_undo_redo().create_action(TTR("Remove Node(s)"));
  987. bool entire_scene = false;
  988. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  989. if (E->get() == edited_scene) {
  990. entire_scene = true;
  991. }
  992. }
  993. if (entire_scene) {
  994. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", (Object *)NULL);
  995. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", edited_scene);
  996. editor_data->get_undo_redo().add_undo_method(edited_scene, "set_owner", edited_scene->get_owner());
  997. editor_data->get_undo_redo().add_undo_reference(edited_scene);
  998. } else {
  999. remove_list.sort_custom<Node::Comparator>(); //sort nodes to keep positions
  1000. List<Pair<NodePath, NodePath> > path_renames;
  1001. //delete from animation
  1002. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1003. Node *n = E->get();
  1004. if (!n->is_inside_tree() || !n->get_parent())
  1005. continue;
  1006. fill_path_renames(n, NULL, &path_renames);
  1007. }
  1008. perform_node_renames(NULL, &path_renames);
  1009. //delete for read
  1010. for (List<Node *>::Element *E = remove_list.front(); E; E = E->next()) {
  1011. Node *n = E->get();
  1012. if (!n->is_inside_tree() || !n->get_parent())
  1013. continue;
  1014. List<Node *> owned;
  1015. n->get_owned_by(n->get_owner(), &owned);
  1016. Array owners;
  1017. for (List<Node *>::Element *E = owned.front(); E; E = E->next()) {
  1018. owners.push_back(E->get());
  1019. }
  1020. editor_data->get_undo_redo().add_do_method(n->get_parent(), "remove_child", n);
  1021. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "add_child", n);
  1022. editor_data->get_undo_redo().add_undo_method(n->get_parent(), "move_child", n, n->get_index());
  1023. if (AnimationPlayerEditor::singleton->get_key_editor()->get_root() == n)
  1024. editor_data->get_undo_redo().add_undo_method(AnimationPlayerEditor::singleton->get_key_editor(), "set_root", n);
  1025. editor_data->get_undo_redo().add_undo_method(this, "_set_owners", edited_scene, owners);
  1026. editor_data->get_undo_redo().add_undo_reference(n);
  1027. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1028. 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());
  1029. 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());
  1030. }
  1031. }
  1032. editor_data->get_undo_redo().commit_action();
  1033. // hack, force 2d editor viewport to refresh after deletion
  1034. if (CanvasItemEditor *editor = CanvasItemEditor::get_singleton())
  1035. editor->get_viewport_control()->update();
  1036. editor->push_item(NULL);
  1037. }
  1038. void SceneTreeDock::_selection_changed() {
  1039. int selection_size = EditorNode::get_singleton()->get_editor_selection()->get_selection().size();
  1040. if (selection_size > 1) {
  1041. //automatically turn on multi-edit
  1042. _tool_selected(TOOL_MULTI_EDIT);
  1043. }
  1044. if (selection_size == 1) {
  1045. if (EditorNode::get_singleton()->get_editor_selection()->get_selection().front()->key()->get_script().is_null()) {
  1046. button_create_script->show();
  1047. button_clear_script->hide();
  1048. } else {
  1049. button_create_script->hide();
  1050. button_clear_script->show();
  1051. }
  1052. } else {
  1053. button_create_script->hide();
  1054. button_clear_script->hide();
  1055. }
  1056. }
  1057. void SceneTreeDock::_create() {
  1058. if (current_option == TOOL_NEW) {
  1059. Node *parent = NULL;
  1060. if (edited_scene) {
  1061. // If root exists in edited scene
  1062. parent = scene_tree->get_selected();
  1063. if (!parent)
  1064. parent = edited_scene;
  1065. } else {
  1066. // If no root exist in edited scene
  1067. parent = scene_root;
  1068. ERR_FAIL_COND(!parent);
  1069. }
  1070. Object *c = create_dialog->instance_selected();
  1071. ERR_FAIL_COND(!c);
  1072. Node *child = Object::cast_to<Node>(c);
  1073. ERR_FAIL_COND(!child);
  1074. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  1075. if (edited_scene) {
  1076. editor_data->get_undo_redo().add_do_method(parent, "add_child", child);
  1077. editor_data->get_undo_redo().add_do_method(child, "set_owner", edited_scene);
  1078. editor_data->get_undo_redo().add_do_method(editor_selection, "clear");
  1079. editor_data->get_undo_redo().add_do_method(editor_selection, "add_node", child);
  1080. editor_data->get_undo_redo().add_do_reference(child);
  1081. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", child);
  1082. String new_name = parent->validate_child_name(child);
  1083. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  1084. editor_data->get_undo_redo().add_do_method(sed, "live_debug_create_node", edited_scene->get_path_to(parent), child->get_class(), new_name);
  1085. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)) + "/" + new_name));
  1086. } else {
  1087. editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", child);
  1088. editor_data->get_undo_redo().add_do_reference(child);
  1089. editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)NULL);
  1090. }
  1091. editor_data->get_undo_redo().commit_action();
  1092. editor->push_item(c);
  1093. if (Object::cast_to<Control>(c)) {
  1094. //make editor more comfortable, so some controls don't appear super shrunk
  1095. Control *ct = Object::cast_to<Control>(c);
  1096. Size2 ms = ct->get_minimum_size();
  1097. if (ms.width < 4)
  1098. ms.width = 40;
  1099. if (ms.height < 4)
  1100. ms.height = 40;
  1101. ct->set_size(ms);
  1102. }
  1103. } else if (current_option == TOOL_REPLACE) {
  1104. Node *n = scene_tree->get_selected();
  1105. ERR_FAIL_COND(!n);
  1106. Object *c = create_dialog->instance_selected();
  1107. ERR_FAIL_COND(!c);
  1108. Node *newnode = Object::cast_to<Node>(c);
  1109. ERR_FAIL_COND(!newnode);
  1110. List<PropertyInfo> pinfo;
  1111. n->get_property_list(&pinfo);
  1112. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1113. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  1114. continue;
  1115. newnode->set(E->get().name, n->get(E->get().name));
  1116. }
  1117. editor->push_item(NULL);
  1118. //reconnect signals
  1119. List<MethodInfo> sl;
  1120. n->get_signal_list(&sl);
  1121. for (List<MethodInfo>::Element *E = sl.front(); E; E = E->next()) {
  1122. List<Object::Connection> cl;
  1123. n->get_signal_connection_list(E->get().name, &cl);
  1124. for (List<Object::Connection>::Element *F = cl.front(); F; F = F->next()) {
  1125. Object::Connection &c = F->get();
  1126. if (!(c.flags & Object::CONNECT_PERSIST))
  1127. continue;
  1128. newnode->connect(c.signal, c.target, c.method, varray(), Object::CONNECT_PERSIST);
  1129. }
  1130. }
  1131. String newname = n->get_name();
  1132. List<Node *> to_erase;
  1133. for (int i = 0; i < n->get_child_count(); i++) {
  1134. if (n->get_child(i)->get_owner() == NULL && n->is_owned_by_parent()) {
  1135. to_erase.push_back(n->get_child(i));
  1136. }
  1137. }
  1138. n->replace_by(newnode, true);
  1139. if (n == edited_scene) {
  1140. edited_scene = newnode;
  1141. editor->set_edited_scene(newnode);
  1142. newnode->set_editable_instances(n->get_editable_instances());
  1143. }
  1144. //small hack to make collisionshapes and other kind of nodes to work
  1145. for (int i = 0; i < newnode->get_child_count(); i++) {
  1146. Node *c = newnode->get_child(i);
  1147. c->call("set_transform", c->call("get_transform"));
  1148. }
  1149. editor_data->get_undo_redo().clear_history();
  1150. newnode->set_name(newname);
  1151. editor->push_item(newnode);
  1152. memdelete(n);
  1153. while (to_erase.front()) {
  1154. memdelete(to_erase.front()->get());
  1155. to_erase.pop_front();
  1156. }
  1157. }
  1158. }
  1159. void SceneTreeDock::set_edited_scene(Node *p_scene) {
  1160. edited_scene = p_scene;
  1161. }
  1162. void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected) {
  1163. scene_tree->set_selected(p_node, p_emit_selected);
  1164. }
  1165. void SceneTreeDock::import_subscene() {
  1166. import_subscene_dialog->popup_centered_ratio();
  1167. }
  1168. void SceneTreeDock::_import_subscene() {
  1169. Node *parent = scene_tree->get_selected();
  1170. if (!parent) {
  1171. parent = editor_data->get_edited_scene_root();
  1172. ERR_FAIL_COND(!parent);
  1173. }
  1174. import_subscene_dialog->move(parent, edited_scene);
  1175. editor_data->get_undo_redo().clear_history(); //no undo for now..
  1176. }
  1177. void SceneTreeDock::_new_scene_from(String p_file) {
  1178. List<Node *> selection = editor_selection->get_selected_node_list();
  1179. if (selection.size() != 1) {
  1180. accept->get_ok()->set_text(TTR("I see.."));
  1181. accept->set_text(TTR("This operation requires a single selected node."));
  1182. accept->popup_centered_minsize();
  1183. return;
  1184. }
  1185. Node *base = selection.front()->get();
  1186. Map<Node *, Node *> reown;
  1187. reown[editor_data->get_edited_scene_root()] = base;
  1188. Node *copy = base->duplicate_and_reown(reown);
  1189. if (copy) {
  1190. Ref<PackedScene> sdata = memnew(PackedScene);
  1191. Error err = sdata->pack(copy);
  1192. memdelete(copy);
  1193. if (err != OK) {
  1194. accept->get_ok()->set_text(TTR("I see.."));
  1195. accept->set_text(TTR("Couldn't save new scene. Likely dependencies (instances) couldn't be satisfied."));
  1196. accept->popup_centered_minsize();
  1197. return;
  1198. }
  1199. int flg = 0;
  1200. if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources"))
  1201. flg |= ResourceSaver::FLAG_COMPRESS;
  1202. err = ResourceSaver::save(p_file, sdata, flg);
  1203. if (err != OK) {
  1204. accept->get_ok()->set_text(TTR("I see.."));
  1205. accept->set_text(TTR("Error saving scene."));
  1206. accept->popup_centered_minsize();
  1207. return;
  1208. }
  1209. _replace_with_branch_scene(p_file, base);
  1210. } else {
  1211. accept->get_ok()->set_text(TTR("I see.."));
  1212. accept->set_text(TTR("Error duplicating scene to save it."));
  1213. accept->popup_centered_minsize();
  1214. return;
  1215. }
  1216. }
  1217. static bool _is_node_visible(Node *p_node) {
  1218. if (!p_node->get_owner())
  1219. return false;
  1220. if (p_node->get_owner() != EditorNode::get_singleton()->get_edited_scene() && !EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node->get_owner()))
  1221. return false;
  1222. return true;
  1223. }
  1224. static bool _has_visible_children(Node *p_node) {
  1225. bool collapsed = p_node->is_displayed_folded();
  1226. if (collapsed)
  1227. return false;
  1228. for (int i = 0; i < p_node->get_child_count(); i++) {
  1229. Node *child = p_node->get_child(i);
  1230. if (!_is_node_visible(child))
  1231. continue;
  1232. return true;
  1233. }
  1234. return false;
  1235. }
  1236. static Node *_find_last_visible(Node *p_node) {
  1237. Node *last = NULL;
  1238. bool collapsed = p_node->is_displayed_folded();
  1239. if (!collapsed) {
  1240. for (int i = 0; i < p_node->get_child_count(); i++) {
  1241. if (_is_node_visible(p_node->get_child(i))) {
  1242. last = p_node->get_child(i);
  1243. }
  1244. }
  1245. }
  1246. if (last) {
  1247. Node *lastc = _find_last_visible(last);
  1248. if (lastc)
  1249. last = lastc;
  1250. } else {
  1251. last = p_node;
  1252. }
  1253. return last;
  1254. }
  1255. void SceneTreeDock::_normalize_drop(Node *&to_node, int &to_pos, int p_type) {
  1256. to_pos = -1;
  1257. if (p_type == -1) {
  1258. //drop at above selected node
  1259. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  1260. to_node = NULL;
  1261. ERR_EXPLAIN("Cannot perform drop above the root node!");
  1262. ERR_FAIL();
  1263. }
  1264. to_pos = to_node->get_index();
  1265. to_node = to_node->get_parent();
  1266. } else if (p_type == 1) {
  1267. //drop at below selected node
  1268. if (to_node == EditorNode::get_singleton()->get_edited_scene()) {
  1269. //if at lower sibling of root node
  1270. to_pos = 0; //just insert at beginning of root node
  1271. return;
  1272. }
  1273. Node *lower_sibling = NULL;
  1274. if (_has_visible_children(to_node)) {
  1275. to_pos = 0;
  1276. } else {
  1277. for (int i = to_node->get_index() + 1; i < to_node->get_parent()->get_child_count(); i++) {
  1278. Node *c = to_node->get_parent()->get_child(i);
  1279. if (_is_node_visible(c)) {
  1280. lower_sibling = c;
  1281. break;
  1282. }
  1283. }
  1284. if (lower_sibling) {
  1285. to_pos = lower_sibling->get_index();
  1286. }
  1287. to_node = to_node->get_parent();
  1288. }
  1289. }
  1290. }
  1291. void SceneTreeDock::_files_dropped(Vector<String> p_files, NodePath p_to, int p_type) {
  1292. Node *node = get_node(p_to);
  1293. ERR_FAIL_COND(!node);
  1294. int to_pos = -1;
  1295. _normalize_drop(node, to_pos, p_type);
  1296. _perform_instance_scenes(p_files, node, to_pos);
  1297. }
  1298. void SceneTreeDock::_script_dropped(String p_file, NodePath p_to) {
  1299. Ref<Script> scr = ResourceLoader::load(p_file);
  1300. ERR_FAIL_COND(!scr.is_valid());
  1301. Node *n = get_node(p_to);
  1302. if (n) {
  1303. n->set_script(scr.get_ref_ptr());
  1304. }
  1305. }
  1306. void SceneTreeDock::_nodes_dragged(Array p_nodes, NodePath p_to, int p_type) {
  1307. Vector<Node *> nodes;
  1308. Node *to_node;
  1309. for (int i = 0; i < p_nodes.size(); i++) {
  1310. Node *n = get_node((p_nodes[i]));
  1311. if (n) {
  1312. nodes.push_back(n);
  1313. }
  1314. }
  1315. if (nodes.size() == 0)
  1316. return;
  1317. to_node = get_node(p_to);
  1318. if (!to_node)
  1319. return;
  1320. int to_pos = -1;
  1321. _normalize_drop(to_node, to_pos, p_type);
  1322. _do_reparent(to_node, to_pos, nodes, true);
  1323. }
  1324. void SceneTreeDock::_add_children_to_popup(Object *p_obj, int p_depth) {
  1325. if (p_depth > 8)
  1326. return;
  1327. List<PropertyInfo> pinfo;
  1328. p_obj->get_property_list(&pinfo);
  1329. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1330. if (!(E->get().usage & PROPERTY_USAGE_EDITOR))
  1331. continue;
  1332. if (E->get().hint != PROPERTY_HINT_RESOURCE_TYPE)
  1333. continue;
  1334. Variant value = p_obj->get(E->get().name);
  1335. if (value.get_type() != Variant::OBJECT)
  1336. continue;
  1337. Object *obj = value;
  1338. if (!obj)
  1339. continue;
  1340. Ref<Texture> icon;
  1341. if (has_icon(obj->get_class(), "EditorIcons"))
  1342. icon = get_icon(obj->get_class(), "EditorIcons");
  1343. else
  1344. icon = get_icon("Object", "EditorIcons");
  1345. if (menu->get_item_count() == 0) {
  1346. menu->add_item(TTR("Sub-Resources:"));
  1347. menu->set_item_disabled(0, true);
  1348. }
  1349. int index = menu->get_item_count();
  1350. menu->add_icon_item(icon, E->get().name.capitalize(), EDIT_SUBRESOURCE_BASE + subresources.size());
  1351. menu->set_item_h_offset(index, p_depth * 10 * EDSCALE);
  1352. subresources.push_back(obj->get_instance_id());
  1353. _add_children_to_popup(obj, p_depth + 1);
  1354. }
  1355. }
  1356. void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
  1357. if (!EditorNode::get_singleton()->get_edited_scene()) {
  1358. menu->clear();
  1359. menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  1360. menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
  1361. menu->set_size(Size2(1, 1));
  1362. menu->set_position(p_menu_pos);
  1363. menu->popup();
  1364. return;
  1365. }
  1366. List<Node *> selection = editor_selection->get_selected_node_list();
  1367. if (selection.size() == 0)
  1368. return;
  1369. menu->clear();
  1370. if (selection.size() == 1) {
  1371. subresources.clear();
  1372. _add_children_to_popup(selection.front()->get(), 0);
  1373. if (menu->get_item_count() > 0)
  1374. menu->add_separator();
  1375. menu->add_icon_shortcut(get_icon("Add", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/add_child_node"), TOOL_NEW);
  1376. menu->add_icon_shortcut(get_icon("Instance", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/instance_scene"), TOOL_INSTANCE);
  1377. menu->add_separator();
  1378. menu->add_icon_shortcut(get_icon("Reload", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/change_node_type"), TOOL_REPLACE);
  1379. menu->add_separator();
  1380. menu->add_icon_shortcut(get_icon("ScriptCreate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/attach_script"), TOOL_ATTACH_SCRIPT);
  1381. menu->add_icon_shortcut(get_icon("ScriptRemove", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/clear_script"), TOOL_CLEAR_SCRIPT);
  1382. menu->add_separator();
  1383. }
  1384. menu->add_icon_shortcut(get_icon("MoveUp", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_up"), TOOL_MOVE_UP);
  1385. menu->add_icon_shortcut(get_icon("MoveDown", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/move_down"), TOOL_MOVE_DOWN);
  1386. menu->add_icon_shortcut(get_icon("Duplicate", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/duplicate"), TOOL_DUPLICATE);
  1387. menu->add_icon_shortcut(get_icon("Reparent", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/reparent"), TOOL_REPARENT);
  1388. if (selection.size() == 1) {
  1389. menu->add_separator();
  1390. menu->add_icon_shortcut(get_icon("Blend", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/merge_from_scene"), TOOL_MERGE_FROM_SCENE);
  1391. menu->add_icon_shortcut(get_icon("CreateNewSceneFrom", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/save_branch_as_scene"), TOOL_NEW_SCENE_FROM);
  1392. menu->add_separator();
  1393. menu->add_icon_shortcut(get_icon("CopyNodePath", "EditorIcons"), ED_GET_SHORTCUT("scene_tree/copy_node_path"), TOOL_COPY_NODE_PATH);
  1394. bool is_external = (selection[0]->get_filename() != "");
  1395. if (is_external) {
  1396. bool is_inherited = selection[0]->get_scene_inherited_state() != NULL;
  1397. bool is_top_level = selection[0]->get_owner() == NULL;
  1398. if (is_inherited && is_top_level) {
  1399. menu->add_separator();
  1400. menu->add_item(TTR("Clear Inheritance"), TOOL_SCENE_CLEAR_INHERITANCE);
  1401. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN_INHERITED);
  1402. } else if (!is_top_level) {
  1403. menu->add_separator();
  1404. bool editable = EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(selection[0]);
  1405. bool placeholder = selection[0]->get_scene_instance_load_placeholder();
  1406. menu->add_check_item(TTR("Editable Children"), TOOL_SCENE_EDITABLE_CHILDREN);
  1407. menu->add_check_item(TTR("Load As Placeholder"), TOOL_SCENE_USE_PLACEHOLDER);
  1408. menu->add_item(TTR("Discard Instancing"), TOOL_SCENE_CLEAR_INSTANCING);
  1409. menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Open in Editor"), TOOL_SCENE_OPEN);
  1410. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Editable Children")), editable);
  1411. menu->set_item_checked(menu->get_item_idx_from_text(TTR("Load As Placeholder")), placeholder);
  1412. }
  1413. }
  1414. }
  1415. menu->add_separator();
  1416. menu->add_icon_shortcut(get_icon("Remove", "EditorIcons"), ED_SHORTCUT("scene_tree/delete", TTR("Delete Node(s)"), KEY_DELETE), TOOL_ERASE);
  1417. menu->set_size(Size2(1, 1));
  1418. menu->set_position(p_menu_pos);
  1419. menu->popup();
  1420. }
  1421. void SceneTreeDock::_filter_changed(const String &p_filter) {
  1422. scene_tree->set_filter(p_filter);
  1423. }
  1424. String SceneTreeDock::get_filter() {
  1425. return filter->get_text();
  1426. }
  1427. void SceneTreeDock::set_filter(const String &p_filter) {
  1428. filter->set_text(p_filter);
  1429. scene_tree->set_filter(p_filter);
  1430. }
  1431. void SceneTreeDock::_focus_node() {
  1432. Node *node = scene_tree->get_selected();
  1433. ERR_FAIL_COND(!node);
  1434. if (node->is_class("CanvasItem")) {
  1435. CanvasItemEditorPlugin *editor = Object::cast_to<CanvasItemEditorPlugin>(editor_data->get_editor("2D"));
  1436. editor->get_canvas_item_editor()->focus_selection();
  1437. } else {
  1438. SpatialEditorPlugin *editor = Object::cast_to<SpatialEditorPlugin>(editor_data->get_editor("3D"));
  1439. editor->get_spatial_editor()->get_editor_viewport(0)->focus_selection();
  1440. }
  1441. }
  1442. void SceneTreeDock::open_script_dialog(Node *p_for_node) {
  1443. scene_tree->set_selected(p_for_node, false);
  1444. _tool_selected(TOOL_ATTACH_SCRIPT);
  1445. }
  1446. void SceneTreeDock::_bind_methods() {
  1447. ClassDB::bind_method(D_METHOD("_tool_selected"), &SceneTreeDock::_tool_selected, DEFVAL(false));
  1448. ClassDB::bind_method(D_METHOD("_create"), &SceneTreeDock::_create);
  1449. ClassDB::bind_method(D_METHOD("_node_reparent"), &SceneTreeDock::_node_reparent);
  1450. ClassDB::bind_method(D_METHOD("_set_owners"), &SceneTreeDock::_set_owners);
  1451. ClassDB::bind_method(D_METHOD("_node_selected"), &SceneTreeDock::_node_selected);
  1452. ClassDB::bind_method(D_METHOD("_node_renamed"), &SceneTreeDock::_node_renamed);
  1453. ClassDB::bind_method(D_METHOD("_script_created"), &SceneTreeDock::_script_created);
  1454. ClassDB::bind_method(D_METHOD("_load_request"), &SceneTreeDock::_load_request);
  1455. ClassDB::bind_method(D_METHOD("_script_open_request"), &SceneTreeDock::_script_open_request);
  1456. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &SceneTreeDock::_unhandled_key_input);
  1457. ClassDB::bind_method(D_METHOD("_input"), &SceneTreeDock::_input);
  1458. ClassDB::bind_method(D_METHOD("_nodes_drag_begin"), &SceneTreeDock::_nodes_drag_begin);
  1459. ClassDB::bind_method(D_METHOD("_delete_confirm"), &SceneTreeDock::_delete_confirm);
  1460. ClassDB::bind_method(D_METHOD("_node_prerenamed"), &SceneTreeDock::_node_prerenamed);
  1461. ClassDB::bind_method(D_METHOD("_import_subscene"), &SceneTreeDock::_import_subscene);
  1462. ClassDB::bind_method(D_METHOD("_selection_changed"), &SceneTreeDock::_selection_changed);
  1463. ClassDB::bind_method(D_METHOD("_new_scene_from"), &SceneTreeDock::_new_scene_from);
  1464. ClassDB::bind_method(D_METHOD("_nodes_dragged"), &SceneTreeDock::_nodes_dragged);
  1465. ClassDB::bind_method(D_METHOD("_files_dropped"), &SceneTreeDock::_files_dropped);
  1466. ClassDB::bind_method(D_METHOD("_script_dropped"), &SceneTreeDock::_script_dropped);
  1467. ClassDB::bind_method(D_METHOD("_tree_rmb"), &SceneTreeDock::_tree_rmb);
  1468. ClassDB::bind_method(D_METHOD("_filter_changed"), &SceneTreeDock::_filter_changed);
  1469. ClassDB::bind_method(D_METHOD("_focus_node"), &SceneTreeDock::_focus_node);
  1470. ClassDB::bind_method(D_METHOD("instance"), &SceneTreeDock::instance);
  1471. }
  1472. SceneTreeDock::SceneTreeDock(EditorNode *p_editor, Node *p_scene_root, EditorSelection *p_editor_selection, EditorData &p_editor_data) {
  1473. editor = p_editor;
  1474. edited_scene = NULL;
  1475. editor_data = &p_editor_data;
  1476. editor_selection = p_editor_selection;
  1477. scene_root = p_scene_root;
  1478. VBoxContainer *vbc = this;
  1479. HBoxContainer *filter_hbc = memnew(HBoxContainer);
  1480. filter_hbc->add_constant_override("separate", 0);
  1481. ToolButton *tb;
  1482. ED_SHORTCUT("scene_tree/add_child_node", TTR("Add Child Node"), KEY_MASK_CMD | KEY_A);
  1483. ED_SHORTCUT("scene_tree/instance_scene", TTR("Instance Child Scene"));
  1484. ED_SHORTCUT("scene_tree/change_node_type", TTR("Change Type"));
  1485. ED_SHORTCUT("scene_tree/attach_script", TTR("Attach Script"));
  1486. ED_SHORTCUT("scene_tree/clear_script", TTR("Clear Script"));
  1487. ED_SHORTCUT("scene_tree/move_up", TTR("Move Up"), KEY_MASK_CMD | KEY_UP);
  1488. ED_SHORTCUT("scene_tree/move_down", TTR("Move Down"), KEY_MASK_CMD | KEY_DOWN);
  1489. ED_SHORTCUT("scene_tree/duplicate", TTR("Duplicate"), KEY_MASK_CMD | KEY_D);
  1490. ED_SHORTCUT("scene_tree/reparent", TTR("Reparent"));
  1491. ED_SHORTCUT("scene_tree/merge_from_scene", TTR("Merge From Scene"));
  1492. ED_SHORTCUT("scene_tree/save_branch_as_scene", TTR("Save Branch as Scene"));
  1493. ED_SHORTCUT("scene_tree/copy_node_path", TTR("Copy Node Path"), KEY_MASK_CMD | KEY_C);
  1494. ED_SHORTCUT("scene_tree/delete_no_confirm", TTR("Delete (No Confirm)"), KEY_MASK_SHIFT | KEY_DELETE);
  1495. ED_SHORTCUT("scene_tree/delete", TTR("Delete"), KEY_DELETE);
  1496. tb = memnew(ToolButton);
  1497. tb->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW, false));
  1498. tb->set_tooltip(TTR("Add/Create a New Node"));
  1499. tb->set_shortcut(ED_GET_SHORTCUT("scene_tree/add_child_node"));
  1500. filter_hbc->add_child(tb);
  1501. button_add = tb;
  1502. tb = memnew(ToolButton);
  1503. tb->connect("pressed", this, "_tool_selected", make_binds(TOOL_INSTANCE, false));
  1504. tb->set_tooltip(TTR("Instance a scene file as a Node. Creates an inherited scene if no root node exists."));
  1505. tb->set_shortcut(ED_GET_SHORTCUT("scene_tree/instance_scene"));
  1506. filter_hbc->add_child(tb);
  1507. button_instance = tb;
  1508. vbc->add_child(filter_hbc);
  1509. filter = memnew(LineEdit);
  1510. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1511. filter->set_placeholder(TTR("Filter nodes"));
  1512. filter_hbc->add_child(filter);
  1513. filter->connect("text_changed", this, "_filter_changed");
  1514. tb = memnew(ToolButton);
  1515. tb->connect("pressed", this, "_tool_selected", make_binds(TOOL_ATTACH_SCRIPT, false));
  1516. tb->set_tooltip(TTR("Attach a new or existing script for the selected node."));
  1517. tb->set_shortcut(ED_GET_SHORTCUT("scene_tree/attach_script"));
  1518. filter_hbc->add_child(tb);
  1519. tb->hide();
  1520. button_create_script = tb;
  1521. tb = memnew(ToolButton);
  1522. tb->connect("pressed", this, "_tool_selected", make_binds(TOOL_CLEAR_SCRIPT, false));
  1523. tb->set_tooltip(TTR("Clear a script for the selected node."));
  1524. tb->set_shortcut(ED_GET_SHORTCUT("scene_tree/clear_script"));
  1525. filter_hbc->add_child(tb);
  1526. button_clear_script = tb;
  1527. tb->hide();
  1528. scene_tree = memnew(SceneTreeEditor(false, true, true));
  1529. vbc->add_child(scene_tree);
  1530. scene_tree->set_v_size_flags(SIZE_EXPAND | SIZE_FILL);
  1531. scene_tree->connect("rmb_pressed", this, "_tree_rmb");
  1532. scene_tree->connect("node_selected", this, "_node_selected", varray(), CONNECT_DEFERRED);
  1533. scene_tree->connect("node_renamed", this, "_node_renamed", varray(), CONNECT_DEFERRED);
  1534. scene_tree->connect("node_prerename", this, "_node_prerenamed");
  1535. scene_tree->connect("open", this, "_load_request");
  1536. scene_tree->connect("open_script", this, "_script_open_request");
  1537. scene_tree->connect("nodes_rearranged", this, "_nodes_dragged");
  1538. scene_tree->connect("files_dropped", this, "_files_dropped");
  1539. scene_tree->connect("script_dropped", this, "_script_dropped");
  1540. scene_tree->connect("nodes_dragged", this, "_nodes_drag_begin");
  1541. scene_tree->get_scene_tree()->connect("item_double_clicked", this, "_focus_node");
  1542. scene_tree->set_undo_redo(&editor_data->get_undo_redo());
  1543. scene_tree->set_editor_selection(editor_selection);
  1544. create_dialog = memnew(CreateDialog);
  1545. create_dialog->set_base_type("Node");
  1546. add_child(create_dialog);
  1547. create_dialog->connect("create", this, "_create");
  1548. script_create_dialog = memnew(ScriptCreateDialog);
  1549. add_child(script_create_dialog);
  1550. script_create_dialog->connect("script_created", this, "_script_created");
  1551. reparent_dialog = memnew(ReparentDialog);
  1552. add_child(reparent_dialog);
  1553. reparent_dialog->connect("reparent", this, "_node_reparent");
  1554. accept = memnew(AcceptDialog);
  1555. add_child(accept);
  1556. file = memnew(EditorFileDialog);
  1557. add_child(file);
  1558. file->connect("file_selected", this, "instance");
  1559. set_process_unhandled_key_input(true);
  1560. delete_dialog = memnew(ConfirmationDialog);
  1561. add_child(delete_dialog);
  1562. delete_dialog->connect("confirmed", this, "_delete_confirm");
  1563. import_subscene_dialog = memnew(EditorSubScene);
  1564. add_child(import_subscene_dialog);
  1565. import_subscene_dialog->connect("subscene_selected", this, "_import_subscene");
  1566. new_scene_from_dialog = memnew(EditorFileDialog);
  1567. new_scene_from_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1568. add_child(new_scene_from_dialog);
  1569. new_scene_from_dialog->connect("file_selected", this, "_new_scene_from");
  1570. menu = memnew(PopupMenu);
  1571. add_child(menu);
  1572. menu->connect("id_pressed", this, "_tool_selected");
  1573. first_enter = true;
  1574. restore_script_editor_on_drag = false;
  1575. clear_inherit_confirm = memnew(ConfirmationDialog);
  1576. clear_inherit_confirm->set_text(TTR("Clear Inheritance? (No Undo!)"));
  1577. clear_inherit_confirm->get_ok()->set_text(TTR("Clear!"));
  1578. add_child(clear_inherit_confirm);
  1579. set_process_input(true);
  1580. }