animation_node_state_machine.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "animation_node_state_machine.h"
  31. #include "scene/main/window.h"
  32. /////////////////////////////////////////////////
  33. void AnimationNodeStateMachineTransition::set_switch_mode(SwitchMode p_mode) {
  34. switch_mode = p_mode;
  35. }
  36. AnimationNodeStateMachineTransition::SwitchMode AnimationNodeStateMachineTransition::get_switch_mode() const {
  37. return switch_mode;
  38. }
  39. void AnimationNodeStateMachineTransition::set_advance_mode(AdvanceMode p_mode) {
  40. advance_mode = p_mode;
  41. }
  42. AnimationNodeStateMachineTransition::AdvanceMode AnimationNodeStateMachineTransition::get_advance_mode() const {
  43. return advance_mode;
  44. }
  45. void AnimationNodeStateMachineTransition::set_advance_condition(const StringName &p_condition) {
  46. String cs = p_condition;
  47. ERR_FAIL_COND(cs.contains("/") || cs.contains(":"));
  48. advance_condition = p_condition;
  49. if (!cs.is_empty()) {
  50. advance_condition_name = "conditions/" + cs;
  51. } else {
  52. advance_condition_name = StringName();
  53. }
  54. emit_signal(SNAME("advance_condition_changed"));
  55. }
  56. StringName AnimationNodeStateMachineTransition::get_advance_condition() const {
  57. return advance_condition;
  58. }
  59. StringName AnimationNodeStateMachineTransition::get_advance_condition_name() const {
  60. return advance_condition_name;
  61. }
  62. void AnimationNodeStateMachineTransition::set_advance_expression(const String &p_expression) {
  63. advance_expression = p_expression;
  64. String advance_expression_stripped = advance_expression.strip_edges();
  65. if (advance_expression_stripped == String()) {
  66. expression.unref();
  67. return;
  68. }
  69. if (expression.is_null()) {
  70. expression.instantiate();
  71. }
  72. expression->parse(advance_expression_stripped);
  73. }
  74. String AnimationNodeStateMachineTransition::get_advance_expression() const {
  75. return advance_expression;
  76. }
  77. void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) {
  78. ERR_FAIL_COND(p_xfade < 0);
  79. xfade_time = p_xfade;
  80. emit_changed();
  81. }
  82. float AnimationNodeStateMachineTransition::get_xfade_time() const {
  83. return xfade_time;
  84. }
  85. void AnimationNodeStateMachineTransition::set_xfade_curve(const Ref<Curve> &p_curve) {
  86. xfade_curve = p_curve;
  87. }
  88. Ref<Curve> AnimationNodeStateMachineTransition::get_xfade_curve() const {
  89. return xfade_curve;
  90. }
  91. void AnimationNodeStateMachineTransition::set_reset(bool p_reset) {
  92. reset = p_reset;
  93. emit_changed();
  94. }
  95. bool AnimationNodeStateMachineTransition::is_reset() const {
  96. return reset;
  97. }
  98. void AnimationNodeStateMachineTransition::set_priority(int p_priority) {
  99. priority = p_priority;
  100. emit_changed();
  101. }
  102. int AnimationNodeStateMachineTransition::get_priority() const {
  103. return priority;
  104. }
  105. void AnimationNodeStateMachineTransition::_bind_methods() {
  106. ClassDB::bind_method(D_METHOD("set_switch_mode", "mode"), &AnimationNodeStateMachineTransition::set_switch_mode);
  107. ClassDB::bind_method(D_METHOD("get_switch_mode"), &AnimationNodeStateMachineTransition::get_switch_mode);
  108. ClassDB::bind_method(D_METHOD("set_advance_mode", "mode"), &AnimationNodeStateMachineTransition::set_advance_mode);
  109. ClassDB::bind_method(D_METHOD("get_advance_mode"), &AnimationNodeStateMachineTransition::get_advance_mode);
  110. ClassDB::bind_method(D_METHOD("set_advance_condition", "name"), &AnimationNodeStateMachineTransition::set_advance_condition);
  111. ClassDB::bind_method(D_METHOD("get_advance_condition"), &AnimationNodeStateMachineTransition::get_advance_condition);
  112. ClassDB::bind_method(D_METHOD("set_xfade_time", "secs"), &AnimationNodeStateMachineTransition::set_xfade_time);
  113. ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeStateMachineTransition::get_xfade_time);
  114. ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeStateMachineTransition::set_xfade_curve);
  115. ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeStateMachineTransition::get_xfade_curve);
  116. ClassDB::bind_method(D_METHOD("set_reset", "reset"), &AnimationNodeStateMachineTransition::set_reset);
  117. ClassDB::bind_method(D_METHOD("is_reset"), &AnimationNodeStateMachineTransition::is_reset);
  118. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority);
  119. ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority);
  120. ClassDB::bind_method(D_METHOD("set_advance_expression", "text"), &AnimationNodeStateMachineTransition::set_advance_expression);
  121. ClassDB::bind_method(D_METHOD("get_advance_expression"), &AnimationNodeStateMachineTransition::get_advance_expression);
  122. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");
  123. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");
  124. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset"), "set_reset", "is_reset");
  125. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,32,1"), "set_priority", "get_priority");
  126. ADD_GROUP("Switch", "");
  127. ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,At End"), "set_switch_mode", "get_switch_mode");
  128. ADD_GROUP("Advance", "advance_");
  129. ADD_PROPERTY(PropertyInfo(Variant::INT, "advance_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled,Auto"), "set_advance_mode", "get_advance_mode");
  130. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "advance_condition"), "set_advance_condition", "get_advance_condition");
  131. ADD_PROPERTY(PropertyInfo(Variant::STRING, "advance_expression", PROPERTY_HINT_EXPRESSION, ""), "set_advance_expression", "get_advance_expression");
  132. BIND_ENUM_CONSTANT(SWITCH_MODE_IMMEDIATE);
  133. BIND_ENUM_CONSTANT(SWITCH_MODE_SYNC);
  134. BIND_ENUM_CONSTANT(SWITCH_MODE_AT_END);
  135. BIND_ENUM_CONSTANT(ADVANCE_MODE_DISABLED);
  136. BIND_ENUM_CONSTANT(ADVANCE_MODE_ENABLED);
  137. BIND_ENUM_CONSTANT(ADVANCE_MODE_AUTO);
  138. ADD_SIGNAL(MethodInfo("advance_condition_changed"));
  139. }
  140. AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() {
  141. }
  142. ////////////////////////////////////////////////////////
  143. void AnimationNodeStateMachinePlayback::travel(const StringName &p_state, bool p_reset_on_teleport) {
  144. travel_request = p_state;
  145. reset_request_on_teleport = p_reset_on_teleport;
  146. stop_request = false;
  147. }
  148. void AnimationNodeStateMachinePlayback::start(const StringName &p_state, bool p_reset) {
  149. travel_request = StringName();
  150. reset_request = p_reset;
  151. _start(p_state);
  152. }
  153. void AnimationNodeStateMachinePlayback::_start(const StringName &p_state) {
  154. start_request = p_state;
  155. stop_request = false;
  156. }
  157. void AnimationNodeStateMachinePlayback::next() {
  158. next_request = true;
  159. }
  160. void AnimationNodeStateMachinePlayback::stop() {
  161. stop_request = true;
  162. }
  163. bool AnimationNodeStateMachinePlayback::is_playing() const {
  164. return playing;
  165. }
  166. StringName AnimationNodeStateMachinePlayback::get_current_node() const {
  167. return current;
  168. }
  169. StringName AnimationNodeStateMachinePlayback::get_fading_from_node() const {
  170. return fading_from;
  171. }
  172. Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const {
  173. return path;
  174. }
  175. float AnimationNodeStateMachinePlayback::get_current_play_pos() const {
  176. return pos_current;
  177. }
  178. float AnimationNodeStateMachinePlayback::get_current_length() const {
  179. return len_current;
  180. }
  181. float AnimationNodeStateMachinePlayback::get_fade_from_play_pos() const {
  182. return pos_fade_from;
  183. }
  184. float AnimationNodeStateMachinePlayback::get_fade_from_length() const {
  185. return len_fade_from;
  186. }
  187. float AnimationNodeStateMachinePlayback::get_fading_time() const {
  188. return fading_time;
  189. }
  190. float AnimationNodeStateMachinePlayback::get_fading_pos() const {
  191. return fading_pos;
  192. }
  193. bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel) {
  194. ERR_FAIL_COND_V(!playing, false);
  195. ERR_FAIL_COND_V(!p_state_machine->states.has(p_travel), false);
  196. ERR_FAIL_COND_V(!p_state_machine->states.has(current), false);
  197. path.clear(); //a new one will be needed
  198. if (current == p_travel) {
  199. return false; // Will teleport oneself (restart).
  200. }
  201. Vector2 current_pos = p_state_machine->states[current].position;
  202. Vector2 target_pos = p_state_machine->states[p_travel].position;
  203. HashMap<StringName, AStarCost> cost_map;
  204. List<int> open_list;
  205. //build open list
  206. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  207. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  208. continue;
  209. }
  210. if (p_state_machine->transitions[i].local_from == current) {
  211. open_list.push_back(i);
  212. float cost = p_state_machine->states[p_state_machine->transitions[i].local_to].position.distance_to(current_pos);
  213. cost *= p_state_machine->transitions[i].transition->get_priority();
  214. AStarCost ap;
  215. ap.prev = current;
  216. ap.distance = cost;
  217. cost_map[p_state_machine->transitions[i].local_to] = ap;
  218. if (p_state_machine->transitions[i].local_to == p_travel) { //prematurely found it! :D
  219. path.push_back(p_travel);
  220. return true;
  221. }
  222. }
  223. }
  224. //begin astar
  225. bool found_route = false;
  226. while (!found_route) {
  227. if (open_list.size() == 0) {
  228. return false; //no path found
  229. }
  230. //find the last cost transition
  231. List<int>::Element *least_cost_transition = nullptr;
  232. float least_cost = 1e20;
  233. for (List<int>::Element *E = open_list.front(); E; E = E->next()) {
  234. float cost = cost_map[p_state_machine->transitions[E->get()].local_to].distance;
  235. cost += p_state_machine->states[p_state_machine->transitions[E->get()].local_to].position.distance_to(target_pos);
  236. if (cost < least_cost) {
  237. least_cost_transition = E;
  238. least_cost = cost;
  239. }
  240. }
  241. StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].local_from;
  242. StringName transition = p_state_machine->transitions[least_cost_transition->get()].local_to;
  243. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  244. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  245. continue;
  246. }
  247. if (p_state_machine->transitions[i].local_from != transition || p_state_machine->transitions[i].local_to == transition_prev) {
  248. continue; //not interested on those
  249. }
  250. 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);
  251. distance *= p_state_machine->transitions[i].transition->get_priority();
  252. distance += cost_map[p_state_machine->transitions[i].local_from].distance;
  253. if (cost_map.has(p_state_machine->transitions[i].local_to)) {
  254. //oh this was visited already, can we win the cost?
  255. if (distance < cost_map[p_state_machine->transitions[i].local_to].distance) {
  256. cost_map[p_state_machine->transitions[i].local_to].distance = distance;
  257. cost_map[p_state_machine->transitions[i].local_to].prev = p_state_machine->transitions[i].local_from;
  258. }
  259. } else {
  260. //add to open list
  261. AStarCost ac;
  262. ac.prev = p_state_machine->transitions[i].local_from;
  263. ac.distance = distance;
  264. cost_map[p_state_machine->transitions[i].local_to] = ac;
  265. open_list.push_back(i);
  266. if (p_state_machine->transitions[i].local_to == p_travel) {
  267. found_route = true;
  268. break;
  269. }
  270. }
  271. }
  272. if (found_route) {
  273. break;
  274. }
  275. open_list.erase(least_cost_transition);
  276. }
  277. //make path
  278. StringName at = p_travel;
  279. while (at != current) {
  280. path.push_back(at);
  281. at = cost_map[at].prev;
  282. }
  283. path.reverse();
  284. return true;
  285. }
  286. double AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, double p_time, bool p_seek, bool p_is_external_seeking) {
  287. double rem = _process(p_state_machine, p_time, p_seek, p_is_external_seeking);
  288. start_request = StringName();
  289. next_request = false;
  290. stop_request = false;
  291. reset_request_on_teleport = false;
  292. return rem;
  293. }
  294. double AnimationNodeStateMachinePlayback::_process(AnimationNodeStateMachine *p_state_machine, double p_time, bool p_seek, bool p_is_external_seeking) {
  295. if (p_time == -1) {
  296. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->states[current].node;
  297. if (anodesm.is_valid()) {
  298. p_state_machine->blend_node(current, p_state_machine->states[current].node, -1, p_seek, p_is_external_seeking, 0, AnimationNode::FILTER_IGNORE, true);
  299. }
  300. playing = false;
  301. return 0;
  302. }
  303. //if not playing and it can restart, then restart
  304. if (!playing && start_request == StringName()) {
  305. if (!stop_request && p_state_machine->start_node) {
  306. _start(p_state_machine->start_node);
  307. } else {
  308. return 0;
  309. }
  310. }
  311. if (playing && stop_request) {
  312. playing = false;
  313. return 0;
  314. }
  315. bool play_start = false;
  316. if (start_request != StringName()) {
  317. // teleport to start
  318. if (p_state_machine->states.has(start_request)) {
  319. path.clear();
  320. current = start_request;
  321. playing = true;
  322. play_start = true;
  323. } else {
  324. StringName node = start_request;
  325. ERR_FAIL_V_MSG(0, "No such node: '" + node + "'");
  326. }
  327. } else if (travel_request != StringName()) {
  328. if (!playing) {
  329. if (!stop_request && p_state_machine->start_node) {
  330. // can restart, just postpone traveling
  331. path.clear();
  332. current = p_state_machine->start_node;
  333. playing = true;
  334. play_start = true;
  335. } else {
  336. // stopped, invalid state
  337. String node_name = travel_request;
  338. travel_request = StringName();
  339. 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?");
  340. }
  341. } else {
  342. if (!_travel(p_state_machine, travel_request)) {
  343. // can't travel, then teleport
  344. if (p_state_machine->states.has(travel_request)) {
  345. path.clear();
  346. if (current != travel_request || reset_request_on_teleport) {
  347. current = travel_request;
  348. play_start = true;
  349. reset_request = reset_request_on_teleport;
  350. }
  351. } else {
  352. StringName node = travel_request;
  353. travel_request = StringName();
  354. ERR_FAIL_V_MSG(0, "No such node: '" + node + "'");
  355. }
  356. }
  357. travel_request = StringName();
  358. }
  359. }
  360. bool do_start = (p_seek && p_time == 0) || play_start || current == StringName();
  361. if (do_start) {
  362. if (p_state_machine->start_node != StringName() && p_seek && p_time == 0 && current == StringName()) {
  363. current = p_state_machine->start_node;
  364. }
  365. if (reset_request) {
  366. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, p_is_external_seeking, 1.0, AnimationNode::FILTER_IGNORE, true);
  367. pos_current = 0;
  368. reset_request = false;
  369. }
  370. }
  371. if (!p_state_machine->states.has(current)) {
  372. playing = false; //current does not exist
  373. current = StringName();
  374. return 0;
  375. }
  376. float fade_blend = 1.0;
  377. if (fading_from != StringName()) {
  378. if (!p_state_machine->states.has(fading_from)) {
  379. fading_from = StringName();
  380. } else {
  381. if (!p_seek) {
  382. fading_pos += p_time;
  383. }
  384. fade_blend = MIN(1.0, fading_pos / fading_time);
  385. }
  386. }
  387. if (current_curve.is_valid()) {
  388. fade_blend = current_curve->sample(fade_blend);
  389. }
  390. double rem = do_start ? len_current : p_state_machine->blend_node(current, p_state_machine->states[current].node, p_time, p_seek, p_is_external_seeking, Math::is_zero_approx(fade_blend) ? CMP_EPSILON : fade_blend, AnimationNode::FILTER_IGNORE, true); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
  391. if (fading_from != StringName()) {
  392. double fade_blend_inv = 1.0 - fade_blend;
  393. float fading_from_rem = 0.0;
  394. fading_from_rem = p_state_machine->blend_node(fading_from, p_state_machine->states[fading_from].node, p_time, p_seek, p_is_external_seeking, Math::is_zero_approx(fade_blend_inv) ? CMP_EPSILON : fade_blend_inv, AnimationNode::FILTER_IGNORE, true); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
  395. //guess playback position
  396. if (fading_from_rem > len_fade_from) { // weird but ok
  397. len_fade_from = fading_from_rem;
  398. }
  399. { //advance and loop check
  400. float next_pos = len_fade_from - fading_from_rem;
  401. pos_fade_from = next_pos; //looped
  402. }
  403. if (fade_blend >= 1.0) {
  404. fading_from = StringName();
  405. }
  406. }
  407. //guess playback position
  408. if (rem > len_current) { // weird but ok
  409. len_current = rem;
  410. }
  411. { //advance and loop check
  412. double next_pos = len_current - rem;
  413. end_loop = next_pos < pos_current;
  414. pos_current = next_pos; //looped
  415. }
  416. //find next
  417. StringName next;
  418. double next_xfade = 0.0;
  419. AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE;
  420. if (path.size()) {
  421. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  422. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  423. continue;
  424. }
  425. if (p_state_machine->transitions[i].local_from == current && p_state_machine->transitions[i].local_to == path[0]) {
  426. next_xfade = p_state_machine->transitions[i].transition->get_xfade_time();
  427. current_curve = p_state_machine->transitions[i].transition->get_xfade_curve();
  428. switch_mode = p_state_machine->transitions[i].transition->get_switch_mode();
  429. reset_request = p_state_machine->transitions[i].transition->is_reset();
  430. next = path[0];
  431. }
  432. }
  433. } else {
  434. float priority_best = 1e20;
  435. int auto_advance_to = -1;
  436. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  437. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  438. continue;
  439. }
  440. // handles end_node: when end_node is reached in a sub state machine, find and activate the current_transition
  441. if (force_auto_advance) {
  442. if (p_state_machine->transitions[i].from == current_transition.from && p_state_machine->transitions[i].to == current_transition.to) {
  443. auto_advance_to = i;
  444. force_auto_advance = false;
  445. break;
  446. }
  447. }
  448. // 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
  449. if (p_state_machine->start_node == current && p_state_machine->transitions[i].local_from == current) {
  450. if (p_state_machine->prev_state_machine != nullptr) {
  451. Ref<AnimationNodeStateMachinePlayback> prev_playback = p_state_machine->prev_state_machine->get_parameter(p_state_machine->playback);
  452. if (prev_playback.is_valid()) {
  453. StringName prev_local_to = String(prev_playback->current_transition.next).replace_first(String(p_state_machine->state_machine_name) + "/", "");
  454. if (p_state_machine->transitions[i].to == prev_local_to) {
  455. auto_advance_to = i;
  456. prev_playback->current_transition.next = StringName();
  457. break;
  458. }
  459. }
  460. }
  461. }
  462. if (p_state_machine->transitions[i].from == current && _check_advance_condition(p_state_machine, p_state_machine->transitions[i].transition)) {
  463. if (p_state_machine->transitions[i].transition->get_priority() <= priority_best) {
  464. priority_best = p_state_machine->transitions[i].transition->get_priority();
  465. auto_advance_to = i;
  466. }
  467. }
  468. }
  469. if (auto_advance_to != -1) {
  470. next = p_state_machine->transitions[auto_advance_to].local_to;
  471. Transition tr;
  472. tr.from = String(p_state_machine->state_machine_name) + "/" + String(p_state_machine->transitions[auto_advance_to].from);
  473. tr.to = String(p_state_machine->transitions[auto_advance_to].to).replace_first("../", "");
  474. tr.next = p_state_machine->transitions[auto_advance_to].to;
  475. current_transition = tr;
  476. current_curve = p_state_machine->transitions[auto_advance_to].transition->get_xfade_curve();
  477. next_xfade = p_state_machine->transitions[auto_advance_to].transition->get_xfade_time();
  478. switch_mode = p_state_machine->transitions[auto_advance_to].transition->get_switch_mode();
  479. reset_request = p_state_machine->transitions[auto_advance_to].transition->is_reset();
  480. }
  481. }
  482. if (next == p_state_machine->end_node) {
  483. AnimationNodeStateMachine *prev_state_machine = p_state_machine->prev_state_machine;
  484. if (prev_state_machine != nullptr) {
  485. Ref<AnimationNodeStateMachinePlayback> prev_playback = prev_state_machine->get_parameter(p_state_machine->playback);
  486. if (prev_playback.is_valid()) {
  487. if (next_xfade) {
  488. prev_playback->current_transition = current_transition;
  489. prev_playback->force_auto_advance = true;
  490. return rem;
  491. }
  492. float priority_best = 1e20;
  493. int auto_advance_to = -1;
  494. for (int i = 0; i < prev_state_machine->transitions.size(); i++) {
  495. if (prev_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  496. continue;
  497. }
  498. if (current_transition.next == prev_state_machine->end_node && _check_advance_condition(prev_state_machine, prev_state_machine->transitions[i].transition)) {
  499. if (prev_state_machine->transitions[i].transition->get_priority() <= priority_best) {
  500. priority_best = prev_state_machine->transitions[i].transition->get_priority();
  501. auto_advance_to = i;
  502. }
  503. }
  504. }
  505. if (auto_advance_to != -1) {
  506. if (prev_state_machine->transitions[auto_advance_to].transition->get_xfade_time()) {
  507. return rem;
  508. }
  509. }
  510. }
  511. }
  512. }
  513. //if next, see when to transition
  514. if (next != StringName()) {
  515. bool goto_next = false;
  516. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
  517. goto_next = next_xfade >= (len_current - pos_current) || end_loop;
  518. if (end_loop) {
  519. next_xfade = 0;
  520. }
  521. } else {
  522. goto_next = fading_from == StringName();
  523. }
  524. if (next_request || goto_next) { //end_loop should be used because fade time may be too small or zero and animation may have looped
  525. if (next_xfade) {
  526. //time to fade, baby
  527. fading_from = current;
  528. fading_time = next_xfade;
  529. fading_pos = 0;
  530. } else {
  531. fading_from = StringName();
  532. fading_pos = 0;
  533. }
  534. if (path.size()) { //if it came from path, remove path
  535. path.remove_at(0);
  536. }
  537. { // if the current node is a state machine, update the "playing" variable to false by passing -1 in p_time
  538. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->states[current].node;
  539. if (anodesm.is_valid()) {
  540. p_state_machine->blend_node(current, p_state_machine->states[current].node, -1, p_seek, p_is_external_seeking, 0, AnimationNode::FILTER_IGNORE, true);
  541. }
  542. }
  543. current = next;
  544. pos_fade_from = pos_current;
  545. len_fade_from = len_current;
  546. if (reset_request) {
  547. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, p_is_external_seeking, CMP_EPSILON, AnimationNode::FILTER_IGNORE, true); // Process next node's first key in here.
  548. }
  549. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
  550. pos_current = MIN(pos_current, len_current);
  551. p_state_machine->blend_node(current, p_state_machine->states[current].node, pos_current, true, p_is_external_seeking, 0, AnimationNode::FILTER_IGNORE, true);
  552. } else {
  553. pos_current = 0;
  554. }
  555. rem = len_current; //so it does not show 0 on transition
  556. }
  557. }
  558. if (current != p_state_machine->end_node) {
  559. rem = 1; // the time remaining must always be 1 because there is no way to predict how long it takes for the entire state machine to complete
  560. } else {
  561. if (p_state_machine->prev_state_machine != nullptr) {
  562. Ref<AnimationNodeStateMachinePlayback> prev_playback = p_state_machine->prev_state_machine->get_parameter(p_state_machine->playback);
  563. if (prev_playback.is_valid()) {
  564. prev_playback->current_transition = current_transition;
  565. prev_playback->force_auto_advance = true;
  566. }
  567. }
  568. }
  569. return rem;
  570. }
  571. bool AnimationNodeStateMachinePlayback::_check_advance_condition(const Ref<AnimationNodeStateMachine> state_machine, const Ref<AnimationNodeStateMachineTransition> transition) const {
  572. if (transition->get_advance_mode() != AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO) {
  573. return false;
  574. }
  575. StringName advance_condition_name = transition->get_advance_condition_name();
  576. if (advance_condition_name != StringName() && !bool(state_machine->get_parameter(advance_condition_name))) {
  577. return false;
  578. }
  579. if (transition->expression.is_valid()) {
  580. AnimationTree *tree_base = state_machine->get_animation_tree();
  581. ERR_FAIL_COND_V(tree_base == nullptr, false);
  582. NodePath advance_expression_base_node_path = tree_base->get_advance_expression_base_node();
  583. Node *expression_base = tree_base->get_node_or_null(advance_expression_base_node_path);
  584. if (expression_base) {
  585. Ref<Expression> exp = transition->expression;
  586. bool ret = exp->execute(Array(), expression_base, false, Engine::get_singleton()->is_editor_hint()); // Avoids allowing the user to crash the system with an expression by only allowing const calls.
  587. if (exp->has_execute_failed() || !ret) {
  588. return false;
  589. }
  590. } else {
  591. WARN_PRINT_ONCE("Animation transition has a valid expression, but no expression base node was set on its AnimationTree.");
  592. }
  593. }
  594. return true;
  595. }
  596. void AnimationNodeStateMachinePlayback::_bind_methods() {
  597. ClassDB::bind_method(D_METHOD("travel", "to_node", "reset_on_teleport"), &AnimationNodeStateMachinePlayback::travel, DEFVAL(true));
  598. ClassDB::bind_method(D_METHOD("start", "node", "reset"), &AnimationNodeStateMachinePlayback::start, DEFVAL(true));
  599. ClassDB::bind_method(D_METHOD("next"), &AnimationNodeStateMachinePlayback::next);
  600. ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
  601. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
  602. ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
  603. ClassDB::bind_method(D_METHOD("get_current_play_position"), &AnimationNodeStateMachinePlayback::get_current_play_pos);
  604. ClassDB::bind_method(D_METHOD("get_current_length"), &AnimationNodeStateMachinePlayback::get_current_length);
  605. ClassDB::bind_method(D_METHOD("get_fading_from_node"), &AnimationNodeStateMachinePlayback::get_fading_from_node);
  606. ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path);
  607. }
  608. AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
  609. set_local_to_scene(true); //only one per instantiated scene
  610. }
  611. ///////////////////////////////////////////////////////
  612. void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
  613. r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE));
  614. List<StringName> advance_conditions;
  615. for (int i = 0; i < transitions.size(); i++) {
  616. StringName ac = transitions[i].transition->get_advance_condition_name();
  617. if (ac != StringName() && advance_conditions.find(ac) == nullptr) {
  618. advance_conditions.push_back(ac);
  619. }
  620. }
  621. advance_conditions.sort_custom<StringName::AlphCompare>();
  622. for (const StringName &E : advance_conditions) {
  623. r_list->push_back(PropertyInfo(Variant::BOOL, E));
  624. }
  625. }
  626. Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
  627. if (p_parameter == playback) {
  628. Ref<AnimationNodeStateMachinePlayback> p;
  629. p.instantiate();
  630. return p;
  631. } else {
  632. return false; //advance condition
  633. }
  634. }
  635. void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  636. ERR_FAIL_COND(states.has(p_name));
  637. ERR_FAIL_COND(p_node.is_null());
  638. ERR_FAIL_COND(String(p_name).contains("/"));
  639. State state_new;
  640. state_new.node = p_node;
  641. state_new.position = p_position;
  642. states[p_name] = state_new;
  643. Ref<AnimationNodeStateMachine> anodesm = p_node;
  644. if (anodesm.is_valid()) {
  645. anodesm->state_machine_name = p_name;
  646. anodesm->prev_state_machine = this;
  647. }
  648. emit_changed();
  649. emit_signal(SNAME("tree_changed"));
  650. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  651. }
  652. void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
  653. ERR_FAIL_COND(states.has(p_name) == false);
  654. ERR_FAIL_COND(p_node.is_null());
  655. ERR_FAIL_COND(String(p_name).contains("/"));
  656. {
  657. Ref<AnimationNode> node = states[p_name].node;
  658. if (node.is_valid()) {
  659. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  660. }
  661. }
  662. states[p_name].node = p_node;
  663. emit_changed();
  664. emit_signal(SNAME("tree_changed"));
  665. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  666. }
  667. bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
  668. if (states.has(p_name)) {
  669. return !(states[p_name].node->is_class("AnimationNodeStartState") || states[p_name].node->is_class("AnimationNodeEndState"));
  670. }
  671. return true;
  672. }
  673. Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
  674. ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>());
  675. return states[p_name].node;
  676. }
  677. StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
  678. for (const KeyValue<StringName, State> &E : states) {
  679. if (E.value.node == p_node) {
  680. return E.key;
  681. }
  682. }
  683. ERR_FAIL_V(StringName());
  684. }
  685. void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
  686. Vector<StringName> nodes;
  687. for (const KeyValue<StringName, State> &E : states) {
  688. nodes.push_back(E.key);
  689. }
  690. nodes.sort_custom<StringName::AlphCompare>();
  691. for (int i = 0; i < nodes.size(); i++) {
  692. ChildNode cn;
  693. cn.name = nodes[i];
  694. cn.node = states[cn.name].node;
  695. r_child_nodes->push_back(cn);
  696. }
  697. }
  698. bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
  699. return states.has(p_name);
  700. }
  701. void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
  702. ERR_FAIL_COND(!states.has(p_name));
  703. if (!can_edit_node(p_name)) {
  704. return;
  705. }
  706. for (int i = 0; i < transitions.size(); i++) {
  707. if (transitions[i].local_from == p_name || transitions[i].local_to == p_name) {
  708. remove_transition_by_index(i);
  709. i--;
  710. }
  711. }
  712. {
  713. Ref<AnimationNode> node = states[p_name].node;
  714. ERR_FAIL_COND(node.is_null());
  715. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  716. }
  717. states.erase(p_name);
  718. emit_changed();
  719. emit_signal(SNAME("tree_changed"));
  720. }
  721. void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
  722. ERR_FAIL_COND(!states.has(p_name));
  723. ERR_FAIL_COND(states.has(p_new_name));
  724. ERR_FAIL_COND(!can_edit_node(p_name));
  725. states[p_new_name] = states[p_name];
  726. states.erase(p_name);
  727. Ref<AnimationNodeStateMachine> anodesm = states[p_new_name].node;
  728. if (anodesm.is_valid()) {
  729. anodesm->state_machine_name = p_new_name;
  730. }
  731. _rename_transitions(p_name, p_new_name);
  732. emit_changed();
  733. emit_signal(SNAME("tree_changed"));
  734. }
  735. void AnimationNodeStateMachine::_rename_transitions(const StringName &p_name, const StringName &p_new_name) {
  736. if (updating_transitions) {
  737. return;
  738. }
  739. updating_transitions = true;
  740. for (int i = 0; i < transitions.size(); i++) {
  741. if (transitions[i].from == p_name) {
  742. Vector<String> path = String(transitions[i].to).split("/");
  743. if (path.size() > 1) {
  744. if (path[0] == "..") {
  745. prev_state_machine->_rename_transitions(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  746. } else {
  747. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_to].node)->_rename_transitions("../" + p_name, "../" + p_new_name);
  748. }
  749. }
  750. if (transitions[i].local_from == p_name) {
  751. transitions.write[i].local_from = p_new_name;
  752. }
  753. transitions.write[i].from = p_new_name;
  754. }
  755. if (transitions[i].to == p_name) {
  756. Vector<String> path = String(transitions[i].from).split("/");
  757. if (path.size() > 1) {
  758. if (path[0] == "..") {
  759. prev_state_machine->_rename_transitions(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  760. } else {
  761. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_from].node)->_rename_transitions("../" + p_name, "../" + p_new_name);
  762. }
  763. }
  764. if (transitions[i].local_to == p_name) {
  765. transitions.write[i].local_to = p_new_name;
  766. }
  767. transitions.write[i].to = p_new_name;
  768. }
  769. }
  770. updating_transitions = false;
  771. }
  772. void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
  773. List<StringName> nodes;
  774. for (const KeyValue<StringName, State> &E : states) {
  775. nodes.push_back(E.key);
  776. }
  777. nodes.sort_custom<StringName::AlphCompare>();
  778. for (const StringName &E : nodes) {
  779. r_nodes->push_back(E);
  780. }
  781. }
  782. AnimationNodeStateMachine *AnimationNodeStateMachine::get_prev_state_machine() const {
  783. return prev_state_machine;
  784. }
  785. bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
  786. StringName from = _get_shortest_path(p_from);
  787. StringName to = _get_shortest_path(p_to);
  788. for (int i = 0; i < transitions.size(); i++) {
  789. if (transitions[i].from == from && transitions[i].to == to) {
  790. return true;
  791. }
  792. }
  793. return false;
  794. }
  795. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  796. StringName from = _get_shortest_path(p_from);
  797. StringName to = _get_shortest_path(p_to);
  798. for (int i = 0; i < transitions.size(); i++) {
  799. if (transitions[i].from == from && transitions[i].to == to) {
  800. return i;
  801. }
  802. }
  803. return -1;
  804. }
  805. bool AnimationNodeStateMachine::_can_connect(const StringName &p_name, Vector<AnimationNodeStateMachine *> p_parents) {
  806. if (p_parents.is_empty()) {
  807. AnimationNodeStateMachine *prev = this;
  808. while (prev != nullptr) {
  809. p_parents.push_back(prev);
  810. prev = prev->prev_state_machine;
  811. }
  812. }
  813. if (states.has(p_name)) {
  814. Ref<AnimationNodeStateMachine> anodesm = states[p_name].node;
  815. if (anodesm.is_valid() && p_parents.find(anodesm.ptr()) != -1) {
  816. return false;
  817. }
  818. return true;
  819. }
  820. String node_name = p_name;
  821. Vector<String> path = node_name.split("/");
  822. if (path.size() < 2) {
  823. return false;
  824. }
  825. if (path[0] == "..") {
  826. if (prev_state_machine != nullptr) {
  827. return prev_state_machine->_can_connect(node_name.replace_first("../", ""), p_parents);
  828. }
  829. } else if (states.has(path[0])) {
  830. Ref<AnimationNodeStateMachine> anodesm = states[path[0]].node;
  831. if (anodesm.is_valid()) {
  832. return anodesm->_can_connect(node_name.replace_first(path[0] + "/", ""), p_parents);
  833. }
  834. }
  835. return false;
  836. }
  837. StringName AnimationNodeStateMachine::_get_shortest_path(const StringName &p_path) const {
  838. // If p_path is something like StateMachine/../StateMachine2/State1,
  839. // the result will be StateMachine2/State1. This avoid duplicate
  840. // transitions when using add_transition. eg, this two calls is the same:
  841. //
  842. // add_transition("State1", "StateMachine/../State2", tr)
  843. // add_transition("State1", "State2", tr)
  844. //
  845. // but the second call must be invalid because the transition already exists
  846. Vector<String> path = String(p_path).split("/");
  847. Vector<String> new_path;
  848. for (int i = 0; i < path.size(); i++) {
  849. if (i > 0 && path[i] == ".." && new_path[i - 1] != "..") {
  850. new_path.remove_at(i - 1);
  851. } else {
  852. new_path.push_back(path[i]);
  853. }
  854. }
  855. String result;
  856. for (int i = 0; i < new_path.size(); i++) {
  857. result += new_path[i] + "/";
  858. }
  859. result.remove_at(result.length() - 1);
  860. return result;
  861. }
  862. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  863. if (updating_transitions) {
  864. return;
  865. }
  866. StringName from = _get_shortest_path(p_from);
  867. StringName to = _get_shortest_path(p_to);
  868. Vector<String> path_from = String(from).split("/");
  869. Vector<String> path_to = String(to).split("/");
  870. ERR_FAIL_COND(from == end_node || to == start_node);
  871. ERR_FAIL_COND(from == to);
  872. ERR_FAIL_COND(!_can_connect(from));
  873. ERR_FAIL_COND(!_can_connect(to));
  874. ERR_FAIL_COND(p_transition.is_null());
  875. for (int i = 0; i < transitions.size(); i++) {
  876. ERR_FAIL_COND(transitions[i].from == from && transitions[i].to == to);
  877. }
  878. if (path_from.size() > 1 || path_to.size() > 1) {
  879. ERR_FAIL_COND(path_from[0] == path_to[0]);
  880. }
  881. updating_transitions = true;
  882. StringName local_from = String(from).get_slicec('/', 0);
  883. StringName local_to = String(to).get_slicec('/', 0);
  884. local_from = local_from == ".." ? "Start" : local_from;
  885. local_to = local_to == ".." ? "End" : local_to;
  886. Transition tr;
  887. tr.from = from;
  888. tr.to = to;
  889. tr.local_from = local_from;
  890. tr.local_to = local_to;
  891. tr.transition = p_transition;
  892. tr.transition->connect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  893. transitions.push_back(tr);
  894. // do recursive
  895. if (path_from.size() > 1) {
  896. StringName local_path = String(from).replace_first(path_from[0] + "/", "");
  897. if (path_from[0] == "..") {
  898. prev_state_machine->add_transition(local_path, String(state_machine_name) + "/" + to, p_transition);
  899. } else {
  900. ((Ref<AnimationNodeStateMachine>)states[path_from[0]].node)->add_transition(local_path, "../" + to, p_transition);
  901. }
  902. }
  903. if (path_to.size() > 1) {
  904. StringName local_path = String(to).replace_first(path_to[0] + "/", "");
  905. if (path_to[0] == "..") {
  906. prev_state_machine->add_transition(String(state_machine_name) + "/" + from, local_path, p_transition);
  907. } else {
  908. ((Ref<AnimationNodeStateMachine>)states[path_to[0]].node)->add_transition("../" + from, local_path, p_transition);
  909. }
  910. }
  911. updating_transitions = false;
  912. }
  913. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  914. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  915. return transitions[p_transition].transition;
  916. }
  917. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  918. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  919. return transitions[p_transition].from;
  920. }
  921. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  922. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  923. return transitions[p_transition].to;
  924. }
  925. int AnimationNodeStateMachine::get_transition_count() const {
  926. return transitions.size();
  927. }
  928. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  929. StringName from = _get_shortest_path(p_from);
  930. StringName to = _get_shortest_path(p_to);
  931. for (int i = 0; i < transitions.size(); i++) {
  932. if (transitions[i].from == from && transitions[i].to == to) {
  933. remove_transition_by_index(i);
  934. return;
  935. }
  936. }
  937. }
  938. void AnimationNodeStateMachine::remove_transition_by_index(const int p_transition) {
  939. ERR_FAIL_INDEX(p_transition, transitions.size());
  940. Transition tr = transitions[p_transition];
  941. transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  942. transitions.remove_at(p_transition);
  943. Vector<String> path_from = String(tr.from).split("/");
  944. Vector<String> path_to = String(tr.to).split("/");
  945. List<Vector<String>> paths;
  946. paths.push_back(path_from);
  947. paths.push_back(path_to);
  948. for (List<Vector<String>>::Element *E = paths.front(); E; E = E->next()) {
  949. if (E->get()[0].size() > 1) {
  950. if (E->get()[0] == "..") {
  951. prev_state_machine->_remove_transition(tr.transition);
  952. } else if (states.has(E->get()[0])) {
  953. Ref<AnimationNodeStateMachine> anodesm = states[E->get()[0]].node;
  954. if (anodesm.is_valid()) {
  955. anodesm->_remove_transition(tr.transition);
  956. }
  957. }
  958. }
  959. }
  960. }
  961. void AnimationNodeStateMachine::_remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition) {
  962. for (int i = 0; i < transitions.size(); i++) {
  963. if (transitions[i].transition == p_transition) {
  964. remove_transition_by_index(i);
  965. return;
  966. }
  967. }
  968. }
  969. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  970. graph_offset = p_offset;
  971. }
  972. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  973. return graph_offset;
  974. }
  975. double AnimationNodeStateMachine::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  976. Ref<AnimationNodeStateMachinePlayback> playback_new = get_parameter(playback);
  977. ERR_FAIL_COND_V(playback_new.is_null(), 0.0);
  978. return playback_new->process(this, p_time, p_seek, p_is_external_seeking);
  979. }
  980. String AnimationNodeStateMachine::get_caption() const {
  981. return "StateMachine";
  982. }
  983. bool AnimationNodeStateMachine::has_local_transition(const StringName &p_from, const StringName &p_to) const {
  984. StringName from = _get_shortest_path(p_from);
  985. StringName to = _get_shortest_path(p_to);
  986. for (int i = 0; i < transitions.size(); i++) {
  987. if (transitions[i].local_from == from && transitions[i].local_to == to) {
  988. return true;
  989. }
  990. }
  991. return false;
  992. }
  993. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
  994. return get_node(p_name);
  995. }
  996. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  997. String prop_name = p_name;
  998. if (prop_name.begins_with("states/")) {
  999. String node_name = prop_name.get_slicec('/', 1);
  1000. String what = prop_name.get_slicec('/', 2);
  1001. if (what == "node") {
  1002. Ref<AnimationNode> anode = p_value;
  1003. if (anode.is_valid()) {
  1004. add_node(node_name, p_value);
  1005. }
  1006. return true;
  1007. }
  1008. if (what == "position") {
  1009. if (states.has(node_name)) {
  1010. states[node_name].position = p_value;
  1011. }
  1012. return true;
  1013. }
  1014. } else if (prop_name == "transitions") {
  1015. Array trans = p_value;
  1016. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  1017. for (int i = 0; i < trans.size(); i += 3) {
  1018. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  1019. }
  1020. return true;
  1021. } else if (prop_name == "graph_offset") {
  1022. set_graph_offset(p_value);
  1023. return true;
  1024. }
  1025. return false;
  1026. }
  1027. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  1028. String prop_name = p_name;
  1029. if (prop_name.begins_with("states/")) {
  1030. String node_name = prop_name.get_slicec('/', 1);
  1031. String what = prop_name.get_slicec('/', 2);
  1032. if (what == "node") {
  1033. if (states.has(node_name) && can_edit_node(node_name)) {
  1034. r_ret = states[node_name].node;
  1035. return true;
  1036. }
  1037. }
  1038. if (what == "position") {
  1039. if (states.has(node_name)) {
  1040. r_ret = states[node_name].position;
  1041. return true;
  1042. }
  1043. }
  1044. } else if (prop_name == "transitions") {
  1045. Array trans;
  1046. for (int i = 0; i < transitions.size(); i++) {
  1047. String from = transitions[i].from;
  1048. String to = transitions[i].to;
  1049. if (from.get_slicec('/', 0) == ".." || to.get_slicec('/', 0) == "..") {
  1050. continue;
  1051. }
  1052. trans.push_back(from);
  1053. trans.push_back(to);
  1054. trans.push_back(transitions[i].transition);
  1055. }
  1056. r_ret = trans;
  1057. return true;
  1058. } else if (prop_name == "graph_offset") {
  1059. r_ret = get_graph_offset();
  1060. return true;
  1061. }
  1062. return false;
  1063. }
  1064. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  1065. List<StringName> names;
  1066. for (const KeyValue<StringName, State> &E : states) {
  1067. names.push_back(E.key);
  1068. }
  1069. names.sort_custom<StringName::AlphCompare>();
  1070. for (const StringName &prop_name : names) {
  1071. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  1072. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1073. }
  1074. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1075. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1076. }
  1077. void AnimationNodeStateMachine::reset_state() {
  1078. states.clear();
  1079. transitions.clear();
  1080. playback = "playback";
  1081. start_node = "Start";
  1082. end_node = "End";
  1083. graph_offset = Vector2();
  1084. Ref<AnimationNodeStartState> s;
  1085. s.instantiate();
  1086. State start;
  1087. start.node = s;
  1088. start.position = Vector2(200, 100);
  1089. states[start_node] = start;
  1090. Ref<AnimationNodeEndState> e;
  1091. e.instantiate();
  1092. State end;
  1093. end.node = e;
  1094. end.position = Vector2(900, 100);
  1095. states[end_node] = end;
  1096. emit_changed();
  1097. emit_signal(SNAME("tree_changed"));
  1098. }
  1099. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  1100. ERR_FAIL_COND(!states.has(p_name));
  1101. states[p_name].position = p_position;
  1102. }
  1103. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  1104. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  1105. return states[p_name].position;
  1106. }
  1107. void AnimationNodeStateMachine::_tree_changed() {
  1108. emit_changed();
  1109. emit_signal(SNAME("tree_changed"));
  1110. }
  1111. void AnimationNodeStateMachine::_bind_methods() {
  1112. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  1113. ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
  1114. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  1115. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  1116. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  1117. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  1118. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  1119. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  1120. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  1121. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  1122. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  1123. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  1124. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  1125. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  1126. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  1127. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  1128. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  1129. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
  1130. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  1131. }
  1132. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  1133. Ref<AnimationNodeStartState> s;
  1134. s.instantiate();
  1135. State start;
  1136. start.node = s;
  1137. start.position = Vector2(200, 100);
  1138. states[start_node] = start;
  1139. Ref<AnimationNodeEndState> e;
  1140. e.instantiate();
  1141. State end;
  1142. end.node = e;
  1143. end.position = Vector2(900, 100);
  1144. states[end_node] = end;
  1145. }