animation_node_state_machine.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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. /////////////////////////////////////////////////
  32. void AnimationNodeStateMachineTransition::set_switch_mode(SwitchMode p_mode) {
  33. switch_mode = p_mode;
  34. }
  35. AnimationNodeStateMachineTransition::SwitchMode AnimationNodeStateMachineTransition::get_switch_mode() const {
  36. return switch_mode;
  37. }
  38. void AnimationNodeStateMachineTransition::set_advance_mode(AdvanceMode p_mode) {
  39. advance_mode = p_mode;
  40. }
  41. AnimationNodeStateMachineTransition::AdvanceMode AnimationNodeStateMachineTransition::get_advance_mode() const {
  42. return advance_mode;
  43. }
  44. void AnimationNodeStateMachineTransition::set_advance_condition(const StringName &p_condition) {
  45. String cs = p_condition;
  46. ERR_FAIL_COND(cs.contains_char('/') || cs.contains_char(':'));
  47. advance_condition = p_condition;
  48. if (!cs.is_empty()) {
  49. advance_condition_name = "conditions/" + cs;
  50. } else {
  51. advance_condition_name = StringName();
  52. }
  53. emit_signal(SNAME("advance_condition_changed"));
  54. }
  55. StringName AnimationNodeStateMachineTransition::get_advance_condition() const {
  56. return advance_condition;
  57. }
  58. StringName AnimationNodeStateMachineTransition::get_advance_condition_name() const {
  59. return advance_condition_name;
  60. }
  61. void AnimationNodeStateMachineTransition::set_advance_expression(const String &p_expression) {
  62. advance_expression = p_expression;
  63. String advance_expression_stripped = advance_expression.strip_edges();
  64. if (advance_expression_stripped == String()) {
  65. expression.unref();
  66. return;
  67. }
  68. if (expression.is_null()) {
  69. expression.instantiate();
  70. }
  71. expression->parse(advance_expression_stripped);
  72. }
  73. String AnimationNodeStateMachineTransition::get_advance_expression() const {
  74. return advance_expression;
  75. }
  76. void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) {
  77. ERR_FAIL_COND(p_xfade < 0);
  78. xfade_time = p_xfade;
  79. emit_changed();
  80. }
  81. float AnimationNodeStateMachineTransition::get_xfade_time() const {
  82. return xfade_time;
  83. }
  84. void AnimationNodeStateMachineTransition::set_xfade_curve(const Ref<Curve> &p_curve) {
  85. xfade_curve = p_curve;
  86. emit_changed();
  87. }
  88. Ref<Curve> AnimationNodeStateMachineTransition::get_xfade_curve() const {
  89. return xfade_curve;
  90. }
  91. void AnimationNodeStateMachineTransition::set_break_loop_at_end(bool p_enable) {
  92. break_loop_at_end = p_enable;
  93. emit_changed();
  94. }
  95. bool AnimationNodeStateMachineTransition::is_loop_broken_at_end() const {
  96. return break_loop_at_end;
  97. }
  98. void AnimationNodeStateMachineTransition::set_reset(bool p_reset) {
  99. reset = p_reset;
  100. emit_changed();
  101. }
  102. bool AnimationNodeStateMachineTransition::is_reset() const {
  103. return reset;
  104. }
  105. void AnimationNodeStateMachineTransition::set_priority(int p_priority) {
  106. priority = p_priority;
  107. emit_changed();
  108. }
  109. int AnimationNodeStateMachineTransition::get_priority() const {
  110. return priority;
  111. }
  112. void AnimationNodeStateMachineTransition::_bind_methods() {
  113. ClassDB::bind_method(D_METHOD("set_switch_mode", "mode"), &AnimationNodeStateMachineTransition::set_switch_mode);
  114. ClassDB::bind_method(D_METHOD("get_switch_mode"), &AnimationNodeStateMachineTransition::get_switch_mode);
  115. ClassDB::bind_method(D_METHOD("set_advance_mode", "mode"), &AnimationNodeStateMachineTransition::set_advance_mode);
  116. ClassDB::bind_method(D_METHOD("get_advance_mode"), &AnimationNodeStateMachineTransition::get_advance_mode);
  117. ClassDB::bind_method(D_METHOD("set_advance_condition", "name"), &AnimationNodeStateMachineTransition::set_advance_condition);
  118. ClassDB::bind_method(D_METHOD("get_advance_condition"), &AnimationNodeStateMachineTransition::get_advance_condition);
  119. ClassDB::bind_method(D_METHOD("set_xfade_time", "secs"), &AnimationNodeStateMachineTransition::set_xfade_time);
  120. ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeStateMachineTransition::get_xfade_time);
  121. ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeStateMachineTransition::set_xfade_curve);
  122. ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeStateMachineTransition::get_xfade_curve);
  123. ClassDB::bind_method(D_METHOD("set_break_loop_at_end", "enable"), &AnimationNodeStateMachineTransition::set_break_loop_at_end);
  124. ClassDB::bind_method(D_METHOD("is_loop_broken_at_end"), &AnimationNodeStateMachineTransition::is_loop_broken_at_end);
  125. ClassDB::bind_method(D_METHOD("set_reset", "reset"), &AnimationNodeStateMachineTransition::set_reset);
  126. ClassDB::bind_method(D_METHOD("is_reset"), &AnimationNodeStateMachineTransition::is_reset);
  127. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority);
  128. ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority);
  129. ClassDB::bind_method(D_METHOD("set_advance_expression", "text"), &AnimationNodeStateMachineTransition::set_advance_expression);
  130. ClassDB::bind_method(D_METHOD("get_advance_expression"), &AnimationNodeStateMachineTransition::get_advance_expression);
  131. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");
  132. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");
  133. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "break_loop_at_end"), "set_break_loop_at_end", "is_loop_broken_at_end");
  134. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset"), "set_reset", "is_reset");
  135. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,32,1"), "set_priority", "get_priority");
  136. ADD_GROUP("Switch", "");
  137. ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,At End"), "set_switch_mode", "get_switch_mode");
  138. ADD_GROUP("Advance", "advance_");
  139. ADD_PROPERTY(PropertyInfo(Variant::INT, "advance_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled,Auto"), "set_advance_mode", "get_advance_mode");
  140. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "advance_condition"), "set_advance_condition", "get_advance_condition");
  141. ADD_PROPERTY(PropertyInfo(Variant::STRING, "advance_expression", PROPERTY_HINT_EXPRESSION, ""), "set_advance_expression", "get_advance_expression");
  142. BIND_ENUM_CONSTANT(SWITCH_MODE_IMMEDIATE);
  143. BIND_ENUM_CONSTANT(SWITCH_MODE_SYNC);
  144. BIND_ENUM_CONSTANT(SWITCH_MODE_AT_END);
  145. BIND_ENUM_CONSTANT(ADVANCE_MODE_DISABLED);
  146. BIND_ENUM_CONSTANT(ADVANCE_MODE_ENABLED);
  147. BIND_ENUM_CONSTANT(ADVANCE_MODE_AUTO);
  148. ADD_SIGNAL(MethodInfo("advance_condition_changed"));
  149. }
  150. AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() {
  151. }
  152. ////////////////////////////////////////////////////////
  153. void AnimationNodeStateMachinePlayback::_set_current(AnimationNodeStateMachine *p_state_machine, const StringName &p_state) {
  154. current = p_state;
  155. if (current == StringName()) {
  156. group_start_transition = Ref<AnimationNodeStateMachineTransition>();
  157. group_end_transition = Ref<AnimationNodeStateMachineTransition>();
  158. return;
  159. }
  160. AnimationTree *tree = p_state_machine->process_state ? p_state_machine->process_state->tree : nullptr;
  161. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(current);
  162. if (anodesm.is_null()) {
  163. group_start_transition = Ref<AnimationNodeStateMachineTransition>();
  164. group_end_transition = Ref<AnimationNodeStateMachineTransition>();
  165. _signal_state_change(tree, current, true);
  166. return;
  167. }
  168. Vector<int> indices = p_state_machine->find_transition_to(current);
  169. int group_start_size = indices.size();
  170. if (group_start_size) {
  171. group_start_transition = p_state_machine->get_transition(indices[0]);
  172. } else {
  173. group_start_transition = Ref<AnimationNodeStateMachineTransition>();
  174. }
  175. indices = p_state_machine->find_transition_from(current);
  176. int group_end_size = indices.size();
  177. if (group_end_size) {
  178. group_end_transition = p_state_machine->get_transition(indices[0]);
  179. } else {
  180. group_end_transition = Ref<AnimationNodeStateMachineTransition>();
  181. }
  182. // Validation.
  183. if (anodesm->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  184. indices = anodesm->find_transition_from(SceneStringName(Start));
  185. int anodesm_start_size = indices.size();
  186. indices = anodesm->find_transition_to(SceneStringName(End));
  187. int anodesm_end_size = indices.size();
  188. if (group_start_size > 1) {
  189. WARN_PRINT_ED("There are two or more transitions to the Grouped AnimationNodeStateMachine in AnimationNodeStateMachine: " + base_path + ", which may result in unintended transitions.");
  190. }
  191. if (group_end_size > 1) {
  192. WARN_PRINT_ED("There are two or more transitions from the Grouped AnimationNodeStateMachine in AnimationNodeStateMachine: " + base_path + ", which may result in unintended transitions.");
  193. }
  194. if (anodesm_start_size > 1) {
  195. WARN_PRINT_ED("There are two or more transitions from the Start of Grouped AnimationNodeStateMachine in AnimationNodeStateMachine: " + base_path + current + ", which may result in unintended transitions.");
  196. }
  197. if (anodesm_end_size > 1) {
  198. WARN_PRINT_ED("There are two or more transitions to the End of Grouped AnimationNodeStateMachine in AnimationNodeStateMachine: " + base_path + current + ", which may result in unintended transitions.");
  199. }
  200. if (anodesm_start_size != group_start_size) {
  201. ERR_PRINT_ED("There is a mismatch in the number of start transitions in and out of the Grouped AnimationNodeStateMachine on AnimationNodeStateMachine: " + base_path + current + ".");
  202. }
  203. if (anodesm_end_size != group_end_size) {
  204. ERR_PRINT_ED("There is a mismatch in the number of end transitions in and out of the Grouped AnimationNodeStateMachine on AnimationNodeStateMachine: " + base_path + current + ".");
  205. }
  206. } else {
  207. _signal_state_change(tree, current, true);
  208. }
  209. }
  210. void AnimationNodeStateMachinePlayback::_set_grouped(bool p_is_grouped) {
  211. is_grouped = p_is_grouped;
  212. }
  213. void AnimationNodeStateMachinePlayback::travel(const StringName &p_state, bool p_reset_on_teleport) {
  214. ERR_FAIL_COND_EDMSG(is_grouped, "Grouped AnimationNodeStateMachinePlayback must be handled by parent AnimationNodeStateMachinePlayback. You need to retrieve the parent Root/Nested AnimationNodeStateMachine.");
  215. ERR_FAIL_COND_EDMSG(String(p_state).contains("/Start") || String(p_state).contains("/End"), "Grouped AnimationNodeStateMachinePlayback doesn't allow to play Start/End directly. Instead, play the prev or next state of group in the parent AnimationNodeStateMachine.");
  216. _travel_main(p_state, p_reset_on_teleport);
  217. }
  218. void AnimationNodeStateMachinePlayback::start(const StringName &p_state, bool p_reset) {
  219. ERR_FAIL_COND_EDMSG(is_grouped, "Grouped AnimationNodeStateMachinePlayback must be handled by parent AnimationNodeStateMachinePlayback. You need to retrieve the parent Root/Nested AnimationNodeStateMachine.");
  220. ERR_FAIL_COND_EDMSG(String(p_state).contains("/Start") || String(p_state).contains("/End"), "Grouped AnimationNodeStateMachinePlayback doesn't allow to play Start/End directly. Instead, play the prev or next state of group in the parent AnimationNodeStateMachine.");
  221. _start_main(p_state, p_reset);
  222. }
  223. void AnimationNodeStateMachinePlayback::next() {
  224. ERR_FAIL_COND_EDMSG(is_grouped, "Grouped AnimationNodeStateMachinePlayback must be handled by parent AnimationNodeStateMachinePlayback. You need to retrieve the parent Root/Nested AnimationNodeStateMachine.");
  225. _next_main();
  226. }
  227. void AnimationNodeStateMachinePlayback::stop() {
  228. ERR_FAIL_COND_EDMSG(is_grouped, "Grouped AnimationNodeStateMachinePlayback must be handled by parent AnimationNodeStateMachinePlayback. You need to retrieve the parent Root/Nested AnimationNodeStateMachine.");
  229. _stop_main();
  230. }
  231. void AnimationNodeStateMachinePlayback::_travel_main(const StringName &p_state, bool p_reset_on_teleport) {
  232. travel_request = p_state;
  233. reset_request_on_teleport = p_reset_on_teleport;
  234. stop_request = false;
  235. }
  236. void AnimationNodeStateMachinePlayback::_start_main(const StringName &p_state, bool p_reset) {
  237. travel_request = StringName();
  238. path.clear();
  239. reset_request = p_reset;
  240. start_request = p_state;
  241. stop_request = false;
  242. }
  243. void AnimationNodeStateMachinePlayback::_next_main() {
  244. next_request = true;
  245. }
  246. void AnimationNodeStateMachinePlayback::_stop_main() {
  247. stop_request = true;
  248. }
  249. bool AnimationNodeStateMachinePlayback::is_playing() const {
  250. return playing;
  251. }
  252. bool AnimationNodeStateMachinePlayback::is_end() const {
  253. return current == SceneStringName(End) && fading_from == StringName();
  254. }
  255. StringName AnimationNodeStateMachinePlayback::get_current_node() const {
  256. return current;
  257. }
  258. StringName AnimationNodeStateMachinePlayback::get_fading_from_node() const {
  259. return fading_from;
  260. }
  261. Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const {
  262. return path;
  263. }
  264. TypedArray<StringName> AnimationNodeStateMachinePlayback::_get_travel_path() const {
  265. return Variant(get_travel_path()).operator Array();
  266. }
  267. float AnimationNodeStateMachinePlayback::get_current_play_pos() const {
  268. return current_nti.position;
  269. }
  270. float AnimationNodeStateMachinePlayback::get_current_length() const {
  271. return current_nti.length;
  272. }
  273. float AnimationNodeStateMachinePlayback::get_fade_from_play_pos() const {
  274. return fadeing_from_nti.position;
  275. }
  276. float AnimationNodeStateMachinePlayback::get_fade_from_length() const {
  277. return fadeing_from_nti.length;
  278. }
  279. float AnimationNodeStateMachinePlayback::get_fading_time() const {
  280. return fading_time;
  281. }
  282. float AnimationNodeStateMachinePlayback::get_fading_pos() const {
  283. return fading_pos;
  284. }
  285. bool _is_grouped_state_machine(const Ref<AnimationNodeStateMachine> p_node) {
  286. return p_node.is_valid() && p_node->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED;
  287. }
  288. void AnimationNodeStateMachinePlayback::_clear_fading(AnimationNodeStateMachine *p_state_machine, const StringName &p_state) {
  289. if (!p_state.is_empty() && !_is_grouped_state_machine(p_state_machine->get_node(p_state))) {
  290. _signal_state_change(p_state_machine->get_animation_tree(), p_state, false);
  291. }
  292. fading_from = StringName();
  293. fadeing_from_nti = AnimationNode::NodeTimeInfo();
  294. }
  295. void AnimationNodeStateMachinePlayback::_signal_state_change(AnimationTree *p_animation_tree, const StringName &p_state, bool p_started) {
  296. if (is_grouped && p_animation_tree && p_state != SceneStringName(Start) && p_state != SceneStringName(End)) {
  297. AnimationNodeStateMachinePlayback *parent_playback = *_get_parent_playback(p_animation_tree);
  298. if (parent_playback) {
  299. String prefix = base_path.substr(base_path.rfind_char('/', base_path.length() - 2) + 1);
  300. parent_playback->_signal_state_change(p_animation_tree, prefix + p_state, p_started);
  301. }
  302. }
  303. emit_signal(p_started ? SceneStringName(state_started) : SceneStringName(state_finished), p_state);
  304. }
  305. void AnimationNodeStateMachinePlayback::_clear_path_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_test_only) {
  306. List<AnimationNode::ChildNode> child_nodes;
  307. p_state_machine->get_child_nodes(&child_nodes);
  308. for (const AnimationNode::ChildNode &child_node : child_nodes) {
  309. Ref<AnimationNodeStateMachine> anodesm = child_node.node;
  310. if (_is_grouped_state_machine(anodesm)) {
  311. Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + child_node.name + "/playback");
  312. ERR_FAIL_COND(playback.is_null());
  313. playback->_set_base_path(base_path + child_node.name + "/");
  314. if (p_test_only) {
  315. playback = playback->duplicate();
  316. }
  317. playback->path.clear();
  318. playback->_clear_path_children(p_tree, anodesm.ptr(), p_test_only);
  319. if (current != child_node.name) {
  320. playback->_start(anodesm.ptr()); // Can restart.
  321. }
  322. }
  323. }
  324. }
  325. void AnimationNodeStateMachinePlayback::_start_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const String &p_path, bool p_test_only) {
  326. if (p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  327. return; // This function must be fired only by the top state machine, do nothing in child state machine.
  328. }
  329. Vector<String> temp_path = p_path.split("/");
  330. if (temp_path.size() > 1) {
  331. for (int i = 1; i < temp_path.size(); i++) {
  332. String concatenated;
  333. for (int j = 0; j < i; j++) {
  334. concatenated += temp_path[j] + (j == i - 1 ? "" : "/");
  335. }
  336. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(concatenated);
  337. if (anodesm.is_valid() && anodesm->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  338. ERR_FAIL_MSG("Root/Nested AnimationNodeStateMachine can't have path from parent AnimationNodeStateMachine.");
  339. }
  340. Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + concatenated + "/playback");
  341. ERR_FAIL_COND(playback.is_null());
  342. playback->_set_base_path(base_path + concatenated + "/");
  343. if (p_test_only) {
  344. playback = playback->duplicate();
  345. }
  346. playback->_start_main(temp_path[i], i == temp_path.size() - 1 ? reset_request : false);
  347. }
  348. reset_request = false;
  349. }
  350. }
  351. bool AnimationNodeStateMachinePlayback::_travel_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const String &p_path, bool p_is_allow_transition_to_self, bool p_is_parent_same_state, bool p_test_only) {
  352. if (p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  353. return false; // This function must be fired only by the top state machine, do nothing in child state machine.
  354. }
  355. Vector<String> temp_path = p_path.split("/");
  356. Vector<ChildStateMachineInfo> children;
  357. bool found_route = true;
  358. bool is_parent_same_state = p_is_parent_same_state;
  359. if (temp_path.size() > 1) {
  360. for (int i = 1; i < temp_path.size(); i++) {
  361. String concatenated;
  362. for (int j = 0; j < i; j++) {
  363. concatenated += temp_path[j] + (j == i - 1 ? "" : "/");
  364. }
  365. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(concatenated);
  366. if (anodesm.is_valid() && anodesm->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  367. ERR_FAIL_V_MSG(false, "Root/Nested AnimationNodeStateMachine can't have path from parent AnimationNodeStateMachine.");
  368. }
  369. Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + concatenated + "/playback");
  370. ERR_FAIL_COND_V(playback.is_null(), false);
  371. playback->_set_base_path(base_path + concatenated + "/");
  372. if (p_test_only) {
  373. playback = playback->duplicate();
  374. }
  375. if (!playback->is_playing()) {
  376. playback->_start(anodesm.ptr());
  377. }
  378. ChildStateMachineInfo child_info;
  379. child_info.playback = playback;
  380. // Process for the case that parent state is changed.
  381. bool child_found_route = true;
  382. bool is_current_same_state = temp_path[i] == playback->get_current_node();
  383. if (!is_parent_same_state) {
  384. // Force travel to end current child state machine.
  385. String child_path = "/" + playback->get_current_node();
  386. while (true) {
  387. Ref<AnimationNodeStateMachine> child_anodesm = p_state_machine->find_node_by_path(concatenated + child_path);
  388. if (child_anodesm.is_null() || child_anodesm->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  389. break;
  390. }
  391. Ref<AnimationNodeStateMachinePlayback> child_playback = p_tree->get(base_path + concatenated + child_path + "/playback");
  392. ERR_FAIL_COND_V(child_playback.is_null(), false);
  393. child_playback->_set_base_path(base_path + concatenated + "/");
  394. if (p_test_only) {
  395. child_playback = child_playback->duplicate();
  396. }
  397. child_playback->_travel_main(SceneStringName(End));
  398. child_found_route &= child_playback->_travel(p_tree, child_anodesm.ptr(), false, p_test_only);
  399. child_path += "/" + child_playback->get_current_node();
  400. }
  401. // Force restart target state machine.
  402. playback->_start(anodesm.ptr());
  403. }
  404. is_parent_same_state = is_current_same_state;
  405. bool is_deepest_state = i == temp_path.size() - 1;
  406. child_info.is_reset = is_deepest_state ? reset_request_on_teleport : false;
  407. playback->_travel_main(temp_path[i], child_info.is_reset);
  408. if (playback->_make_travel_path(p_tree, anodesm.ptr(), is_deepest_state ? p_is_allow_transition_to_self : false, child_info.path, p_test_only)) {
  409. found_route &= child_found_route;
  410. } else {
  411. child_info.path.push_back(temp_path[i]);
  412. found_route = false;
  413. }
  414. children.push_back(child_info);
  415. }
  416. reset_request_on_teleport = false;
  417. }
  418. if (found_route) {
  419. for (int i = 0; i < children.size(); i++) {
  420. children.write[i].playback->clear_path();
  421. for (int j = 0; j < children[i].path.size(); j++) {
  422. children.write[i].playback->push_path(children[i].path[j]);
  423. }
  424. }
  425. } else {
  426. for (int i = 0; i < children.size(); i++) {
  427. children.write[i].playback->_travel_main(StringName(), children[i].is_reset); // Clear travel.
  428. if (children[i].path.size()) {
  429. children.write[i].playback->_start_main(children[i].path[children[i].path.size() - 1], children[i].is_reset);
  430. }
  431. }
  432. }
  433. return found_route;
  434. }
  435. void AnimationNodeStateMachinePlayback::_start(AnimationNodeStateMachine *p_state_machine) {
  436. playing = true;
  437. _set_current(p_state_machine, start_request != StringName() ? start_request : SceneStringName(Start));
  438. teleport_request = true;
  439. stop_request = false;
  440. start_request = StringName();
  441. }
  442. bool AnimationNodeStateMachinePlayback::_travel(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_is_allow_transition_to_self, bool p_test_only) {
  443. return _make_travel_path(p_tree, p_state_machine, p_is_allow_transition_to_self, path, p_test_only);
  444. }
  445. String AnimationNodeStateMachinePlayback::_validate_path(AnimationNodeStateMachine *p_state_machine, const String &p_path) {
  446. if (p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  447. return p_path; // Grouped state machine doesn't allow validate-able request.
  448. }
  449. String target = p_path;
  450. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(target);
  451. while (anodesm.is_valid() && anodesm->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  452. Vector<int> indices = anodesm->find_transition_from(SceneStringName(Start));
  453. if (indices.size()) {
  454. target = target + "/" + anodesm->get_transition_to(indices[0]); // Find next state of Start.
  455. } else {
  456. break; // There is no transition in Start state of grouped state machine.
  457. }
  458. anodesm = p_state_machine->find_node_by_path(target);
  459. }
  460. return target;
  461. }
  462. bool AnimationNodeStateMachinePlayback::_make_travel_path(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_is_allow_transition_to_self, Vector<StringName> &r_path, bool p_test_only) {
  463. StringName travel = travel_request;
  464. travel_request = StringName();
  465. if (!playing) {
  466. _start(p_state_machine);
  467. }
  468. ERR_FAIL_COND_V(!p_state_machine->states.has(travel), false);
  469. ERR_FAIL_COND_V(!p_state_machine->states.has(current), false);
  470. if (current == travel) {
  471. return !p_is_allow_transition_to_self;
  472. }
  473. Vector<StringName> new_path;
  474. Vector2 current_pos = p_state_machine->states[current].position;
  475. Vector2 target_pos = p_state_machine->states[travel].position;
  476. bool found_route = false;
  477. HashMap<StringName, AStarCost> cost_map;
  478. List<int> open_list;
  479. // Build open list.
  480. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  481. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  482. continue;
  483. }
  484. if (p_state_machine->transitions[i].from == current) {
  485. open_list.push_back(i);
  486. float cost = p_state_machine->states[p_state_machine->transitions[i].to].position.distance_to(current_pos);
  487. cost *= p_state_machine->transitions[i].transition->get_priority();
  488. AStarCost ap;
  489. ap.prev = current;
  490. ap.distance = cost;
  491. cost_map[p_state_machine->transitions[i].to] = ap;
  492. if (p_state_machine->transitions[i].to == travel) { // Prematurely found it! :D
  493. found_route = true;
  494. break;
  495. }
  496. }
  497. }
  498. // Begin astar.
  499. while (!found_route) {
  500. if (open_list.is_empty()) {
  501. break; // No path found.
  502. }
  503. // Find the last cost transition.
  504. List<int>::Element *least_cost_transition = nullptr;
  505. float least_cost = 1e20;
  506. for (List<int>::Element *E = open_list.front(); E; E = E->next()) {
  507. float cost = cost_map[p_state_machine->transitions[E->get()].to].distance;
  508. cost += p_state_machine->states[p_state_machine->transitions[E->get()].to].position.distance_to(target_pos);
  509. if (cost < least_cost) {
  510. least_cost_transition = E;
  511. least_cost = cost;
  512. }
  513. }
  514. StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].from;
  515. StringName transition = p_state_machine->transitions[least_cost_transition->get()].to;
  516. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  517. if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  518. continue;
  519. }
  520. if (p_state_machine->transitions[i].from != transition || p_state_machine->transitions[i].to == transition_prev) {
  521. continue; // Not interested on those.
  522. }
  523. float distance = p_state_machine->states[p_state_machine->transitions[i].from].position.distance_to(p_state_machine->states[p_state_machine->transitions[i].to].position);
  524. distance *= p_state_machine->transitions[i].transition->get_priority();
  525. distance += cost_map[p_state_machine->transitions[i].from].distance;
  526. if (cost_map.has(p_state_machine->transitions[i].to)) {
  527. // Oh this was visited already, can we win the cost?
  528. if (distance < cost_map[p_state_machine->transitions[i].to].distance) {
  529. cost_map[p_state_machine->transitions[i].to].distance = distance;
  530. cost_map[p_state_machine->transitions[i].to].prev = p_state_machine->transitions[i].from;
  531. }
  532. } else {
  533. // Add to open list.
  534. AStarCost ac;
  535. ac.prev = p_state_machine->transitions[i].from;
  536. ac.distance = distance;
  537. cost_map[p_state_machine->transitions[i].to] = ac;
  538. open_list.push_back(i);
  539. if (p_state_machine->transitions[i].to == travel) {
  540. found_route = true;
  541. break;
  542. }
  543. }
  544. }
  545. if (found_route) {
  546. break;
  547. }
  548. open_list.erase(least_cost_transition);
  549. }
  550. // Check child grouped state machine.
  551. if (found_route) {
  552. // Make path.
  553. StringName at = travel;
  554. while (at != current) {
  555. new_path.push_back(at);
  556. at = cost_map[at].prev;
  557. }
  558. new_path.reverse();
  559. // Check internal paths of child grouped state machine.
  560. // For example:
  561. // [current - End] - [Start - End] - [Start - End] - [Start - target]
  562. String current_path = current;
  563. int len = new_path.size() + 1;
  564. for (int i = 0; i < len; i++) {
  565. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(current_path);
  566. if (anodesm.is_valid() && anodesm->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  567. Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + current_path + "/playback");
  568. ERR_FAIL_COND_V(playback.is_null(), false);
  569. playback->_set_base_path(base_path + current_path + "/");
  570. if (p_test_only) {
  571. playback = playback->duplicate();
  572. }
  573. if (i > 0) {
  574. playback->_start(anodesm.ptr());
  575. }
  576. if (i >= new_path.size()) {
  577. break; // Tracing has been finished, needs to break.
  578. }
  579. playback->_travel_main(SceneStringName(End));
  580. if (!playback->_travel(p_tree, anodesm.ptr(), false, p_test_only)) {
  581. found_route = false;
  582. break;
  583. }
  584. }
  585. if (i >= new_path.size()) {
  586. break; // Tracing has been finished, needs to break.
  587. }
  588. current_path = new_path[i];
  589. }
  590. }
  591. // Finally, rewrite path if route is found.
  592. if (found_route) {
  593. r_path = new_path;
  594. return true;
  595. } else {
  596. return false;
  597. }
  598. }
  599. AnimationNode::NodeTimeInfo AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  600. AnimationNode::NodeTimeInfo nti = _process(p_state_machine, p_playback_info, p_test_only);
  601. start_request = StringName();
  602. next_request = false;
  603. stop_request = false;
  604. reset_request_on_teleport = false;
  605. return nti;
  606. }
  607. AnimationNode::NodeTimeInfo AnimationNodeStateMachinePlayback::_process(AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  608. AnimationTree *tree = p_state_machine->process_state->tree;
  609. double p_time = p_playback_info.time;
  610. double p_delta = p_playback_info.delta;
  611. bool p_seek = p_playback_info.seeked;
  612. bool p_is_external_seeking = p_playback_info.is_external_seeking;
  613. // Check seek to 0 (means reset) by parent AnimationNode.
  614. if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) {
  615. if (p_state_machine->state_machine_type != AnimationNodeStateMachine::STATE_MACHINE_TYPE_NESTED || is_end() || !playing) {
  616. // Restart state machine.
  617. if (p_state_machine->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  618. path.clear();
  619. _clear_path_children(tree, p_state_machine, p_test_only);
  620. }
  621. _start(p_state_machine);
  622. reset_request = true;
  623. } else {
  624. // Reset current state.
  625. reset_request = true;
  626. teleport_request = true;
  627. }
  628. }
  629. if (stop_request) {
  630. start_request = StringName();
  631. travel_request = StringName();
  632. path.clear();
  633. playing = false;
  634. return AnimationNode::NodeTimeInfo();
  635. }
  636. if (!playing && start_request != StringName() && travel_request != StringName()) {
  637. return AnimationNode::NodeTimeInfo();
  638. }
  639. // Process start/travel request.
  640. if (start_request != StringName() || travel_request != StringName()) {
  641. if (p_state_machine->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  642. _clear_path_children(tree, p_state_machine, p_test_only);
  643. }
  644. }
  645. if (start_request != StringName()) {
  646. path.clear();
  647. String start_target = _validate_path(p_state_machine, start_request);
  648. Vector<String> start_path = String(start_target).split("/");
  649. start_request = start_path[0];
  650. if (start_path.size()) {
  651. _start_children(tree, p_state_machine, start_target, p_test_only);
  652. }
  653. // Teleport to start.
  654. if (p_state_machine->states.has(start_request)) {
  655. _start(p_state_machine);
  656. } else {
  657. StringName node = start_request;
  658. ERR_FAIL_V_MSG(AnimationNode::NodeTimeInfo(), "No such node: '" + node + "'");
  659. }
  660. }
  661. if (travel_request != StringName()) {
  662. // Fix path.
  663. String travel_target = _validate_path(p_state_machine, travel_request);
  664. Vector<String> travel_path = travel_target.split("/");
  665. travel_request = travel_path[0];
  666. StringName temp_travel_request = travel_request; // For the case that can't travel.
  667. // Process children.
  668. Vector<StringName> new_path;
  669. bool can_travel = _make_travel_path(tree, p_state_machine, travel_path.size() <= 1 ? p_state_machine->is_allow_transition_to_self() : false, new_path, p_test_only);
  670. if (travel_path.size()) {
  671. if (can_travel) {
  672. can_travel = _travel_children(tree, p_state_machine, travel_target, p_state_machine->is_allow_transition_to_self(), travel_path[0] == current, p_test_only);
  673. } else {
  674. _start_children(tree, p_state_machine, travel_target, p_test_only);
  675. }
  676. }
  677. // Process to travel.
  678. if (can_travel) {
  679. path = new_path;
  680. } else {
  681. // Can't travel, then teleport.
  682. if (p_state_machine->states.has(temp_travel_request)) {
  683. path.clear();
  684. if (current != temp_travel_request || reset_request_on_teleport) {
  685. _set_current(p_state_machine, temp_travel_request);
  686. reset_request = reset_request_on_teleport;
  687. teleport_request = true;
  688. }
  689. } else {
  690. ERR_FAIL_V_MSG(AnimationNode::NodeTimeInfo(), "No such node: '" + temp_travel_request + "'");
  691. }
  692. }
  693. }
  694. AnimationMixer::PlaybackInfo pi = p_playback_info;
  695. if (teleport_request) {
  696. teleport_request = false;
  697. // Clear fading on teleport.
  698. fading_from = StringName();
  699. fadeing_from_nti = AnimationNode::NodeTimeInfo();
  700. fading_pos = 0;
  701. // Init current length.
  702. pi.time = 0;
  703. pi.seeked = true;
  704. pi.is_external_seeking = false;
  705. pi.weight = 0;
  706. current_nti = p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, true);
  707. // Don't process first node if not necessary, instead process next node.
  708. _transition_to_next_recursive(tree, p_state_machine, p_delta, p_test_only);
  709. }
  710. // Check current node existence.
  711. if (!p_state_machine->states.has(current)) {
  712. playing = false; // Current does not exist.
  713. _set_current(p_state_machine, StringName());
  714. return AnimationNode::NodeTimeInfo();
  715. }
  716. // Special case for grouped state machine Start/End to make priority with parent blend (means don't treat Start and End states as RESET animations).
  717. bool is_start_of_group = false;
  718. bool is_end_of_group = false;
  719. if (!p_state_machine->are_ends_reset() || p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  720. is_start_of_group = fading_from == SceneStringName(Start);
  721. is_end_of_group = current == SceneStringName(End);
  722. }
  723. // Calc blend amount by cross-fade.
  724. float fade_blend = 1.0;
  725. if (fading_time && fading_from != StringName()) {
  726. if (!p_state_machine->states.has(fading_from)) {
  727. fading_from = StringName();
  728. } else {
  729. if (!p_seek) {
  730. fading_pos += Math::abs(p_delta);
  731. }
  732. fade_blend = MIN(1.0, fading_pos / fading_time);
  733. }
  734. }
  735. if (current_curve.is_valid()) {
  736. fade_blend = current_curve->sample(fade_blend);
  737. }
  738. fade_blend = Math::is_zero_approx(fade_blend) ? CMP_EPSILON : fade_blend;
  739. if (is_start_of_group) {
  740. fade_blend = 1.0;
  741. } else if (is_end_of_group) {
  742. fade_blend = 0.0;
  743. }
  744. // Main process.
  745. pi = p_playback_info;
  746. pi.weight = fade_blend;
  747. if (reset_request) {
  748. reset_request = false;
  749. pi.time = 0;
  750. pi.seeked = true;
  751. }
  752. current_nti = p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, p_test_only); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
  753. // Cross-fade process.
  754. if (fading_from != StringName()) {
  755. double fade_blend_inv = 1.0 - fade_blend;
  756. fade_blend_inv = Math::is_zero_approx(fade_blend_inv) ? CMP_EPSILON : fade_blend_inv;
  757. if (is_start_of_group) {
  758. fade_blend_inv = 0.0;
  759. } else if (is_end_of_group) {
  760. fade_blend_inv = 1.0;
  761. }
  762. pi = p_playback_info;
  763. pi.weight = fade_blend_inv;
  764. if (_reset_request_for_fading_from) {
  765. _reset_request_for_fading_from = false;
  766. pi.time = 0;
  767. pi.seeked = true;
  768. }
  769. fadeing_from_nti = p_state_machine->blend_node(p_state_machine->states[fading_from].node, fading_from, pi, AnimationNode::FILTER_IGNORE, true, p_test_only); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
  770. if (Animation::is_greater_or_equal_approx(fading_pos, fading_time)) {
  771. // Finish fading.
  772. _clear_fading(p_state_machine, fading_from);
  773. }
  774. }
  775. // Find next and see when to transition.
  776. bool will_end = _transition_to_next_recursive(tree, p_state_machine, p_delta, p_test_only) || current == SceneStringName(End);
  777. // Predict remaining time.
  778. if (will_end || ((p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_NESTED) && !p_state_machine->has_transition_from(current))) {
  779. // There is no next transition.
  780. if (fading_from != StringName()) {
  781. return Animation::is_greater_approx(current_nti.get_remain(), fadeing_from_nti.get_remain()) ? current_nti : fadeing_from_nti;
  782. }
  783. return current_nti;
  784. }
  785. if (!is_end()) {
  786. current_nti.is_infinity = true;
  787. }
  788. return current_nti;
  789. }
  790. bool AnimationNodeStateMachinePlayback::_transition_to_next_recursive(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, double p_delta, bool p_test_only) {
  791. _reset_request_for_fading_from = false;
  792. AnimationMixer::PlaybackInfo pi;
  793. pi.delta = p_delta;
  794. NextInfo next;
  795. Vector<StringName> transition_path;
  796. transition_path.push_back(current);
  797. while (true) {
  798. next = _find_next(p_tree, p_state_machine);
  799. if (!_can_transition_to_next(p_tree, p_state_machine, next, p_test_only)) {
  800. break; // Finish transition.
  801. }
  802. if (transition_path.has(next.node)) {
  803. WARN_PRINT_ONCE_ED("AnimationNodeStateMachinePlayback: " + base_path + "playback has detected one or more looped transitions in a single frame and aborted to prevent an infinite loop. You may need to check the transition settings.");
  804. break; // Maybe infinity loop, do nothing more.
  805. }
  806. transition_path.push_back(next.node);
  807. // Setting for fading.
  808. if (next.xfade) {
  809. // Time to fade.
  810. fading_from = current;
  811. fading_time = next.xfade;
  812. fading_pos = 0;
  813. } else {
  814. if (reset_request) {
  815. // There is no possibility of processing doubly. Now we can apply reset actually in here.
  816. pi.time = 0;
  817. pi.seeked = true;
  818. pi.is_external_seeking = false;
  819. pi.weight = 0;
  820. p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, p_test_only);
  821. }
  822. _clear_fading(p_state_machine, current);
  823. fading_time = 0;
  824. fading_pos = 0;
  825. }
  826. // If it came from path, remove path.
  827. if (path.size()) {
  828. path.remove_at(0);
  829. }
  830. // Update current status.
  831. _set_current(p_state_machine, next.node);
  832. current_curve = next.curve;
  833. if (current == SceneStringName(End)) {
  834. break;
  835. }
  836. _reset_request_for_fading_from = reset_request; // To avoid processing doubly, it must be reset in the fading process within _process().
  837. reset_request = next.is_reset;
  838. fadeing_from_nti = current_nti;
  839. if (next.switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
  840. pi.time = current_nti.position;
  841. pi.seeked = true;
  842. pi.is_external_seeking = false;
  843. pi.weight = 0;
  844. p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, p_test_only);
  845. }
  846. // Just get length to find next recursive.
  847. pi.time = 0;
  848. pi.is_external_seeking = false;
  849. pi.weight = 0;
  850. pi.seeked = next.is_reset;
  851. current_nti = p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, true); // Just retrieve remain length, don't process.
  852. // Fading must be processed.
  853. if (fading_time) {
  854. break;
  855. }
  856. }
  857. return next.node == SceneStringName(End);
  858. }
  859. bool AnimationNodeStateMachinePlayback::_can_transition_to_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, NextInfo p_next, bool p_test_only) {
  860. if (p_next.node == StringName()) {
  861. return false;
  862. }
  863. if (next_request) {
  864. // Process request only once.
  865. next_request = false;
  866. // Next request must be applied to only deepest state machine.
  867. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(current);
  868. if (anodesm.is_valid() && anodesm->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  869. Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + current + "/playback");
  870. ERR_FAIL_COND_V(playback.is_null(), false);
  871. playback->_set_base_path(base_path + current + "/");
  872. if (p_test_only) {
  873. playback = playback->duplicate();
  874. }
  875. playback->_next_main();
  876. // Then, fading should end.
  877. _clear_fading(p_state_machine, fading_from);
  878. fading_pos = 0;
  879. } else {
  880. return true;
  881. }
  882. }
  883. if (fading_from != StringName()) {
  884. return false;
  885. }
  886. if (current != SceneStringName(Start) && p_next.switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
  887. return Animation::is_less_or_equal_approx(current_nti.get_remain(p_next.break_loop_at_end), p_next.xfade);
  888. }
  889. return true;
  890. }
  891. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachinePlayback::_check_group_transition(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const AnimationNodeStateMachine::Transition &p_transition, Ref<AnimationNodeStateMachine> &r_state_machine, bool &r_bypass) const {
  892. Ref<AnimationNodeStateMachineTransition> temp_transition;
  893. Ref<AnimationNodeStateMachinePlayback> parent_playback;
  894. if (r_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  895. if (p_transition.from == SceneStringName(Start)) {
  896. parent_playback = _get_parent_playback(p_tree);
  897. if (parent_playback.is_valid()) {
  898. r_bypass = true;
  899. temp_transition = parent_playback->_get_group_start_transition();
  900. }
  901. } else if (p_transition.to == SceneStringName(End)) {
  902. parent_playback = _get_parent_playback(p_tree);
  903. if (parent_playback.is_valid()) {
  904. temp_transition = parent_playback->_get_group_end_transition();
  905. }
  906. }
  907. if (temp_transition.is_valid()) {
  908. r_state_machine = _get_parent_state_machine(p_tree);
  909. return temp_transition;
  910. }
  911. }
  912. return p_transition.transition;
  913. }
  914. AnimationNodeStateMachinePlayback::NextInfo AnimationNodeStateMachinePlayback::_find_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine) const {
  915. NextInfo next;
  916. if (path.size()) {
  917. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  918. Ref<AnimationNodeStateMachine> anodesm = p_state_machine;
  919. bool bypass = false;
  920. Ref<AnimationNodeStateMachineTransition> ref_transition = _check_group_transition(p_tree, p_state_machine, p_state_machine->transitions[i], anodesm, bypass);
  921. if (ref_transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  922. continue;
  923. }
  924. if (p_state_machine->transitions[i].from == current && p_state_machine->transitions[i].to == path[0]) {
  925. next.node = path[0];
  926. next.xfade = ref_transition->get_xfade_time();
  927. next.curve = ref_transition->get_xfade_curve();
  928. next.switch_mode = ref_transition->get_switch_mode();
  929. next.is_reset = ref_transition->is_reset();
  930. next.break_loop_at_end = ref_transition->is_loop_broken_at_end();
  931. }
  932. }
  933. } else {
  934. int auto_advance_to = -1;
  935. float priority_best = 1e20;
  936. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  937. Ref<AnimationNodeStateMachine> anodesm = p_state_machine;
  938. bool bypass = false;
  939. Ref<AnimationNodeStateMachineTransition> ref_transition = _check_group_transition(p_tree, p_state_machine, p_state_machine->transitions[i], anodesm, bypass);
  940. if (ref_transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {
  941. continue;
  942. }
  943. if (p_state_machine->transitions[i].from == current && (_check_advance_condition(anodesm, ref_transition) || bypass)) {
  944. if (ref_transition->get_priority() <= priority_best) {
  945. priority_best = ref_transition->get_priority();
  946. auto_advance_to = i;
  947. }
  948. }
  949. }
  950. if (auto_advance_to != -1) {
  951. next.node = p_state_machine->transitions[auto_advance_to].to;
  952. Ref<AnimationNodeStateMachine> anodesm = p_state_machine;
  953. bool bypass = false;
  954. Ref<AnimationNodeStateMachineTransition> ref_transition = _check_group_transition(p_tree, p_state_machine, p_state_machine->transitions[auto_advance_to], anodesm, bypass);
  955. next.xfade = ref_transition->get_xfade_time();
  956. next.curve = ref_transition->get_xfade_curve();
  957. next.switch_mode = ref_transition->get_switch_mode();
  958. next.is_reset = ref_transition->is_reset();
  959. next.break_loop_at_end = ref_transition->is_loop_broken_at_end();
  960. }
  961. }
  962. return next;
  963. }
  964. bool AnimationNodeStateMachinePlayback::_check_advance_condition(const Ref<AnimationNodeStateMachine> state_machine, const Ref<AnimationNodeStateMachineTransition> transition) const {
  965. if (transition->get_advance_mode() != AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO) {
  966. return false;
  967. }
  968. StringName advance_condition_name = transition->get_advance_condition_name();
  969. if (advance_condition_name != StringName() && !bool(state_machine->get_parameter(advance_condition_name))) {
  970. return false;
  971. }
  972. if (transition->expression.is_valid()) {
  973. AnimationTree *tree_base = state_machine->get_animation_tree();
  974. ERR_FAIL_NULL_V(tree_base, false);
  975. NodePath advance_expression_base_node_path = tree_base->get_advance_expression_base_node();
  976. Node *expression_base = tree_base->get_node_or_null(advance_expression_base_node_path);
  977. if (expression_base) {
  978. Ref<Expression> exp = transition->expression;
  979. 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.
  980. if (exp->has_execute_failed() || !ret) {
  981. return false;
  982. }
  983. } else {
  984. WARN_PRINT_ONCE("Animation transition has a valid expression, but no expression base node was set on its AnimationTree.");
  985. }
  986. }
  987. return true;
  988. }
  989. void AnimationNodeStateMachinePlayback::clear_path() {
  990. path.clear();
  991. }
  992. void AnimationNodeStateMachinePlayback::push_path(const StringName &p_state) {
  993. path.push_back(p_state);
  994. }
  995. void AnimationNodeStateMachinePlayback::_set_base_path(const String &p_base_path) {
  996. base_path = p_base_path;
  997. }
  998. Ref<AnimationNodeStateMachinePlayback> AnimationNodeStateMachinePlayback::_get_parent_playback(AnimationTree *p_tree) const {
  999. if (base_path.is_empty()) {
  1000. return Ref<AnimationNodeStateMachinePlayback>();
  1001. }
  1002. Vector<String> split = base_path.split("/");
  1003. ERR_FAIL_COND_V_MSG(split.size() < 2, Ref<AnimationNodeStateMachinePlayback>(), "Path is too short.");
  1004. StringName self_path = split[split.size() - 2];
  1005. split.remove_at(split.size() - 2);
  1006. String playback_path = String("/").join(split) + "playback";
  1007. Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(playback_path);
  1008. if (playback.is_null()) {
  1009. ERR_PRINT_ONCE("Can't get parent AnimationNodeStateMachinePlayback with path: " + playback_path + ". Maybe there is no Root/Nested AnimationNodeStateMachine in the parent of the Grouped AnimationNodeStateMachine.");
  1010. return Ref<AnimationNodeStateMachinePlayback>();
  1011. }
  1012. if (playback->get_current_node() != self_path) {
  1013. return Ref<AnimationNodeStateMachinePlayback>();
  1014. }
  1015. return playback;
  1016. }
  1017. Ref<AnimationNodeStateMachine> AnimationNodeStateMachinePlayback::_get_parent_state_machine(AnimationTree *p_tree) const {
  1018. if (base_path.is_empty()) {
  1019. return Ref<AnimationNodeStateMachine>();
  1020. }
  1021. Vector<String> split = base_path.split("/");
  1022. ERR_FAIL_COND_V_MSG(split.size() < 3, Ref<AnimationNodeStateMachine>(), "Path is too short.");
  1023. split = split.slice(1, split.size() - 2);
  1024. Ref<AnimationNode> root = p_tree->get_root_animation_node();
  1025. ERR_FAIL_COND_V_MSG(root.is_null(), Ref<AnimationNodeStateMachine>(), "There is no root AnimationNode in AnimationTree: " + String(p_tree->get_name()));
  1026. String anodesm_path = String("/").join(split);
  1027. Ref<AnimationNodeStateMachine> anodesm = !anodesm_path.size() ? root : root->find_node_by_path(anodesm_path);
  1028. ERR_FAIL_COND_V_MSG(anodesm.is_null(), Ref<AnimationNodeStateMachine>(), "Can't get state machine with path: " + anodesm_path);
  1029. return anodesm;
  1030. }
  1031. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachinePlayback::_get_group_start_transition() const {
  1032. ERR_FAIL_COND_V_MSG(group_start_transition.is_null(), Ref<AnimationNodeStateMachineTransition>(), "Group start transition is null.");
  1033. return group_start_transition;
  1034. }
  1035. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachinePlayback::_get_group_end_transition() const {
  1036. ERR_FAIL_COND_V_MSG(group_end_transition.is_null(), Ref<AnimationNodeStateMachineTransition>(), "Group end transition is null.");
  1037. return group_end_transition;
  1038. }
  1039. void AnimationNodeStateMachinePlayback::_bind_methods() {
  1040. ClassDB::bind_method(D_METHOD("travel", "to_node", "reset_on_teleport"), &AnimationNodeStateMachinePlayback::travel, DEFVAL(true));
  1041. ClassDB::bind_method(D_METHOD("start", "node", "reset"), &AnimationNodeStateMachinePlayback::start, DEFVAL(true));
  1042. ClassDB::bind_method(D_METHOD("next"), &AnimationNodeStateMachinePlayback::next);
  1043. ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
  1044. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
  1045. ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
  1046. ClassDB::bind_method(D_METHOD("get_current_play_position"), &AnimationNodeStateMachinePlayback::get_current_play_pos);
  1047. ClassDB::bind_method(D_METHOD("get_current_length"), &AnimationNodeStateMachinePlayback::get_current_length);
  1048. ClassDB::bind_method(D_METHOD("get_fading_from_node"), &AnimationNodeStateMachinePlayback::get_fading_from_node);
  1049. ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::_get_travel_path);
  1050. ADD_SIGNAL(MethodInfo(SceneStringName(state_started), PropertyInfo(Variant::STRING_NAME, "state")));
  1051. ADD_SIGNAL(MethodInfo(SceneStringName(state_finished), PropertyInfo(Variant::STRING_NAME, "state")));
  1052. }
  1053. AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
  1054. set_local_to_scene(true); // Only one per instantiated scene.
  1055. default_transition.instantiate();
  1056. default_transition->set_xfade_time(0);
  1057. default_transition->set_reset(true);
  1058. default_transition->set_advance_mode(AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO);
  1059. default_transition->set_switch_mode(AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE);
  1060. }
  1061. ///////////////////////////////////////////////////////
  1062. void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
  1063. AnimationNode::get_parameter_list(r_list);
  1064. r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ALWAYS_DUPLICATE)); // Don't store this object in .tres, it always needs to be made as unique object.
  1065. List<StringName> advance_conditions;
  1066. for (int i = 0; i < transitions.size(); i++) {
  1067. StringName ac = transitions[i].transition->get_advance_condition_name();
  1068. if (ac != StringName() && advance_conditions.find(ac) == nullptr) {
  1069. advance_conditions.push_back(ac);
  1070. }
  1071. }
  1072. advance_conditions.sort_custom<StringName::AlphCompare>();
  1073. for (const StringName &E : advance_conditions) {
  1074. r_list->push_back(PropertyInfo(Variant::BOOL, E));
  1075. }
  1076. }
  1077. Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
  1078. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  1079. if (ret != Variant()) {
  1080. return ret;
  1081. }
  1082. if (p_parameter == playback) {
  1083. Ref<AnimationNodeStateMachinePlayback> p;
  1084. p.instantiate();
  1085. return p;
  1086. } else {
  1087. return false; // Advance condition.
  1088. }
  1089. }
  1090. bool AnimationNodeStateMachine::is_parameter_read_only(const StringName &p_parameter) const {
  1091. if (AnimationNode::is_parameter_read_only(p_parameter)) {
  1092. return true;
  1093. }
  1094. if (p_parameter == playback) {
  1095. return true;
  1096. }
  1097. return false;
  1098. }
  1099. void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  1100. ERR_FAIL_COND(states.has(p_name));
  1101. ERR_FAIL_COND(p_node.is_null());
  1102. ERR_FAIL_COND(String(p_name).contains_char('/'));
  1103. State state_new;
  1104. state_new.node = p_node;
  1105. state_new.position = p_position;
  1106. states[p_name] = state_new;
  1107. emit_changed();
  1108. emit_signal(SNAME("tree_changed"));
  1109. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  1110. p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  1111. p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  1112. }
  1113. void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
  1114. ERR_FAIL_COND(states.has(p_name) == false);
  1115. ERR_FAIL_COND(p_node.is_null());
  1116. ERR_FAIL_COND(String(p_name).contains_char('/'));
  1117. {
  1118. Ref<AnimationNode> node = states[p_name].node;
  1119. if (node.is_valid()) {
  1120. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  1121. node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));
  1122. node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));
  1123. }
  1124. }
  1125. states[p_name].node = p_node;
  1126. emit_changed();
  1127. emit_signal(SNAME("tree_changed"));
  1128. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  1129. p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  1130. p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  1131. }
  1132. void AnimationNodeStateMachine::set_state_machine_type(StateMachineType p_state_machine_type) {
  1133. state_machine_type = p_state_machine_type;
  1134. emit_changed();
  1135. emit_signal(SNAME("tree_changed"));
  1136. notify_property_list_changed();
  1137. }
  1138. AnimationNodeStateMachine::StateMachineType AnimationNodeStateMachine::get_state_machine_type() const {
  1139. return state_machine_type;
  1140. }
  1141. void AnimationNodeStateMachine::set_allow_transition_to_self(bool p_enable) {
  1142. allow_transition_to_self = p_enable;
  1143. }
  1144. bool AnimationNodeStateMachine::is_allow_transition_to_self() const {
  1145. return allow_transition_to_self;
  1146. }
  1147. void AnimationNodeStateMachine::set_reset_ends(bool p_enable) {
  1148. reset_ends = p_enable;
  1149. }
  1150. bool AnimationNodeStateMachine::are_ends_reset() const {
  1151. return reset_ends;
  1152. }
  1153. bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
  1154. if (states.has(p_name)) {
  1155. const AnimationNode *anode = states[p_name].node.ptr();
  1156. return !(Object::cast_to<AnimationNodeStartState>(anode) || Object::cast_to<AnimationNodeEndState>(anode));
  1157. }
  1158. return true;
  1159. }
  1160. Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
  1161. ERR_FAIL_COND_V_EDMSG(!states.has(p_name), Ref<AnimationNode>(), String(p_name) + " is not found current state.");
  1162. return states[p_name].node;
  1163. }
  1164. StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
  1165. for (const KeyValue<StringName, State> &E : states) {
  1166. if (E.value.node == p_node) {
  1167. return E.key;
  1168. }
  1169. }
  1170. ERR_FAIL_V(StringName());
  1171. }
  1172. void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
  1173. Vector<StringName> nodes;
  1174. for (const KeyValue<StringName, State> &E : states) {
  1175. nodes.push_back(E.key);
  1176. }
  1177. nodes.sort_custom<StringName::AlphCompare>();
  1178. for (int i = 0; i < nodes.size(); i++) {
  1179. ChildNode cn;
  1180. cn.name = nodes[i];
  1181. cn.node = states[cn.name].node;
  1182. r_child_nodes->push_back(cn);
  1183. }
  1184. }
  1185. bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
  1186. return states.has(p_name);
  1187. }
  1188. void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
  1189. ERR_FAIL_COND(!states.has(p_name));
  1190. if (!can_edit_node(p_name)) {
  1191. return;
  1192. }
  1193. for (int i = 0; i < transitions.size(); i++) {
  1194. if (transitions[i].from == p_name || transitions[i].to == p_name) {
  1195. remove_transition_by_index(i);
  1196. i--;
  1197. }
  1198. }
  1199. {
  1200. Ref<AnimationNode> node = states[p_name].node;
  1201. ERR_FAIL_COND(node.is_null());
  1202. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  1203. node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));
  1204. node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));
  1205. }
  1206. states.erase(p_name);
  1207. emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);
  1208. emit_changed();
  1209. emit_signal(SNAME("tree_changed"));
  1210. }
  1211. void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
  1212. ERR_FAIL_COND(!states.has(p_name));
  1213. ERR_FAIL_COND(states.has(p_new_name));
  1214. ERR_FAIL_COND(!can_edit_node(p_name));
  1215. states[p_new_name] = states[p_name];
  1216. states.erase(p_name);
  1217. _rename_transitions(p_name, p_new_name);
  1218. emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);
  1219. emit_changed();
  1220. emit_signal(SNAME("tree_changed"));
  1221. }
  1222. void AnimationNodeStateMachine::_rename_transitions(const StringName &p_name, const StringName &p_new_name) {
  1223. if (updating_transitions) {
  1224. return;
  1225. }
  1226. updating_transitions = true;
  1227. for (int i = 0; i < transitions.size(); i++) {
  1228. if (transitions[i].from == p_name) {
  1229. transitions.write[i].from = p_new_name;
  1230. }
  1231. if (transitions[i].to == p_name) {
  1232. transitions.write[i].to = p_new_name;
  1233. }
  1234. }
  1235. updating_transitions = false;
  1236. }
  1237. LocalVector<StringName> AnimationNodeStateMachine::get_node_list() const {
  1238. LocalVector<StringName> nodes;
  1239. nodes.reserve(states.size());
  1240. for (const KeyValue<StringName, State> &E : states) {
  1241. nodes.push_back(E.key);
  1242. }
  1243. nodes.sort_custom<StringName::AlphCompare>();
  1244. return nodes;
  1245. }
  1246. TypedArray<StringName> AnimationNodeStateMachine::get_node_list_as_typed_array() const {
  1247. TypedArray<StringName> typed_arr;
  1248. LocalVector<StringName> vec = get_node_list();
  1249. typed_arr.resize(vec.size());
  1250. for (uint32_t i = 0; i < vec.size(); i++) {
  1251. typed_arr[i] = vec[i];
  1252. }
  1253. return typed_arr;
  1254. }
  1255. bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
  1256. for (int i = 0; i < transitions.size(); i++) {
  1257. if (transitions[i].from == p_from && transitions[i].to == p_to) {
  1258. return true;
  1259. }
  1260. }
  1261. return false;
  1262. }
  1263. bool AnimationNodeStateMachine::has_transition_from(const StringName &p_from) const {
  1264. for (int i = 0; i < transitions.size(); i++) {
  1265. if (transitions[i].from == p_from) {
  1266. return true;
  1267. }
  1268. }
  1269. return false;
  1270. }
  1271. bool AnimationNodeStateMachine::has_transition_to(const StringName &p_to) const {
  1272. for (int i = 0; i < transitions.size(); i++) {
  1273. if (transitions[i].to == p_to) {
  1274. return true;
  1275. }
  1276. }
  1277. return false;
  1278. }
  1279. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  1280. for (int i = 0; i < transitions.size(); i++) {
  1281. if (transitions[i].from == p_from && transitions[i].to == p_to) {
  1282. return i;
  1283. }
  1284. }
  1285. return -1;
  1286. }
  1287. Vector<int> AnimationNodeStateMachine::find_transition_from(const StringName &p_from) const {
  1288. Vector<int> ret;
  1289. for (int i = 0; i < transitions.size(); i++) {
  1290. if (transitions[i].from == p_from) {
  1291. ret.push_back(i);
  1292. }
  1293. }
  1294. return ret;
  1295. }
  1296. Vector<int> AnimationNodeStateMachine::find_transition_to(const StringName &p_to) const {
  1297. Vector<int> ret;
  1298. for (int i = 0; i < transitions.size(); i++) {
  1299. if (transitions[i].to == p_to) {
  1300. ret.push_back(i);
  1301. }
  1302. }
  1303. return ret;
  1304. }
  1305. bool AnimationNodeStateMachine::_can_connect(const StringName &p_name) {
  1306. if (states.has(p_name)) {
  1307. return true;
  1308. }
  1309. String node_name = p_name;
  1310. Vector<String> path = node_name.split("/");
  1311. if (path.size() < 2) {
  1312. return false;
  1313. }
  1314. return false;
  1315. }
  1316. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  1317. if (updating_transitions) {
  1318. return;
  1319. }
  1320. ERR_FAIL_COND(p_from == SceneStringName(End) || p_to == SceneStringName(Start));
  1321. ERR_FAIL_COND(p_from == p_to);
  1322. ERR_FAIL_COND(!_can_connect(p_from));
  1323. ERR_FAIL_COND(!_can_connect(p_to));
  1324. ERR_FAIL_COND(p_transition.is_null());
  1325. for (int i = 0; i < transitions.size(); i++) {
  1326. ERR_FAIL_COND(transitions[i].from == p_from && transitions[i].to == p_to);
  1327. }
  1328. updating_transitions = true;
  1329. Transition tr;
  1330. tr.from = p_from;
  1331. tr.to = p_to;
  1332. tr.transition = p_transition;
  1333. tr.transition->connect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  1334. transitions.push_back(tr);
  1335. updating_transitions = false;
  1336. }
  1337. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  1338. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  1339. return transitions[p_transition].transition;
  1340. }
  1341. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  1342. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  1343. return transitions[p_transition].from;
  1344. }
  1345. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  1346. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  1347. return transitions[p_transition].to;
  1348. }
  1349. bool AnimationNodeStateMachine::is_transition_across_group(int p_transition) const {
  1350. ERR_FAIL_INDEX_V(p_transition, transitions.size(), false);
  1351. if (get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  1352. if (transitions[p_transition].from == SceneStringName(Start) || transitions[p_transition].to == SceneStringName(End)) {
  1353. return true;
  1354. }
  1355. }
  1356. return false;
  1357. }
  1358. int AnimationNodeStateMachine::get_transition_count() const {
  1359. return transitions.size();
  1360. }
  1361. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  1362. for (int i = 0; i < transitions.size(); i++) {
  1363. if (transitions[i].from == p_from && transitions[i].to == p_to) {
  1364. remove_transition_by_index(i);
  1365. return;
  1366. }
  1367. }
  1368. }
  1369. void AnimationNodeStateMachine::remove_transition_by_index(const int p_transition) {
  1370. ERR_FAIL_INDEX(p_transition, transitions.size());
  1371. transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  1372. transitions.remove_at(p_transition);
  1373. }
  1374. void AnimationNodeStateMachine::_remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition) {
  1375. for (int i = 0; i < transitions.size(); i++) {
  1376. if (transitions[i].transition == p_transition) {
  1377. remove_transition_by_index(i);
  1378. return;
  1379. }
  1380. }
  1381. }
  1382. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  1383. graph_offset = p_offset;
  1384. }
  1385. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  1386. return graph_offset;
  1387. }
  1388. AnimationNode::NodeTimeInfo AnimationNodeStateMachine::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  1389. Ref<AnimationNodeStateMachinePlayback> playback_new = get_parameter(playback);
  1390. ERR_FAIL_COND_V(playback_new.is_null(), AnimationNode::NodeTimeInfo());
  1391. playback_new->_set_base_path(node_state.get_base_path());
  1392. playback_new->_set_grouped(state_machine_type == STATE_MACHINE_TYPE_GROUPED);
  1393. if (p_test_only) {
  1394. playback_new = playback_new->duplicate(); // Don't process original when testing.
  1395. }
  1396. return playback_new->process(this, p_playback_info, p_test_only);
  1397. }
  1398. String AnimationNodeStateMachine::get_caption() const {
  1399. return "StateMachine";
  1400. }
  1401. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) const {
  1402. return get_node(p_name);
  1403. }
  1404. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  1405. String prop_name = p_name;
  1406. if (prop_name.begins_with("states/")) {
  1407. String node_name = prop_name.get_slicec('/', 1);
  1408. String what = prop_name.get_slicec('/', 2);
  1409. if (what == "node") {
  1410. Ref<AnimationNode> anode = p_value;
  1411. if (anode.is_valid()) {
  1412. add_node(node_name, p_value);
  1413. }
  1414. return true;
  1415. }
  1416. if (what == "position") {
  1417. if (states.has(node_name)) {
  1418. states[node_name].position = p_value;
  1419. }
  1420. return true;
  1421. }
  1422. } else if (prop_name == "transitions") {
  1423. Array trans = p_value;
  1424. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  1425. for (int i = 0; i < trans.size(); i += 3) {
  1426. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  1427. }
  1428. return true;
  1429. } else if (prop_name == "graph_offset") {
  1430. set_graph_offset(p_value);
  1431. return true;
  1432. }
  1433. return false;
  1434. }
  1435. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  1436. String prop_name = p_name;
  1437. if (prop_name.begins_with("states/")) {
  1438. String node_name = prop_name.get_slicec('/', 1);
  1439. String what = prop_name.get_slicec('/', 2);
  1440. if (what == "node") {
  1441. if (states.has(node_name) && can_edit_node(node_name)) {
  1442. r_ret = states[node_name].node;
  1443. return true;
  1444. }
  1445. }
  1446. if (what == "position") {
  1447. if (states.has(node_name)) {
  1448. r_ret = states[node_name].position;
  1449. return true;
  1450. }
  1451. }
  1452. } else if (prop_name == "transitions") {
  1453. Array trans;
  1454. for (int i = 0; i < transitions.size(); i++) {
  1455. String from = transitions[i].from;
  1456. String to = transitions[i].to;
  1457. trans.push_back(from);
  1458. trans.push_back(to);
  1459. trans.push_back(transitions[i].transition);
  1460. }
  1461. r_ret = trans;
  1462. return true;
  1463. } else if (prop_name == "graph_offset") {
  1464. r_ret = get_graph_offset();
  1465. return true;
  1466. }
  1467. return false;
  1468. }
  1469. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  1470. LocalVector<StringName> names;
  1471. names.reserve(states.size());
  1472. for (const KeyValue<StringName, State> &E : states) {
  1473. names.push_back(E.key);
  1474. }
  1475. names.sort_custom<StringName::AlphCompare>();
  1476. for (const StringName &prop_name : names) {
  1477. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  1478. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1479. }
  1480. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1481. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1482. }
  1483. void AnimationNodeStateMachine::_validate_property(PropertyInfo &p_property) const {
  1484. if (p_property.name == "allow_transition_to_self" || p_property.name == "reset_ends") {
  1485. if (state_machine_type == STATE_MACHINE_TYPE_GROUPED) {
  1486. p_property.usage = PROPERTY_USAGE_NONE;
  1487. }
  1488. }
  1489. }
  1490. void AnimationNodeStateMachine::reset_state() {
  1491. states.clear();
  1492. transitions.clear();
  1493. playback = "playback";
  1494. graph_offset = Vector2();
  1495. Ref<AnimationNodeStartState> s;
  1496. s.instantiate();
  1497. State start;
  1498. start.node = s;
  1499. start.position = Vector2(200, 100);
  1500. states[SceneStringName(Start)] = start;
  1501. Ref<AnimationNodeEndState> e;
  1502. e.instantiate();
  1503. State end;
  1504. end.node = e;
  1505. end.position = Vector2(900, 100);
  1506. states[SceneStringName(End)] = end;
  1507. emit_changed();
  1508. emit_signal(SNAME("tree_changed"));
  1509. }
  1510. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  1511. ERR_FAIL_COND(!states.has(p_name));
  1512. states[p_name].position = p_position;
  1513. }
  1514. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  1515. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  1516. return states[p_name].position;
  1517. }
  1518. void AnimationNodeStateMachine::_tree_changed() {
  1519. emit_changed();
  1520. AnimationRootNode::_tree_changed();
  1521. }
  1522. void AnimationNodeStateMachine::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  1523. AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
  1524. }
  1525. void AnimationNodeStateMachine::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  1526. AnimationRootNode::_animation_node_removed(p_oid, p_node);
  1527. }
  1528. #ifdef TOOLS_ENABLED
  1529. void AnimationNodeStateMachine::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1530. const String pf = p_function;
  1531. bool add_state_options = false;
  1532. if (p_idx == 0) {
  1533. add_state_options = (pf == "get_node" || pf == "has_node" || pf == "rename_node" || pf == "remove_node" || pf == "replace_node" || pf == "set_node_position" || pf == "get_node_position");
  1534. } else if (p_idx <= 1) {
  1535. add_state_options = (pf == "has_transition" || pf == "add_transition" || pf == "remove_transition");
  1536. }
  1537. if (add_state_options) {
  1538. for (const KeyValue<StringName, State> &E : states) {
  1539. r_options->push_back(String(E.key).quote());
  1540. }
  1541. }
  1542. AnimationRootNode::get_argument_options(p_function, p_idx, r_options);
  1543. }
  1544. #endif
  1545. void AnimationNodeStateMachine::_bind_methods() {
  1546. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  1547. ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
  1548. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  1549. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  1550. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  1551. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  1552. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  1553. ClassDB::bind_method(D_METHOD("get_node_list"), &AnimationNodeStateMachine::get_node_list_as_typed_array);
  1554. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  1555. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  1556. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  1557. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  1558. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  1559. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  1560. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  1561. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  1562. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  1563. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  1564. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
  1565. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  1566. ClassDB::bind_method(D_METHOD("set_state_machine_type", "state_machine_type"), &AnimationNodeStateMachine::set_state_machine_type);
  1567. ClassDB::bind_method(D_METHOD("get_state_machine_type"), &AnimationNodeStateMachine::get_state_machine_type);
  1568. ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeStateMachine::set_allow_transition_to_self);
  1569. ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeStateMachine::is_allow_transition_to_self);
  1570. ClassDB::bind_method(D_METHOD("set_reset_ends", "enable"), &AnimationNodeStateMachine::set_reset_ends);
  1571. ClassDB::bind_method(D_METHOD("are_ends_reset"), &AnimationNodeStateMachine::are_ends_reset);
  1572. ADD_PROPERTY(PropertyInfo(Variant::INT, "state_machine_type", PROPERTY_HINT_ENUM, "Root,Nested,Grouped"), "set_state_machine_type", "get_state_machine_type");
  1573. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
  1574. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_ends"), "set_reset_ends", "are_ends_reset");
  1575. BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_ROOT);
  1576. BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_NESTED);
  1577. BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_GROUPED);
  1578. }
  1579. Vector<StringName> AnimationNodeStateMachine::get_nodes_with_transitions_from(const StringName &p_node) const {
  1580. Vector<StringName> result;
  1581. for (const Transition &transition : transitions) {
  1582. if (transition.from == p_node) {
  1583. result.push_back(transition.to);
  1584. }
  1585. }
  1586. return result;
  1587. }
  1588. Vector<StringName> AnimationNodeStateMachine::get_nodes_with_transitions_to(const StringName &p_node) const {
  1589. Vector<StringName> result;
  1590. for (const Transition &transition : transitions) {
  1591. if (transition.to == p_node) {
  1592. result.push_back(transition.from);
  1593. }
  1594. }
  1595. return result;
  1596. }
  1597. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  1598. Ref<AnimationNodeStartState> s;
  1599. s.instantiate();
  1600. State start;
  1601. start.node = s;
  1602. start.position = Vector2(200, 100);
  1603. states[SceneStringName(Start)] = start;
  1604. Ref<AnimationNodeEndState> e;
  1605. e.instantiate();
  1606. State end;
  1607. end.node = e;
  1608. end.position = Vector2(900, 100);
  1609. states[SceneStringName(End)] = end;
  1610. }