animation_state_machine_editor.cpp 48 KB

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