animation_state_machine_editor.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/math/geometry_2d.h"
  35. #include "core/os/keyboard.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/window.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_keycode() == 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() == MOUSE_BUTTON_RIGHT) || (tool_create->is_pressed() && mb->get_button_index() == MOUSE_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 (const StringName &E : names) {
  86. animations_menu->add_icon_item(get_theme_icon(SNAME("Animation"), SNAME("EditorIcons")), E);
  87. animations_to_add.push_back(E);
  88. }
  89. }
  90. }
  91. for (const StringName &E : classes) {
  92. String name = String(E).replace_first("AnimationNode", "");
  93. if (name == "Animation") {
  94. continue; // nope
  95. }
  96. int idx = menu->get_item_count();
  97. menu->add_item(vformat("Add %s", name), idx);
  98. menu->set_item_metadata(idx, E);
  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_position(state_machine_draw->get_screen_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->is_shift_pressed() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MOUSE_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_theme_stylebox(SNAME("normal"), SNAME("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_popup->set_position(state_machine_draw->get_screen_transform().xform(edit_rect.position));
  134. name_edit_popup->set_size(edit_rect.size);
  135. name_edit->set_text(node_rects[i].node_name);
  136. name_edit_popup->popup();
  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(SNAME("_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 = Geometry2D::get_closest_point_to_segment(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() == MOUSE_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->is_shift_pressed()) || tool_connect->is_pressed()) && mb->get_button_index() == MOUSE_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() == MOUSE_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.instantiate();
  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() & MOUSE_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 (const StringName &E : nodes) {
  277. if (E == selected_node) {
  278. continue;
  279. }
  280. Vector2 npos = state_machine->get_node_position(E);
  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;
  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;
  292. }
  293. }
  294. }
  295. state_machine_draw->update();
  296. }
  297. //put ibeam (text cursor) over names to make it clearer that they are editable
  298. if (mm.is_valid()) {
  299. state_machine_draw->grab_focus();
  300. bool over_text_now = false;
  301. String new_over_node = StringName();
  302. int new_over_node_what = -1;
  303. if (tool_select->is_pressed()) {
  304. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  305. if (node_rects[i].name.has_point(mm->get_position())) {
  306. over_text_now = true;
  307. break;
  308. }
  309. if (node_rects[i].node.has_point(mm->get_position())) {
  310. new_over_node = node_rects[i].node_name;
  311. if (node_rects[i].play.has_point(mm->get_position())) {
  312. new_over_node_what = 0;
  313. }
  314. if (node_rects[i].edit.has_point(mm->get_position())) {
  315. new_over_node_what = 1;
  316. }
  317. }
  318. }
  319. }
  320. if (new_over_node != over_node || new_over_node_what != over_node_what) {
  321. over_node = new_over_node;
  322. over_node_what = new_over_node_what;
  323. state_machine_draw->update();
  324. }
  325. if (over_text != over_text_now) {
  326. if (over_text_now) {
  327. state_machine_draw->set_default_cursor_shape(CURSOR_IBEAM);
  328. } else {
  329. state_machine_draw->set_default_cursor_shape(CURSOR_ARROW);
  330. }
  331. over_text = over_text_now;
  332. }
  333. }
  334. Ref<InputEventPanGesture> pan_gesture = p_event;
  335. if (pan_gesture.is_valid()) {
  336. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  337. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  338. }
  339. }
  340. void AnimationNodeStateMachineEditor::_file_opened(const String &p_file) {
  341. file_loaded = ResourceLoader::load(p_file);
  342. if (file_loaded.is_valid()) {
  343. _add_menu_type(MENU_LOAD_FILE_CONFIRM);
  344. }
  345. }
  346. void AnimationNodeStateMachineEditor::_add_menu_type(int p_index) {
  347. String base_name;
  348. Ref<AnimationRootNode> node;
  349. if (p_index == MENU_LOAD_FILE) {
  350. open_file->clear_filters();
  351. List<String> filters;
  352. ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
  353. for (const String &E : filters) {
  354. open_file->add_filter("*." + E);
  355. }
  356. open_file->popup_file_dialog();
  357. return;
  358. } else if (p_index == MENU_LOAD_FILE_CONFIRM) {
  359. node = file_loaded;
  360. file_loaded.unref();
  361. } else if (p_index == MENU_PASTE) {
  362. node = EditorSettings::get_singleton()->get_resource_clipboard();
  363. } else {
  364. String type = menu->get_item_metadata(p_index);
  365. Object *obj = ClassDB::instantiate(type);
  366. ERR_FAIL_COND(!obj);
  367. AnimationNode *an = Object::cast_to<AnimationNode>(obj);
  368. ERR_FAIL_COND(!an);
  369. node = Ref<AnimationNode>(an);
  370. base_name = type.replace_first("AnimationNode", "");
  371. }
  372. if (!node.is_valid()) {
  373. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
  374. return;
  375. }
  376. if (base_name == String()) {
  377. base_name = node->get_class().replace_first("AnimationNode", "");
  378. }
  379. int base = 1;
  380. String name = base_name;
  381. while (state_machine->has_node(name)) {
  382. base++;
  383. name = base_name + " " + itos(base);
  384. }
  385. updating = true;
  386. undo_redo->create_action(TTR("Add Node"));
  387. undo_redo->add_do_method(state_machine.ptr(), "add_node", name, node, add_node_pos);
  388. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
  389. undo_redo->add_do_method(this, "_update_graph");
  390. undo_redo->add_undo_method(this, "_update_graph");
  391. undo_redo->commit_action();
  392. updating = false;
  393. state_machine_draw->update();
  394. }
  395. void AnimationNodeStateMachineEditor::_add_animation_type(int p_index) {
  396. Ref<AnimationNodeAnimation> anim;
  397. anim.instantiate();
  398. anim->set_animation(animations_to_add[p_index]);
  399. String base_name = animations_to_add[p_index];
  400. int base = 1;
  401. String name = base_name;
  402. while (state_machine->has_node(name)) {
  403. base++;
  404. name = base_name + " " + itos(base);
  405. }
  406. updating = true;
  407. undo_redo->create_action(TTR("Add Node"));
  408. undo_redo->add_do_method(state_machine.ptr(), "add_node", name, anim, add_node_pos);
  409. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
  410. undo_redo->add_do_method(this, "_update_graph");
  411. undo_redo->add_undo_method(this, "_update_graph");
  412. undo_redo->commit_action();
  413. updating = false;
  414. state_machine_draw->update();
  415. }
  416. 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) {
  417. Color linecolor = get_theme_color(SNAME("font_color"), SNAME("Label"));
  418. Color icon_color(1, 1, 1);
  419. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  420. if (!p_enabled) {
  421. linecolor.a *= 0.2;
  422. icon_color.a *= 0.2;
  423. accent.a *= 0.6;
  424. }
  425. Ref<Texture2D> icons[6] = {
  426. get_theme_icon(SNAME("TransitionImmediateBig"), SNAME("EditorIcons")),
  427. get_theme_icon(SNAME("TransitionSyncBig"), SNAME("EditorIcons")),
  428. get_theme_icon(SNAME("TransitionEndBig"), SNAME("EditorIcons")),
  429. get_theme_icon(SNAME("TransitionImmediateAutoBig"), SNAME("EditorIcons")),
  430. get_theme_icon(SNAME("TransitionSyncAutoBig"), SNAME("EditorIcons")),
  431. get_theme_icon(SNAME("TransitionEndAutoBig"), SNAME("EditorIcons"))
  432. };
  433. if (p_selected) {
  434. state_machine_draw->draw_line(p_from, p_to, accent, 6);
  435. }
  436. if (p_travel) {
  437. linecolor = accent;
  438. linecolor.set_hsv(1.0, linecolor.get_s(), linecolor.get_v());
  439. }
  440. state_machine_draw->draw_line(p_from, p_to, linecolor, 2);
  441. Ref<Texture2D> icon = icons[p_mode + (p_auto_advance ? 3 : 0)];
  442. Transform2D xf;
  443. xf.elements[0] = (p_to - p_from).normalized();
  444. xf.elements[1] = xf.elements[0].orthogonal();
  445. 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;
  446. state_machine_draw->draw_set_transform_matrix(xf);
  447. state_machine_draw->draw_texture(icon, Vector2(), icon_color);
  448. state_machine_draw->draw_set_transform_matrix(Transform2D());
  449. }
  450. void AnimationNodeStateMachineEditor::_clip_src_line_to_rect(Vector2 &r_from, Vector2 &r_to, const Rect2 &p_rect) {
  451. if (r_to == r_from) {
  452. return;
  453. }
  454. //this could be optimized...
  455. Vector2 n = (r_to - r_from).normalized();
  456. while (p_rect.has_point(r_from)) {
  457. r_from += n;
  458. }
  459. }
  460. void AnimationNodeStateMachineEditor::_clip_dst_line_to_rect(Vector2 &r_from, Vector2 &r_to, const Rect2 &p_rect) {
  461. if (r_to == r_from) {
  462. return;
  463. }
  464. //this could be optimized...
  465. Vector2 n = (r_to - r_from).normalized();
  466. while (p_rect.has_point(r_to)) {
  467. r_to -= n;
  468. }
  469. }
  470. void AnimationNodeStateMachineEditor::_state_machine_draw() {
  471. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  472. Ref<StyleBox> style = get_theme_stylebox(SNAME("state_machine_frame"), SNAME("GraphNode"));
  473. Ref<StyleBox> style_selected = get_theme_stylebox(SNAME("state_machine_selectedframe"), SNAME("GraphNode"));
  474. Ref<Font> font = get_theme_font(SNAME("title_font"), SNAME("GraphNode"));
  475. int font_size = get_theme_font_size(SNAME("title_font_size"), SNAME("GraphNode"));
  476. Color font_color = get_theme_color(SNAME("title_color"), SNAME("GraphNode"));
  477. Ref<Texture2D> play = get_theme_icon(SNAME("Play"), SNAME("EditorIcons"));
  478. Ref<Texture2D> auto_play = get_theme_icon(SNAME("AutoPlay"), SNAME("EditorIcons"));
  479. Ref<Texture2D> edit = get_theme_icon(SNAME("Edit"), SNAME("EditorIcons"));
  480. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  481. Color linecolor = get_theme_color(SNAME("font_color"), SNAME("Label"));
  482. linecolor.a *= 0.3;
  483. Ref<StyleBox> playing_overlay = get_theme_stylebox(SNAME("position"), SNAME("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 (const StringName &E : nodes) {
  516. Ref<AnimationNode> anode = state_machine->get_node(E);
  517. String name = E;
  518. bool needs_editor = EditorNode::get_singleton()->item_has_editor(anode.ptr());
  519. Ref<StyleBox> sb = E == selected_node ? style_selected : style;
  520. Size2 s = sb->get_minimum_size();
  521. int strsize = font->get_string_size(name, font_size).width;
  522. s.width += strsize;
  523. s.height += MAX(font->get_height(font_size), 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) * EDSCALE;
  530. if (selected_node == E && 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;
  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<Texture2D> tr_reference_icon = get_theme_icon(SNAME("TransitionImmediateBig"), SNAME("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().orthogonal() * 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, font_size).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(font_size) - 3 * EDSCALE + font->get_ascent(font_size)), TTR("Start"), HALIGN_LEFT, -1, font_size, font_color);
  640. }
  641. if (state_machine->get_end_node() == name) {
  642. int endofs = nr.node.size.x - font->get_string_size(TTR("End"), font_size).x;
  643. state_machine_draw->draw_string(font, offset + Vector2(endofs, -font->get_height(font_size) - 3 * EDSCALE + font->get_ascent(font_size)), TTR("End"), HALIGN_LEFT, -1, font_size, 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<Texture2D> 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(font_size)) / 2).floor();
  656. nr.name.size = Vector2(strsize, font->get_height(font_size));
  657. state_machine_draw->draw_string(font, nr.name.position + Vector2(0, font->get_ascent(font_size)), name, HALIGN_LEFT, -1, font_size, 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_theme_color(SNAME("font_color"), SNAME("Label"));
  715. Color bg = fg;
  716. bg.a *= 0.3;
  717. state_machine_play_pos->draw_line(from, to, bg, 2);
  718. to = from.lerp(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 || p_what == NOTIFICATION_LAYOUT_DIRECTION_CHANGED || p_what == NOTIFICATION_TRANSLATION_CHANGED) {
  731. error_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  732. error_label->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  733. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  734. tool_select->set_icon(get_theme_icon(SNAME("ToolSelect"), SNAME("EditorIcons")));
  735. tool_create->set_icon(get_theme_icon(SNAME("ToolAddNode"), SNAME("EditorIcons")));
  736. tool_connect->set_icon(get_theme_icon(SNAME("ToolConnect"), SNAME("EditorIcons")));
  737. transition_mode->clear();
  738. transition_mode->add_icon_item(get_theme_icon(SNAME("TransitionImmediate"), SNAME("EditorIcons")), TTR("Immediate"));
  739. transition_mode->add_icon_item(get_theme_icon(SNAME("TransitionSync"), SNAME("EditorIcons")), TTR("Sync"));
  740. transition_mode->add_icon_item(get_theme_icon(SNAME("TransitionEnd"), SNAME("EditorIcons")), TTR("At End"));
  741. tool_erase->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  742. tool_autoplay->set_icon(get_theme_icon(SNAME("AutoPlay"), SNAME("EditorIcons")));
  743. tool_end->set_icon(get_theme_icon(SNAME("AutoEnd"), SNAME("EditorIcons")));
  744. play_mode->clear();
  745. play_mode->add_icon_item(get_theme_icon(SNAME("PlayTravel"), SNAME("EditorIcons")), TTR("Travel"));
  746. play_mode->add_icon_item(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")), TTR("Immediate"));
  747. }
  748. if (p_what == NOTIFICATION_PROCESS) {
  749. String error;
  750. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  751. if (error_time > 0) {
  752. error = error_text;
  753. error_time -= get_process_delta_time();
  754. } else if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
  755. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  756. } else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  757. error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
  758. /*} else if (state_machine->get_parent().is_valid() && state_machine->get_parent()->is_class("AnimationNodeStateMachine")) {
  759. if (state_machine->get_start_node() == StringName() || state_machine->get_end_node() == StringName()) {
  760. error = TTR("Start and end nodes are needed for a sub-transition.");
  761. }*/
  762. } else if (playback.is_null()) {
  763. error = vformat(TTR("No playback resource set at path: %s."), AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  764. }
  765. if (error != error_label->get_text()) {
  766. error_label->set_text(error);
  767. if (error != String()) {
  768. error_panel->show();
  769. } else {
  770. error_panel->hide();
  771. }
  772. }
  773. for (int i = 0; i < transition_lines.size(); i++) {
  774. int tidx = -1;
  775. for (int j = 0; j < state_machine->get_transition_count(); j++) {
  776. if (transition_lines[i].from_node == state_machine->get_transition_from(j) && transition_lines[i].to_node == state_machine->get_transition_to(j)) {
  777. tidx = j;
  778. break;
  779. }
  780. }
  781. if (tidx == -1) { //missing transition, should redraw
  782. state_machine_draw->update();
  783. break;
  784. }
  785. if (transition_lines[i].disabled != state_machine->get_transition(tidx)->is_disabled()) {
  786. state_machine_draw->update();
  787. break;
  788. }
  789. if (transition_lines[i].auto_advance != state_machine->get_transition(tidx)->has_auto_advance()) {
  790. state_machine_draw->update();
  791. break;
  792. }
  793. if (transition_lines[i].advance_condition_name != state_machine->get_transition(tidx)->get_advance_condition_name()) {
  794. state_machine_draw->update();
  795. break;
  796. }
  797. if (transition_lines[i].mode != state_machine->get_transition(tidx)->get_switch_mode()) {
  798. state_machine_draw->update();
  799. break;
  800. }
  801. 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)));
  802. if (transition_lines[i].advance_condition_state != acstate) {
  803. state_machine_draw->update();
  804. break;
  805. }
  806. }
  807. bool same_travel_path = true;
  808. Vector<StringName> tp;
  809. bool is_playing = false;
  810. StringName current_node;
  811. StringName blend_from_node;
  812. play_pos = 0;
  813. current_length = 0;
  814. if (playback.is_valid()) {
  815. tp = playback->get_travel_path();
  816. is_playing = playback->is_playing();
  817. current_node = playback->get_current_node();
  818. blend_from_node = playback->get_blend_from_node();
  819. play_pos = playback->get_current_play_pos();
  820. current_length = playback->get_current_length();
  821. }
  822. {
  823. if (last_travel_path.size() != tp.size()) {
  824. same_travel_path = false;
  825. } else {
  826. for (int i = 0; i < last_travel_path.size(); i++) {
  827. if (last_travel_path[i] != tp[i]) {
  828. same_travel_path = false;
  829. break;
  830. }
  831. }
  832. }
  833. }
  834. //update if travel state changed
  835. if (!same_travel_path || last_active != is_playing || last_current_node != current_node || last_blend_from_node != blend_from_node) {
  836. state_machine_draw->update();
  837. last_travel_path = tp;
  838. last_current_node = current_node;
  839. last_active = is_playing;
  840. last_blend_from_node = blend_from_node;
  841. state_machine_play_pos->update();
  842. }
  843. {
  844. if (current_node != StringName() && state_machine->has_node(current_node)) {
  845. String next = current_node;
  846. Ref<AnimationNodeStateMachine> anodesm = state_machine->get_node(next);
  847. Ref<AnimationNodeStateMachinePlayback> current_node_playback;
  848. while (anodesm.is_valid()) {
  849. current_node_playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + next + "/playback");
  850. next += "/" + current_node_playback->get_current_node();
  851. anodesm = anodesm->get_node(current_node_playback->get_current_node());
  852. }
  853. // when current_node is a state machine, use playback of current_node to set play_pos
  854. if (current_node_playback.is_valid()) {
  855. play_pos = current_node_playback->get_current_play_pos();
  856. current_length = current_node_playback->get_current_length();
  857. }
  858. }
  859. }
  860. if (last_play_pos != play_pos) {
  861. last_play_pos = play_pos;
  862. state_machine_play_pos->update();
  863. }
  864. }
  865. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  866. over_node = StringName();
  867. set_process(is_visible_in_tree());
  868. }
  869. }
  870. void AnimationNodeStateMachineEditor::_open_editor(const String &p_name) {
  871. AnimationTreeEditor::get_singleton()->enter_editor(p_name);
  872. }
  873. void AnimationNodeStateMachineEditor::_removed_from_graph() {
  874. EditorNode::get_singleton()->edit_item(nullptr);
  875. }
  876. void AnimationNodeStateMachineEditor::_name_edited(const String &p_text) {
  877. const String &new_name = p_text;
  878. ERR_FAIL_COND(new_name == "" || new_name.find(".") != -1 || new_name.find("/") != -1);
  879. if (new_name == prev_name) {
  880. return; // Nothing to do.
  881. }
  882. const String &base_name = new_name;
  883. int base = 1;
  884. String name = base_name;
  885. while (state_machine->has_node(name)) {
  886. base++;
  887. name = base_name + " " + itos(base);
  888. }
  889. updating = true;
  890. undo_redo->create_action(TTR("Node Renamed"));
  891. undo_redo->add_do_method(state_machine.ptr(), "rename_node", prev_name, name);
  892. undo_redo->add_undo_method(state_machine.ptr(), "rename_node", name, prev_name);
  893. undo_redo->add_do_method(this, "_update_graph");
  894. undo_redo->add_undo_method(this, "_update_graph");
  895. undo_redo->commit_action();
  896. name_edit_popup->hide();
  897. updating = false;
  898. state_machine_draw->update();
  899. }
  900. void AnimationNodeStateMachineEditor::_name_edited_focus_out() {
  901. if (updating) {
  902. return;
  903. }
  904. _name_edited(name_edit->get_text());
  905. }
  906. void AnimationNodeStateMachineEditor::_scroll_changed(double) {
  907. if (updating) {
  908. return;
  909. }
  910. state_machine->set_graph_offset(Vector2(h_scroll->get_value(), v_scroll->get_value()));
  911. state_machine_draw->update();
  912. }
  913. void AnimationNodeStateMachineEditor::_erase_selected() {
  914. if (selected_node != StringName() && state_machine->has_node(selected_node)) {
  915. updating = true;
  916. undo_redo->create_action(TTR("Node Removed"));
  917. undo_redo->add_do_method(state_machine.ptr(), "remove_node", selected_node);
  918. 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));
  919. for (int i = 0; i < state_machine->get_transition_count(); i++) {
  920. String from = state_machine->get_transition_from(i);
  921. String to = state_machine->get_transition_to(i);
  922. if (from == selected_node || to == selected_node) {
  923. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", from, to, state_machine->get_transition(i));
  924. }
  925. }
  926. if (String(state_machine->get_start_node()) == selected_node) {
  927. undo_redo->add_undo_method(state_machine.ptr(), "set_start_node", selected_node);
  928. }
  929. undo_redo->add_do_method(this, "_update_graph");
  930. undo_redo->add_undo_method(this, "_update_graph");
  931. undo_redo->commit_action();
  932. updating = false;
  933. selected_node = StringName();
  934. }
  935. if (selected_transition_to != StringName() && selected_transition_from != StringName() && state_machine->has_transition(selected_transition_from, selected_transition_to)) {
  936. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(state_machine->find_transition(selected_transition_from, selected_transition_to));
  937. updating = true;
  938. undo_redo->create_action(TTR("Transition Removed"));
  939. undo_redo->add_do_method(state_machine.ptr(), "remove_transition", selected_transition_from, selected_transition_to);
  940. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", selected_transition_from, selected_transition_to, tr);
  941. undo_redo->add_do_method(this, "_update_graph");
  942. undo_redo->add_undo_method(this, "_update_graph");
  943. undo_redo->commit_action();
  944. updating = false;
  945. selected_transition_from = StringName();
  946. selected_transition_to = StringName();
  947. }
  948. state_machine_draw->update();
  949. }
  950. void AnimationNodeStateMachineEditor::_autoplay_selected() {
  951. if (selected_node != StringName() && state_machine->has_node(selected_node)) {
  952. StringName new_start_node;
  953. if (state_machine->get_start_node() == selected_node) { //toggle it
  954. new_start_node = StringName();
  955. } else {
  956. new_start_node = selected_node;
  957. }
  958. updating = true;
  959. undo_redo->create_action(TTR("Set Start Node (Autoplay)"));
  960. undo_redo->add_do_method(state_machine.ptr(), "set_start_node", new_start_node);
  961. undo_redo->add_undo_method(state_machine.ptr(), "set_start_node", state_machine->get_start_node());
  962. undo_redo->add_do_method(this, "_update_graph");
  963. undo_redo->add_undo_method(this, "_update_graph");
  964. undo_redo->commit_action();
  965. updating = false;
  966. state_machine_draw->update();
  967. }
  968. }
  969. void AnimationNodeStateMachineEditor::_end_selected() {
  970. if (selected_node != StringName() && state_machine->has_node(selected_node)) {
  971. StringName new_end_node;
  972. if (state_machine->get_end_node() == selected_node) { //toggle it
  973. new_end_node = StringName();
  974. } else {
  975. new_end_node = selected_node;
  976. }
  977. updating = true;
  978. undo_redo->create_action(TTR("Set Start Node (Autoplay)"));
  979. undo_redo->add_do_method(state_machine.ptr(), "set_end_node", new_end_node);
  980. undo_redo->add_undo_method(state_machine.ptr(), "set_end_node", state_machine->get_end_node());
  981. undo_redo->add_do_method(this, "_update_graph");
  982. undo_redo->add_undo_method(this, "_update_graph");
  983. undo_redo->commit_action();
  984. updating = false;
  985. state_machine_draw->update();
  986. }
  987. }
  988. void AnimationNodeStateMachineEditor::_update_mode() {
  989. if (tool_select->is_pressed()) {
  990. tool_erase_hb->show();
  991. tool_erase->set_disabled(selected_node == StringName() && selected_transition_from == StringName() && selected_transition_to == StringName());
  992. tool_autoplay->set_disabled(selected_node == StringName());
  993. tool_end->set_disabled(selected_node == StringName());
  994. } else {
  995. tool_erase_hb->hide();
  996. }
  997. }
  998. void AnimationNodeStateMachineEditor::_bind_methods() {
  999. ClassDB::bind_method("_update_graph", &AnimationNodeStateMachineEditor::_update_graph);
  1000. ClassDB::bind_method("_removed_from_graph", &AnimationNodeStateMachineEditor::_removed_from_graph);
  1001. ClassDB::bind_method("_open_editor", &AnimationNodeStateMachineEditor::_open_editor);
  1002. }
  1003. AnimationNodeStateMachineEditor *AnimationNodeStateMachineEditor::singleton = nullptr;
  1004. AnimationNodeStateMachineEditor::AnimationNodeStateMachineEditor() {
  1005. singleton = this;
  1006. updating = false;
  1007. HBoxContainer *top_hb = memnew(HBoxContainer);
  1008. add_child(top_hb);
  1009. Ref<ButtonGroup> bg;
  1010. bg.instantiate();
  1011. tool_select = memnew(Button);
  1012. tool_select->set_flat(true);
  1013. top_hb->add_child(tool_select);
  1014. tool_select->set_toggle_mode(true);
  1015. tool_select->set_button_group(bg);
  1016. tool_select->set_pressed(true);
  1017. tool_select->set_tooltip(TTR("Select and move nodes.\nRMB to add new nodes.\nShift+LMB to create connections."));
  1018. tool_select->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), varray(), CONNECT_DEFERRED);
  1019. tool_create = memnew(Button);
  1020. tool_create->set_flat(true);
  1021. top_hb->add_child(tool_create);
  1022. tool_create->set_toggle_mode(true);
  1023. tool_create->set_button_group(bg);
  1024. tool_create->set_tooltip(TTR("Create new nodes."));
  1025. tool_create->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), varray(), CONNECT_DEFERRED);
  1026. tool_connect = memnew(Button);
  1027. tool_connect->set_flat(true);
  1028. top_hb->add_child(tool_connect);
  1029. tool_connect->set_toggle_mode(true);
  1030. tool_connect->set_button_group(bg);
  1031. tool_connect->set_tooltip(TTR("Connect nodes."));
  1032. tool_connect->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), varray(), CONNECT_DEFERRED);
  1033. tool_erase_hb = memnew(HBoxContainer);
  1034. top_hb->add_child(tool_erase_hb);
  1035. tool_erase_hb->add_child(memnew(VSeparator));
  1036. tool_erase = memnew(Button);
  1037. tool_erase->set_flat(true);
  1038. tool_erase->set_tooltip(TTR("Remove selected node or transition."));
  1039. tool_erase_hb->add_child(tool_erase);
  1040. tool_erase->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_erase_selected));
  1041. tool_erase->set_disabled(true);
  1042. tool_erase_hb->add_child(memnew(VSeparator));
  1043. tool_autoplay = memnew(Button);
  1044. tool_autoplay->set_flat(true);
  1045. tool_autoplay->set_tooltip(TTR("Toggle autoplay this animation on start, restart or seek to zero."));
  1046. tool_erase_hb->add_child(tool_autoplay);
  1047. tool_autoplay->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_autoplay_selected));
  1048. tool_autoplay->set_disabled(true);
  1049. tool_end = memnew(Button);
  1050. tool_end->set_flat(true);
  1051. tool_end->set_tooltip(TTR("Set the end animation. This is useful for sub-transitions."));
  1052. tool_erase_hb->add_child(tool_end);
  1053. tool_end->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_end_selected));
  1054. tool_end->set_disabled(true);
  1055. top_hb->add_child(memnew(VSeparator));
  1056. top_hb->add_child(memnew(Label(TTR("Transition: "))));
  1057. transition_mode = memnew(OptionButton);
  1058. top_hb->add_child(transition_mode);
  1059. top_hb->add_spacer();
  1060. top_hb->add_child(memnew(Label(TTR("Play Mode:"))));
  1061. play_mode = memnew(OptionButton);
  1062. top_hb->add_child(play_mode);
  1063. panel = memnew(PanelContainer);
  1064. panel->set_clip_contents(true);
  1065. add_child(panel);
  1066. panel->set_v_size_flags(SIZE_EXPAND_FILL);
  1067. state_machine_draw = memnew(Control);
  1068. panel->add_child(state_machine_draw);
  1069. state_machine_draw->connect("gui_input", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_gui_input));
  1070. state_machine_draw->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_draw));
  1071. state_machine_draw->set_focus_mode(FOCUS_ALL);
  1072. state_machine_play_pos = memnew(Control);
  1073. state_machine_draw->add_child(state_machine_play_pos);
  1074. state_machine_play_pos->set_mouse_filter(MOUSE_FILTER_PASS); //pass all to parent
  1075. state_machine_play_pos->set_anchors_and_offsets_preset(PRESET_WIDE);
  1076. state_machine_play_pos->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_pos_draw));
  1077. v_scroll = memnew(VScrollBar);
  1078. state_machine_draw->add_child(v_scroll);
  1079. v_scroll->set_anchors_and_offsets_preset(PRESET_RIGHT_WIDE);
  1080. v_scroll->connect("value_changed", callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));
  1081. h_scroll = memnew(HScrollBar);
  1082. state_machine_draw->add_child(h_scroll);
  1083. h_scroll->set_anchors_and_offsets_preset(PRESET_BOTTOM_WIDE);
  1084. h_scroll->set_offset(SIDE_RIGHT, -v_scroll->get_size().x * EDSCALE);
  1085. h_scroll->connect("value_changed", callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));
  1086. error_panel = memnew(PanelContainer);
  1087. add_child(error_panel);
  1088. error_label = memnew(Label);
  1089. error_panel->add_child(error_label);
  1090. error_panel->hide();
  1091. undo_redo = EditorNode::get_undo_redo();
  1092. set_custom_minimum_size(Size2(0, 300 * EDSCALE));
  1093. menu = memnew(PopupMenu);
  1094. add_child(menu);
  1095. menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_menu_type));
  1096. animations_menu = memnew(PopupMenu);
  1097. menu->add_child(animations_menu);
  1098. animations_menu->set_name("animations");
  1099. animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_animation_type));
  1100. name_edit_popup = memnew(Popup);
  1101. add_child(name_edit_popup);
  1102. name_edit = memnew(LineEdit);
  1103. name_edit_popup->add_child(name_edit);
  1104. name_edit->set_anchors_and_offsets_preset(PRESET_WIDE);
  1105. name_edit->connect("text_submitted", callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited));
  1106. name_edit->connect("focus_exited", callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited_focus_out));
  1107. open_file = memnew(EditorFileDialog);
  1108. add_child(open_file);
  1109. open_file->set_title(TTR("Open Animation Node"));
  1110. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1111. open_file->connect("file_selected", callable_mp(this, &AnimationNodeStateMachineEditor::_file_opened));
  1112. undo_redo = EditorNode::get_undo_redo();
  1113. over_text = false;
  1114. over_node_what = -1;
  1115. dragging_selected_attempt = false;
  1116. connecting = false;
  1117. last_active = false;
  1118. error_time = 0;
  1119. }