animation_state_machine_editor.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /*************************************************************************/
  2. /* animation_state_machine_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation_state_machine_editor.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/math/delaunay.h"
  33. #include "core/os/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/project_settings.h"
  36. #include "scene/animation/animation_blend_tree.h"
  37. #include "scene/animation/animation_player.h"
  38. #include "scene/gui/menu_button.h"
  39. #include "scene/gui/panel.h"
  40. #include "scene/main/viewport.h"
  41. bool AnimationNodeStateMachineEditor::can_edit(const Ref<AnimationNode> &p_node) {
  42. Ref<AnimationNodeStateMachine> ansm = p_node;
  43. return ansm.is_valid();
  44. }
  45. void AnimationNodeStateMachineEditor::edit(const Ref<AnimationNode> &p_node) {
  46. state_machine = p_node;
  47. if (state_machine.is_valid()) {
  48. selected_transition_from = StringName();
  49. selected_transition_to = StringName();
  50. selected_node = StringName();
  51. _update_mode();
  52. _update_graph();
  53. }
  54. }
  55. void AnimationNodeStateMachineEditor::_state_machine_gui_input(const Ref<InputEvent> &p_event) {
  56. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  57. if (playback.is_null())
  58. return;
  59. Ref<InputEventKey> k = p_event;
  60. if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_scancode() == KEY_DELETE && !k->is_echo()) {
  61. if (selected_node != StringName() || selected_transition_to != StringName() || selected_transition_from != StringName()) {
  62. _erase_selected();
  63. accept_event();
  64. }
  65. }
  66. Ref<InputEventMouseButton> mb = p_event;
  67. //Add new node
  68. if (mb.is_valid() && mb->is_pressed() && ((tool_select->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) || (tool_create->is_pressed() && mb->get_button_index() == BUTTON_LEFT))) {
  69. menu->clear();
  70. animations_menu->clear();
  71. animations_to_add.clear();
  72. List<StringName> classes;
  73. classes.sort_custom<StringName::AlphCompare>();
  74. ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
  75. menu->add_submenu_item(TTR("Add Animation"), "animations");
  76. AnimationTree *gp = AnimationTreeEditor::get_singleton()->get_tree();
  77. ERR_FAIL_COND(!gp);
  78. if (gp && gp->has_node(gp->get_animation_player())) {
  79. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(gp->get_node(gp->get_animation_player()));
  80. if (ap) {
  81. List<StringName> names;
  82. ap->get_animation_list(&names);
  83. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  84. animations_menu->add_icon_item(get_icon("Animation", "EditorIcons"), E->get());
  85. animations_to_add.push_back(E->get());
  86. }
  87. }
  88. }
  89. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  90. String name = String(E->get()).replace_first("AnimationNode", "");
  91. if (name == "Animation")
  92. continue; // nope
  93. int idx = menu->get_item_count();
  94. menu->add_item(vformat("Add %s", name), idx);
  95. menu->set_item_metadata(idx, E->get());
  96. }
  97. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  98. if (clipb.is_valid()) {
  99. menu->add_separator();
  100. menu->add_item(TTR("Paste"), MENU_PASTE);
  101. }
  102. menu->add_separator();
  103. menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
  104. menu->set_global_position(state_machine_draw->get_global_transform().xform(mb->get_position()));
  105. menu->popup();
  106. add_node_pos = mb->get_position() / EDSCALE + state_machine->get_graph_offset();
  107. }
  108. // select node or push a field inside
  109. if (mb.is_valid() && !mb->get_shift() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  110. selected_transition_from = StringName();
  111. selected_transition_to = StringName();
  112. selected_node = StringName();
  113. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  114. if (node_rects[i].play.has_point(mb->get_position())) { //edit name
  115. if (play_mode->get_selected() == 1 || !playback->is_playing()) {
  116. //start
  117. playback->start(node_rects[i].node_name);
  118. } else {
  119. //travel
  120. playback->travel(node_rects[i].node_name);
  121. }
  122. state_machine_draw->update();
  123. return;
  124. }
  125. if (node_rects[i].name.has_point(mb->get_position())) { //edit name
  126. Ref<StyleBox> line_sb = get_stylebox("normal", "LineEdit");
  127. Rect2 edit_rect = node_rects[i].name;
  128. edit_rect.position -= line_sb->get_offset();
  129. edit_rect.size += line_sb->get_minimum_size();
  130. name_edit->set_global_position(state_machine_draw->get_global_transform().xform(edit_rect.position));
  131. name_edit->set_size(edit_rect.size);
  132. name_edit->set_text(node_rects[i].node_name);
  133. name_edit->show_modal();
  134. name_edit->grab_focus();
  135. name_edit->select_all();
  136. prev_name = node_rects[i].node_name;
  137. return;
  138. }
  139. if (node_rects[i].edit.has_point(mb->get_position())) { //edit name
  140. call_deferred("_open_editor", node_rects[i].node_name);
  141. return;
  142. }
  143. if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected
  144. selected_node = node_rects[i].node_name;
  145. Ref<AnimationNode> anode = state_machine->get_node(selected_node);
  146. EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
  147. state_machine_draw->update();
  148. dragging_selected_attempt = true;
  149. dragging_selected = false;
  150. drag_from = mb->get_position();
  151. snap_x = StringName();
  152. snap_y = StringName();
  153. _update_mode();
  154. return;
  155. }
  156. }
  157. //test the lines now
  158. int closest = -1;
  159. float closest_d = 1e20;
  160. for (int i = 0; i < transition_lines.size(); i++) {
  161. Vector2 s[2] = {
  162. transition_lines[i].from,
  163. transition_lines[i].to
  164. };
  165. Vector2 cpoint = Geometry::get_closest_point_to_segment_2d(mb->get_position(), s);
  166. float d = cpoint.distance_to(mb->get_position());
  167. if (d > transition_lines[i].width) {
  168. continue;
  169. }
  170. if (d < closest_d) {
  171. closest = i;
  172. closest_d = d;
  173. }
  174. }
  175. if (closest >= 0) {
  176. selected_transition_from = transition_lines[closest].from_node;
  177. selected_transition_to = transition_lines[closest].to_node;
  178. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(closest);
  179. EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
  180. }
  181. state_machine_draw->update();
  182. _update_mode();
  183. }
  184. //end moving node
  185. if (mb.is_valid() && dragging_selected_attempt && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
  186. if (dragging_selected) {
  187. Ref<AnimationNode> an = state_machine->get_node(selected_node);
  188. updating = true;
  189. undo_redo->create_action("Move Node");
  190. undo_redo->add_do_method(state_machine.ptr(), "set_node_position", selected_node, state_machine->get_node_position(selected_node) + drag_ofs / EDSCALE);
  191. undo_redo->add_undo_method(state_machine.ptr(), "set_node_position", selected_node, state_machine->get_node_position(selected_node));
  192. undo_redo->add_do_method(this, "_update_graph");
  193. undo_redo->add_undo_method(this, "_update_graph");
  194. undo_redo->commit_action();
  195. updating = false;
  196. }
  197. snap_x = StringName();
  198. snap_y = StringName();
  199. dragging_selected_attempt = false;
  200. dragging_selected = false;
  201. state_machine_draw->update();
  202. }
  203. //connect nodes
  204. if (mb.is_valid() && ((tool_select->is_pressed() && mb->get_shift()) || tool_connect->is_pressed()) && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  205. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  206. if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected
  207. connecting = true;
  208. connecting_from = node_rects[i].node_name;
  209. connecting_to = mb->get_position();
  210. connecting_to_node = StringName();
  211. return;
  212. }
  213. }
  214. }
  215. //end connecting nodes
  216. if (mb.is_valid() && connecting && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
  217. if (connecting_to_node != StringName()) {
  218. if (state_machine->has_transition(connecting_from, connecting_to_node)) {
  219. EditorNode::get_singleton()->show_warning("Transition exists!");
  220. } else {
  221. Ref<AnimationNodeStateMachineTransition> tr;
  222. tr.instance();
  223. tr->set_switch_mode(AnimationNodeStateMachineTransition::SwitchMode(transition_mode->get_selected()));
  224. updating = true;
  225. undo_redo->create_action("Add Transition");
  226. undo_redo->add_do_method(state_machine.ptr(), "add_transition", connecting_from, connecting_to_node, tr);
  227. undo_redo->add_undo_method(state_machine.ptr(), "remove_transition", connecting_from, connecting_to_node);
  228. undo_redo->add_do_method(this, "_update_graph");
  229. undo_redo->add_undo_method(this, "_update_graph");
  230. undo_redo->commit_action();
  231. updating = false;
  232. selected_transition_from = connecting_from;
  233. selected_transition_to = connecting_to_node;
  234. EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
  235. _update_mode();
  236. }
  237. }
  238. connecting_to_node = StringName();
  239. connecting = false;
  240. state_machine_draw->update();
  241. }
  242. Ref<InputEventMouseMotion> mm = p_event;
  243. //pan window
  244. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_MIDDLE) {
  245. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  246. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  247. }
  248. //move mouse while connecting
  249. if (mm.is_valid() && connecting) {
  250. connecting_to = mm->get_position();
  251. connecting_to_node = StringName();
  252. state_machine_draw->update();
  253. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  254. if (node_rects[i].node_name != connecting_from && node_rects[i].node.has_point(connecting_to)) { //select node since nothing else was selected
  255. connecting_to_node = node_rects[i].node_name;
  256. return;
  257. }
  258. }
  259. }
  260. //move mouse while moving a node
  261. if (mm.is_valid() && dragging_selected_attempt) {
  262. dragging_selected = true;
  263. drag_ofs = mm->get_position() - drag_from;
  264. snap_x = StringName();
  265. snap_y = StringName();
  266. {
  267. //snap
  268. Vector2 cpos = state_machine->get_node_position(selected_node) + drag_ofs / EDSCALE;
  269. List<StringName> nodes;
  270. state_machine->get_node_list(&nodes);
  271. float best_d_x = 1e20;
  272. float best_d_y = 1e20;
  273. for (List<StringName>::Element *E = nodes.front(); E; E = E->next()) {
  274. if (E->get() == selected_node)
  275. continue;
  276. Vector2 npos = state_machine->get_node_position(E->get());
  277. float d_x = ABS(npos.x - cpos.x);
  278. if (d_x < MIN(5, best_d_x)) {
  279. drag_ofs.x -= cpos.x - npos.x;
  280. best_d_x = d_x;
  281. snap_x = E->get();
  282. }
  283. float d_y = ABS(npos.y - cpos.y);
  284. if (d_y < MIN(5, best_d_y)) {
  285. drag_ofs.y -= cpos.y - npos.y;
  286. best_d_y = d_y;
  287. snap_y = E->get();
  288. }
  289. }
  290. }
  291. state_machine_draw->update();
  292. }
  293. //put ibeam (text cursor) over names to make it clearer that they are editable
  294. if (mm.is_valid()) {
  295. state_machine_draw->grab_focus();
  296. bool over_text_now = false;
  297. String new_over_node = StringName();
  298. int new_over_node_what = -1;
  299. if (tool_select->is_pressed()) {
  300. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  301. if (node_rects[i].name.has_point(mm->get_position())) {
  302. over_text_now = true;
  303. break;
  304. }
  305. if (node_rects[i].node.has_point(mm->get_position())) {
  306. new_over_node = node_rects[i].node_name;
  307. if (node_rects[i].play.has_point(mm->get_position())) {
  308. new_over_node_what = 0;
  309. }
  310. if (node_rects[i].edit.has_point(mm->get_position())) {
  311. new_over_node_what = 1;
  312. }
  313. }
  314. }
  315. }
  316. if (new_over_node != over_node || new_over_node_what != over_node_what) {
  317. over_node = new_over_node;
  318. over_node_what = new_over_node_what;
  319. state_machine_draw->update();
  320. }
  321. if (over_text != over_text_now) {
  322. if (over_text_now) {
  323. state_machine_draw->set_default_cursor_shape(CURSOR_IBEAM);
  324. } else {
  325. state_machine_draw->set_default_cursor_shape(CURSOR_ARROW);
  326. }
  327. over_text = over_text_now;
  328. }
  329. }
  330. }
  331. void AnimationNodeStateMachineEditor::_file_opened(const String &p_file) {
  332. file_loaded = ResourceLoader::load(p_file);
  333. if (file_loaded.is_valid()) {
  334. _add_menu_type(MENU_LOAD_FILE_CONFIRM);
  335. }
  336. }
  337. void AnimationNodeStateMachineEditor::_add_menu_type(int p_index) {
  338. String base_name;
  339. Ref<AnimationRootNode> node;
  340. if (p_index == MENU_LOAD_FILE) {
  341. open_file->clear_filters();
  342. List<String> filters;
  343. ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
  344. for (List<String>::Element *E = filters.front(); E; E = E->next()) {
  345. open_file->add_filter("*." + E->get());
  346. }
  347. open_file->popup_centered_ratio();
  348. return;
  349. } else if (p_index == MENU_LOAD_FILE_CONFIRM) {
  350. node = file_loaded;
  351. file_loaded.unref();
  352. } else if (p_index == MENU_PASTE) {
  353. node = EditorSettings::get_singleton()->get_resource_clipboard();
  354. } else {
  355. String type = menu->get_item_metadata(p_index);
  356. Object *obj = ClassDB::instance(type);
  357. ERR_FAIL_COND(!obj);
  358. AnimationNode *an = Object::cast_to<AnimationNode>(obj);
  359. ERR_FAIL_COND(!an);
  360. node = Ref<AnimationNode>(an);
  361. base_name = type.replace_first("AnimationNode", "");
  362. }
  363. if (!node.is_valid()) {
  364. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
  365. return;
  366. }
  367. if (base_name == String()) {
  368. base_name = node->get_class().replace_first("AnimationNode", "");
  369. }
  370. int base = 1;
  371. String name = base_name;
  372. while (state_machine->has_node(name)) {
  373. base++;
  374. name = base_name + " " + itos(base);
  375. }
  376. updating = true;
  377. undo_redo->create_action("Add Node");
  378. undo_redo->add_do_method(state_machine.ptr(), "add_node", name, node, add_node_pos);
  379. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
  380. undo_redo->add_do_method(this, "_update_graph");
  381. undo_redo->add_undo_method(this, "_update_graph");
  382. undo_redo->commit_action();
  383. updating = false;
  384. state_machine_draw->update();
  385. }
  386. void AnimationNodeStateMachineEditor::_add_animation_type(int p_index) {
  387. Ref<AnimationNodeAnimation> anim;
  388. anim.instance();
  389. anim->set_animation(animations_to_add[p_index]);
  390. String base_name = animations_to_add[p_index];
  391. int base = 1;
  392. String name = base_name;
  393. while (state_machine->has_node(name)) {
  394. base++;
  395. name = base_name + " " + itos(base);
  396. }
  397. updating = true;
  398. undo_redo->create_action("Add Node");
  399. undo_redo->add_do_method(state_machine.ptr(), "add_node", name, anim, add_node_pos);
  400. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
  401. undo_redo->add_do_method(this, "_update_graph");
  402. undo_redo->add_undo_method(this, "_update_graph");
  403. undo_redo->commit_action();
  404. updating = false;
  405. state_machine_draw->update();
  406. }
  407. void AnimationNodeStateMachineEditor::_connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, bool p_auto_advance) {
  408. Color linecolor = get_color("font_color", "Label");
  409. Color icon_color(1, 1, 1);
  410. Color accent = get_color("accent_color", "Editor");
  411. if (!p_enabled) {
  412. linecolor.a *= 0.2;
  413. icon_color.a *= 0.2;
  414. accent.a *= 0.6;
  415. }
  416. Ref<Texture> icons[6] = {
  417. get_icon("TransitionImmediateBig", "EditorIcons"),
  418. get_icon("TransitionSyncBig", "EditorIcons"),
  419. get_icon("TransitionEndBig", "EditorIcons"),
  420. get_icon("TransitionImmediateAutoBig", "EditorIcons"),
  421. get_icon("TransitionSyncAutoBig", "EditorIcons"),
  422. get_icon("TransitionEndAutoBig", "EditorIcons")
  423. };
  424. if (p_selected) {
  425. state_machine_draw->draw_line(p_from, p_to, accent, 6, true);
  426. }
  427. if (p_travel) {
  428. linecolor = accent;
  429. linecolor.set_hsv(1.0, linecolor.get_s(), linecolor.get_v());
  430. }
  431. state_machine_draw->draw_line(p_from, p_to, linecolor, 2, true);
  432. Ref<Texture> icon = icons[p_mode + (p_auto_advance ? 3 : 0)];
  433. Transform2D xf;
  434. xf.elements[0] = (p_to - p_from).normalized();
  435. xf.elements[1] = xf.elements[0].tangent();
  436. xf.elements[2] = (p_from + p_to) * 0.5 - xf.elements[1] * icon->get_height() * 0.5 - xf.elements[0] * icon->get_height() * 0.5;
  437. state_machine_draw->draw_set_transform_matrix(xf);
  438. state_machine_draw->draw_texture(icon, Vector2(), icon_color);
  439. state_machine_draw->draw_set_transform_matrix(Transform2D());
  440. }
  441. void AnimationNodeStateMachineEditor::_clip_src_line_to_rect(Vector2 &r_from, Vector2 &r_to, const Rect2 &p_rect) {
  442. if (r_to == r_from)
  443. return;
  444. //this could be optimized...
  445. Vector2 n = (r_to - r_from).normalized();
  446. while (p_rect.has_point(r_from)) {
  447. r_from += n;
  448. }
  449. }
  450. void AnimationNodeStateMachineEditor::_clip_dst_line_to_rect(Vector2 &r_from, Vector2 &r_to, const Rect2 &p_rect) {
  451. if (r_to == r_from)
  452. return;
  453. //this could be optimized...
  454. Vector2 n = (r_to - r_from).normalized();
  455. while (p_rect.has_point(r_to)) {
  456. r_to -= n;
  457. }
  458. }
  459. void AnimationNodeStateMachineEditor::_state_machine_draw() {
  460. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  461. Ref<StyleBox> style = get_stylebox("state_machine_frame", "GraphNode");
  462. Ref<StyleBox> style_selected = get_stylebox("state_machine_selectedframe", "GraphNode");
  463. Ref<Font> font = get_font("title_font", "GraphNode");
  464. Color font_color = get_color("title_color", "GraphNode");
  465. Ref<Texture> play = get_icon("Play", "EditorIcons");
  466. Ref<Texture> auto_play = get_icon("AutoPlay", "EditorIcons");
  467. Ref<Texture> edit = get_icon("Edit", "EditorIcons");
  468. Color accent = get_color("accent_color", "Editor");
  469. Color linecolor = get_color("font_color", "Label");
  470. linecolor.a *= 0.3;
  471. Ref<StyleBox> playing_overlay = get_stylebox("position", "GraphNode");
  472. bool playing = false;
  473. StringName current;
  474. StringName blend_from;
  475. Vector<StringName> travel_path;
  476. if (playback.is_valid()) {
  477. playing = playback->is_playing();
  478. current = playback->get_current_node();
  479. blend_from = playback->get_blend_from_node();
  480. travel_path = playback->get_travel_path();
  481. }
  482. if (state_machine_draw->has_focus()) {
  483. state_machine_draw->draw_rect(Rect2(Point2(), state_machine_draw->get_size()), accent, false);
  484. }
  485. int sep = 3 * EDSCALE;
  486. List<StringName> nodes;
  487. state_machine->get_node_list(&nodes);
  488. node_rects.clear();
  489. Rect2 scroll_range(Point2(), state_machine_draw->get_size());
  490. //snap lines
  491. if (dragging_selected) {
  492. Vector2 from = (state_machine->get_node_position(selected_node) * EDSCALE) + drag_ofs - state_machine->get_graph_offset() * EDSCALE;
  493. if (snap_x != StringName()) {
  494. Vector2 to = (state_machine->get_node_position(snap_x) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  495. state_machine_draw->draw_line(from, to, linecolor, 2);
  496. }
  497. if (snap_y != StringName()) {
  498. Vector2 to = (state_machine->get_node_position(snap_y) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  499. state_machine_draw->draw_line(from, to, linecolor, 2);
  500. }
  501. }
  502. //pre pass nodes so we know the rectangles
  503. for (List<StringName>::Element *E = nodes.front(); E; E = E->next()) {
  504. Ref<AnimationNode> anode = state_machine->get_node(E->get());
  505. String name = E->get();
  506. bool needs_editor = EditorNode::get_singleton()->item_has_editor(anode.ptr());
  507. Ref<StyleBox> sb = E->get() == selected_node ? style_selected : style;
  508. Size2 s = sb->get_minimum_size();
  509. int strsize = font->get_string_size(name).width;
  510. s.width += strsize;
  511. s.height += MAX(font->get_height(), play->get_height());
  512. s.width += sep + play->get_width();
  513. if (needs_editor) {
  514. s.width += sep + edit->get_width();
  515. }
  516. Vector2 offset;
  517. offset += state_machine->get_node_position(E->get()) * EDSCALE;
  518. if (selected_node == E->get() && dragging_selected) {
  519. offset += drag_ofs;
  520. }
  521. offset -= s / 2;
  522. offset = offset.floor();
  523. //prepre rect
  524. NodeRect nr;
  525. nr.node = Rect2(offset, s);
  526. nr.node_name = E->get();
  527. scroll_range = scroll_range.merge(nr.node); //merge with range
  528. //now scroll it to draw
  529. nr.node.position -= state_machine->get_graph_offset() * EDSCALE;
  530. node_rects.push_back(nr);
  531. }
  532. transition_lines.clear();
  533. //draw connecting line for potential new transition
  534. if (connecting) {
  535. Vector2 from = (state_machine->get_node_position(connecting_from) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  536. Vector2 to;
  537. if (connecting_to_node != StringName()) {
  538. to = (state_machine->get_node_position(connecting_to_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  539. } else {
  540. to = connecting_to;
  541. }
  542. for (int i = 0; i < node_rects.size(); i++) {
  543. if (node_rects[i].node_name == connecting_from) {
  544. _clip_src_line_to_rect(from, to, node_rects[i].node);
  545. }
  546. if (node_rects[i].node_name == connecting_to_node) {
  547. _clip_dst_line_to_rect(from, to, node_rects[i].node);
  548. }
  549. }
  550. _connection_draw(from, to, AnimationNodeStateMachineTransition::SwitchMode(transition_mode->get_selected()), true, false, false, false);
  551. }
  552. Ref<Texture> tr_reference_icon = get_icon("TransitionImmediateBig", "EditorIcons");
  553. float tr_bidi_offset = int(tr_reference_icon->get_height() * 0.8);
  554. //draw transition lines
  555. for (int i = 0; i < state_machine->get_transition_count(); i++) {
  556. TransitionLine tl;
  557. tl.from_node = state_machine->get_transition_from(i);
  558. Vector2 ofs_from = (dragging_selected && tl.from_node == selected_node) ? drag_ofs : Vector2();
  559. tl.from = (state_machine->get_node_position(tl.from_node) * EDSCALE) + ofs_from - state_machine->get_graph_offset() * EDSCALE;
  560. tl.to_node = state_machine->get_transition_to(i);
  561. Vector2 ofs_to = (dragging_selected && tl.to_node == selected_node) ? drag_ofs : Vector2();
  562. tl.to = (state_machine->get_node_position(tl.to_node) * EDSCALE) + ofs_to - state_machine->get_graph_offset() * EDSCALE;
  563. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(i);
  564. tl.disabled = tr->is_disabled();
  565. tl.auto_advance = tr->has_auto_advance();
  566. tl.advance_condition_name = tr->get_advance_condition_name();
  567. tl.advance_condition_state = false;
  568. tl.mode = tr->get_switch_mode();
  569. tl.width = tr_bidi_offset;
  570. if (state_machine->has_transition(tl.to_node, tl.from_node)) { //offset if same exists
  571. Vector2 offset = -(tl.from - tl.to).normalized().tangent() * tr_bidi_offset;
  572. tl.from += offset;
  573. tl.to += offset;
  574. }
  575. for (int i = 0; i < node_rects.size(); i++) {
  576. if (node_rects[i].node_name == tl.from_node) {
  577. _clip_src_line_to_rect(tl.from, tl.to, node_rects[i].node);
  578. }
  579. if (node_rects[i].node_name == tl.to_node) {
  580. _clip_dst_line_to_rect(tl.from, tl.to, node_rects[i].node);
  581. }
  582. }
  583. bool selected = selected_transition_from == tl.from_node && selected_transition_to == tl.to_node;
  584. bool travel = false;
  585. if (blend_from == tl.from_node && current == tl.to_node) {
  586. travel = true;
  587. }
  588. if (travel_path.size()) {
  589. if (current == tl.from_node && travel_path[0] == tl.to_node) {
  590. travel = true;
  591. } else {
  592. for (int j = 0; j < travel_path.size() - 1; j++) {
  593. if (travel_path[j] == tl.from_node && travel_path[j + 1] == tl.to_node) {
  594. travel = true;
  595. break;
  596. }
  597. }
  598. }
  599. }
  600. bool auto_advance = tl.auto_advance;
  601. StringName fullpath = AnimationTreeEditor::get_singleton()->get_base_path() + String(tl.advance_condition_name);
  602. if (tl.advance_condition_name != StringName() && bool(AnimationTreeEditor::get_singleton()->get_tree()->get(fullpath))) {
  603. tl.advance_condition_state = true;
  604. auto_advance = true;
  605. }
  606. _connection_draw(tl.from, tl.to, tl.mode, !tl.disabled, selected, travel, auto_advance);
  607. transition_lines.push_back(tl);
  608. }
  609. //draw actual nodes
  610. for (int i = 0; i < node_rects.size(); i++) {
  611. String name = node_rects[i].node_name;
  612. Ref<AnimationNode> anode = state_machine->get_node(name);
  613. bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);
  614. Ref<StyleBox> sb = name == selected_node ? style_selected : style;
  615. int strsize = font->get_string_size(name).width;
  616. NodeRect &nr = node_rects.write[i];
  617. Vector2 offset = nr.node.position;
  618. int h = nr.node.size.height;
  619. //prepre rect
  620. //now scroll it to draw
  621. state_machine_draw->draw_style_box(sb, nr.node);
  622. if (playing && (blend_from == name || current == name || travel_path.find(name) != -1)) {
  623. state_machine_draw->draw_style_box(playing_overlay, nr.node);
  624. }
  625. bool onstart = state_machine->get_start_node() == name;
  626. if (onstart) {
  627. state_machine_draw->draw_string(font, offset + Vector2(0, -font->get_height() - 3 * EDSCALE + font->get_ascent()), TTR("Start"), font_color);
  628. }
  629. if (state_machine->get_end_node() == name) {
  630. int endofs = nr.node.size.x - font->get_string_size(TTR("End")).x;
  631. state_machine_draw->draw_string(font, offset + Vector2(endofs, -font->get_height() - 3 * EDSCALE + font->get_ascent()), TTR("End"), font_color);
  632. }
  633. offset.x += sb->get_offset().x;
  634. nr.play.position = offset + Vector2(0, (h - play->get_height()) / 2).floor();
  635. nr.play.size = play->get_size();
  636. Ref<Texture> play_tex = onstart ? auto_play : play;
  637. if (over_node == name && over_node_what == 0) {
  638. state_machine_draw->draw_texture(play_tex, nr.play.position, accent);
  639. } else {
  640. state_machine_draw->draw_texture(play_tex, nr.play.position);
  641. }
  642. offset.x += sep + play->get_width();
  643. nr.name.position = offset + Vector2(0, (h - font->get_height()) / 2).floor();
  644. nr.name.size = Vector2(strsize, font->get_height());
  645. state_machine_draw->draw_string(font, nr.name.position + Vector2(0, font->get_ascent()), name, font_color);
  646. offset.x += strsize + sep;
  647. if (needs_editor) {
  648. nr.edit.position = offset + Vector2(0, (h - edit->get_height()) / 2).floor();
  649. nr.edit.size = edit->get_size();
  650. if (over_node == name && over_node_what == 1) {
  651. state_machine_draw->draw_texture(edit, nr.edit.position, accent);
  652. } else {
  653. state_machine_draw->draw_texture(edit, nr.edit.position);
  654. }
  655. offset.x += sep + edit->get_width();
  656. }
  657. }
  658. scroll_range = scroll_range.grow(200 * EDSCALE);
  659. //adjust scrollbars
  660. updating = true;
  661. h_scroll->set_min(scroll_range.position.x);
  662. h_scroll->set_max(scroll_range.position.x + scroll_range.size.x);
  663. h_scroll->set_page(state_machine_draw->get_size().x);
  664. h_scroll->set_value(state_machine->get_graph_offset().x);
  665. v_scroll->set_min(scroll_range.position.y);
  666. v_scroll->set_max(scroll_range.position.y + scroll_range.size.y);
  667. v_scroll->set_page(state_machine_draw->get_size().y);
  668. v_scroll->set_value(state_machine->get_graph_offset().y);
  669. updating = false;
  670. state_machine_play_pos->update();
  671. }
  672. void AnimationNodeStateMachineEditor::_state_machine_pos_draw() {
  673. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  674. if (!playback.is_valid() || !playback->is_playing())
  675. return;
  676. int idx = -1;
  677. for (int i = 0; i < node_rects.size(); i++) {
  678. if (node_rects[i].node_name == playback->get_current_node()) {
  679. idx = i;
  680. break;
  681. }
  682. }
  683. if (idx == -1)
  684. return;
  685. const NodeRect &nr = node_rects[idx];
  686. Vector2 from;
  687. from.x = nr.play.position.x;
  688. from.y = (nr.play.position.y + nr.play.size.y + nr.node.position.y + nr.node.size.y) * 0.5;
  689. Vector2 to;
  690. if (nr.edit.size.x) {
  691. to.x = nr.edit.position.x + nr.edit.size.x;
  692. } else {
  693. to.x = nr.name.position.x + nr.name.size.x;
  694. }
  695. to.y = from.y;
  696. float len = MAX(0.0001, playback->get_current_length());
  697. float pos = CLAMP(playback->get_current_play_pos(), 0, len);
  698. float c = pos / len;
  699. Color fg = get_color("font_color", "Label");
  700. Color bg = fg;
  701. bg.a *= 0.3;
  702. state_machine_play_pos->draw_line(from, to, bg, 2);
  703. to = from.linear_interpolate(to, c);
  704. state_machine_play_pos->draw_line(from, to, fg, 2);
  705. }
  706. void AnimationNodeStateMachineEditor::_update_graph() {
  707. if (updating)
  708. return;
  709. updating = true;
  710. state_machine_draw->update();
  711. updating = false;
  712. }
  713. void AnimationNodeStateMachineEditor::_notification(int p_what) {
  714. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  715. error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  716. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  717. panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  718. tool_select->set_icon(get_icon("ToolSelect", "EditorIcons"));
  719. tool_create->set_icon(get_icon("ToolAddNode", "EditorIcons"));
  720. tool_connect->set_icon(get_icon("ToolConnect", "EditorIcons"));
  721. transition_mode->clear();
  722. transition_mode->add_icon_item(get_icon("TransitionImmediate", "EditorIcons"), TTR("Immediate"));
  723. transition_mode->add_icon_item(get_icon("TransitionSync", "EditorIcons"), TTR("Sync"));
  724. transition_mode->add_icon_item(get_icon("TransitionEnd", "EditorIcons"), TTR("At End"));
  725. //force filter on those, so they deform better
  726. get_icon("TransitionImmediateBig", "EditorIcons")->set_flags(Texture::FLAG_FILTER);
  727. get_icon("TransitionEndBig", "EditorIcons")->set_flags(Texture::FLAG_FILTER);
  728. get_icon("TransitionSyncBig", "EditorIcons")->set_flags(Texture::FLAG_FILTER);
  729. get_icon("TransitionImmediateAutoBig", "EditorIcons")->set_flags(Texture::FLAG_FILTER);
  730. get_icon("TransitionEndAutoBig", "EditorIcons")->set_flags(Texture::FLAG_FILTER);
  731. get_icon("TransitionSyncAutoBig", "EditorIcons")->set_flags(Texture::FLAG_FILTER);
  732. tool_erase->set_icon(get_icon("Remove", "EditorIcons"));
  733. tool_autoplay->set_icon(get_icon("AutoPlay", "EditorIcons"));
  734. tool_end->set_icon(get_icon("AutoEnd", "EditorIcons"));
  735. play_mode->clear();
  736. play_mode->add_icon_item(get_icon("PlayTravel", "EditorIcons"), TTR("Travel"));
  737. play_mode->add_icon_item(get_icon("Play", "EditorIcons"), TTR("Immediate"));
  738. }
  739. if (p_what == NOTIFICATION_PROCESS) {
  740. String error;
  741. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  742. if (error_time > 0) {
  743. error = error_text;
  744. error_time -= get_process_delta_time();
  745. } else if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
  746. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  747. } else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  748. error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
  749. /*} else if (state_machine->get_parent().is_valid() && state_machine->get_parent()->is_class("AnimationNodeStateMachine")) {
  750. if (state_machine->get_start_node() == StringName() || state_machine->get_end_node() == StringName()) {
  751. error = TTR("Start and end nodes are needed for a sub-transition.");
  752. }*/
  753. } else if (playback.is_null()) {
  754. error = vformat(TTR("No playback resource set at path: %s."), AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  755. }
  756. if (error != error_label->get_text()) {
  757. error_label->set_text(error);
  758. if (error != String()) {
  759. error_panel->show();
  760. } else {
  761. error_panel->hide();
  762. }
  763. }
  764. for (int i = 0; i < transition_lines.size(); i++) {
  765. int tidx = -1;
  766. for (int j = 0; j < state_machine->get_transition_count(); j++) {
  767. if (transition_lines[i].from_node == state_machine->get_transition_from(j) && transition_lines[i].to_node == state_machine->get_transition_to(j)) {
  768. tidx = j;
  769. break;
  770. }
  771. }
  772. if (tidx == -1) { //missing transition, should redraw
  773. state_machine_draw->update();
  774. break;
  775. }
  776. if (transition_lines[i].disabled != state_machine->get_transition(tidx)->is_disabled()) {
  777. state_machine_draw->update();
  778. break;
  779. }
  780. if (transition_lines[i].auto_advance != state_machine->get_transition(tidx)->has_auto_advance()) {
  781. state_machine_draw->update();
  782. break;
  783. }
  784. if (transition_lines[i].advance_condition_name != state_machine->get_transition(tidx)->get_advance_condition_name()) {
  785. state_machine_draw->update();
  786. break;
  787. }
  788. if (transition_lines[i].mode != state_machine->get_transition(tidx)->get_switch_mode()) {
  789. state_machine_draw->update();
  790. break;
  791. }
  792. bool acstate = transition_lines[i].advance_condition_name != StringName() && bool(AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + String(transition_lines[i].advance_condition_name)));
  793. if (transition_lines[i].advance_condition_state != acstate) {
  794. state_machine_draw->update();
  795. break;
  796. }
  797. }
  798. bool same_travel_path = true;
  799. Vector<StringName> tp;
  800. bool is_playing = false;
  801. StringName current_node;
  802. StringName blend_from_node;
  803. float play_pos = 0;
  804. if (playback.is_valid()) {
  805. tp = playback->get_travel_path();
  806. is_playing = playback->is_playing();
  807. current_node = playback->get_current_node();
  808. blend_from_node = playback->get_blend_from_node();
  809. play_pos = playback->get_current_play_pos();
  810. }
  811. {
  812. if (last_travel_path.size() != tp.size()) {
  813. same_travel_path = false;
  814. } else {
  815. for (int i = 0; i < last_travel_path.size(); i++) {
  816. if (last_travel_path[i] != tp[i]) {
  817. same_travel_path = false;
  818. break;
  819. }
  820. }
  821. }
  822. }
  823. //update if travel state changed
  824. if (!same_travel_path || last_active != is_playing || last_current_node != current_node || last_blend_from_node != blend_from_node) {
  825. state_machine_draw->update();
  826. last_travel_path = tp;
  827. last_current_node = current_node;
  828. last_active = is_playing;
  829. last_blend_from_node = blend_from_node;
  830. state_machine_play_pos->update();
  831. }
  832. if (last_play_pos != play_pos) {
  833. last_play_pos = play_pos;
  834. state_machine_play_pos->update();
  835. }
  836. }
  837. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  838. over_node = StringName();
  839. set_process(is_visible_in_tree());
  840. }
  841. }
  842. void AnimationNodeStateMachineEditor::_open_editor(const String &p_name) {
  843. AnimationTreeEditor::get_singleton()->enter_editor(p_name);
  844. }
  845. void AnimationNodeStateMachineEditor::_removed_from_graph() {
  846. EditorNode::get_singleton()->edit_item(NULL);
  847. }
  848. void AnimationNodeStateMachineEditor::_name_edited(const String &p_text) {
  849. String new_name = p_text;
  850. ERR_FAIL_COND(new_name == "" || new_name.find(".") != -1 || new_name.find("/") != -1)
  851. if (new_name == prev_name) {
  852. return; // Nothing to do.
  853. }
  854. String base_name = new_name;
  855. int base = 1;
  856. String name = base_name;
  857. while (state_machine->has_node(name)) {
  858. base++;
  859. name = base_name + " " + itos(base);
  860. }
  861. updating = true;
  862. undo_redo->create_action("Node Renamed");
  863. undo_redo->add_do_method(state_machine.ptr(), "rename_node", prev_name, name);
  864. undo_redo->add_undo_method(state_machine.ptr(), "rename_node", name, prev_name);
  865. undo_redo->add_do_method(this, "_update_graph");
  866. undo_redo->add_undo_method(this, "_update_graph");
  867. undo_redo->commit_action();
  868. updating = false;
  869. state_machine_draw->update();
  870. name_edit->hide();
  871. }
  872. void AnimationNodeStateMachineEditor::_name_edited_focus_out() {
  873. _name_edited(name_edit->get_text());
  874. }
  875. void AnimationNodeStateMachineEditor::_scroll_changed(double) {
  876. if (updating)
  877. return;
  878. state_machine->set_graph_offset(Vector2(h_scroll->get_value(), v_scroll->get_value()));
  879. state_machine_draw->update();
  880. }
  881. void AnimationNodeStateMachineEditor::_erase_selected() {
  882. if (selected_node != StringName() && state_machine->has_node(selected_node)) {
  883. updating = true;
  884. undo_redo->create_action("Node Removed");
  885. undo_redo->add_do_method(state_machine.ptr(), "remove_node", selected_node);
  886. undo_redo->add_undo_method(state_machine.ptr(), "add_node", selected_node, state_machine->get_node(selected_node), state_machine->get_node_position(selected_node));
  887. for (int i = 0; i < state_machine->get_transition_count(); i++) {
  888. String from = state_machine->get_transition_from(i);
  889. String to = state_machine->get_transition_to(i);
  890. if (from == selected_node || to == selected_node) {
  891. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", from, to, state_machine->get_transition(i));
  892. }
  893. }
  894. if (String(state_machine->get_start_node()) == selected_node) {
  895. undo_redo->add_undo_method(state_machine.ptr(), "set_start_node", selected_node);
  896. }
  897. undo_redo->add_do_method(this, "_update_graph");
  898. undo_redo->add_undo_method(this, "_update_graph");
  899. undo_redo->commit_action();
  900. updating = false;
  901. selected_node = StringName();
  902. }
  903. if (selected_transition_to != StringName() && selected_transition_from != StringName() && state_machine->has_transition(selected_transition_from, selected_transition_to)) {
  904. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(state_machine->find_transition(selected_transition_from, selected_transition_to));
  905. updating = true;
  906. undo_redo->create_action("Transition Removed");
  907. undo_redo->add_do_method(state_machine.ptr(), "remove_transition", selected_transition_from, selected_transition_to);
  908. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", selected_transition_from, selected_transition_to, tr);
  909. undo_redo->add_do_method(this, "_update_graph");
  910. undo_redo->add_undo_method(this, "_update_graph");
  911. undo_redo->commit_action();
  912. updating = false;
  913. selected_transition_from = StringName();
  914. selected_transition_to = StringName();
  915. }
  916. state_machine_draw->update();
  917. }
  918. void AnimationNodeStateMachineEditor::_autoplay_selected() {
  919. if (selected_node != StringName() && state_machine->has_node(selected_node)) {
  920. StringName new_start_node;
  921. if (state_machine->get_start_node() == selected_node) { //toggle it
  922. new_start_node = StringName();
  923. } else {
  924. new_start_node = selected_node;
  925. }
  926. updating = true;
  927. undo_redo->create_action("Set Start Node (Autoplay)");
  928. undo_redo->add_do_method(state_machine.ptr(), "set_start_node", new_start_node);
  929. undo_redo->add_undo_method(state_machine.ptr(), "set_start_node", state_machine->get_start_node());
  930. undo_redo->add_do_method(this, "_update_graph");
  931. undo_redo->add_undo_method(this, "_update_graph");
  932. undo_redo->commit_action();
  933. updating = false;
  934. state_machine_draw->update();
  935. }
  936. }
  937. void AnimationNodeStateMachineEditor::_end_selected() {
  938. if (selected_node != StringName() && state_machine->has_node(selected_node)) {
  939. StringName new_end_node;
  940. if (state_machine->get_end_node() == selected_node) { //toggle it
  941. new_end_node = StringName();
  942. } else {
  943. new_end_node = selected_node;
  944. }
  945. updating = true;
  946. undo_redo->create_action("Set Start Node (Autoplay)");
  947. undo_redo->add_do_method(state_machine.ptr(), "set_end_node", new_end_node);
  948. undo_redo->add_undo_method(state_machine.ptr(), "set_end_node", state_machine->get_end_node());
  949. undo_redo->add_do_method(this, "_update_graph");
  950. undo_redo->add_undo_method(this, "_update_graph");
  951. undo_redo->commit_action();
  952. updating = false;
  953. state_machine_draw->update();
  954. }
  955. }
  956. void AnimationNodeStateMachineEditor::_update_mode() {
  957. if (tool_select->is_pressed()) {
  958. tool_erase_hb->show();
  959. tool_erase->set_disabled(selected_node == StringName() && selected_transition_from == StringName() && selected_transition_to == StringName());
  960. tool_autoplay->set_disabled(selected_node == StringName());
  961. tool_end->set_disabled(selected_node == StringName());
  962. } else {
  963. tool_erase_hb->hide();
  964. }
  965. }
  966. void AnimationNodeStateMachineEditor::_bind_methods() {
  967. ClassDB::bind_method("_state_machine_gui_input", &AnimationNodeStateMachineEditor::_state_machine_gui_input);
  968. ClassDB::bind_method("_state_machine_draw", &AnimationNodeStateMachineEditor::_state_machine_draw);
  969. ClassDB::bind_method("_state_machine_pos_draw", &AnimationNodeStateMachineEditor::_state_machine_pos_draw);
  970. ClassDB::bind_method("_update_graph", &AnimationNodeStateMachineEditor::_update_graph);
  971. ClassDB::bind_method("_add_menu_type", &AnimationNodeStateMachineEditor::_add_menu_type);
  972. ClassDB::bind_method("_add_animation_type", &AnimationNodeStateMachineEditor::_add_animation_type);
  973. ClassDB::bind_method("_name_edited", &AnimationNodeStateMachineEditor::_name_edited);
  974. ClassDB::bind_method("_name_edited_focus_out", &AnimationNodeStateMachineEditor::_name_edited_focus_out);
  975. ClassDB::bind_method("_removed_from_graph", &AnimationNodeStateMachineEditor::_removed_from_graph);
  976. ClassDB::bind_method("_open_editor", &AnimationNodeStateMachineEditor::_open_editor);
  977. ClassDB::bind_method("_scroll_changed", &AnimationNodeStateMachineEditor::_scroll_changed);
  978. ClassDB::bind_method("_erase_selected", &AnimationNodeStateMachineEditor::_erase_selected);
  979. ClassDB::bind_method("_autoplay_selected", &AnimationNodeStateMachineEditor::_autoplay_selected);
  980. ClassDB::bind_method("_end_selected", &AnimationNodeStateMachineEditor::_end_selected);
  981. ClassDB::bind_method("_update_mode", &AnimationNodeStateMachineEditor::_update_mode);
  982. ClassDB::bind_method("_file_opened", &AnimationNodeStateMachineEditor::_file_opened);
  983. }
  984. AnimationNodeStateMachineEditor *AnimationNodeStateMachineEditor::singleton = NULL;
  985. AnimationNodeStateMachineEditor::AnimationNodeStateMachineEditor() {
  986. singleton = this;
  987. updating = false;
  988. HBoxContainer *top_hb = memnew(HBoxContainer);
  989. add_child(top_hb);
  990. Ref<ButtonGroup> bg;
  991. bg.instance();
  992. tool_select = memnew(ToolButton);
  993. top_hb->add_child(tool_select);
  994. tool_select->set_toggle_mode(true);
  995. tool_select->set_button_group(bg);
  996. tool_select->set_pressed(true);
  997. tool_select->set_tooltip(TTR("Select and move nodes.\nRMB to add new nodes.\nShift+LMB to create connections."));
  998. tool_select->connect("pressed", this, "_update_mode", varray(), CONNECT_DEFERRED);
  999. tool_create = memnew(ToolButton);
  1000. top_hb->add_child(tool_create);
  1001. tool_create->set_toggle_mode(true);
  1002. tool_create->set_button_group(bg);
  1003. tool_create->set_tooltip(TTR("Create new nodes."));
  1004. tool_create->connect("pressed", this, "_update_mode", varray(), CONNECT_DEFERRED);
  1005. tool_connect = memnew(ToolButton);
  1006. top_hb->add_child(tool_connect);
  1007. tool_connect->set_toggle_mode(true);
  1008. tool_connect->set_button_group(bg);
  1009. tool_connect->set_tooltip(TTR("Connect nodes."));
  1010. tool_connect->connect("pressed", this, "_update_mode", varray(), CONNECT_DEFERRED);
  1011. tool_erase_hb = memnew(HBoxContainer);
  1012. top_hb->add_child(tool_erase_hb);
  1013. tool_erase_hb->add_child(memnew(VSeparator));
  1014. tool_erase = memnew(ToolButton);
  1015. tool_erase->set_tooltip(TTR("Remove selected node or transition."));
  1016. tool_erase_hb->add_child(tool_erase);
  1017. tool_erase->connect("pressed", this, "_erase_selected");
  1018. tool_erase->set_disabled(true);
  1019. tool_erase_hb->add_child(memnew(VSeparator));
  1020. tool_autoplay = memnew(ToolButton);
  1021. tool_autoplay->set_tooltip(TTR("Toggle autoplay this animation on start, restart or seek to zero."));
  1022. tool_erase_hb->add_child(tool_autoplay);
  1023. tool_autoplay->connect("pressed", this, "_autoplay_selected");
  1024. tool_autoplay->set_disabled(true);
  1025. tool_end = memnew(ToolButton);
  1026. tool_end->set_tooltip(TTR("Set the end animation. This is useful for sub-transitions."));
  1027. tool_erase_hb->add_child(tool_end);
  1028. tool_end->connect("pressed", this, "_end_selected");
  1029. tool_end->set_disabled(true);
  1030. top_hb->add_child(memnew(VSeparator));
  1031. top_hb->add_child(memnew(Label(TTR("Transition: "))));
  1032. transition_mode = memnew(OptionButton);
  1033. top_hb->add_child(transition_mode);
  1034. top_hb->add_spacer();
  1035. top_hb->add_child(memnew(Label("Play Mode:")));
  1036. play_mode = memnew(OptionButton);
  1037. top_hb->add_child(play_mode);
  1038. GridContainer *main_grid = memnew(GridContainer);
  1039. main_grid->set_columns(2);
  1040. add_child(main_grid);
  1041. main_grid->set_v_size_flags(SIZE_EXPAND_FILL);
  1042. panel = memnew(PanelContainer);
  1043. panel->set_clip_contents(true);
  1044. main_grid->add_child(panel);
  1045. panel->set_h_size_flags(SIZE_EXPAND_FILL);
  1046. state_machine_draw = memnew(Control);
  1047. state_machine_draw->connect("gui_input", this, "_state_machine_gui_input");
  1048. state_machine_draw->connect("draw", this, "_state_machine_draw");
  1049. state_machine_draw->set_focus_mode(FOCUS_ALL);
  1050. state_machine_play_pos = memnew(Control);
  1051. state_machine_draw->add_child(state_machine_play_pos);
  1052. state_machine_play_pos->set_mouse_filter(MOUSE_FILTER_PASS); //pass all to parent
  1053. state_machine_play_pos->set_anchors_and_margins_preset(PRESET_WIDE);
  1054. state_machine_play_pos->connect("draw", this, "_state_machine_pos_draw");
  1055. panel->add_child(state_machine_draw);
  1056. panel->set_v_size_flags(SIZE_EXPAND_FILL);
  1057. v_scroll = memnew(VScrollBar);
  1058. main_grid->add_child(v_scroll);
  1059. v_scroll->connect("value_changed", this, "_scroll_changed");
  1060. h_scroll = memnew(HScrollBar);
  1061. main_grid->add_child(h_scroll);
  1062. h_scroll->connect("value_changed", this, "_scroll_changed");
  1063. main_grid->add_child(memnew(Control)); //empty bottom right
  1064. error_panel = memnew(PanelContainer);
  1065. add_child(error_panel);
  1066. error_label = memnew(Label);
  1067. error_panel->add_child(error_label);
  1068. error_label->set_text("eh");
  1069. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  1070. set_custom_minimum_size(Size2(0, 300 * EDSCALE));
  1071. menu = memnew(PopupMenu);
  1072. add_child(menu);
  1073. menu->connect("id_pressed", this, "_add_menu_type");
  1074. animations_menu = memnew(PopupMenu);
  1075. menu->add_child(animations_menu);
  1076. animations_menu->set_name("animations");
  1077. animations_menu->connect("index_pressed", this, "_add_animation_type");
  1078. name_edit = memnew(LineEdit);
  1079. state_machine_draw->add_child(name_edit);
  1080. name_edit->hide();
  1081. name_edit->connect("text_entered", this, "_name_edited");
  1082. name_edit->connect("focus_exited", this, "_name_edited_focus_out");
  1083. name_edit->set_as_toplevel(true);
  1084. open_file = memnew(EditorFileDialog);
  1085. add_child(open_file);
  1086. open_file->set_title(TTR("Open Animation Node"));
  1087. open_file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  1088. open_file->connect("file_selected", this, "_file_opened");
  1089. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  1090. over_text = false;
  1091. over_node_what = -1;
  1092. dragging_selected_attempt = false;
  1093. connecting = false;
  1094. last_active = false;
  1095. error_time = 0;
  1096. }