animation_node_state_machine.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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 !p_state_machine->is_allow_transition_to_self();
  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. p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  652. p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  653. }
  654. void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
  655. ERR_FAIL_COND(states.has(p_name) == false);
  656. ERR_FAIL_COND(p_node.is_null());
  657. ERR_FAIL_COND(String(p_name).contains("/"));
  658. {
  659. Ref<AnimationNode> node = states[p_name].node;
  660. if (node.is_valid()) {
  661. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  662. node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));
  663. node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));
  664. }
  665. }
  666. states[p_name].node = p_node;
  667. emit_changed();
  668. emit_signal(SNAME("tree_changed"));
  669. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  670. p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  671. p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  672. }
  673. void AnimationNodeStateMachine::set_allow_transition_to_self(bool p_enable) {
  674. allow_transition_to_self = p_enable;
  675. }
  676. bool AnimationNodeStateMachine::is_allow_transition_to_self() const {
  677. return allow_transition_to_self;
  678. }
  679. bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
  680. if (states.has(p_name)) {
  681. return !(states[p_name].node->is_class("AnimationNodeStartState") || states[p_name].node->is_class("AnimationNodeEndState"));
  682. }
  683. return true;
  684. }
  685. Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
  686. ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>());
  687. return states[p_name].node;
  688. }
  689. StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
  690. for (const KeyValue<StringName, State> &E : states) {
  691. if (E.value.node == p_node) {
  692. return E.key;
  693. }
  694. }
  695. ERR_FAIL_V(StringName());
  696. }
  697. void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
  698. Vector<StringName> nodes;
  699. for (const KeyValue<StringName, State> &E : states) {
  700. nodes.push_back(E.key);
  701. }
  702. nodes.sort_custom<StringName::AlphCompare>();
  703. for (int i = 0; i < nodes.size(); i++) {
  704. ChildNode cn;
  705. cn.name = nodes[i];
  706. cn.node = states[cn.name].node;
  707. r_child_nodes->push_back(cn);
  708. }
  709. }
  710. bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
  711. return states.has(p_name);
  712. }
  713. void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
  714. ERR_FAIL_COND(!states.has(p_name));
  715. if (!can_edit_node(p_name)) {
  716. return;
  717. }
  718. for (int i = 0; i < transitions.size(); i++) {
  719. if (transitions[i].local_from == p_name || transitions[i].local_to == p_name) {
  720. remove_transition_by_index(i);
  721. i--;
  722. }
  723. }
  724. {
  725. Ref<AnimationNode> node = states[p_name].node;
  726. ERR_FAIL_COND(node.is_null());
  727. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  728. node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));
  729. node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));
  730. }
  731. states.erase(p_name);
  732. emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);
  733. emit_changed();
  734. emit_signal(SNAME("tree_changed"));
  735. }
  736. void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
  737. ERR_FAIL_COND(!states.has(p_name));
  738. ERR_FAIL_COND(states.has(p_new_name));
  739. ERR_FAIL_COND(!can_edit_node(p_name));
  740. states[p_new_name] = states[p_name];
  741. states.erase(p_name);
  742. Ref<AnimationNodeStateMachine> anodesm = states[p_new_name].node;
  743. if (anodesm.is_valid()) {
  744. anodesm->state_machine_name = p_new_name;
  745. }
  746. _rename_transitions(p_name, p_new_name);
  747. emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);
  748. emit_changed();
  749. emit_signal(SNAME("tree_changed"));
  750. }
  751. void AnimationNodeStateMachine::_rename_transitions(const StringName &p_name, const StringName &p_new_name) {
  752. if (updating_transitions) {
  753. return;
  754. }
  755. updating_transitions = true;
  756. for (int i = 0; i < transitions.size(); i++) {
  757. if (transitions[i].from == p_name) {
  758. Vector<String> path = String(transitions[i].to).split("/");
  759. if (path.size() > 1) {
  760. if (path[0] == "..") {
  761. prev_state_machine->_rename_transitions(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  762. } else {
  763. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_to].node)->_rename_transitions("../" + p_name, "../" + p_new_name);
  764. }
  765. }
  766. if (transitions[i].local_from == p_name) {
  767. transitions.write[i].local_from = p_new_name;
  768. }
  769. transitions.write[i].from = p_new_name;
  770. }
  771. if (transitions[i].to == p_name) {
  772. Vector<String> path = String(transitions[i].from).split("/");
  773. if (path.size() > 1) {
  774. if (path[0] == "..") {
  775. prev_state_machine->_rename_transitions(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  776. } else {
  777. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_from].node)->_rename_transitions("../" + p_name, "../" + p_new_name);
  778. }
  779. }
  780. if (transitions[i].local_to == p_name) {
  781. transitions.write[i].local_to = p_new_name;
  782. }
  783. transitions.write[i].to = p_new_name;
  784. }
  785. }
  786. updating_transitions = false;
  787. }
  788. void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
  789. List<StringName> nodes;
  790. for (const KeyValue<StringName, State> &E : states) {
  791. nodes.push_back(E.key);
  792. }
  793. nodes.sort_custom<StringName::AlphCompare>();
  794. for (const StringName &E : nodes) {
  795. r_nodes->push_back(E);
  796. }
  797. }
  798. AnimationNodeStateMachine *AnimationNodeStateMachine::get_prev_state_machine() const {
  799. return prev_state_machine;
  800. }
  801. bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
  802. StringName from = _get_shortest_path(p_from);
  803. StringName to = _get_shortest_path(p_to);
  804. for (int i = 0; i < transitions.size(); i++) {
  805. if (transitions[i].from == from && transitions[i].to == to) {
  806. return true;
  807. }
  808. }
  809. return false;
  810. }
  811. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  812. StringName from = _get_shortest_path(p_from);
  813. StringName to = _get_shortest_path(p_to);
  814. for (int i = 0; i < transitions.size(); i++) {
  815. if (transitions[i].from == from && transitions[i].to == to) {
  816. return i;
  817. }
  818. }
  819. return -1;
  820. }
  821. bool AnimationNodeStateMachine::_can_connect(const StringName &p_name, Vector<AnimationNodeStateMachine *> p_parents) {
  822. if (p_parents.is_empty()) {
  823. AnimationNodeStateMachine *prev = this;
  824. while (prev != nullptr) {
  825. p_parents.push_back(prev);
  826. prev = prev->prev_state_machine;
  827. }
  828. }
  829. if (states.has(p_name)) {
  830. Ref<AnimationNodeStateMachine> anodesm = states[p_name].node;
  831. if (anodesm.is_valid() && p_parents.find(anodesm.ptr()) != -1) {
  832. return false;
  833. }
  834. return true;
  835. }
  836. String node_name = p_name;
  837. Vector<String> path = node_name.split("/");
  838. if (path.size() < 2) {
  839. return false;
  840. }
  841. if (path[0] == "..") {
  842. if (prev_state_machine != nullptr) {
  843. return prev_state_machine->_can_connect(node_name.replace_first("../", ""), p_parents);
  844. }
  845. } else if (states.has(path[0])) {
  846. Ref<AnimationNodeStateMachine> anodesm = states[path[0]].node;
  847. if (anodesm.is_valid()) {
  848. return anodesm->_can_connect(node_name.replace_first(path[0] + "/", ""), p_parents);
  849. }
  850. }
  851. return false;
  852. }
  853. StringName AnimationNodeStateMachine::_get_shortest_path(const StringName &p_path) const {
  854. // If p_path is something like StateMachine/../StateMachine2/State1,
  855. // the result will be StateMachine2/State1. This avoid duplicate
  856. // transitions when using add_transition. eg, this two calls is the same:
  857. //
  858. // add_transition("State1", "StateMachine/../State2", tr)
  859. // add_transition("State1", "State2", tr)
  860. //
  861. // but the second call must be invalid because the transition already exists
  862. Vector<String> path = String(p_path).split("/");
  863. Vector<String> new_path;
  864. for (int i = 0; i < path.size(); i++) {
  865. if (i > 0 && path[i] == ".." && new_path[i - 1] != "..") {
  866. new_path.remove_at(i - 1);
  867. } else {
  868. new_path.push_back(path[i]);
  869. }
  870. }
  871. String result;
  872. for (int i = 0; i < new_path.size(); i++) {
  873. result += new_path[i] + "/";
  874. }
  875. result.remove_at(result.length() - 1);
  876. return result;
  877. }
  878. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  879. if (updating_transitions) {
  880. return;
  881. }
  882. StringName from = _get_shortest_path(p_from);
  883. StringName to = _get_shortest_path(p_to);
  884. Vector<String> path_from = String(from).split("/");
  885. Vector<String> path_to = String(to).split("/");
  886. ERR_FAIL_COND(from == end_node || to == start_node);
  887. ERR_FAIL_COND(from == to);
  888. ERR_FAIL_COND(!_can_connect(from));
  889. ERR_FAIL_COND(!_can_connect(to));
  890. ERR_FAIL_COND(p_transition.is_null());
  891. for (int i = 0; i < transitions.size(); i++) {
  892. ERR_FAIL_COND(transitions[i].from == from && transitions[i].to == to);
  893. }
  894. if (path_from.size() > 1 || path_to.size() > 1) {
  895. ERR_FAIL_COND(path_from[0] == path_to[0]);
  896. }
  897. updating_transitions = true;
  898. StringName local_from = String(from).get_slicec('/', 0);
  899. StringName local_to = String(to).get_slicec('/', 0);
  900. local_from = local_from == ".." ? "Start" : local_from;
  901. local_to = local_to == ".." ? "End" : local_to;
  902. Transition tr;
  903. tr.from = from;
  904. tr.to = to;
  905. tr.local_from = local_from;
  906. tr.local_to = local_to;
  907. tr.transition = p_transition;
  908. tr.transition->connect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  909. transitions.push_back(tr);
  910. // do recursive
  911. if (path_from.size() > 1) {
  912. StringName local_path = String(from).replace_first(path_from[0] + "/", "");
  913. if (path_from[0] == "..") {
  914. prev_state_machine->add_transition(local_path, String(state_machine_name) + "/" + to, p_transition);
  915. } else {
  916. ((Ref<AnimationNodeStateMachine>)states[path_from[0]].node)->add_transition(local_path, "../" + to, p_transition);
  917. }
  918. }
  919. if (path_to.size() > 1) {
  920. StringName local_path = String(to).replace_first(path_to[0] + "/", "");
  921. if (path_to[0] == "..") {
  922. prev_state_machine->add_transition(String(state_machine_name) + "/" + from, local_path, p_transition);
  923. } else {
  924. ((Ref<AnimationNodeStateMachine>)states[path_to[0]].node)->add_transition("../" + from, local_path, p_transition);
  925. }
  926. }
  927. updating_transitions = false;
  928. }
  929. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  930. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  931. return transitions[p_transition].transition;
  932. }
  933. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  934. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  935. return transitions[p_transition].from;
  936. }
  937. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  938. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  939. return transitions[p_transition].to;
  940. }
  941. int AnimationNodeStateMachine::get_transition_count() const {
  942. return transitions.size();
  943. }
  944. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  945. StringName from = _get_shortest_path(p_from);
  946. StringName to = _get_shortest_path(p_to);
  947. for (int i = 0; i < transitions.size(); i++) {
  948. if (transitions[i].from == from && transitions[i].to == to) {
  949. remove_transition_by_index(i);
  950. return;
  951. }
  952. }
  953. }
  954. void AnimationNodeStateMachine::remove_transition_by_index(const int p_transition) {
  955. ERR_FAIL_INDEX(p_transition, transitions.size());
  956. Transition tr = transitions[p_transition];
  957. transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  958. transitions.remove_at(p_transition);
  959. Vector<String> path_from = String(tr.from).split("/");
  960. Vector<String> path_to = String(tr.to).split("/");
  961. List<Vector<String>> paths;
  962. paths.push_back(path_from);
  963. paths.push_back(path_to);
  964. for (List<Vector<String>>::Element *E = paths.front(); E; E = E->next()) {
  965. if (E->get()[0].size() > 1) {
  966. if (E->get()[0] == "..") {
  967. prev_state_machine->_remove_transition(tr.transition);
  968. } else if (states.has(E->get()[0])) {
  969. Ref<AnimationNodeStateMachine> anodesm = states[E->get()[0]].node;
  970. if (anodesm.is_valid()) {
  971. anodesm->_remove_transition(tr.transition);
  972. }
  973. }
  974. }
  975. }
  976. }
  977. void AnimationNodeStateMachine::_remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition) {
  978. for (int i = 0; i < transitions.size(); i++) {
  979. if (transitions[i].transition == p_transition) {
  980. remove_transition_by_index(i);
  981. return;
  982. }
  983. }
  984. }
  985. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  986. graph_offset = p_offset;
  987. }
  988. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  989. return graph_offset;
  990. }
  991. double AnimationNodeStateMachine::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  992. Ref<AnimationNodeStateMachinePlayback> playback_new = get_parameter(playback);
  993. ERR_FAIL_COND_V(playback_new.is_null(), 0.0);
  994. return playback_new->process(this, p_time, p_seek, p_is_external_seeking);
  995. }
  996. String AnimationNodeStateMachine::get_caption() const {
  997. return "StateMachine";
  998. }
  999. bool AnimationNodeStateMachine::has_local_transition(const StringName &p_from, const StringName &p_to) const {
  1000. StringName from = _get_shortest_path(p_from);
  1001. StringName to = _get_shortest_path(p_to);
  1002. for (int i = 0; i < transitions.size(); i++) {
  1003. if (transitions[i].local_from == from && transitions[i].local_to == to) {
  1004. return true;
  1005. }
  1006. }
  1007. return false;
  1008. }
  1009. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
  1010. return get_node(p_name);
  1011. }
  1012. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  1013. String prop_name = p_name;
  1014. if (prop_name.begins_with("states/")) {
  1015. String node_name = prop_name.get_slicec('/', 1);
  1016. String what = prop_name.get_slicec('/', 2);
  1017. if (what == "node") {
  1018. Ref<AnimationNode> anode = p_value;
  1019. if (anode.is_valid()) {
  1020. add_node(node_name, p_value);
  1021. }
  1022. return true;
  1023. }
  1024. if (what == "position") {
  1025. if (states.has(node_name)) {
  1026. states[node_name].position = p_value;
  1027. }
  1028. return true;
  1029. }
  1030. } else if (prop_name == "transitions") {
  1031. Array trans = p_value;
  1032. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  1033. for (int i = 0; i < trans.size(); i += 3) {
  1034. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  1035. }
  1036. return true;
  1037. } else if (prop_name == "graph_offset") {
  1038. set_graph_offset(p_value);
  1039. return true;
  1040. }
  1041. return false;
  1042. }
  1043. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  1044. String prop_name = p_name;
  1045. if (prop_name.begins_with("states/")) {
  1046. String node_name = prop_name.get_slicec('/', 1);
  1047. String what = prop_name.get_slicec('/', 2);
  1048. if (what == "node") {
  1049. if (states.has(node_name) && can_edit_node(node_name)) {
  1050. r_ret = states[node_name].node;
  1051. return true;
  1052. }
  1053. }
  1054. if (what == "position") {
  1055. if (states.has(node_name)) {
  1056. r_ret = states[node_name].position;
  1057. return true;
  1058. }
  1059. }
  1060. } else if (prop_name == "transitions") {
  1061. Array trans;
  1062. for (int i = 0; i < transitions.size(); i++) {
  1063. String from = transitions[i].from;
  1064. String to = transitions[i].to;
  1065. if (from.get_slicec('/', 0) == ".." || to.get_slicec('/', 0) == "..") {
  1066. continue;
  1067. }
  1068. trans.push_back(from);
  1069. trans.push_back(to);
  1070. trans.push_back(transitions[i].transition);
  1071. }
  1072. r_ret = trans;
  1073. return true;
  1074. } else if (prop_name == "graph_offset") {
  1075. r_ret = get_graph_offset();
  1076. return true;
  1077. }
  1078. return false;
  1079. }
  1080. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  1081. List<StringName> names;
  1082. for (const KeyValue<StringName, State> &E : states) {
  1083. names.push_back(E.key);
  1084. }
  1085. names.sort_custom<StringName::AlphCompare>();
  1086. for (const StringName &prop_name : names) {
  1087. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  1088. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1089. }
  1090. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1091. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1092. }
  1093. void AnimationNodeStateMachine::reset_state() {
  1094. states.clear();
  1095. transitions.clear();
  1096. playback = "playback";
  1097. start_node = "Start";
  1098. end_node = "End";
  1099. graph_offset = Vector2();
  1100. Ref<AnimationNodeStartState> s;
  1101. s.instantiate();
  1102. State start;
  1103. start.node = s;
  1104. start.position = Vector2(200, 100);
  1105. states[start_node] = start;
  1106. Ref<AnimationNodeEndState> e;
  1107. e.instantiate();
  1108. State end;
  1109. end.node = e;
  1110. end.position = Vector2(900, 100);
  1111. states[end_node] = end;
  1112. emit_changed();
  1113. emit_signal(SNAME("tree_changed"));
  1114. }
  1115. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  1116. ERR_FAIL_COND(!states.has(p_name));
  1117. states[p_name].position = p_position;
  1118. }
  1119. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  1120. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  1121. return states[p_name].position;
  1122. }
  1123. void AnimationNodeStateMachine::_tree_changed() {
  1124. emit_changed();
  1125. AnimationRootNode::_tree_changed();
  1126. }
  1127. void AnimationNodeStateMachine::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  1128. AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
  1129. }
  1130. void AnimationNodeStateMachine::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  1131. AnimationRootNode::_animation_node_removed(p_oid, p_node);
  1132. }
  1133. void AnimationNodeStateMachine::_bind_methods() {
  1134. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  1135. ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
  1136. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  1137. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  1138. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  1139. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  1140. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  1141. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  1142. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  1143. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  1144. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  1145. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  1146. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  1147. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  1148. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  1149. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  1150. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  1151. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
  1152. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  1153. ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeStateMachine::set_allow_transition_to_self);
  1154. ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeStateMachine::is_allow_transition_to_self);
  1155. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
  1156. }
  1157. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  1158. Ref<AnimationNodeStartState> s;
  1159. s.instantiate();
  1160. State start;
  1161. start.node = s;
  1162. start.position = Vector2(200, 100);
  1163. states[start_node] = start;
  1164. Ref<AnimationNodeEndState> e;
  1165. e.instantiate();
  1166. State end;
  1167. end.node = e;
  1168. end.position = Vector2(900, 100);
  1169. states[end_node] = end;
  1170. }