animation_node_state_machine.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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_blend_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. bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel) {
  182. ERR_FAIL_COND_V(!playing, false);
  183. ERR_FAIL_COND_V(!p_state_machine->states.has(p_travel), false);
  184. ERR_FAIL_COND_V(!p_state_machine->states.has(current), false);
  185. path.clear(); //a new one will be needed
  186. if (current == p_travel) {
  187. return false; // Will teleport oneself (restart).
  188. }
  189. Vector2 current_pos = p_state_machine->states[current].position;
  190. Vector2 target_pos = p_state_machine->states[p_travel].position;
  191. HashMap<StringName, AStarCost> cost_map;
  192. List<int> open_list;
  193. //build open list
  194. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  195. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  196. continue;
  197. }
  198. if (p_state_machine->transitions[i].local_from == current) {
  199. open_list.push_back(i);
  200. float cost = p_state_machine->states[p_state_machine->transitions[i].local_to].position.distance_to(current_pos);
  201. cost *= p_state_machine->transitions[i].transition->get_priority();
  202. AStarCost ap;
  203. ap.prev = current;
  204. ap.distance = cost;
  205. cost_map[p_state_machine->transitions[i].local_to] = ap;
  206. if (p_state_machine->transitions[i].local_to == p_travel) { //prematurely found it! :D
  207. path.push_back(p_travel);
  208. return true;
  209. }
  210. }
  211. }
  212. //begin astar
  213. bool found_route = false;
  214. while (!found_route) {
  215. if (open_list.size() == 0) {
  216. return false; //no path found
  217. }
  218. //find the last cost transition
  219. List<int>::Element *least_cost_transition = nullptr;
  220. float least_cost = 1e20;
  221. for (List<int>::Element *E = open_list.front(); E; E = E->next()) {
  222. float cost = cost_map[p_state_machine->transitions[E->get()].local_to].distance;
  223. cost += p_state_machine->states[p_state_machine->transitions[E->get()].local_to].position.distance_to(target_pos);
  224. if (cost < least_cost) {
  225. least_cost_transition = E;
  226. least_cost = cost;
  227. }
  228. }
  229. StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].local_from;
  230. StringName transition = p_state_machine->transitions[least_cost_transition->get()].local_to;
  231. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  232. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  233. continue;
  234. }
  235. if (p_state_machine->transitions[i].local_from != transition || p_state_machine->transitions[i].local_to == transition_prev) {
  236. continue; //not interested on those
  237. }
  238. 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);
  239. distance *= p_state_machine->transitions[i].transition->get_priority();
  240. distance += cost_map[p_state_machine->transitions[i].local_from].distance;
  241. if (cost_map.has(p_state_machine->transitions[i].local_to)) {
  242. //oh this was visited already, can we win the cost?
  243. if (distance < cost_map[p_state_machine->transitions[i].local_to].distance) {
  244. cost_map[p_state_machine->transitions[i].local_to].distance = distance;
  245. cost_map[p_state_machine->transitions[i].local_to].prev = p_state_machine->transitions[i].local_from;
  246. }
  247. } else {
  248. //add to open list
  249. AStarCost ac;
  250. ac.prev = p_state_machine->transitions[i].local_from;
  251. ac.distance = distance;
  252. cost_map[p_state_machine->transitions[i].local_to] = ac;
  253. open_list.push_back(i);
  254. if (p_state_machine->transitions[i].local_to == p_travel) {
  255. found_route = true;
  256. break;
  257. }
  258. }
  259. }
  260. if (found_route) {
  261. break;
  262. }
  263. open_list.erase(least_cost_transition);
  264. }
  265. //make path
  266. StringName at = p_travel;
  267. while (at != current) {
  268. path.push_back(at);
  269. at = cost_map[at].prev;
  270. }
  271. path.reverse();
  272. return true;
  273. }
  274. double AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, double p_time, bool p_seek, bool p_is_external_seeking) {
  275. double rem = _process(p_state_machine, p_time, p_seek, p_is_external_seeking);
  276. start_request = StringName();
  277. next_request = false;
  278. stop_request = false;
  279. reset_request_on_teleport = false;
  280. return rem;
  281. }
  282. double AnimationNodeStateMachinePlayback::_process(AnimationNodeStateMachine *p_state_machine, double p_time, bool p_seek, bool p_is_external_seeking) {
  283. if (p_time == -1) {
  284. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->states[current].node;
  285. if (anodesm.is_valid()) {
  286. p_state_machine->blend_node(current, p_state_machine->states[current].node, -1, p_seek, p_is_external_seeking, 0, AnimationNode::FILTER_IGNORE, true);
  287. }
  288. playing = false;
  289. return 0;
  290. }
  291. //if not playing and it can restart, then restart
  292. if (!playing && start_request == StringName()) {
  293. if (!stop_request && p_state_machine->start_node) {
  294. _start(p_state_machine->start_node);
  295. } else {
  296. return 0;
  297. }
  298. }
  299. if (playing && stop_request) {
  300. playing = false;
  301. return 0;
  302. }
  303. bool play_start = false;
  304. if (start_request != StringName()) {
  305. // teleport to start
  306. if (p_state_machine->states.has(start_request)) {
  307. path.clear();
  308. current = start_request;
  309. playing = true;
  310. play_start = true;
  311. } else {
  312. StringName node = start_request;
  313. ERR_FAIL_V_MSG(0, "No such node: '" + node + "'");
  314. }
  315. } else if (travel_request != StringName()) {
  316. if (!playing) {
  317. if (!stop_request && p_state_machine->start_node) {
  318. // can restart, just postpone traveling
  319. path.clear();
  320. current = p_state_machine->start_node;
  321. playing = true;
  322. play_start = true;
  323. } else {
  324. // stopped, invalid state
  325. String node_name = travel_request;
  326. travel_request = StringName();
  327. 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?");
  328. }
  329. } else {
  330. if (!_travel(p_state_machine, travel_request)) {
  331. // can't travel, then teleport
  332. if (p_state_machine->states.has(travel_request)) {
  333. path.clear();
  334. current = travel_request;
  335. play_start = true;
  336. reset_request = reset_request_on_teleport;
  337. } else {
  338. StringName node = travel_request;
  339. travel_request = StringName();
  340. ERR_FAIL_V_MSG(0, "No such node: '" + node + "'");
  341. }
  342. }
  343. travel_request = StringName();
  344. }
  345. }
  346. bool do_start = (p_seek && p_time == 0) || play_start || current == StringName();
  347. if (do_start) {
  348. if (p_state_machine->start_node != StringName() && p_seek && p_time == 0 && current == StringName()) {
  349. current = p_state_machine->start_node;
  350. }
  351. if (reset_request) {
  352. 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);
  353. pos_current = 0;
  354. reset_request = false;
  355. }
  356. }
  357. if (!p_state_machine->states.has(current)) {
  358. playing = false; //current does not exist
  359. current = StringName();
  360. return 0;
  361. }
  362. float fade_blend = 1.0;
  363. if (fading_from != StringName()) {
  364. if (!p_state_machine->states.has(fading_from)) {
  365. fading_from = StringName();
  366. } else {
  367. if (!p_seek) {
  368. fading_pos += p_time;
  369. }
  370. fade_blend = MIN(1.0, fading_pos / fading_time);
  371. }
  372. }
  373. if (current_curve.is_valid()) {
  374. fade_blend = current_curve->sample(fade_blend);
  375. }
  376. 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.
  377. if (fading_from != StringName()) {
  378. double fade_blend_inv = 1.0 - fade_blend;
  379. 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.
  380. if (fade_blend >= 1.0) {
  381. fading_from = StringName();
  382. }
  383. }
  384. //guess playback position
  385. if (rem > len_current) { // weird but ok
  386. len_current = rem;
  387. }
  388. { //advance and loop check
  389. double next_pos = len_current - rem;
  390. end_loop = next_pos < pos_current;
  391. pos_current = next_pos; //looped
  392. }
  393. //find next
  394. StringName next;
  395. double next_xfade = 0.0;
  396. AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE;
  397. if (path.size()) {
  398. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  399. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  400. continue;
  401. }
  402. if (p_state_machine->transitions[i].local_from == current && p_state_machine->transitions[i].local_to == path[0]) {
  403. next_xfade = p_state_machine->transitions[i].transition->get_xfade_time();
  404. current_curve = p_state_machine->transitions[i].transition->get_xfade_curve();
  405. switch_mode = p_state_machine->transitions[i].transition->get_switch_mode();
  406. reset_request = p_state_machine->transitions[i].transition->is_reset();
  407. next = path[0];
  408. }
  409. }
  410. } else {
  411. float priority_best = 1e20;
  412. int auto_advance_to = -1;
  413. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  414. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  415. continue;
  416. }
  417. // handles end_node: when end_node is reached in a sub state machine, find and activate the current_transition
  418. if (force_auto_advance) {
  419. if (p_state_machine->transitions[i].from == current_transition.from && p_state_machine->transitions[i].to == current_transition.to) {
  420. auto_advance_to = i;
  421. force_auto_advance = false;
  422. break;
  423. }
  424. }
  425. // 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
  426. if (p_state_machine->start_node == current && p_state_machine->transitions[i].local_from == current) {
  427. if (p_state_machine->prev_state_machine != nullptr) {
  428. Ref<AnimationNodeStateMachinePlayback> prev_playback = p_state_machine->prev_state_machine->get_parameter(p_state_machine->playback);
  429. if (prev_playback.is_valid()) {
  430. StringName prev_local_to = String(prev_playback->current_transition.next).replace_first(String(p_state_machine->state_machine_name) + "/", "");
  431. if (p_state_machine->transitions[i].to == prev_local_to) {
  432. auto_advance_to = i;
  433. prev_playback->current_transition.next = StringName();
  434. break;
  435. }
  436. }
  437. }
  438. }
  439. if (p_state_machine->transitions[i].from == current && _check_advance_condition(p_state_machine, p_state_machine->transitions[i].transition)) {
  440. if (p_state_machine->transitions[i].transition->get_priority() <= priority_best) {
  441. priority_best = p_state_machine->transitions[i].transition->get_priority();
  442. auto_advance_to = i;
  443. }
  444. }
  445. }
  446. if (auto_advance_to != -1) {
  447. next = p_state_machine->transitions[auto_advance_to].local_to;
  448. Transition tr;
  449. tr.from = String(p_state_machine->state_machine_name) + "/" + String(p_state_machine->transitions[auto_advance_to].from);
  450. tr.to = String(p_state_machine->transitions[auto_advance_to].to).replace_first("../", "");
  451. tr.next = p_state_machine->transitions[auto_advance_to].to;
  452. current_transition = tr;
  453. current_curve = p_state_machine->transitions[auto_advance_to].transition->get_xfade_curve();
  454. next_xfade = p_state_machine->transitions[auto_advance_to].transition->get_xfade_time();
  455. switch_mode = p_state_machine->transitions[auto_advance_to].transition->get_switch_mode();
  456. reset_request = p_state_machine->transitions[auto_advance_to].transition->is_reset();
  457. }
  458. }
  459. if (next == p_state_machine->end_node) {
  460. AnimationNodeStateMachine *prev_state_machine = p_state_machine->prev_state_machine;
  461. if (prev_state_machine != nullptr) {
  462. Ref<AnimationNodeStateMachinePlayback> prev_playback = prev_state_machine->get_parameter(p_state_machine->playback);
  463. if (prev_playback.is_valid()) {
  464. if (next_xfade) {
  465. prev_playback->current_transition = current_transition;
  466. prev_playback->force_auto_advance = true;
  467. return rem;
  468. }
  469. float priority_best = 1e20;
  470. int auto_advance_to = -1;
  471. for (int i = 0; i < prev_state_machine->transitions.size(); i++) {
  472. if (prev_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  473. continue;
  474. }
  475. if (current_transition.next == prev_state_machine->end_node && _check_advance_condition(prev_state_machine, prev_state_machine->transitions[i].transition)) {
  476. if (prev_state_machine->transitions[i].transition->get_priority() <= priority_best) {
  477. priority_best = prev_state_machine->transitions[i].transition->get_priority();
  478. auto_advance_to = i;
  479. }
  480. }
  481. }
  482. if (auto_advance_to != -1) {
  483. if (prev_state_machine->transitions[auto_advance_to].transition->get_xfade_time()) {
  484. return rem;
  485. }
  486. }
  487. }
  488. }
  489. }
  490. //if next, see when to transition
  491. if (next != StringName()) {
  492. bool goto_next = false;
  493. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
  494. goto_next = next_xfade >= (len_current - pos_current) || end_loop;
  495. if (end_loop) {
  496. next_xfade = 0;
  497. }
  498. } else {
  499. goto_next = fading_from == StringName();
  500. }
  501. if (next_request || goto_next) { //end_loop should be used because fade time may be too small or zero and animation may have looped
  502. if (next_xfade) {
  503. //time to fade, baby
  504. fading_from = current;
  505. fading_time = next_xfade;
  506. fading_pos = 0;
  507. } else {
  508. fading_from = StringName();
  509. fading_pos = 0;
  510. }
  511. if (path.size()) { //if it came from path, remove path
  512. path.remove_at(0);
  513. }
  514. { // if the current node is a state machine, update the "playing" variable to false by passing -1 in p_time
  515. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->states[current].node;
  516. if (anodesm.is_valid()) {
  517. p_state_machine->blend_node(current, p_state_machine->states[current].node, -1, p_seek, p_is_external_seeking, 0, AnimationNode::FILTER_IGNORE, true);
  518. }
  519. }
  520. current = next;
  521. if (reset_request) {
  522. 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.
  523. }
  524. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
  525. pos_current = MIN(pos_current, len_current);
  526. p_state_machine->blend_node(current, p_state_machine->states[current].node, pos_current, true, p_is_external_seeking, 0, AnimationNode::FILTER_IGNORE, true);
  527. } else {
  528. pos_current = 0;
  529. }
  530. rem = len_current; //so it does not show 0 on transition
  531. }
  532. }
  533. if (current != p_state_machine->end_node) {
  534. 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
  535. } else {
  536. if (p_state_machine->prev_state_machine != nullptr) {
  537. Ref<AnimationNodeStateMachinePlayback> prev_playback = p_state_machine->prev_state_machine->get_parameter(p_state_machine->playback);
  538. if (prev_playback.is_valid()) {
  539. prev_playback->current_transition = current_transition;
  540. prev_playback->force_auto_advance = true;
  541. }
  542. }
  543. }
  544. return rem;
  545. }
  546. bool AnimationNodeStateMachinePlayback::_check_advance_condition(const Ref<AnimationNodeStateMachine> state_machine, const Ref<AnimationNodeStateMachineTransition> transition) const {
  547. if (transition->get_advance_mode() != AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO) {
  548. return false;
  549. }
  550. StringName advance_condition_name = transition->get_advance_condition_name();
  551. if (advance_condition_name != StringName() && !bool(state_machine->get_parameter(advance_condition_name))) {
  552. return false;
  553. }
  554. if (transition->expression.is_valid()) {
  555. AnimationTree *tree_base = state_machine->get_animation_tree();
  556. ERR_FAIL_COND_V(tree_base == nullptr, false);
  557. NodePath advance_expression_base_node_path = tree_base->get_advance_expression_base_node();
  558. Node *expression_base = tree_base->get_node_or_null(advance_expression_base_node_path);
  559. if (expression_base) {
  560. Ref<Expression> exp = transition->expression;
  561. 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.
  562. if (exp->has_execute_failed() || !ret) {
  563. return false;
  564. }
  565. } else {
  566. WARN_PRINT_ONCE("Animation transition has a valid expression, but no expression base node was set on its AnimationTree.");
  567. }
  568. }
  569. return true;
  570. }
  571. void AnimationNodeStateMachinePlayback::_bind_methods() {
  572. ClassDB::bind_method(D_METHOD("travel", "to_node", "reset_on_teleport"), &AnimationNodeStateMachinePlayback::travel, DEFVAL(true));
  573. ClassDB::bind_method(D_METHOD("start", "node", "reset"), &AnimationNodeStateMachinePlayback::start, DEFVAL(true));
  574. ClassDB::bind_method(D_METHOD("next"), &AnimationNodeStateMachinePlayback::next);
  575. ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
  576. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
  577. ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
  578. ClassDB::bind_method(D_METHOD("get_current_play_position"), &AnimationNodeStateMachinePlayback::get_current_play_pos);
  579. ClassDB::bind_method(D_METHOD("get_current_length"), &AnimationNodeStateMachinePlayback::get_current_length);
  580. ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path);
  581. }
  582. AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
  583. set_local_to_scene(true); //only one per instantiated scene
  584. }
  585. ///////////////////////////////////////////////////////
  586. void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
  587. r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  588. List<StringName> advance_conditions;
  589. for (int i = 0; i < transitions.size(); i++) {
  590. StringName ac = transitions[i].transition->get_advance_condition_name();
  591. if (ac != StringName() && advance_conditions.find(ac) == nullptr) {
  592. advance_conditions.push_back(ac);
  593. }
  594. }
  595. advance_conditions.sort_custom<StringName::AlphCompare>();
  596. for (const StringName &E : advance_conditions) {
  597. r_list->push_back(PropertyInfo(Variant::BOOL, E));
  598. }
  599. }
  600. Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
  601. if (p_parameter == playback) {
  602. Ref<AnimationNodeStateMachinePlayback> p;
  603. p.instantiate();
  604. return p;
  605. } else {
  606. return false; //advance condition
  607. }
  608. }
  609. void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  610. ERR_FAIL_COND(states.has(p_name));
  611. ERR_FAIL_COND(p_node.is_null());
  612. ERR_FAIL_COND(String(p_name).contains("/"));
  613. State state_new;
  614. state_new.node = p_node;
  615. state_new.position = p_position;
  616. states[p_name] = state_new;
  617. Ref<AnimationNodeStateMachine> anodesm = p_node;
  618. if (anodesm.is_valid()) {
  619. anodesm->state_machine_name = p_name;
  620. anodesm->prev_state_machine = this;
  621. }
  622. emit_changed();
  623. emit_signal(SNAME("tree_changed"));
  624. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  625. }
  626. void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
  627. ERR_FAIL_COND(states.has(p_name) == false);
  628. ERR_FAIL_COND(p_node.is_null());
  629. ERR_FAIL_COND(String(p_name).contains("/"));
  630. {
  631. Ref<AnimationNode> node = states[p_name].node;
  632. if (node.is_valid()) {
  633. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  634. }
  635. }
  636. states[p_name].node = p_node;
  637. emit_changed();
  638. emit_signal(SNAME("tree_changed"));
  639. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  640. }
  641. bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
  642. if (states.has(p_name)) {
  643. return !(states[p_name].node->is_class("AnimationNodeStartState") || states[p_name].node->is_class("AnimationNodeEndState"));
  644. }
  645. return true;
  646. }
  647. Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
  648. ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>());
  649. return states[p_name].node;
  650. }
  651. StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
  652. for (const KeyValue<StringName, State> &E : states) {
  653. if (E.value.node == p_node) {
  654. return E.key;
  655. }
  656. }
  657. ERR_FAIL_V(StringName());
  658. }
  659. void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
  660. Vector<StringName> nodes;
  661. for (const KeyValue<StringName, State> &E : states) {
  662. nodes.push_back(E.key);
  663. }
  664. nodes.sort_custom<StringName::AlphCompare>();
  665. for (int i = 0; i < nodes.size(); i++) {
  666. ChildNode cn;
  667. cn.name = nodes[i];
  668. cn.node = states[cn.name].node;
  669. r_child_nodes->push_back(cn);
  670. }
  671. }
  672. bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
  673. return states.has(p_name);
  674. }
  675. void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
  676. ERR_FAIL_COND(!states.has(p_name));
  677. if (!can_edit_node(p_name)) {
  678. return;
  679. }
  680. for (int i = 0; i < transitions.size(); i++) {
  681. if (transitions[i].local_from == p_name || transitions[i].local_to == p_name) {
  682. remove_transition_by_index(i);
  683. i--;
  684. }
  685. }
  686. {
  687. Ref<AnimationNode> node = states[p_name].node;
  688. ERR_FAIL_COND(node.is_null());
  689. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  690. }
  691. states.erase(p_name);
  692. emit_changed();
  693. emit_signal(SNAME("tree_changed"));
  694. }
  695. void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
  696. ERR_FAIL_COND(!states.has(p_name));
  697. ERR_FAIL_COND(states.has(p_new_name));
  698. ERR_FAIL_COND(!can_edit_node(p_name));
  699. states[p_new_name] = states[p_name];
  700. states.erase(p_name);
  701. Ref<AnimationNodeStateMachine> anodesm = states[p_new_name].node;
  702. if (anodesm.is_valid()) {
  703. anodesm->state_machine_name = p_new_name;
  704. }
  705. _rename_transitions(p_name, p_new_name);
  706. emit_changed();
  707. emit_signal(SNAME("tree_changed"));
  708. }
  709. void AnimationNodeStateMachine::_rename_transitions(const StringName &p_name, const StringName &p_new_name) {
  710. if (updating_transitions) {
  711. return;
  712. }
  713. updating_transitions = true;
  714. for (int i = 0; i < transitions.size(); i++) {
  715. if (transitions[i].from == p_name) {
  716. Vector<String> path = String(transitions[i].to).split("/");
  717. if (path.size() > 1) {
  718. if (path[0] == "..") {
  719. prev_state_machine->_rename_transitions(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  720. } else {
  721. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_to].node)->_rename_transitions("../" + p_name, "../" + p_new_name);
  722. }
  723. }
  724. if (transitions[i].local_from == p_name) {
  725. transitions.write[i].local_from = p_new_name;
  726. }
  727. transitions.write[i].from = p_new_name;
  728. }
  729. if (transitions[i].to == p_name) {
  730. Vector<String> path = String(transitions[i].from).split("/");
  731. if (path.size() > 1) {
  732. if (path[0] == "..") {
  733. prev_state_machine->_rename_transitions(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  734. } else {
  735. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_from].node)->_rename_transitions("../" + p_name, "../" + p_new_name);
  736. }
  737. }
  738. if (transitions[i].local_to == p_name) {
  739. transitions.write[i].local_to = p_new_name;
  740. }
  741. transitions.write[i].to = p_new_name;
  742. }
  743. }
  744. updating_transitions = false;
  745. }
  746. void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
  747. List<StringName> nodes;
  748. for (const KeyValue<StringName, State> &E : states) {
  749. nodes.push_back(E.key);
  750. }
  751. nodes.sort_custom<StringName::AlphCompare>();
  752. for (const StringName &E : nodes) {
  753. r_nodes->push_back(E);
  754. }
  755. }
  756. AnimationNodeStateMachine *AnimationNodeStateMachine::get_prev_state_machine() const {
  757. return prev_state_machine;
  758. }
  759. bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
  760. StringName from = _get_shortest_path(p_from);
  761. StringName to = _get_shortest_path(p_to);
  762. for (int i = 0; i < transitions.size(); i++) {
  763. if (transitions[i].from == from && transitions[i].to == to) {
  764. return true;
  765. }
  766. }
  767. return false;
  768. }
  769. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  770. StringName from = _get_shortest_path(p_from);
  771. StringName to = _get_shortest_path(p_to);
  772. for (int i = 0; i < transitions.size(); i++) {
  773. if (transitions[i].from == from && transitions[i].to == to) {
  774. return i;
  775. }
  776. }
  777. return -1;
  778. }
  779. bool AnimationNodeStateMachine::_can_connect(const StringName &p_name, Vector<AnimationNodeStateMachine *> p_parents) {
  780. if (p_parents.is_empty()) {
  781. AnimationNodeStateMachine *prev = this;
  782. while (prev != nullptr) {
  783. p_parents.push_back(prev);
  784. prev = prev->prev_state_machine;
  785. }
  786. }
  787. if (states.has(p_name)) {
  788. Ref<AnimationNodeStateMachine> anodesm = states[p_name].node;
  789. if (anodesm.is_valid() && p_parents.find(anodesm.ptr()) != -1) {
  790. return false;
  791. }
  792. return true;
  793. }
  794. String node_name = p_name;
  795. Vector<String> path = node_name.split("/");
  796. if (path.size() < 2) {
  797. return false;
  798. }
  799. if (path[0] == "..") {
  800. if (prev_state_machine != nullptr) {
  801. return prev_state_machine->_can_connect(node_name.replace_first("../", ""), p_parents);
  802. }
  803. } else if (states.has(path[0])) {
  804. Ref<AnimationNodeStateMachine> anodesm = states[path[0]].node;
  805. if (anodesm.is_valid()) {
  806. return anodesm->_can_connect(node_name.replace_first(path[0] + "/", ""), p_parents);
  807. }
  808. }
  809. return false;
  810. }
  811. StringName AnimationNodeStateMachine::_get_shortest_path(const StringName &p_path) const {
  812. // If p_path is something like StateMachine/../StateMachine2/State1,
  813. // the result will be StateMachine2/State1. This avoid duplicate
  814. // transitions when using add_transition. eg, this two calls is the same:
  815. //
  816. // add_transition("State1", "StateMachine/../State2", tr)
  817. // add_transition("State1", "State2", tr)
  818. //
  819. // but the second call must be invalid because the transition already exists
  820. Vector<String> path = String(p_path).split("/");
  821. Vector<String> new_path;
  822. for (int i = 0; i < path.size(); i++) {
  823. if (i > 0 && path[i] == ".." && new_path[i - 1] != "..") {
  824. new_path.remove_at(i - 1);
  825. } else {
  826. new_path.push_back(path[i]);
  827. }
  828. }
  829. String result;
  830. for (int i = 0; i < new_path.size(); i++) {
  831. result += new_path[i] + "/";
  832. }
  833. result.remove_at(result.length() - 1);
  834. return result;
  835. }
  836. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  837. if (updating_transitions) {
  838. return;
  839. }
  840. StringName from = _get_shortest_path(p_from);
  841. StringName to = _get_shortest_path(p_to);
  842. Vector<String> path_from = String(from).split("/");
  843. Vector<String> path_to = String(to).split("/");
  844. ERR_FAIL_COND(from == end_node || to == start_node);
  845. ERR_FAIL_COND(from == to);
  846. ERR_FAIL_COND(!_can_connect(from));
  847. ERR_FAIL_COND(!_can_connect(to));
  848. ERR_FAIL_COND(p_transition.is_null());
  849. for (int i = 0; i < transitions.size(); i++) {
  850. ERR_FAIL_COND(transitions[i].from == from && transitions[i].to == to);
  851. }
  852. if (path_from.size() > 1 || path_to.size() > 1) {
  853. ERR_FAIL_COND(path_from[0] == path_to[0]);
  854. }
  855. updating_transitions = true;
  856. StringName local_from = String(from).get_slicec('/', 0);
  857. StringName local_to = String(to).get_slicec('/', 0);
  858. local_from = local_from == ".." ? "Start" : local_from;
  859. local_to = local_to == ".." ? "End" : local_to;
  860. Transition tr;
  861. tr.from = from;
  862. tr.to = to;
  863. tr.local_from = local_from;
  864. tr.local_to = local_to;
  865. tr.transition = p_transition;
  866. tr.transition->connect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  867. transitions.push_back(tr);
  868. // do recursive
  869. if (path_from.size() > 1) {
  870. StringName local_path = String(from).replace_first(path_from[0] + "/", "");
  871. if (path_from[0] == "..") {
  872. prev_state_machine->add_transition(local_path, String(state_machine_name) + "/" + to, p_transition);
  873. } else {
  874. ((Ref<AnimationNodeStateMachine>)states[path_from[0]].node)->add_transition(local_path, "../" + to, p_transition);
  875. }
  876. }
  877. if (path_to.size() > 1) {
  878. StringName local_path = String(to).replace_first(path_to[0] + "/", "");
  879. if (path_to[0] == "..") {
  880. prev_state_machine->add_transition(String(state_machine_name) + "/" + from, local_path, p_transition);
  881. } else {
  882. ((Ref<AnimationNodeStateMachine>)states[path_to[0]].node)->add_transition("../" + from, local_path, p_transition);
  883. }
  884. }
  885. updating_transitions = false;
  886. }
  887. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  888. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  889. return transitions[p_transition].transition;
  890. }
  891. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  892. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  893. return transitions[p_transition].from;
  894. }
  895. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  896. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  897. return transitions[p_transition].to;
  898. }
  899. int AnimationNodeStateMachine::get_transition_count() const {
  900. return transitions.size();
  901. }
  902. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  903. StringName from = _get_shortest_path(p_from);
  904. StringName to = _get_shortest_path(p_to);
  905. for (int i = 0; i < transitions.size(); i++) {
  906. if (transitions[i].from == from && transitions[i].to == to) {
  907. remove_transition_by_index(i);
  908. return;
  909. }
  910. }
  911. }
  912. void AnimationNodeStateMachine::remove_transition_by_index(const int p_transition) {
  913. ERR_FAIL_INDEX(p_transition, transitions.size());
  914. Transition tr = transitions[p_transition];
  915. transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  916. transitions.remove_at(p_transition);
  917. Vector<String> path_from = String(tr.from).split("/");
  918. Vector<String> path_to = String(tr.to).split("/");
  919. List<Vector<String>> paths;
  920. paths.push_back(path_from);
  921. paths.push_back(path_to);
  922. for (List<Vector<String>>::Element *E = paths.front(); E; E = E->next()) {
  923. if (E->get()[0].size() > 1) {
  924. if (E->get()[0] == "..") {
  925. prev_state_machine->_remove_transition(tr.transition);
  926. } else if (states.has(E->get()[0])) {
  927. Ref<AnimationNodeStateMachine> anodesm = states[E->get()[0]].node;
  928. if (anodesm.is_valid()) {
  929. anodesm->_remove_transition(tr.transition);
  930. }
  931. }
  932. }
  933. }
  934. }
  935. void AnimationNodeStateMachine::_remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition) {
  936. for (int i = 0; i < transitions.size(); i++) {
  937. if (transitions[i].transition == p_transition) {
  938. remove_transition_by_index(i);
  939. return;
  940. }
  941. }
  942. }
  943. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  944. graph_offset = p_offset;
  945. }
  946. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  947. return graph_offset;
  948. }
  949. double AnimationNodeStateMachine::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  950. Ref<AnimationNodeStateMachinePlayback> playback_new = get_parameter(playback);
  951. ERR_FAIL_COND_V(playback_new.is_null(), 0.0);
  952. return playback_new->process(this, p_time, p_seek, p_is_external_seeking);
  953. }
  954. String AnimationNodeStateMachine::get_caption() const {
  955. return "StateMachine";
  956. }
  957. bool AnimationNodeStateMachine::has_local_transition(const StringName &p_from, const StringName &p_to) const {
  958. StringName from = _get_shortest_path(p_from);
  959. StringName to = _get_shortest_path(p_to);
  960. for (int i = 0; i < transitions.size(); i++) {
  961. if (transitions[i].local_from == from && transitions[i].local_to == to) {
  962. return true;
  963. }
  964. }
  965. return false;
  966. }
  967. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
  968. return get_node(p_name);
  969. }
  970. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  971. String prop_name = p_name;
  972. if (prop_name.begins_with("states/")) {
  973. String node_name = prop_name.get_slicec('/', 1);
  974. String what = prop_name.get_slicec('/', 2);
  975. if (what == "node") {
  976. Ref<AnimationNode> anode = p_value;
  977. if (anode.is_valid()) {
  978. add_node(node_name, p_value);
  979. }
  980. return true;
  981. }
  982. if (what == "position") {
  983. if (states.has(node_name)) {
  984. states[node_name].position = p_value;
  985. }
  986. return true;
  987. }
  988. } else if (prop_name == "transitions") {
  989. Array trans = p_value;
  990. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  991. for (int i = 0; i < trans.size(); i += 3) {
  992. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  993. }
  994. return true;
  995. } else if (prop_name == "graph_offset") {
  996. set_graph_offset(p_value);
  997. return true;
  998. }
  999. return false;
  1000. }
  1001. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  1002. String prop_name = p_name;
  1003. if (prop_name.begins_with("states/")) {
  1004. String node_name = prop_name.get_slicec('/', 1);
  1005. String what = prop_name.get_slicec('/', 2);
  1006. if (what == "node") {
  1007. if (states.has(node_name) && can_edit_node(node_name)) {
  1008. r_ret = states[node_name].node;
  1009. return true;
  1010. }
  1011. }
  1012. if (what == "position") {
  1013. if (states.has(node_name)) {
  1014. r_ret = states[node_name].position;
  1015. return true;
  1016. }
  1017. }
  1018. } else if (prop_name == "transitions") {
  1019. Array trans;
  1020. for (int i = 0; i < transitions.size(); i++) {
  1021. String from = transitions[i].from;
  1022. String to = transitions[i].to;
  1023. if (from.get_slicec('/', 0) == ".." || to.get_slicec('/', 0) == "..") {
  1024. continue;
  1025. }
  1026. trans.push_back(from);
  1027. trans.push_back(to);
  1028. trans.push_back(transitions[i].transition);
  1029. }
  1030. r_ret = trans;
  1031. return true;
  1032. } else if (prop_name == "graph_offset") {
  1033. r_ret = get_graph_offset();
  1034. return true;
  1035. }
  1036. return false;
  1037. }
  1038. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  1039. List<StringName> names;
  1040. for (const KeyValue<StringName, State> &E : states) {
  1041. names.push_back(E.key);
  1042. }
  1043. names.sort_custom<StringName::AlphCompare>();
  1044. for (const StringName &prop_name : names) {
  1045. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  1046. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1047. }
  1048. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1049. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1050. }
  1051. void AnimationNodeStateMachine::reset_state() {
  1052. states.clear();
  1053. transitions.clear();
  1054. playback = "playback";
  1055. start_node = "Start";
  1056. end_node = "End";
  1057. graph_offset = Vector2();
  1058. Ref<AnimationNodeStartState> s;
  1059. s.instantiate();
  1060. State start;
  1061. start.node = s;
  1062. start.position = Vector2(200, 100);
  1063. states[start_node] = start;
  1064. Ref<AnimationNodeEndState> e;
  1065. e.instantiate();
  1066. State end;
  1067. end.node = e;
  1068. end.position = Vector2(900, 100);
  1069. states[end_node] = end;
  1070. emit_changed();
  1071. emit_signal(SNAME("tree_changed"));
  1072. }
  1073. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  1074. ERR_FAIL_COND(!states.has(p_name));
  1075. states[p_name].position = p_position;
  1076. }
  1077. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  1078. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  1079. return states[p_name].position;
  1080. }
  1081. void AnimationNodeStateMachine::_tree_changed() {
  1082. emit_changed();
  1083. emit_signal(SNAME("tree_changed"));
  1084. }
  1085. void AnimationNodeStateMachine::_bind_methods() {
  1086. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  1087. ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
  1088. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  1089. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  1090. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  1091. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  1092. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  1093. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  1094. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  1095. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  1096. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  1097. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  1098. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  1099. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  1100. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  1101. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  1102. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  1103. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
  1104. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  1105. }
  1106. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  1107. Ref<AnimationNodeStartState> s;
  1108. s.instantiate();
  1109. State start;
  1110. start.node = s;
  1111. start.position = Vector2(200, 100);
  1112. states[start_node] = start;
  1113. Ref<AnimationNodeEndState> e;
  1114. e.instantiate();
  1115. State end;
  1116. end.node = e;
  1117. end.position = Vector2(900, 100);
  1118. states[end_node] = end;
  1119. }