animation_node_state_machine.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*************************************************************************/
  2. /* animation_node_state_machine.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_node_state_machine.h"
  31. /////////////////////////////////////////////////
  32. void AnimationNodeStateMachineTransition::set_switch_mode(SwitchMode p_mode) {
  33. switch_mode = p_mode;
  34. }
  35. AnimationNodeStateMachineTransition::SwitchMode AnimationNodeStateMachineTransition::get_switch_mode() const {
  36. return switch_mode;
  37. }
  38. void AnimationNodeStateMachineTransition::set_auto_advance(bool p_enable) {
  39. auto_advance = p_enable;
  40. }
  41. bool AnimationNodeStateMachineTransition::has_auto_advance() const {
  42. return auto_advance;
  43. }
  44. void AnimationNodeStateMachineTransition::set_advance_condition(const StringName &p_condition) {
  45. String cs = p_condition;
  46. ERR_FAIL_COND(cs.contains("/") || cs.contains(":"));
  47. advance_condition = p_condition;
  48. if (!cs.is_empty()) {
  49. advance_condition_name = "conditions/" + cs;
  50. } else {
  51. advance_condition_name = StringName();
  52. }
  53. emit_signal(SNAME("advance_condition_changed"));
  54. }
  55. StringName AnimationNodeStateMachineTransition::get_advance_condition() const {
  56. return advance_condition;
  57. }
  58. StringName AnimationNodeStateMachineTransition::get_advance_condition_name() const {
  59. return advance_condition_name;
  60. }
  61. void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) {
  62. ERR_FAIL_COND(p_xfade < 0);
  63. xfade = p_xfade;
  64. emit_changed();
  65. }
  66. float AnimationNodeStateMachineTransition::get_xfade_time() const {
  67. return xfade;
  68. }
  69. void AnimationNodeStateMachineTransition::set_disabled(bool p_disabled) {
  70. disabled = p_disabled;
  71. emit_changed();
  72. }
  73. bool AnimationNodeStateMachineTransition::is_disabled() const {
  74. return disabled;
  75. }
  76. void AnimationNodeStateMachineTransition::set_priority(int p_priority) {
  77. priority = p_priority;
  78. emit_changed();
  79. }
  80. int AnimationNodeStateMachineTransition::get_priority() const {
  81. return priority;
  82. }
  83. void AnimationNodeStateMachineTransition::_bind_methods() {
  84. ClassDB::bind_method(D_METHOD("set_switch_mode", "mode"), &AnimationNodeStateMachineTransition::set_switch_mode);
  85. ClassDB::bind_method(D_METHOD("get_switch_mode"), &AnimationNodeStateMachineTransition::get_switch_mode);
  86. ClassDB::bind_method(D_METHOD("set_auto_advance", "auto_advance"), &AnimationNodeStateMachineTransition::set_auto_advance);
  87. ClassDB::bind_method(D_METHOD("has_auto_advance"), &AnimationNodeStateMachineTransition::has_auto_advance);
  88. ClassDB::bind_method(D_METHOD("set_advance_condition", "name"), &AnimationNodeStateMachineTransition::set_advance_condition);
  89. ClassDB::bind_method(D_METHOD("get_advance_condition"), &AnimationNodeStateMachineTransition::get_advance_condition);
  90. ClassDB::bind_method(D_METHOD("set_xfade_time", "secs"), &AnimationNodeStateMachineTransition::set_xfade_time);
  91. ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeStateMachineTransition::get_xfade_time);
  92. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &AnimationNodeStateMachineTransition::set_disabled);
  93. ClassDB::bind_method(D_METHOD("is_disabled"), &AnimationNodeStateMachineTransition::is_disabled);
  94. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority);
  95. ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority);
  96. ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,At End"), "set_switch_mode", "get_switch_mode");
  97. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_advance"), "set_auto_advance", "has_auto_advance");
  98. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "advance_condition"), "set_advance_condition", "get_advance_condition");
  99. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01"), "set_xfade_time", "get_xfade_time");
  100. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,32,1"), "set_priority", "get_priority");
  101. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  102. BIND_ENUM_CONSTANT(SWITCH_MODE_IMMEDIATE);
  103. BIND_ENUM_CONSTANT(SWITCH_MODE_SYNC);
  104. BIND_ENUM_CONSTANT(SWITCH_MODE_AT_END);
  105. ADD_SIGNAL(MethodInfo("advance_condition_changed"));
  106. }
  107. AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() {
  108. }
  109. ////////////////////////////////////////////////////////
  110. void AnimationNodeStateMachinePlayback::travel(const StringName &p_state) {
  111. start_request_travel = true;
  112. start_request = p_state;
  113. stop_request = false;
  114. }
  115. void AnimationNodeStateMachinePlayback::start(const StringName &p_state) {
  116. start_request_travel = false;
  117. start_request = p_state;
  118. stop_request = false;
  119. }
  120. void AnimationNodeStateMachinePlayback::stop() {
  121. stop_request = true;
  122. }
  123. bool AnimationNodeStateMachinePlayback::is_playing() const {
  124. return playing;
  125. }
  126. StringName AnimationNodeStateMachinePlayback::get_current_node() const {
  127. return current;
  128. }
  129. StringName AnimationNodeStateMachinePlayback::get_blend_from_node() const {
  130. return fading_from;
  131. }
  132. Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const {
  133. return path;
  134. }
  135. float AnimationNodeStateMachinePlayback::get_current_play_pos() const {
  136. return pos_current;
  137. }
  138. float AnimationNodeStateMachinePlayback::get_current_length() const {
  139. return len_current;
  140. }
  141. bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel) {
  142. ERR_FAIL_COND_V(!playing, false);
  143. ERR_FAIL_COND_V(!p_state_machine->states.has(p_travel), false);
  144. ERR_FAIL_COND_V(!p_state_machine->states.has(current), false);
  145. path.clear(); //a new one will be needed
  146. if (current == p_travel) {
  147. return true; //nothing to do
  148. }
  149. Vector2 current_pos = p_state_machine->states[current].position;
  150. Vector2 target_pos = p_state_machine->states[p_travel].position;
  151. HashMap<StringName, AStarCost> cost_map;
  152. List<int> open_list;
  153. //build open list
  154. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  155. if (p_state_machine->transitions[i].transition->is_disabled()) {
  156. continue;
  157. }
  158. if (p_state_machine->transitions[i].local_from == current) {
  159. open_list.push_back(i);
  160. float cost = p_state_machine->states[p_state_machine->transitions[i].local_to].position.distance_to(current_pos);
  161. cost *= p_state_machine->transitions[i].transition->get_priority();
  162. AStarCost ap;
  163. ap.prev = current;
  164. ap.distance = cost;
  165. cost_map[p_state_machine->transitions[i].local_to] = ap;
  166. if (p_state_machine->transitions[i].local_to == p_travel) { //prematurely found it! :D
  167. path.push_back(p_travel);
  168. return true;
  169. }
  170. }
  171. }
  172. //begin astar
  173. bool found_route = false;
  174. while (!found_route) {
  175. if (open_list.size() == 0) {
  176. return false; //no path found
  177. }
  178. //find the last cost transition
  179. List<int>::Element *least_cost_transition = nullptr;
  180. float least_cost = 1e20;
  181. for (List<int>::Element *E = open_list.front(); E; E = E->next()) {
  182. float cost = cost_map[p_state_machine->transitions[E->get()].local_to].distance;
  183. cost += p_state_machine->states[p_state_machine->transitions[E->get()].local_to].position.distance_to(target_pos);
  184. if (cost < least_cost) {
  185. least_cost_transition = E;
  186. least_cost = cost;
  187. }
  188. }
  189. StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].local_from;
  190. StringName transition = p_state_machine->transitions[least_cost_transition->get()].local_to;
  191. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  192. if (p_state_machine->transitions[i].transition->is_disabled()) {
  193. continue;
  194. }
  195. if (p_state_machine->transitions[i].local_from != transition || p_state_machine->transitions[i].local_to == transition_prev) {
  196. continue; //not interested on those
  197. }
  198. float distance = p_state_machine->states[p_state_machine->transitions[i].local_from].position.distance_to(p_state_machine->states[p_state_machine->transitions[i].local_to].position);
  199. distance *= p_state_machine->transitions[i].transition->get_priority();
  200. distance += cost_map[p_state_machine->transitions[i].local_from].distance;
  201. if (cost_map.has(p_state_machine->transitions[i].local_to)) {
  202. //oh this was visited already, can we win the cost?
  203. if (distance < cost_map[p_state_machine->transitions[i].local_to].distance) {
  204. cost_map[p_state_machine->transitions[i].local_to].distance = distance;
  205. cost_map[p_state_machine->transitions[i].local_to].prev = p_state_machine->transitions[i].local_from;
  206. }
  207. } else {
  208. //add to open list
  209. AStarCost ac;
  210. ac.prev = p_state_machine->transitions[i].local_from;
  211. ac.distance = distance;
  212. cost_map[p_state_machine->transitions[i].local_to] = ac;
  213. open_list.push_back(i);
  214. if (p_state_machine->transitions[i].local_to == p_travel) {
  215. found_route = true;
  216. break;
  217. }
  218. }
  219. }
  220. if (found_route) {
  221. break;
  222. }
  223. open_list.erase(least_cost_transition);
  224. }
  225. //make path
  226. StringName at = p_travel;
  227. while (at != current) {
  228. path.push_back(at);
  229. at = cost_map[at].prev;
  230. }
  231. path.reverse();
  232. return true;
  233. }
  234. double AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, double p_time, bool p_seek, bool p_seek_root) {
  235. //if not playing and it can restart, then restart
  236. if (!playing && start_request == StringName()) {
  237. if (!stop_request && p_state_machine->start_node) {
  238. start(p_state_machine->start_node);
  239. } else {
  240. return 0;
  241. }
  242. }
  243. if (playing && stop_request) {
  244. stop_request = false;
  245. playing = false;
  246. return 0;
  247. }
  248. bool play_start = false;
  249. if (start_request != StringName()) {
  250. if (start_request_travel) {
  251. if (!playing) {
  252. if (!stop_request && p_state_machine->start_node) {
  253. // can restart, just postpone traveling
  254. path.clear();
  255. current = p_state_machine->start_node;
  256. playing = true;
  257. play_start = true;
  258. } else {
  259. // stopped, invalid state
  260. String node_name = start_request;
  261. start_request = StringName(); //clear start request
  262. ERR_FAIL_V_MSG(0, "Can't travel to '" + node_name + "' if state machine is not playing. Maybe you need to enable Autoplay on Load for one of the nodes in your state machine or call .start() first?");
  263. }
  264. } else {
  265. if (!_travel(p_state_machine, start_request)) {
  266. // can't travel, then teleport
  267. path.clear();
  268. current = start_request;
  269. }
  270. start_request = StringName(); //clear start request
  271. }
  272. } else {
  273. // teleport to start
  274. if (p_state_machine->states.has(start_request)) {
  275. path.clear();
  276. current = start_request;
  277. playing = true;
  278. play_start = true;
  279. start_request = StringName(); //clear start request
  280. } else {
  281. StringName node = start_request;
  282. start_request = StringName(); //clear start request
  283. ERR_FAIL_V_MSG(0, "No such node: '" + node + "'");
  284. }
  285. }
  286. }
  287. bool do_start = (p_seek && p_time == 0) || play_start || current == StringName();
  288. if (do_start) {
  289. if (p_state_machine->start_node != StringName() && p_seek && p_time == 0) {
  290. current = p_state_machine->start_node;
  291. }
  292. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, p_seek_root, 1.0, AnimationNode::FILTER_IGNORE, false);
  293. pos_current = 0;
  294. }
  295. if (!p_state_machine->states.has(current)) {
  296. playing = false; //current does not exist
  297. current = StringName();
  298. return 0;
  299. }
  300. float fade_blend = 1.0;
  301. if (fading_from != StringName()) {
  302. if (!p_state_machine->states.has(fading_from)) {
  303. fading_from = StringName();
  304. } else {
  305. if (!p_seek) {
  306. fading_pos += p_time;
  307. }
  308. fade_blend = MIN(1.0, fading_pos / fading_time);
  309. if (fade_blend >= 1.0) {
  310. fading_from = StringName();
  311. }
  312. }
  313. }
  314. float rem = p_state_machine->blend_node(current, p_state_machine->states[current].node, p_time, p_seek, p_seek_root, fade_blend, AnimationNode::FILTER_IGNORE, false);
  315. if (fading_from != StringName()) {
  316. p_state_machine->blend_node(fading_from, p_state_machine->states[fading_from].node, p_time, p_seek, p_seek_root, 1.0 - fade_blend, AnimationNode::FILTER_IGNORE, false);
  317. }
  318. //guess playback position
  319. if (rem > len_current) { // weird but ok
  320. len_current = rem;
  321. }
  322. { //advance and loop check
  323. float next_pos = len_current - rem;
  324. end_loop = next_pos < pos_current;
  325. pos_current = next_pos; //looped
  326. }
  327. //find next
  328. StringName next;
  329. float next_xfade = 0.0;
  330. AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE;
  331. if (path.size()) {
  332. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  333. if (p_state_machine->transitions[i].transition->is_disabled()) {
  334. continue;
  335. }
  336. if (p_state_machine->transitions[i].local_from == current && p_state_machine->transitions[i].local_to == path[0]) {
  337. next_xfade = p_state_machine->transitions[i].transition->get_xfade_time();
  338. switch_mode = p_state_machine->transitions[i].transition->get_switch_mode();
  339. next = path[0];
  340. }
  341. }
  342. } else {
  343. float priority_best = 1e20;
  344. int auto_advance_to = -1;
  345. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  346. if (p_state_machine->transitions[i].transition->is_disabled()) {
  347. continue;
  348. }
  349. // handles end_node: when end_node is reached in a sub state machine, find and activate the current_transition
  350. if (force_auto_advance) {
  351. if (p_state_machine->transitions[i].from == current_transition.from && p_state_machine->transitions[i].to == current_transition.to) {
  352. auto_advance_to = i;
  353. force_auto_advance = false;
  354. break;
  355. }
  356. }
  357. // handles start_node: if previous state machine is pointing to a node inside the current state machine, starts the current machine from start_node to prev_local_to
  358. if (p_state_machine->start_node == current && p_state_machine->transitions[i].local_from == current) {
  359. if (p_state_machine->prev_state_machine != nullptr) {
  360. Ref<AnimationNodeStateMachinePlayback> prev_playback = p_state_machine->prev_state_machine->get_parameter("playback");
  361. if (prev_playback.is_valid()) {
  362. StringName prev_local_to = String(prev_playback->current_transition.next).replace_first(String(p_state_machine->state_machine_name) + "/", "");
  363. if (p_state_machine->transitions[i].to == prev_local_to) {
  364. auto_advance_to = i;
  365. prev_playback->current_transition.next = StringName();
  366. break;
  367. }
  368. }
  369. }
  370. }
  371. if (p_state_machine->transitions[i].from == current && _check_advance_condition(p_state_machine, p_state_machine->transitions[i].transition)) {
  372. if (p_state_machine->transitions[i].transition->get_priority() <= priority_best) {
  373. priority_best = p_state_machine->transitions[i].transition->get_priority();
  374. auto_advance_to = i;
  375. }
  376. }
  377. }
  378. if (auto_advance_to != -1) {
  379. next = p_state_machine->transitions[auto_advance_to].local_to;
  380. Transition tr;
  381. tr.from = String(p_state_machine->state_machine_name) + "/" + String(p_state_machine->transitions[auto_advance_to].from);
  382. tr.to = String(p_state_machine->transitions[auto_advance_to].to).replace_first("../", "");
  383. tr.next = p_state_machine->transitions[auto_advance_to].to;
  384. current_transition = tr;
  385. next_xfade = p_state_machine->transitions[auto_advance_to].transition->get_xfade_time();
  386. switch_mode = p_state_machine->transitions[auto_advance_to].transition->get_switch_mode();
  387. }
  388. }
  389. if (next == p_state_machine->end_node) {
  390. AnimationNodeStateMachine *prev_state_machine = p_state_machine->prev_state_machine;
  391. if (prev_state_machine != nullptr) {
  392. Ref<AnimationNodeStateMachinePlayback> prev_playback = prev_state_machine->get_parameter("playback");
  393. if (prev_playback.is_valid()) {
  394. if (next_xfade) {
  395. prev_playback->current_transition = current_transition;
  396. prev_playback->force_auto_advance = true;
  397. return rem;
  398. }
  399. float priority_best = 1e20;
  400. int auto_advance_to = -1;
  401. for (int i = 0; i < prev_state_machine->transitions.size(); i++) {
  402. if (prev_state_machine->transitions[i].transition->is_disabled()) {
  403. continue;
  404. }
  405. if (current_transition.next == prev_state_machine->end_node && _check_advance_condition(prev_state_machine, prev_state_machine->transitions[i].transition)) {
  406. if (prev_state_machine->transitions[i].transition->get_priority() <= priority_best) {
  407. priority_best = prev_state_machine->transitions[i].transition->get_priority();
  408. auto_advance_to = i;
  409. }
  410. }
  411. }
  412. if (auto_advance_to != -1) {
  413. if (prev_state_machine->transitions[auto_advance_to].transition->get_xfade_time()) {
  414. return rem;
  415. }
  416. }
  417. }
  418. }
  419. }
  420. //if next, see when to transition
  421. if (next != StringName()) {
  422. bool goto_next = false;
  423. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
  424. goto_next = next_xfade >= (len_current - pos_current) || end_loop;
  425. if (end_loop) {
  426. next_xfade = 0;
  427. }
  428. } else {
  429. goto_next = fading_from == StringName();
  430. }
  431. if (goto_next) { //end_loop should be used because fade time may be too small or zero and animation may have looped
  432. if (next_xfade) {
  433. //time to fade, baby
  434. fading_from = current;
  435. fading_time = next_xfade;
  436. fading_pos = 0;
  437. } else {
  438. fading_from = StringName();
  439. fading_pos = 0;
  440. }
  441. if (path.size()) { //if it came from path, remove path
  442. path.remove_at(0);
  443. }
  444. current = next;
  445. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
  446. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, p_seek_root, 0, AnimationNode::FILTER_IGNORE, false);
  447. pos_current = MIN(pos_current, len_current);
  448. p_state_machine->blend_node(current, p_state_machine->states[current].node, pos_current, true, p_seek_root, 0, AnimationNode::FILTER_IGNORE, false);
  449. } else {
  450. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, p_seek_root, 0, AnimationNode::FILTER_IGNORE, false);
  451. pos_current = 0;
  452. }
  453. rem = len_current; //so it does not show 0 on transition
  454. }
  455. }
  456. // time left must always be 1 because the end node don't length to compute
  457. if (p_state_machine->end_node != current) {
  458. rem = 1;
  459. } else {
  460. Ref<AnimationNodeStateMachinePlayback> prev_playback = p_state_machine->prev_state_machine->get_parameter("playback");
  461. if (prev_playback.is_valid()) {
  462. prev_playback->current_transition = current_transition;
  463. prev_playback->force_auto_advance = true;
  464. }
  465. }
  466. return rem;
  467. }
  468. bool AnimationNodeStateMachinePlayback::_check_advance_condition(const Ref<AnimationNodeStateMachine> state_machine, const Ref<AnimationNodeStateMachineTransition> transition) const {
  469. if (transition->has_auto_advance()) {
  470. return true;
  471. }
  472. StringName advance_condition_name = transition->get_advance_condition_name();
  473. if (advance_condition_name != StringName() && bool(state_machine->get_parameter(advance_condition_name))) {
  474. return true;
  475. }
  476. return false;
  477. }
  478. void AnimationNodeStateMachinePlayback::_bind_methods() {
  479. ClassDB::bind_method(D_METHOD("travel", "to_node"), &AnimationNodeStateMachinePlayback::travel);
  480. ClassDB::bind_method(D_METHOD("start", "node"), &AnimationNodeStateMachinePlayback::start);
  481. ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
  482. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
  483. ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
  484. ClassDB::bind_method(D_METHOD("get_current_play_position"), &AnimationNodeStateMachinePlayback::get_current_play_pos);
  485. ClassDB::bind_method(D_METHOD("get_current_length"), &AnimationNodeStateMachinePlayback::get_current_length);
  486. ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path);
  487. }
  488. AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
  489. set_local_to_scene(true); //only one per instantiated scene
  490. }
  491. ///////////////////////////////////////////////////////
  492. void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
  493. r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  494. List<StringName> advance_conditions;
  495. for (int i = 0; i < transitions.size(); i++) {
  496. StringName ac = transitions[i].transition->get_advance_condition_name();
  497. if (ac != StringName() && advance_conditions.find(ac) == nullptr) {
  498. advance_conditions.push_back(ac);
  499. }
  500. }
  501. advance_conditions.sort_custom<StringName::AlphCompare>();
  502. for (const StringName &E : advance_conditions) {
  503. r_list->push_back(PropertyInfo(Variant::BOOL, E));
  504. }
  505. // for (const KeyValue<StringName, State> &E : states) {
  506. // if (E->node == ansm) {
  507. // for (int i = 0; i < E->node->transitions.size(); i++) {
  508. // StringName ac = E->node->transitions[i].transition->get_advance_condition_name();
  509. // if (ac != StringName() && advance_conditions.find(ac) == nullptr) {
  510. // advance_conditions.push_back(ac);
  511. // }
  512. // }
  513. // advance_conditions.sort_custom<StringName::AlphCompare>();
  514. // for (const StringName &E : advance_conditions) {
  515. // r_list->push_back(PropertyInfo(Variant::BOOL, E));
  516. // }
  517. // }
  518. // }
  519. }
  520. Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
  521. if (p_parameter == playback) {
  522. Ref<AnimationNodeStateMachinePlayback> p;
  523. p.instantiate();
  524. return p;
  525. } else {
  526. return false; //advance condition
  527. }
  528. }
  529. void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  530. ERR_FAIL_COND(states.has(p_name));
  531. ERR_FAIL_COND(p_node.is_null());
  532. ERR_FAIL_COND(String(p_name).contains("/"));
  533. State state;
  534. state.node = p_node;
  535. state.position = p_position;
  536. states[p_name] = state;
  537. Ref<AnimationNodeStateMachine> anodesm = p_node;
  538. if (anodesm.is_valid()) {
  539. anodesm->state_machine_name = p_name;
  540. anodesm->prev_state_machine = this;
  541. }
  542. emit_changed();
  543. emit_signal(SNAME("tree_changed"));
  544. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), varray(), CONNECT_REFERENCE_COUNTED);
  545. }
  546. void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
  547. ERR_FAIL_COND(states.has(p_name) == false);
  548. ERR_FAIL_COND(p_node.is_null());
  549. ERR_FAIL_COND(String(p_name).contains("/"));
  550. {
  551. Ref<AnimationNode> node = states[p_name].node;
  552. if (node.is_valid()) {
  553. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  554. }
  555. }
  556. states[p_name].node = p_node;
  557. emit_changed();
  558. emit_signal(SNAME("tree_changed"));
  559. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), varray(), CONNECT_REFERENCE_COUNTED);
  560. }
  561. bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
  562. if (states.has(p_name)) {
  563. return !(states[p_name].node->is_class("AnimationNodeStartState") || states[p_name].node->is_class("AnimationNodeEndState"));
  564. }
  565. return true;
  566. }
  567. Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
  568. ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>());
  569. return states[p_name].node;
  570. }
  571. StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
  572. for (const KeyValue<StringName, State> &E : states) {
  573. if (E.value.node == p_node) {
  574. return E.key;
  575. }
  576. }
  577. ERR_FAIL_V(StringName());
  578. }
  579. void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
  580. Vector<StringName> nodes;
  581. for (const KeyValue<StringName, State> &E : states) {
  582. nodes.push_back(E.key);
  583. }
  584. nodes.sort_custom<StringName::AlphCompare>();
  585. for (int i = 0; i < nodes.size(); i++) {
  586. ChildNode cn;
  587. cn.name = nodes[i];
  588. cn.node = states[cn.name].node;
  589. r_child_nodes->push_back(cn);
  590. }
  591. }
  592. bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
  593. return states.has(p_name);
  594. }
  595. void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
  596. ERR_FAIL_COND(!states.has(p_name));
  597. if (!can_edit_node(p_name)) {
  598. return;
  599. }
  600. for (int i = 0; i < transitions.size(); i++) {
  601. if (transitions[i].local_from == p_name || transitions[i].local_to == p_name) {
  602. remove_transition_by_index(i);
  603. i--;
  604. }
  605. }
  606. {
  607. Ref<AnimationNode> node = states[p_name].node;
  608. ERR_FAIL_COND(node.is_null());
  609. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  610. }
  611. states.erase(p_name);
  612. emit_changed();
  613. emit_signal(SNAME("tree_changed"));
  614. }
  615. void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
  616. ERR_FAIL_COND(!states.has(p_name));
  617. ERR_FAIL_COND(states.has(p_new_name));
  618. ERR_FAIL_COND(!can_edit_node(p_name));
  619. states[p_new_name] = states[p_name];
  620. states.erase(p_name);
  621. Ref<AnimationNodeStateMachine> anodesm = states[p_new_name].node;
  622. if (anodesm.is_valid()) {
  623. anodesm->state_machine_name = p_new_name;
  624. }
  625. for (int i = 0; i < transitions.size(); i++) {
  626. if (transitions[i].local_from == p_name) {
  627. _rename_transition(transitions[i].from, String(transitions[i].from).replace_first(p_name, p_new_name));
  628. }
  629. if (transitions[i].local_to == p_name) {
  630. _rename_transition(transitions[i].to, String(transitions[i].to).replace_first(p_name, p_new_name));
  631. }
  632. }
  633. emit_signal("tree_changed");
  634. }
  635. void AnimationNodeStateMachine::_rename_transition(const StringName &p_name, const StringName &p_new_name) {
  636. if (updating_transitions) {
  637. return;
  638. }
  639. updating_transitions = true;
  640. for (int i = 0; i < transitions.size(); i++) {
  641. if (transitions[i].from == p_name) {
  642. Vector<String> path = String(transitions[i].to).split("/");
  643. if (path.size() > 1) {
  644. if (path[0] == "..") {
  645. prev_state_machine->_rename_transition(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  646. } else {
  647. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_to].node)->_rename_transition("../" + p_name, "../" + p_new_name);
  648. }
  649. }
  650. transitions.write[i].from = p_new_name;
  651. }
  652. if (transitions[i].to == p_name) {
  653. Vector<String> path = String(transitions[i].from).split("/");
  654. if (path.size() > 1) {
  655. if (path[0] == "..") {
  656. prev_state_machine->_rename_transition(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  657. } else {
  658. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_from].node)->_rename_transition("../" + p_name, "../" + p_new_name);
  659. }
  660. }
  661. transitions.write[i].to = p_new_name;
  662. }
  663. updating_transitions = false;
  664. }
  665. }
  666. void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
  667. List<StringName> nodes;
  668. for (const KeyValue<StringName, State> &E : states) {
  669. if (E.key == end_node && prev_state_machine == nullptr) {
  670. continue;
  671. }
  672. nodes.push_back(E.key);
  673. }
  674. nodes.sort_custom<StringName::AlphCompare>();
  675. for (const StringName &E : nodes) {
  676. r_nodes->push_back(E);
  677. }
  678. }
  679. AnimationNodeStateMachine *AnimationNodeStateMachine::get_prev_state_machine() const {
  680. return prev_state_machine;
  681. }
  682. bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
  683. StringName from = _get_shortest_path(p_from);
  684. StringName to = _get_shortest_path(p_to);
  685. for (int i = 0; i < transitions.size(); i++) {
  686. if (transitions[i].from == from && transitions[i].to == to) {
  687. return true;
  688. }
  689. }
  690. return false;
  691. }
  692. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  693. StringName from = _get_shortest_path(p_from);
  694. StringName to = _get_shortest_path(p_to);
  695. for (int i = 0; i < transitions.size(); i++) {
  696. if (transitions[i].from == from && transitions[i].to == to) {
  697. return i;
  698. }
  699. }
  700. return -1;
  701. }
  702. bool AnimationNodeStateMachine::_can_connect(const StringName &p_name, Vector<AnimationNodeStateMachine *> p_parents) {
  703. if (p_parents.is_empty()) {
  704. AnimationNodeStateMachine *prev = this;
  705. while (prev != nullptr) {
  706. p_parents.push_back(prev);
  707. prev = prev->prev_state_machine;
  708. }
  709. }
  710. if (states.has(p_name)) {
  711. Ref<AnimationNodeStateMachine> anodesm = states[p_name].node;
  712. if (anodesm.is_valid() && p_parents.find(anodesm.ptr()) != -1) {
  713. return false;
  714. }
  715. return true;
  716. }
  717. String name = p_name;
  718. Vector<String> path = name.split("/");
  719. if (path.size() < 2) {
  720. return false;
  721. }
  722. if (path[0] == "..") {
  723. if (prev_state_machine != nullptr) {
  724. return prev_state_machine->_can_connect(name.replace_first("../", ""), p_parents);
  725. }
  726. } else if (states.has(path[0])) {
  727. Ref<AnimationNodeStateMachine> anodesm = states[path[0]].node;
  728. if (anodesm.is_valid()) {
  729. return anodesm->_can_connect(name.replace_first(path[0] + "/", ""), p_parents);
  730. }
  731. }
  732. return false;
  733. }
  734. StringName AnimationNodeStateMachine::_get_shortest_path(const StringName &p_path) const {
  735. // If p_path is something like StateMachine/../StateMachine2/State1,
  736. // the result will be StateMachine2/State1. This avoid duplicate
  737. // transitions when using add_transition. eg, this two calls is the same:
  738. //
  739. // add_transition("State1", "StateMachine/../State2", tr)
  740. // add_transition("State1", "State2", tr)
  741. //
  742. // but the second call must be invalid because the transition already exists
  743. Vector<String> path = String(p_path).split("/");
  744. Vector<String> new_path;
  745. for (int i = 0; i < path.size(); i++) {
  746. if (i > 0 && path[i] == ".." && new_path[i - 1] != "..") {
  747. new_path.remove_at(i - 1);
  748. } else {
  749. new_path.push_back(path[i]);
  750. }
  751. }
  752. String result;
  753. for (int i = 0; i < new_path.size(); i++) {
  754. result += new_path[i] + "/";
  755. }
  756. result.remove_at(result.length() - 1);
  757. return result;
  758. }
  759. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  760. if (updating_transitions) {
  761. return;
  762. }
  763. StringName from = _get_shortest_path(p_from);
  764. StringName to = _get_shortest_path(p_to);
  765. Vector<String> path_from = String(from).split("/");
  766. Vector<String> path_to = String(to).split("/");
  767. ERR_FAIL_COND(from == end_node || to == start_node);
  768. ERR_FAIL_COND(from == to);
  769. ERR_FAIL_COND(!_can_connect(from));
  770. ERR_FAIL_COND(!_can_connect(to));
  771. ERR_FAIL_COND(p_transition.is_null());
  772. for (int i = 0; i < transitions.size(); i++) {
  773. ERR_FAIL_COND(transitions[i].from == from && transitions[i].to == to);
  774. }
  775. if (path_from.size() > 1 || path_to.size() > 1) {
  776. ERR_FAIL_COND(path_from[0] == path_to[0]);
  777. }
  778. updating_transitions = true;
  779. StringName local_from = String(from).get_slicec('/', 0);
  780. StringName local_to = String(to).get_slicec('/', 0);
  781. local_from = local_from == ".." ? "Start" : local_from;
  782. local_to = local_to == ".." ? "End" : local_to;
  783. Transition tr;
  784. tr.from = from;
  785. tr.to = to;
  786. tr.local_from = local_from;
  787. tr.local_to = local_to;
  788. tr.transition = p_transition;
  789. tr.transition->connect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), varray(), CONNECT_REFERENCE_COUNTED);
  790. transitions.push_back(tr);
  791. // do recursive
  792. if (path_from.size() > 1) {
  793. StringName local_path = String(from).replace_first(path_from[0] + "/", "");
  794. if (path_from[0] == "..") {
  795. prev_state_machine->add_transition(local_path, String(state_machine_name) + "/" + to, p_transition);
  796. } else {
  797. ((Ref<AnimationNodeStateMachine>)states[path_from[0]].node)->add_transition(local_path, "../" + to, p_transition);
  798. }
  799. }
  800. if (path_to.size() > 1) {
  801. StringName local_path = String(to).replace_first(path_to[0] + "/", "");
  802. if (path_to[0] == "..") {
  803. prev_state_machine->add_transition(String(state_machine_name) + "/" + from, local_path, p_transition);
  804. } else {
  805. ((Ref<AnimationNodeStateMachine>)states[path_to[0]].node)->add_transition("../" + from, local_path, p_transition);
  806. }
  807. }
  808. updating_transitions = false;
  809. }
  810. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  811. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  812. return transitions[p_transition].transition;
  813. }
  814. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  815. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  816. return transitions[p_transition].from;
  817. }
  818. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  819. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  820. return transitions[p_transition].to;
  821. }
  822. int AnimationNodeStateMachine::get_transition_count() const {
  823. return transitions.size();
  824. }
  825. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  826. StringName from = _get_shortest_path(p_from);
  827. StringName to = _get_shortest_path(p_to);
  828. for (int i = 0; i < transitions.size(); i++) {
  829. if (transitions[i].from == from && transitions[i].to == to) {
  830. remove_transition_by_index(i);
  831. return;
  832. }
  833. }
  834. }
  835. void AnimationNodeStateMachine::remove_transition_by_index(const int p_transition) {
  836. ERR_FAIL_INDEX(p_transition, transitions.size());
  837. Transition tr = transitions[p_transition];
  838. transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  839. transitions.remove_at(p_transition);
  840. Vector<String> path_from = String(tr.from).split("/");
  841. Vector<String> path_to = String(tr.to).split("/");
  842. List<Vector<String>> paths;
  843. paths.push_back(path_from);
  844. paths.push_back(path_to);
  845. for (List<Vector<String>>::Element *E = paths.front(); E; E = E->next()) {
  846. if (E->get()[0].size() > 1) {
  847. if (E->get()[0] == "..") {
  848. prev_state_machine->_remove_transition(tr.transition);
  849. } else if (states.has(E->get()[0])) {
  850. Ref<AnimationNodeStateMachine> anodesm = states[E->get()[0]].node;
  851. if (anodesm.is_valid()) {
  852. anodesm->_remove_transition(tr.transition);
  853. }
  854. }
  855. }
  856. }
  857. }
  858. void AnimationNodeStateMachine::_remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition) {
  859. for (int i = 0; i < transitions.size(); i++) {
  860. if (transitions[i].transition == p_transition) {
  861. remove_transition_by_index(i);
  862. return;
  863. }
  864. }
  865. }
  866. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  867. graph_offset = p_offset;
  868. }
  869. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  870. return graph_offset;
  871. }
  872. double AnimationNodeStateMachine::process(double p_time, bool p_seek, bool p_seek_root) {
  873. Ref<AnimationNodeStateMachinePlayback> playback = get_parameter(this->playback);
  874. ERR_FAIL_COND_V(playback.is_null(), 0.0);
  875. return playback->process(this, p_time, p_seek, p_seek_root);
  876. }
  877. String AnimationNodeStateMachine::get_caption() const {
  878. return "StateMachine";
  879. }
  880. bool AnimationNodeStateMachine::has_local_transition(const StringName &p_from, const StringName &p_to) const {
  881. StringName from = _get_shortest_path(p_from);
  882. StringName to = _get_shortest_path(p_to);
  883. for (int i = 0; i < transitions.size(); i++) {
  884. if (transitions[i].local_from == from && transitions[i].local_to == to) {
  885. return true;
  886. }
  887. }
  888. return false;
  889. }
  890. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
  891. return get_node(p_name);
  892. }
  893. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  894. String name = p_name;
  895. if (name.begins_with("states/")) {
  896. String node_name = name.get_slicec('/', 1);
  897. String what = name.get_slicec('/', 2);
  898. if (what == "node") {
  899. Ref<AnimationNode> anode = p_value;
  900. if (anode.is_valid()) {
  901. add_node(node_name, p_value);
  902. }
  903. return true;
  904. }
  905. if (what == "position") {
  906. if (states.has(node_name)) {
  907. states[node_name].position = p_value;
  908. }
  909. return true;
  910. }
  911. } else if (name == "transitions") {
  912. Array trans = p_value;
  913. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  914. for (int i = 0; i < trans.size(); i += 3) {
  915. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  916. }
  917. return true;
  918. } else if (name == "graph_offset") {
  919. set_graph_offset(p_value);
  920. return true;
  921. }
  922. return false;
  923. }
  924. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  925. String name = p_name;
  926. if (name.begins_with("states/")) {
  927. String node_name = name.get_slicec('/', 1);
  928. String what = name.get_slicec('/', 2);
  929. if (what == "node") {
  930. if (states.has(node_name) && can_edit_node(node_name)) {
  931. r_ret = states[node_name].node;
  932. return true;
  933. }
  934. }
  935. if (what == "position") {
  936. if (states.has(node_name)) {
  937. r_ret = states[node_name].position;
  938. return true;
  939. }
  940. }
  941. } else if (name == "transitions") {
  942. Array trans;
  943. for (int i = 0; i < transitions.size(); i++) {
  944. String from = transitions[i].from;
  945. String to = transitions[i].to;
  946. if (from.get_slicec('/', 0) == ".." || to.get_slicec('/', 0) == "..") {
  947. continue;
  948. }
  949. trans.push_back(from);
  950. trans.push_back(to);
  951. trans.push_back(transitions[i].transition);
  952. }
  953. r_ret = trans;
  954. return true;
  955. } else if (name == "graph_offset") {
  956. r_ret = get_graph_offset();
  957. return true;
  958. }
  959. return false;
  960. }
  961. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  962. List<StringName> names;
  963. for (const KeyValue<StringName, State> &E : states) {
  964. names.push_back(E.key);
  965. }
  966. names.sort_custom<StringName::AlphCompare>();
  967. for (const StringName &name : names) {
  968. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  969. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  970. }
  971. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  972. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  973. }
  974. void AnimationNodeStateMachine::reset_state() {
  975. states.clear();
  976. transitions.clear();
  977. playback = "playback";
  978. start_node = "Start";
  979. end_node = "End";
  980. graph_offset = Vector2();
  981. Ref<AnimationNodeStartState> s;
  982. s.instantiate();
  983. State start;
  984. start.node = s;
  985. start.position = Vector2(200, 100);
  986. states[start_node] = start;
  987. Ref<AnimationNodeEndState> e;
  988. e.instantiate();
  989. State end;
  990. end.node = e;
  991. end.position = Vector2(900, 100);
  992. states[end_node] = end;
  993. emit_changed();
  994. emit_signal(SNAME("tree_changed"));
  995. }
  996. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  997. ERR_FAIL_COND(!states.has(p_name));
  998. states[p_name].position = p_position;
  999. }
  1000. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  1001. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  1002. return states[p_name].position;
  1003. }
  1004. void AnimationNodeStateMachine::_tree_changed() {
  1005. emit_signal(SNAME("tree_changed"));
  1006. }
  1007. void AnimationNodeStateMachine::_bind_methods() {
  1008. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  1009. ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
  1010. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  1011. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  1012. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  1013. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  1014. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  1015. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  1016. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  1017. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  1018. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  1019. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  1020. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  1021. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  1022. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  1023. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  1024. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  1025. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
  1026. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  1027. }
  1028. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  1029. Ref<AnimationNodeStartState> s;
  1030. s.instantiate();
  1031. State start;
  1032. start.node = s;
  1033. start.position = Vector2(200, 100);
  1034. states[start_node] = start;
  1035. Ref<AnimationNodeEndState> e;
  1036. e.instantiate();
  1037. State end;
  1038. end.node = e;
  1039. end.position = Vector2(900, 100);
  1040. states[end_node] = end;
  1041. }