scene_tree_dock.cpp 60 KB

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