scene_tree_dock.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*************************************************************************/
  2. /* scene_tree_dock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "scene_tree_dock.h"
  30. #include "editor_node.h"
  31. #include "globals.h"
  32. #include "os/keyboard.h"
  33. #include "scene/resources/packed_scene.h"
  34. #include "editor_settings.h"
  35. #include "tools/editor/plugins/canvas_item_editor_plugin.h"
  36. void SceneTreeDock::_unhandled_key_input(InputEvent p_event) {
  37. uint32_t sc = p_event.key.get_scancode_with_modifiers();
  38. if (!p_event.key.pressed || p_event.key.echo)
  39. return;
  40. switch(sc) {
  41. case KEY_MASK_CMD|KEY_A: { _tool_selected(TOOL_NEW); } break;
  42. case KEY_MASK_CMD|KEY_D: { _tool_selected(TOOL_DUPLICATE); } break;
  43. case KEY_MASK_CMD|KEY_UP: { _tool_selected(TOOL_MOVE_UP); } break;
  44. case KEY_MASK_CMD|KEY_DOWN: { _tool_selected(TOOL_MOVE_DOWN); } break;
  45. case KEY_MASK_SHIFT|KEY_DELETE: { _tool_selected(TOOL_ERASE, true); } break;
  46. case KEY_DELETE: { _tool_selected(TOOL_ERASE); } break;
  47. }
  48. }
  49. Node* SceneTreeDock::instance(const String& p_file) {
  50. Node *parent = scene_tree->get_selected();
  51. if (!parent || !edited_scene) {
  52. current_option=-1;
  53. //accept->get_cancel()->hide();
  54. accept->get_ok()->set_text("Ok :( ");
  55. accept->set_text("No parent to instance a child at.");
  56. accept->popup_centered(Size2(300,70));
  57. return NULL;
  58. };
  59. ERR_FAIL_COND_V(!parent,NULL);
  60. Node*instanced_scene=NULL;
  61. Ref<PackedScene> sdata = ResourceLoader::load(p_file);
  62. if (sdata.is_valid())
  63. instanced_scene=sdata->instance();
  64. if (!instanced_scene) {
  65. current_option=-1;
  66. //accept->get_cancel()->hide();
  67. accept->get_ok()->set_text("Ugh");
  68. accept->set_text(String("Error loading scene from ")+p_file);
  69. accept->popup_centered(Size2(300,70));;
  70. return NULL;
  71. }
  72. instanced_scene->generate_instance_state();
  73. instanced_scene->set_filename( Globals::get_singleton()->localize_path(p_file) );
  74. editor_data->get_undo_redo().create_action("Instance Scene");
  75. editor_data->get_undo_redo().add_do_method(parent,"add_child",instanced_scene);
  76. editor_data->get_undo_redo().add_do_method(instanced_scene,"set_owner",edited_scene);
  77. editor_data->get_undo_redo().add_do_method(editor_selection,"clear");
  78. editor_data->get_undo_redo().add_do_method(editor_selection,"add_node",instanced_scene);
  79. editor_data->get_undo_redo().add_do_reference(instanced_scene);
  80. editor_data->get_undo_redo().add_undo_method(parent,"remove_child",instanced_scene);
  81. editor_data->get_undo_redo().commit_action();
  82. return instanced_scene;
  83. }
  84. void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
  85. current_option=p_tool;
  86. switch(p_tool) {
  87. case TOOL_NEW: {
  88. create_dialog->popup_centered_ratio();
  89. } break;
  90. case TOOL_INSTANCE: {
  91. Node *scene = edited_scene;
  92. if (!scene) {
  93. current_option=-1;
  94. //confirmation->get_cancel()->hide();
  95. accept->get_ok()->set_text("I see..");
  96. accept->set_text("This operation can't be done without a tree root.");
  97. accept->popup_centered(Size2(300,70));;
  98. break;
  99. }
  100. file->set_mode(FileDialog::MODE_OPEN_FILE);
  101. List<String> extensions;
  102. ResourceLoader::get_recognized_extensions_for_type("PackedScene",&extensions);
  103. file->clear_filters();
  104. for(int i=0;i<extensions.size();i++) {
  105. file->add_filter("*."+extensions[i]+" ; "+extensions[i].to_upper());
  106. }
  107. //file->set_current_path(current_path);
  108. file->popup_centered_ratio();
  109. } break;
  110. case TOOL_REPLACE: {
  111. create_dialog->popup_centered_ratio();
  112. } break;
  113. case TOOL_CONNECT: {
  114. Node *current = scene_tree->get_selected();
  115. if (!current)
  116. break;
  117. connect_dialog->popup_centered_ratio();
  118. connect_dialog->set_node(current);
  119. } break;
  120. case TOOL_GROUP: {
  121. Node *current = scene_tree->get_selected();
  122. if (!current)
  123. break;
  124. groups_editor->set_current(current);
  125. groups_editor->popup_centered_ratio();
  126. } break;
  127. case TOOL_SCRIPT: {
  128. Node *selected = scene_tree->get_selected();
  129. if (!selected)
  130. break;
  131. Ref<Script> existing = selected->get_script();
  132. if (existing.is_valid())
  133. editor->push_item(existing.ptr());
  134. else {
  135. String path = selected->get_filename();
  136. script_create_dialog->config(selected->get_type(),path);
  137. script_create_dialog->popup_centered(Size2(300,290));
  138. //script_create_dialog->popup_centered_minsize();
  139. }
  140. } break;
  141. case TOOL_MOVE_UP:
  142. case TOOL_MOVE_DOWN: {
  143. if (!scene_tree->get_selected())
  144. break;
  145. if (scene_tree->get_selected()==edited_scene) {
  146. current_option=-1;
  147. //accept->get_cancel()->hide();
  148. accept->get_ok()->set_text("I see..");
  149. accept->set_text("This operation can't be done on the tree root.");
  150. accept->popup_centered(Size2(300,70));;
  151. break;
  152. }
  153. Node * node=scene_tree->get_selected();
  154. ERR_FAIL_COND(!node->get_parent());
  155. int current_pos = node->get_index();
  156. int next_pos = current_pos + ((p_tool==TOOL_MOVE_DOWN)?1:-1);
  157. if (next_pos< 0 || next_pos>=node->get_parent()->get_child_count())
  158. break; // invalid position
  159. editor_data->get_undo_redo().create_action("Move Node In Parent");
  160. editor_data->get_undo_redo().add_do_method(node->get_parent(),"move_child",node,next_pos);
  161. editor_data->get_undo_redo().add_undo_method(node->get_parent(),"move_child",node,current_pos);
  162. editor_data->get_undo_redo().commit_action();
  163. } break;
  164. case TOOL_DUPLICATE: {
  165. if (!edited_scene)
  166. break;
  167. if (editor_selection->is_selected(edited_scene)) {
  168. current_option=-1;
  169. //accept->get_cancel()->hide();
  170. accept->get_ok()->set_text("I see..");
  171. accept->set_text("This operation can't be done on the tree root.");
  172. accept->popup_centered(Size2(300,70));;
  173. break;
  174. }
  175. List<Node*> selection = editor_selection->get_selected_node_list();
  176. List<Node*> reselect;
  177. editor_data->get_undo_redo().create_action("Duplicate Node(s)");
  178. editor_data->get_undo_redo().add_do_method(editor_selection,"clear");
  179. Node *dupsingle=NULL;
  180. for (List<Node*>::Element *E=selection.front();E;E=E->next()) {
  181. Node *node = E->get();
  182. Node *parent = node->get_parent();
  183. List<Node*> owned;
  184. node->get_owned_by(node->get_owner(),&owned);
  185. Map<Node*,Node*> duplimap;
  186. Node * dup = _duplicate(node,duplimap);
  187. ERR_CONTINUE(!dup);
  188. if (selection.size()==1)
  189. dupsingle=dup;
  190. String name = node->get_name();
  191. String nums;
  192. for(int i=name.length()-1;i>=0;i--) {
  193. CharType n=name[i];
  194. if (n>='0' && n<='9') {
  195. nums=String::chr(name[i])+nums;
  196. } else {
  197. break;
  198. }
  199. }
  200. int num=nums.to_int();
  201. if (num<2)
  202. num=2;
  203. else
  204. num++;
  205. name=name.substr(0,name.length()-nums.length()).strip_edges();
  206. String attempt=name+" "+itos(num);
  207. while(parent->has_node(attempt)) {
  208. num++;
  209. attempt=name+" "+itos(num);
  210. }
  211. dup->set_name(attempt);
  212. editor_data->get_undo_redo().add_do_method(parent,"add_child",dup);
  213. for (List<Node*>::Element *F=owned.front();F;F=F->next()) {
  214. if (!duplimap.has(F->get())) {
  215. continue;
  216. }
  217. Node *d=duplimap[F->get()];
  218. editor_data->get_undo_redo().add_do_method(d,"set_owner",node->get_owner());
  219. }
  220. editor_data->get_undo_redo().add_do_method(editor_selection,"add_node",dup);
  221. editor_data->get_undo_redo().add_undo_method(parent,"remove_child",dup);
  222. editor_data->get_undo_redo().add_do_reference(dup);
  223. //parent->add_child(dup);
  224. //reselect.push_back(dup);
  225. }
  226. editor_data->get_undo_redo().commit_action();
  227. if (dupsingle)
  228. editor->push_item(dupsingle);
  229. } break;
  230. case TOOL_REPARENT: {
  231. if (!scene_tree->get_selected())
  232. break;
  233. if (editor_selection->is_selected(edited_scene)) {
  234. current_option=-1;
  235. //confirmation->get_cancel()->hide();
  236. accept->get_ok()->set_text("I see..");
  237. accept->set_text("This operation can't be done on the tree root.");
  238. accept->popup_centered(Size2(300,70));;
  239. break;
  240. }
  241. List<Node*> nodes = editor_selection->get_selected_node_list();
  242. Set<Node*> nodeset;
  243. for(List<Node*>::Element *E=nodes.front();E;E=E->next()) {
  244. nodeset.insert(E->get());
  245. }
  246. reparent_dialog->popup_centered_ratio();
  247. reparent_dialog->set_current( nodeset );
  248. } break;
  249. case TOOL_ERASE: {
  250. List<Node*> remove_list = editor_selection->get_selected_node_list();
  251. if (remove_list.empty())
  252. return;
  253. if (p_confirm_override) {
  254. _delete_confirm();
  255. // hack, force 2d editor viewport to refresh after deletion
  256. if (CanvasItemEditor *editor = CanvasItemEditor::get_singleton())
  257. editor->get_viewport_control()->update();
  258. } else {
  259. delete_dialog->set_text("Delete Node(s)?");
  260. delete_dialog->popup_centered(Size2(200,80));
  261. }
  262. } break;
  263. }
  264. }
  265. void SceneTreeDock::_notification(int p_what) {
  266. switch(p_what) {
  267. case NOTIFICATION_ENTER_SCENE: {
  268. static const char* button_names[TOOL_BUTTON_MAX]={
  269. "New",
  270. "Add",
  271. "Replace",
  272. "Connect",
  273. "Groups",
  274. "Script",
  275. "MoveUp",
  276. "MoveDown",
  277. "Duplicate",
  278. "Reparent",
  279. "Del",
  280. };
  281. for(int i=0;i<TOOL_BUTTON_MAX;i++)
  282. tool_buttons[i]->set_icon(get_icon(button_names[i],"EditorIcons"));
  283. } break;
  284. case NOTIFICATION_READY: {
  285. CanvasItemEditorPlugin *canvas_item_plugin = editor_data->get_editor("2D")->cast_to<CanvasItemEditorPlugin>();
  286. if (canvas_item_plugin) {
  287. canvas_item_plugin->get_canvas_item_editor()->connect("item_lock_status_changed", scene_tree, "_update_tree");
  288. canvas_item_plugin->get_canvas_item_editor()->connect("item_group_status_changed", scene_tree, "_update_tree");
  289. scene_tree->connect("node_changed", canvas_item_plugin->get_canvas_item_editor()->get_viewport_control(), "update");
  290. }
  291. } break;
  292. }
  293. }
  294. void SceneTreeDock::_load_request(const String& p_path) {
  295. editor->open_request(p_path);
  296. }
  297. void SceneTreeDock::_script_open_request(const Ref<Script>& p_script) {
  298. editor->edit_resource(p_script);
  299. }
  300. void SceneTreeDock::_node_selected() {
  301. Node *node=scene_tree->get_selected();
  302. if (!node) {
  303. editor->push_item(NULL);
  304. return;
  305. }
  306. editor->push_item(node);
  307. }
  308. Node *SceneTreeDock::_duplicate(Node *p_node, Map<Node*,Node*> &duplimap) {
  309. Node *node=NULL;
  310. if (p_node->get_filename()!="") { //an instance
  311. Ref<PackedScene> sd = ResourceLoader::load( p_node->get_filename() );
  312. ERR_FAIL_COND_V(!sd.is_valid(),NULL);
  313. node = sd->instance();
  314. ERR_FAIL_COND_V(!node,NULL);
  315. node->generate_instance_state();
  316. } else {
  317. Object *obj = ObjectTypeDB::instance(p_node->get_type());
  318. ERR_FAIL_COND_V(!obj,NULL);
  319. node = obj->cast_to<Node>();
  320. if (!node)
  321. memdelete(obj);
  322. ERR_FAIL_COND_V(!node,NULL);
  323. }
  324. List<PropertyInfo> plist;
  325. p_node->get_property_list(&plist);
  326. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  327. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  328. continue;
  329. String name = E->get().name;
  330. node->set( name, p_node->get(name) );
  331. }
  332. node->set_name(p_node->get_name());
  333. duplimap[p_node]=node;
  334. for(int i=0;i<p_node->get_child_count();i++) {
  335. Node *child = p_node->get_child(i);
  336. if (p_node->get_owner()!=child->get_owner())
  337. continue; //don't bother with not in-scene nodes.
  338. Node *dup = _duplicate(child,duplimap);
  339. if (!dup) {
  340. memdelete(node);
  341. return NULL;
  342. }
  343. node->add_child(dup);
  344. }
  345. return node;
  346. }
  347. void SceneTreeDock::_set_owners(Node *p_owner, const Array& p_nodes) {
  348. for(int i=0;i<p_nodes.size();i++) {
  349. Object *obj=p_nodes[i];
  350. if (!obj)
  351. continue;
  352. Node *n=obj->cast_to<Node>();
  353. if (!n)
  354. continue;
  355. n->set_owner(p_owner);
  356. }
  357. }
  358. void SceneTreeDock::_fill_path_renames(Vector<StringName> base_path,Vector<StringName> new_base_path,Node * p_node, List<Pair<NodePath,NodePath> > *p_renames) {
  359. base_path.push_back(p_node->get_name());
  360. if (new_base_path.size())
  361. new_base_path.push_back(p_node->get_name());
  362. NodePath from( base_path,true );
  363. NodePath to;
  364. if (new_base_path.size())
  365. to=NodePath( new_base_path,true );
  366. Pair<NodePath,NodePath> npp;
  367. npp.first=from;
  368. npp.second=to;
  369. p_renames->push_back(npp);
  370. for(int i=0;i<p_node->get_child_count();i++) {
  371. _fill_path_renames(base_path,new_base_path,p_node->get_child(i),p_renames);
  372. }
  373. }
  374. void SceneTreeDock::fill_path_renames(Node* p_node, Node *p_new_parent, List<Pair<NodePath,NodePath> > *p_renames) {
  375. if (!bool(EDITOR_DEF("animation/autorename_animation_tracks",true)))
  376. return;
  377. Vector<StringName> base_path;
  378. Node *n = p_node->get_parent();
  379. while(n) {
  380. base_path.push_back(n->get_name());
  381. n=n->get_parent();
  382. }
  383. base_path.invert();
  384. Vector<StringName> new_base_path;
  385. if (p_new_parent) {
  386. n = p_new_parent;
  387. while(n) {
  388. new_base_path.push_back(n->get_name());
  389. n=n->get_parent();
  390. }
  391. new_base_path.invert();
  392. }
  393. _fill_path_renames(base_path,new_base_path,p_node,p_renames);
  394. }
  395. void SceneTreeDock::perform_node_renames(Node* p_base,List<Pair<NodePath,NodePath> > *p_renames, Map<Ref<Animation>, Set<int> > *r_rem_anims) {
  396. Map<Ref<Animation>, Set<int> > rem_anims;
  397. if (!r_rem_anims)
  398. r_rem_anims=&rem_anims;
  399. if (!bool(EDITOR_DEF("animation/autorename_animation_tracks",true)))
  400. return;
  401. if (!p_base) {
  402. p_base=edited_scene;
  403. }
  404. if (!p_base)
  405. return;
  406. if (p_base->cast_to<AnimationPlayer>()) {
  407. AnimationPlayer *ap=p_base->cast_to<AnimationPlayer>();
  408. List<StringName> anims;
  409. ap->get_animation_list(&anims);
  410. Node *root = ap->get_node(ap->get_root());
  411. if (root) {
  412. NodePath root_path=root->get_path();
  413. NodePath new_root_path=root_path;
  414. for(List<Pair<NodePath,NodePath> >::Element* E=p_renames->front();E;E=E->next()) {
  415. if (E->get().first==root_path) {
  416. new_root_path=E->get().second;
  417. break;
  418. }
  419. }
  420. if (new_root_path!=NodePath()) {
  421. //will not be erased
  422. for(List<StringName>::Element *E=anims.front();E;E=E->next()) {
  423. Ref<Animation> anim=ap->get_animation(E->get());
  424. if (!r_rem_anims->has(anim)) {
  425. r_rem_anims->insert(anim,Set<int>());
  426. Set<int> &ran = r_rem_anims->find(anim)->get();
  427. for(int i=0;i<anim->get_track_count();i++)
  428. ran.insert(i);
  429. }
  430. Set<int> &ran = r_rem_anims->find(anim)->get();
  431. if (anim.is_null())
  432. continue;
  433. for(int i=0;i<anim->get_track_count();i++) {
  434. NodePath track_np=anim->track_get_path(i);
  435. Node *n = root->get_node(track_np);
  436. if (!n) {
  437. continue;
  438. }
  439. NodePath old_np = n->get_path();
  440. if (!ran.has(i))
  441. continue; //channel was removed
  442. for(List<Pair<NodePath,NodePath> >::Element* E=p_renames->front();E;E=E->next()) {
  443. if (E->get().first==old_np) {
  444. if (E->get().second==NodePath()) {
  445. //will be erased
  446. int idx=0;
  447. Set<int>::Element *EI=ran.front();
  448. ERR_FAIL_COND(!EI); //bug
  449. while(EI->get()!=i) {
  450. idx++;
  451. EI=EI->next();
  452. ERR_FAIL_COND(!EI); //another bug
  453. }
  454. editor_data->get_undo_redo().add_do_method(anim.ptr(),"remove_track",idx);
  455. editor_data->get_undo_redo().add_undo_method(anim.ptr(),"add_track",anim->track_get_type(i),idx);
  456. editor_data->get_undo_redo().add_undo_method(anim.ptr(),"track_set_path",idx,track_np);
  457. editor_data->get_undo_redo().add_undo_method(anim.ptr(),"track_set_interpolation_type",idx,anim->track_get_interpolation_type(i));
  458. for(int j=0;j<anim->track_get_key_count(i);j++) {
  459. 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));
  460. }
  461. ran.erase(i); //byebye channel
  462. } else {
  463. //will be renamed
  464. NodePath rel_path = new_root_path.rel_path_to(E->get().second);
  465. NodePath new_path = NodePath( rel_path.get_names(), track_np.get_subnames(), false, track_np.get_property() );
  466. if (new_path==track_np)
  467. continue; //bleh
  468. editor_data->get_undo_redo().add_do_method(anim.ptr(),"track_set_path",i,new_path);
  469. editor_data->get_undo_redo().add_undo_method(anim.ptr(),"track_set_path",i,track_np);
  470. }
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. }
  478. for(int i=0;i<p_base->get_child_count();i++)
  479. perform_node_renames(p_base->get_child(i),p_renames,r_rem_anims);
  480. }
  481. void SceneTreeDock::_node_prerenamed(Node* p_node, const String& p_new_name) {
  482. List<Pair<NodePath,NodePath> > path_renames;
  483. Vector<StringName> base_path;
  484. Node *n = p_node->get_parent();
  485. while(n) {
  486. base_path.push_back(n->get_name());
  487. n=n->get_parent();
  488. }
  489. base_path.invert();
  490. Vector<StringName> new_base_path=base_path;
  491. base_path.push_back(p_node->get_name());
  492. new_base_path.push_back(p_new_name);
  493. Pair<NodePath,NodePath> npp;
  494. npp.first = NodePath(base_path,true);
  495. npp.second = NodePath(new_base_path,true);
  496. path_renames.push_back(npp);
  497. for(int i=0;i<p_node->get_child_count();i++)
  498. _fill_path_renames(base_path,new_base_path,p_node->get_child(i),&path_renames);
  499. perform_node_renames(NULL,&path_renames);
  500. }
  501. void SceneTreeDock::_node_reparent(NodePath p_path,bool p_node_only) {
  502. Node *node = scene_tree->get_selected();
  503. ERR_FAIL_COND(!node);
  504. ERR_FAIL_COND(node==edited_scene);
  505. Node *new_parent = scene_root->get_node(p_path);
  506. ERR_FAIL_COND(!new_parent);
  507. Node *validate=new_parent;
  508. while(validate) {
  509. if (editor_selection->is_selected(validate)) {
  510. ERR_EXPLAIN("Selection changed at some point.. can't reparent");
  511. ERR_FAIL();
  512. return;
  513. }
  514. validate=validate->get_parent();
  515. }
  516. //ok all valid
  517. List<Node*> selection = editor_selection->get_selected_node_list();
  518. if (selection.empty())
  519. return; //nothing to reparent
  520. //sort by tree order, so re-adding is easy
  521. selection.sort_custom<Node::Comparator>();
  522. editor_data->get_undo_redo().create_action("Reparent Node");
  523. List<Pair<NodePath,NodePath> > path_renames;
  524. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  525. //no undo for now, sorry
  526. Node *node = E->get();
  527. fill_path_renames(node,new_parent,&path_renames);
  528. List<Node*> owned;
  529. node->get_owned_by(node->get_owner(),&owned);
  530. Array owners;
  531. for(List<Node*>::Element *E=owned.front();E;E=E->next()) {
  532. owners.push_back(E->get());
  533. }
  534. editor_data->get_undo_redo().add_do_method(node->get_parent(),"remove_child",node);
  535. editor_data->get_undo_redo().add_do_method(new_parent,"add_child",node);
  536. editor_data->get_undo_redo().add_do_method(this,"_set_owners",edited_scene,owners);
  537. if (editor->get_animation_editor()->get_root()==node)
  538. editor_data->get_undo_redo().add_do_method(editor->get_animation_editor(),"set_root",node);
  539. editor_data->get_undo_redo().add_undo_method(new_parent,"remove_child",node);
  540. }
  541. //add and move in a second step.. (so old order is preserved)
  542. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  543. Node *node = E->get();
  544. List<Node*> owned;
  545. node->get_owned_by(node->get_owner(),&owned);
  546. Array owners;
  547. for(List<Node*>::Element *E=owned.front();E;E=E->next()) {
  548. owners.push_back(E->get());
  549. }
  550. int child_pos = node->get_position_in_parent();
  551. editor_data->get_undo_redo().add_undo_method(node->get_parent(),"add_child",node);
  552. editor_data->get_undo_redo().add_undo_method(node->get_parent(),"move_child",node,child_pos);
  553. editor_data->get_undo_redo().add_undo_method(this,"_set_owners",edited_scene,owners);
  554. if (editor->get_animation_editor()->get_root()==node)
  555. editor_data->get_undo_redo().add_undo_method(editor->get_animation_editor(),"set_root",node);
  556. }
  557. perform_node_renames(NULL,&path_renames);
  558. editor_data->get_undo_redo().commit_action();
  559. //node->set_owner(owner);
  560. }
  561. void SceneTreeDock::_script_created(Ref<Script> p_script) {
  562. Node *selected = scene_tree->get_selected();
  563. if (!selected)
  564. return;
  565. selected->set_script(p_script.get_ref_ptr());
  566. editor->push_item(p_script.operator->());
  567. }
  568. void SceneTreeDock::_delete_confirm() {
  569. List<Node*> remove_list = editor_selection->get_selected_node_list();
  570. if (remove_list.empty())
  571. return;
  572. if (editor->get_editor_plugin_over())
  573. editor->get_editor_plugin_over()->make_visible(false);
  574. editor_data->get_undo_redo().create_action("Remove Node(s)");
  575. bool entire_scene=false;
  576. for(List<Node*>::Element *E=remove_list.front();E;E=E->next()) {
  577. if (E->get()==edited_scene) {
  578. entire_scene=true;
  579. }
  580. }
  581. if (entire_scene) {
  582. editor_data->get_undo_redo().add_do_method(editor,"set_edited_scene",(Object*)NULL);
  583. editor_data->get_undo_redo().add_undo_method(editor,"set_edited_scene",edited_scene);
  584. editor_data->get_undo_redo().add_undo_method(edited_scene,"set_owner",edited_scene->get_owner());
  585. editor_data->get_undo_redo().add_undo_reference(edited_scene);
  586. } else {
  587. remove_list.sort_custom<Node::Comparator>(); //sort nodes to keep positions
  588. List<Pair<NodePath,NodePath> > path_renames;
  589. //delete from animation
  590. for(List<Node*>::Element *E=remove_list.front();E;E=E->next()) {
  591. Node *n = E->get();
  592. if (!n->is_inside_scene() || !n->get_parent())
  593. continue;
  594. fill_path_renames(n,NULL,&path_renames);
  595. }
  596. perform_node_renames(NULL,&path_renames);
  597. //delete for read
  598. for(List<Node*>::Element *E=remove_list.front();E;E=E->next()) {
  599. Node *n = E->get();
  600. if (!n->is_inside_scene() || !n->get_parent())
  601. continue;
  602. List<Node*> owned;
  603. n->get_owned_by(n->get_owner(),&owned);
  604. Array owners;
  605. for(List<Node*>::Element *E=owned.front();E;E=E->next()) {
  606. owners.push_back(E->get());
  607. }
  608. editor_data->get_undo_redo().add_do_method(n->get_parent(),"remove_child",n);
  609. editor_data->get_undo_redo().add_undo_method(n->get_parent(),"add_child",n);
  610. editor_data->get_undo_redo().add_undo_method(n->get_parent(),"move_child",n,n->get_index());
  611. if (editor->get_animation_editor()->get_root()==n)
  612. editor_data->get_undo_redo().add_undo_method(editor->get_animation_editor(),"set_root",n);
  613. editor_data->get_undo_redo().add_undo_method(this,"_set_owners",edited_scene,owners);
  614. //editor_data->get_undo_redo().add_undo_method(n,"set_owner",n->get_owner());
  615. editor_data->get_undo_redo().add_undo_reference(n);
  616. }
  617. }
  618. editor_data->get_undo_redo().commit_action();
  619. _update_tool_buttons();
  620. }
  621. void SceneTreeDock::_update_tool_buttons() {
  622. Node *sel = scene_tree->get_selected();
  623. bool disable = !sel;
  624. bool disable_root = disable || sel->get_parent()==scene_root;
  625. tool_buttons[TOOL_INSTANCE]->set_disabled(disable);
  626. tool_buttons[TOOL_REPLACE]->set_disabled(disable);
  627. tool_buttons[TOOL_CONNECT]->set_disabled(disable);
  628. tool_buttons[TOOL_GROUP]->set_disabled(disable);
  629. tool_buttons[TOOL_SCRIPT]->set_disabled(disable);
  630. tool_buttons[TOOL_MOVE_UP]->set_disabled(disable_root);
  631. tool_buttons[TOOL_MOVE_DOWN]->set_disabled(disable_root);
  632. tool_buttons[TOOL_DUPLICATE]->set_disabled(disable_root);
  633. tool_buttons[TOOL_REPARENT]->set_disabled(disable_root);
  634. tool_buttons[TOOL_ERASE]->set_disabled(disable);
  635. }
  636. void SceneTreeDock::_create() {
  637. if (current_option==TOOL_NEW) {
  638. Node *parent=NULL;
  639. if (edited_scene) {
  640. parent = scene_tree->get_selected();
  641. ERR_FAIL_COND(!parent);
  642. } else {
  643. parent = scene_root;
  644. ERR_FAIL_COND(!parent);
  645. }
  646. Object *c = create_dialog->instance_selected();
  647. ERR_FAIL_COND(!c);
  648. Node *child=c->cast_to<Node>();
  649. ERR_FAIL_COND(!child);
  650. editor_data->get_undo_redo().create_action("Create Node");
  651. if (edited_scene) {
  652. editor_data->get_undo_redo().add_do_method(parent,"add_child",child);
  653. editor_data->get_undo_redo().add_do_method(child,"set_owner",edited_scene);
  654. editor_data->get_undo_redo().add_do_method(editor_selection,"clear");
  655. editor_data->get_undo_redo().add_do_method(editor_selection,"add_node",child);
  656. editor_data->get_undo_redo().add_do_reference(child);
  657. editor_data->get_undo_redo().add_undo_method(parent,"remove_child",child);
  658. } else {
  659. editor_data->get_undo_redo().add_do_method(editor,"set_edited_scene",child);
  660. editor_data->get_undo_redo().add_do_reference(child);
  661. editor_data->get_undo_redo().add_undo_method(editor,"set_edited_scene",(Object*)NULL);
  662. }
  663. editor_data->get_undo_redo().commit_action();
  664. editor->push_item(c);
  665. if (c->cast_to<Control>()) {
  666. //make editor more comfortable, so some controls don't appear super shrunk
  667. Control *ct = c->cast_to<Control>();
  668. Size2 ms = ct->get_minimum_size();
  669. if (ms.width<4)
  670. ms.width=40;
  671. if (ms.height<4)
  672. ms.height=40;
  673. ct->set_size(ms);
  674. }
  675. } else if (current_option==TOOL_REPLACE) {
  676. Node * n = scene_tree->get_selected();
  677. ERR_FAIL_COND(!n);
  678. Object *c = create_dialog->instance_selected();
  679. ERR_FAIL_COND(!c);
  680. Node *newnode=c->cast_to<Node>();
  681. ERR_FAIL_COND(!newnode);
  682. List<PropertyInfo> pinfo;
  683. n->get_property_list(&pinfo);
  684. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  685. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  686. continue;
  687. newnode->set(E->get().name,n->get(E->get().name));
  688. }
  689. editor->push_item(NULL);
  690. //reconnect signals
  691. List<MethodInfo> sl;
  692. n->get_signal_list(&sl);
  693. for (List<MethodInfo>::Element *E=sl.front();E;E=E->next()) {
  694. List<Object::Connection> cl;
  695. n->get_signal_connection_list(E->get().name,&cl);
  696. for(List<Object::Connection>::Element *F=cl.front();F;F=F->next()) {
  697. Object::Connection &c=F->get();
  698. if (!(c.flags&Object::CONNECT_PERSIST))
  699. continue;
  700. newnode->connect(c.signal,c.target,c.method,varray(),Object::CONNECT_PERSIST);
  701. }
  702. }
  703. String newname=n->get_name();
  704. n->replace_by(newnode,true);
  705. if (n==edited_scene) {
  706. edited_scene=newnode;
  707. editor->set_edited_scene(newnode);
  708. }
  709. editor_data->get_undo_redo().clear_history();
  710. memdelete(n);
  711. newnode->set_name(newname);
  712. editor->push_item(newnode);
  713. _update_tool_buttons();
  714. }
  715. }
  716. void SceneTreeDock::set_edited_scene(Node* p_scene) {
  717. edited_scene=p_scene;
  718. }
  719. void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected ) {
  720. scene_tree->set_selected(p_node,p_emit_selected);
  721. _update_tool_buttons();
  722. }
  723. void SceneTreeDock::import_subscene() {
  724. import_subscene_dialog->popup_centered_ratio();
  725. }
  726. void SceneTreeDock::_import_subscene() {
  727. Node* parent = scene_tree->get_selected();
  728. ERR_FAIL_COND(!parent);
  729. import_subscene_dialog->move(parent,edited_scene);
  730. editor_data->get_undo_redo().clear_history(); //no undo for now..
  731. /*
  732. editor_data->get_undo_redo().create_action("Import Subscene");
  733. editor_data->get_undo_redo().add_do_method(parent,"add_child",ss);
  734. //editor_data->get_undo_redo().add_do_method(editor_selection,"clear");
  735. //editor_data->get_undo_redo().add_do_method(editor_selection,"add_node",child);
  736. editor_data->get_undo_redo().add_do_reference(ss);
  737. editor_data->get_undo_redo().add_undo_method(parent,"remove_child",ss);
  738. editor_data->get_undo_redo().commit_action();
  739. */
  740. }
  741. void SceneTreeDock::_bind_methods() {
  742. ObjectTypeDB::bind_method(_MD("_tool_selected"),&SceneTreeDock::_tool_selected);
  743. ObjectTypeDB::bind_method(_MD("_create"),&SceneTreeDock::_create);
  744. //ObjectTypeDB::bind_method(_MD("_script_created"),&SceneTreeDock::_script_created);
  745. ObjectTypeDB::bind_method(_MD("_node_reparent"),&SceneTreeDock::_node_reparent);
  746. ObjectTypeDB::bind_method(_MD("_set_owners"),&SceneTreeDock::_set_owners);
  747. ObjectTypeDB::bind_method(_MD("_node_selected"),&SceneTreeDock::_node_selected);
  748. ObjectTypeDB::bind_method(_MD("_script_created"),&SceneTreeDock::_script_created);
  749. ObjectTypeDB::bind_method(_MD("_load_request"),&SceneTreeDock::_load_request);
  750. ObjectTypeDB::bind_method(_MD("_script_open_request"),&SceneTreeDock::_script_open_request);
  751. ObjectTypeDB::bind_method(_MD("_unhandled_key_input"),&SceneTreeDock::_unhandled_key_input);
  752. ObjectTypeDB::bind_method(_MD("_delete_confirm"),&SceneTreeDock::_delete_confirm);
  753. ObjectTypeDB::bind_method(_MD("_node_prerenamed"),&SceneTreeDock::_node_prerenamed);
  754. ObjectTypeDB::bind_method(_MD("_import_subscene"),&SceneTreeDock::_import_subscene);
  755. ObjectTypeDB::bind_method(_MD("instance"),&SceneTreeDock::instance);
  756. }
  757. SceneTreeDock::SceneTreeDock(EditorNode *p_editor,Node *p_scene_root,EditorSelection *p_editor_selection,EditorData &p_editor_data) {
  758. editor=p_editor;
  759. edited_scene=NULL;
  760. editor_data=&p_editor_data;
  761. editor_selection=p_editor_selection;
  762. scene_root=p_scene_root;
  763. VBoxContainer *vbc = this;
  764. HBoxContainer *hbc_top = memnew( HBoxContainer );
  765. vbc->add_child(hbc_top);
  766. ToolButton *tb;
  767. tb = memnew( ToolButton );
  768. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_NEW, false));
  769. tb->set_tooltip("Add/Create a New Node\n("+keycode_get_string(KEY_MASK_CMD|KEY_A)+")");
  770. hbc_top->add_child(tb);
  771. tool_buttons[TOOL_NEW]=tb;
  772. tb = memnew( ToolButton );
  773. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_INSTANCE, false));
  774. tb->set_tooltip("Instance a Node from scene file.");
  775. hbc_top->add_child(tb);
  776. tool_buttons[TOOL_INSTANCE]=tb;
  777. tb = memnew( ToolButton );
  778. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_REPLACE, false));
  779. tb->set_tooltip("Replace a Node by Another Node Type");
  780. hbc_top->add_child(tb);
  781. tool_buttons[TOOL_REPLACE]=tb;
  782. hbc_top->add_spacer();
  783. tb = memnew( ToolButton );
  784. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_CONNECT, false));
  785. tb->set_tooltip("Edit the Node Connections");
  786. hbc_top->add_child(tb);
  787. tool_buttons[TOOL_CONNECT]=tb;
  788. tb = memnew( ToolButton );
  789. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_GROUP, false));
  790. tb->set_tooltip("Edit the Node Groups");
  791. hbc_top->add_child(tb);
  792. tool_buttons[TOOL_GROUP]=tb;
  793. tb = memnew( ToolButton );
  794. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_SCRIPT, false));
  795. tb->set_tooltip("Edit/Create the Node Script");
  796. hbc_top->add_child(tb);
  797. tool_buttons[TOOL_SCRIPT]=tb;
  798. scene_tree = memnew( SceneTreeEditor(false,true,true ));
  799. vbc->add_child(scene_tree);
  800. scene_tree->set_v_size_flags(SIZE_EXPAND|SIZE_FILL);
  801. scene_tree->connect("node_selected", this,"_node_selected",varray(),CONNECT_DEFERRED);
  802. scene_tree->connect("node_renamed", this,"_node_renamed",varray(),CONNECT_DEFERRED);
  803. scene_tree->connect("node_prerename", this,"_node_prerenamed");
  804. scene_tree->connect("open",this,"_load_request");
  805. scene_tree->connect("open_script",this,"_script_open_request");
  806. scene_tree->set_undo_redo(&editor_data->get_undo_redo());
  807. scene_tree->set_editor_selection(editor_selection);
  808. HBoxContainer *hbc_bottom = memnew( HBoxContainer );
  809. vbc->add_child(hbc_bottom);
  810. tb = memnew( ToolButton );
  811. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_MOVE_UP, false));
  812. tb->set_tooltip("Move Node Up\n("+keycode_get_string(KEY_MASK_CMD|KEY_UP)+")");
  813. hbc_bottom->add_child(tb);
  814. tool_buttons[TOOL_MOVE_UP]=tb;
  815. tb = memnew( ToolButton );
  816. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_MOVE_DOWN, false));
  817. tb->set_tooltip("Move Node Down\n("+keycode_get_string(KEY_MASK_CMD|KEY_DOWN)+")");
  818. hbc_bottom->add_child(tb);
  819. tool_buttons[TOOL_MOVE_DOWN]=tb;
  820. tb = memnew( ToolButton );
  821. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_DUPLICATE, false));
  822. tb->set_tooltip("Duplicate Selected Node(s)\n("+keycode_get_string(KEY_MASK_CMD|KEY_D)+")");
  823. hbc_bottom->add_child(tb);
  824. tool_buttons[TOOL_DUPLICATE]=tb;
  825. tb = memnew( ToolButton );
  826. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_REPARENT, false));
  827. tb->set_tooltip("Reparent Selected Node(s)");
  828. hbc_bottom->add_child(tb);
  829. tool_buttons[TOOL_REPARENT]=tb;
  830. hbc_bottom->add_spacer();
  831. tb = memnew( ToolButton );
  832. tb->connect("pressed",this,"_tool_selected",make_binds(TOOL_ERASE, false));
  833. tb->set_tooltip("Erase Selected Node(s)");
  834. hbc_bottom->add_child(tb);
  835. tool_buttons[TOOL_ERASE]=tb;
  836. create_dialog = memnew( CreateDialog );
  837. create_dialog->set_base_type("Node");
  838. add_child(create_dialog);
  839. create_dialog->connect("create",this,"_create");
  840. groups_editor = memnew( GroupsEditor );
  841. add_child(groups_editor);
  842. groups_editor->set_undo_redo(&editor_data->get_undo_redo());
  843. connect_dialog = memnew( ConnectionsDialog(p_editor) );
  844. add_child(connect_dialog);
  845. connect_dialog->set_undoredo(&editor_data->get_undo_redo());
  846. script_create_dialog = memnew( ScriptCreateDialog );
  847. add_child(script_create_dialog);
  848. script_create_dialog->connect("script_created",this,"_script_created");
  849. reparent_dialog = memnew( ReparentDialog );
  850. add_child(reparent_dialog);
  851. reparent_dialog->connect("reparent",this,"_node_reparent");
  852. accept = memnew( AcceptDialog );
  853. add_child(accept);
  854. file = memnew( FileDialog );
  855. add_child(file);
  856. file->connect("file_selected",this,"instance");
  857. set_process_unhandled_key_input(true);
  858. delete_dialog = memnew( ConfirmationDialog );
  859. add_child(delete_dialog);
  860. delete_dialog->connect("confirmed",this,"_delete_confirm");
  861. import_subscene_dialog = memnew( EditorSubScene );
  862. add_child(import_subscene_dialog);
  863. import_subscene_dialog->connect("subscene_selected",this,"_import_subscene");
  864. }