animation_node_state_machine.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  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_fading_from_play_pos() const {
  274. return fadeing_from_nti.position;
  275. }
  276. float AnimationNodeStateMachinePlayback::get_fading_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_fading_from_play_position"), &AnimationNodeStateMachinePlayback::get_fading_from_play_pos);
  1050. ClassDB::bind_method(D_METHOD("get_fading_from_length"), &AnimationNodeStateMachinePlayback::get_fading_from_length);
  1051. ClassDB::bind_method(D_METHOD("get_fading_position"), &AnimationNodeStateMachinePlayback::get_fading_pos);
  1052. ClassDB::bind_method(D_METHOD("get_fading_length"), &AnimationNodeStateMachinePlayback::get_fading_time);
  1053. ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::_get_travel_path);
  1054. ADD_SIGNAL(MethodInfo(SceneStringName(state_started), PropertyInfo(Variant::STRING_NAME, "state")));
  1055. ADD_SIGNAL(MethodInfo(SceneStringName(state_finished), PropertyInfo(Variant::STRING_NAME, "state")));
  1056. }
  1057. AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
  1058. set_local_to_scene(true); // Only one per instantiated scene.
  1059. default_transition.instantiate();
  1060. default_transition->set_xfade_time(0);
  1061. default_transition->set_reset(true);
  1062. default_transition->set_advance_mode(AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO);
  1063. default_transition->set_switch_mode(AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE);
  1064. }
  1065. ///////////////////////////////////////////////////////
  1066. void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
  1067. AnimationNode::get_parameter_list(r_list);
  1068. 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.
  1069. List<StringName> advance_conditions;
  1070. for (int i = 0; i < transitions.size(); i++) {
  1071. StringName ac = transitions[i].transition->get_advance_condition_name();
  1072. if (ac != StringName() && advance_conditions.find(ac) == nullptr) {
  1073. advance_conditions.push_back(ac);
  1074. }
  1075. }
  1076. advance_conditions.sort_custom<StringName::AlphCompare>();
  1077. for (const StringName &E : advance_conditions) {
  1078. r_list->push_back(PropertyInfo(Variant::BOOL, E));
  1079. }
  1080. }
  1081. Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
  1082. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  1083. if (ret != Variant()) {
  1084. return ret;
  1085. }
  1086. if (p_parameter == playback) {
  1087. Ref<AnimationNodeStateMachinePlayback> p;
  1088. p.instantiate();
  1089. return p;
  1090. } else {
  1091. return false; // Advance condition.
  1092. }
  1093. }
  1094. bool AnimationNodeStateMachine::is_parameter_read_only(const StringName &p_parameter) const {
  1095. if (AnimationNode::is_parameter_read_only(p_parameter)) {
  1096. return true;
  1097. }
  1098. if (p_parameter == playback) {
  1099. return true;
  1100. }
  1101. return false;
  1102. }
  1103. void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  1104. ERR_FAIL_COND(states.has(p_name));
  1105. ERR_FAIL_COND(p_node.is_null());
  1106. ERR_FAIL_COND(String(p_name).contains_char('/'));
  1107. State state_new;
  1108. state_new.node = p_node;
  1109. state_new.position = p_position;
  1110. states[p_name] = state_new;
  1111. emit_changed();
  1112. emit_signal(SNAME("tree_changed"));
  1113. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  1114. p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  1115. p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  1116. }
  1117. void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
  1118. ERR_FAIL_COND(states.has(p_name) == false);
  1119. ERR_FAIL_COND(p_node.is_null());
  1120. ERR_FAIL_COND(String(p_name).contains_char('/'));
  1121. {
  1122. Ref<AnimationNode> node = states[p_name].node;
  1123. if (node.is_valid()) {
  1124. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  1125. node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));
  1126. node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));
  1127. }
  1128. }
  1129. states[p_name].node = p_node;
  1130. emit_changed();
  1131. emit_signal(SNAME("tree_changed"));
  1132. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  1133. p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  1134. p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  1135. }
  1136. void AnimationNodeStateMachine::set_state_machine_type(StateMachineType p_state_machine_type) {
  1137. state_machine_type = p_state_machine_type;
  1138. emit_changed();
  1139. emit_signal(SNAME("tree_changed"));
  1140. notify_property_list_changed();
  1141. }
  1142. AnimationNodeStateMachine::StateMachineType AnimationNodeStateMachine::get_state_machine_type() const {
  1143. return state_machine_type;
  1144. }
  1145. void AnimationNodeStateMachine::set_allow_transition_to_self(bool p_enable) {
  1146. allow_transition_to_self = p_enable;
  1147. }
  1148. bool AnimationNodeStateMachine::is_allow_transition_to_self() const {
  1149. return allow_transition_to_self;
  1150. }
  1151. void AnimationNodeStateMachine::set_reset_ends(bool p_enable) {
  1152. reset_ends = p_enable;
  1153. }
  1154. bool AnimationNodeStateMachine::are_ends_reset() const {
  1155. return reset_ends;
  1156. }
  1157. bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
  1158. if (states.has(p_name)) {
  1159. const AnimationNode *anode = states[p_name].node.ptr();
  1160. return !(Object::cast_to<AnimationNodeStartState>(anode) || Object::cast_to<AnimationNodeEndState>(anode));
  1161. }
  1162. return true;
  1163. }
  1164. Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
  1165. ERR_FAIL_COND_V_EDMSG(!states.has(p_name), Ref<AnimationNode>(), String(p_name) + " is not found current state.");
  1166. return states[p_name].node;
  1167. }
  1168. StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
  1169. for (const KeyValue<StringName, State> &E : states) {
  1170. if (E.value.node == p_node) {
  1171. return E.key;
  1172. }
  1173. }
  1174. ERR_FAIL_V(StringName());
  1175. }
  1176. void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
  1177. Vector<StringName> nodes;
  1178. for (const KeyValue<StringName, State> &E : states) {
  1179. nodes.push_back(E.key);
  1180. }
  1181. nodes.sort_custom<StringName::AlphCompare>();
  1182. for (int i = 0; i < nodes.size(); i++) {
  1183. ChildNode cn;
  1184. cn.name = nodes[i];
  1185. cn.node = states[cn.name].node;
  1186. r_child_nodes->push_back(cn);
  1187. }
  1188. }
  1189. bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
  1190. return states.has(p_name);
  1191. }
  1192. void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
  1193. ERR_FAIL_COND(!states.has(p_name));
  1194. if (!can_edit_node(p_name)) {
  1195. return;
  1196. }
  1197. for (int i = 0; i < transitions.size(); i++) {
  1198. if (transitions[i].from == p_name || transitions[i].to == p_name) {
  1199. remove_transition_by_index(i);
  1200. i--;
  1201. }
  1202. }
  1203. {
  1204. Ref<AnimationNode> node = states[p_name].node;
  1205. ERR_FAIL_COND(node.is_null());
  1206. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  1207. node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));
  1208. node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));
  1209. }
  1210. states.erase(p_name);
  1211. emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);
  1212. emit_changed();
  1213. emit_signal(SNAME("tree_changed"));
  1214. }
  1215. void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
  1216. ERR_FAIL_COND(!states.has(p_name));
  1217. ERR_FAIL_COND(states.has(p_new_name));
  1218. ERR_FAIL_COND(!can_edit_node(p_name));
  1219. states[p_new_name] = states[p_name];
  1220. states.erase(p_name);
  1221. _rename_transitions(p_name, p_new_name);
  1222. emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);
  1223. emit_changed();
  1224. emit_signal(SNAME("tree_changed"));
  1225. }
  1226. void AnimationNodeStateMachine::_rename_transitions(const StringName &p_name, const StringName &p_new_name) {
  1227. if (updating_transitions) {
  1228. return;
  1229. }
  1230. updating_transitions = true;
  1231. for (int i = 0; i < transitions.size(); i++) {
  1232. if (transitions[i].from == p_name) {
  1233. transitions.write[i].from = p_new_name;
  1234. }
  1235. if (transitions[i].to == p_name) {
  1236. transitions.write[i].to = p_new_name;
  1237. }
  1238. }
  1239. updating_transitions = false;
  1240. }
  1241. LocalVector<StringName> AnimationNodeStateMachine::get_node_list() const {
  1242. LocalVector<StringName> nodes;
  1243. nodes.reserve(states.size());
  1244. for (const KeyValue<StringName, State> &E : states) {
  1245. nodes.push_back(E.key);
  1246. }
  1247. nodes.sort_custom<StringName::AlphCompare>();
  1248. return nodes;
  1249. }
  1250. TypedArray<StringName> AnimationNodeStateMachine::get_node_list_as_typed_array() const {
  1251. TypedArray<StringName> typed_arr;
  1252. LocalVector<StringName> vec = get_node_list();
  1253. typed_arr.resize(vec.size());
  1254. for (uint32_t i = 0; i < vec.size(); i++) {
  1255. typed_arr[i] = vec[i];
  1256. }
  1257. return typed_arr;
  1258. }
  1259. bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
  1260. for (int i = 0; i < transitions.size(); i++) {
  1261. if (transitions[i].from == p_from && transitions[i].to == p_to) {
  1262. return true;
  1263. }
  1264. }
  1265. return false;
  1266. }
  1267. bool AnimationNodeStateMachine::has_transition_from(const StringName &p_from) const {
  1268. for (int i = 0; i < transitions.size(); i++) {
  1269. if (transitions[i].from == p_from) {
  1270. return true;
  1271. }
  1272. }
  1273. return false;
  1274. }
  1275. bool AnimationNodeStateMachine::has_transition_to(const StringName &p_to) const {
  1276. for (int i = 0; i < transitions.size(); i++) {
  1277. if (transitions[i].to == p_to) {
  1278. return true;
  1279. }
  1280. }
  1281. return false;
  1282. }
  1283. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  1284. for (int i = 0; i < transitions.size(); i++) {
  1285. if (transitions[i].from == p_from && transitions[i].to == p_to) {
  1286. return i;
  1287. }
  1288. }
  1289. return -1;
  1290. }
  1291. Vector<int> AnimationNodeStateMachine::find_transition_from(const StringName &p_from) const {
  1292. Vector<int> ret;
  1293. for (int i = 0; i < transitions.size(); i++) {
  1294. if (transitions[i].from == p_from) {
  1295. ret.push_back(i);
  1296. }
  1297. }
  1298. return ret;
  1299. }
  1300. Vector<int> AnimationNodeStateMachine::find_transition_to(const StringName &p_to) const {
  1301. Vector<int> ret;
  1302. for (int i = 0; i < transitions.size(); i++) {
  1303. if (transitions[i].to == p_to) {
  1304. ret.push_back(i);
  1305. }
  1306. }
  1307. return ret;
  1308. }
  1309. bool AnimationNodeStateMachine::_can_connect(const StringName &p_name) {
  1310. if (states.has(p_name)) {
  1311. return true;
  1312. }
  1313. String node_name = p_name;
  1314. if (node_name.get_slice_count("/") < 2) {
  1315. return false;
  1316. }
  1317. return false;
  1318. }
  1319. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  1320. if (updating_transitions) {
  1321. return;
  1322. }
  1323. ERR_FAIL_COND(p_from == SceneStringName(End) || p_to == SceneStringName(Start));
  1324. ERR_FAIL_COND(p_from == p_to);
  1325. ERR_FAIL_COND(!_can_connect(p_from));
  1326. ERR_FAIL_COND(!_can_connect(p_to));
  1327. ERR_FAIL_COND(p_transition.is_null());
  1328. for (int i = 0; i < transitions.size(); i++) {
  1329. ERR_FAIL_COND(transitions[i].from == p_from && transitions[i].to == p_to);
  1330. }
  1331. updating_transitions = true;
  1332. Transition tr;
  1333. tr.from = p_from;
  1334. tr.to = p_to;
  1335. tr.transition = p_transition;
  1336. tr.transition->connect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  1337. transitions.push_back(tr);
  1338. updating_transitions = false;
  1339. }
  1340. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  1341. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  1342. return transitions[p_transition].transition;
  1343. }
  1344. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  1345. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  1346. return transitions[p_transition].from;
  1347. }
  1348. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  1349. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  1350. return transitions[p_transition].to;
  1351. }
  1352. bool AnimationNodeStateMachine::is_transition_across_group(int p_transition) const {
  1353. ERR_FAIL_INDEX_V(p_transition, transitions.size(), false);
  1354. if (get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
  1355. if (transitions[p_transition].from == SceneStringName(Start) || transitions[p_transition].to == SceneStringName(End)) {
  1356. return true;
  1357. }
  1358. }
  1359. return false;
  1360. }
  1361. int AnimationNodeStateMachine::get_transition_count() const {
  1362. return transitions.size();
  1363. }
  1364. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  1365. for (int i = 0; i < transitions.size(); i++) {
  1366. if (transitions[i].from == p_from && transitions[i].to == p_to) {
  1367. remove_transition_by_index(i);
  1368. return;
  1369. }
  1370. }
  1371. }
  1372. void AnimationNodeStateMachine::remove_transition_by_index(const int p_transition) {
  1373. ERR_FAIL_INDEX(p_transition, transitions.size());
  1374. transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  1375. transitions.remove_at(p_transition);
  1376. }
  1377. void AnimationNodeStateMachine::_remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition) {
  1378. for (int i = 0; i < transitions.size(); i++) {
  1379. if (transitions[i].transition == p_transition) {
  1380. remove_transition_by_index(i);
  1381. return;
  1382. }
  1383. }
  1384. }
  1385. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  1386. graph_offset = p_offset;
  1387. }
  1388. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  1389. return graph_offset;
  1390. }
  1391. AnimationNode::NodeTimeInfo AnimationNodeStateMachine::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  1392. Ref<AnimationNodeStateMachinePlayback> playback_new = get_parameter(playback);
  1393. ERR_FAIL_COND_V(playback_new.is_null(), AnimationNode::NodeTimeInfo());
  1394. playback_new->_set_base_path(node_state.get_base_path());
  1395. playback_new->_set_grouped(state_machine_type == STATE_MACHINE_TYPE_GROUPED);
  1396. if (p_test_only) {
  1397. playback_new = playback_new->duplicate(); // Don't process original when testing.
  1398. }
  1399. return playback_new->process(this, p_playback_info, p_test_only);
  1400. }
  1401. String AnimationNodeStateMachine::get_caption() const {
  1402. return "StateMachine";
  1403. }
  1404. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) const {
  1405. return get_node(p_name);
  1406. }
  1407. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  1408. String prop_name = p_name;
  1409. if (prop_name.begins_with("states/")) {
  1410. String node_name = prop_name.get_slicec('/', 1);
  1411. String what = prop_name.get_slicec('/', 2);
  1412. if (what == "node") {
  1413. Ref<AnimationNode> anode = p_value;
  1414. if (anode.is_valid()) {
  1415. add_node(node_name, p_value);
  1416. }
  1417. return true;
  1418. }
  1419. if (what == "position") {
  1420. if (states.has(node_name)) {
  1421. states[node_name].position = p_value;
  1422. }
  1423. return true;
  1424. }
  1425. } else if (prop_name == "transitions") {
  1426. Array trans = p_value;
  1427. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  1428. for (int i = 0; i < trans.size(); i += 3) {
  1429. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  1430. }
  1431. return true;
  1432. } else if (prop_name == "graph_offset") {
  1433. set_graph_offset(p_value);
  1434. return true;
  1435. }
  1436. return false;
  1437. }
  1438. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  1439. String prop_name = p_name;
  1440. if (prop_name.begins_with("states/")) {
  1441. String node_name = prop_name.get_slicec('/', 1);
  1442. String what = prop_name.get_slicec('/', 2);
  1443. if (what == "node") {
  1444. if (states.has(node_name) && can_edit_node(node_name)) {
  1445. r_ret = states[node_name].node;
  1446. return true;
  1447. }
  1448. }
  1449. if (what == "position") {
  1450. if (states.has(node_name)) {
  1451. r_ret = states[node_name].position;
  1452. return true;
  1453. }
  1454. }
  1455. } else if (prop_name == "transitions") {
  1456. Array trans;
  1457. for (int i = 0; i < transitions.size(); i++) {
  1458. String from = transitions[i].from;
  1459. String to = transitions[i].to;
  1460. trans.push_back(from);
  1461. trans.push_back(to);
  1462. trans.push_back(transitions[i].transition);
  1463. }
  1464. r_ret = trans;
  1465. return true;
  1466. } else if (prop_name == "graph_offset") {
  1467. r_ret = get_graph_offset();
  1468. return true;
  1469. }
  1470. return false;
  1471. }
  1472. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  1473. LocalVector<StringName> names;
  1474. names.reserve(states.size());
  1475. for (const KeyValue<StringName, State> &E : states) {
  1476. names.push_back(E.key);
  1477. }
  1478. names.sort_custom<StringName::AlphCompare>();
  1479. for (const StringName &prop_name : names) {
  1480. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  1481. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1482. }
  1483. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1484. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1485. }
  1486. void AnimationNodeStateMachine::_validate_property(PropertyInfo &p_property) const {
  1487. if (p_property.name == "allow_transition_to_self" || p_property.name == "reset_ends") {
  1488. if (state_machine_type == STATE_MACHINE_TYPE_GROUPED) {
  1489. p_property.usage = PROPERTY_USAGE_NONE;
  1490. }
  1491. }
  1492. }
  1493. void AnimationNodeStateMachine::reset_state() {
  1494. states.clear();
  1495. transitions.clear();
  1496. playback = "playback";
  1497. graph_offset = Vector2();
  1498. Ref<AnimationNodeStartState> s;
  1499. s.instantiate();
  1500. State start;
  1501. start.node = s;
  1502. start.position = Vector2(200, 100);
  1503. states[SceneStringName(Start)] = start;
  1504. Ref<AnimationNodeEndState> e;
  1505. e.instantiate();
  1506. State end;
  1507. end.node = e;
  1508. end.position = Vector2(900, 100);
  1509. states[SceneStringName(End)] = end;
  1510. emit_changed();
  1511. emit_signal(SNAME("tree_changed"));
  1512. }
  1513. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  1514. ERR_FAIL_COND(!states.has(p_name));
  1515. states[p_name].position = p_position;
  1516. }
  1517. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  1518. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  1519. return states[p_name].position;
  1520. }
  1521. void AnimationNodeStateMachine::_tree_changed() {
  1522. emit_changed();
  1523. AnimationRootNode::_tree_changed();
  1524. }
  1525. void AnimationNodeStateMachine::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  1526. AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
  1527. }
  1528. void AnimationNodeStateMachine::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  1529. AnimationRootNode::_animation_node_removed(p_oid, p_node);
  1530. }
  1531. #ifdef TOOLS_ENABLED
  1532. void AnimationNodeStateMachine::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1533. const String pf = p_function;
  1534. bool add_state_options = false;
  1535. if (p_idx == 0) {
  1536. 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");
  1537. } else if (p_idx <= 1) {
  1538. add_state_options = (pf == "has_transition" || pf == "add_transition" || pf == "remove_transition");
  1539. }
  1540. if (add_state_options) {
  1541. for (const KeyValue<StringName, State> &E : states) {
  1542. r_options->push_back(String(E.key).quote());
  1543. }
  1544. }
  1545. AnimationRootNode::get_argument_options(p_function, p_idx, r_options);
  1546. }
  1547. #endif
  1548. void AnimationNodeStateMachine::_bind_methods() {
  1549. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  1550. ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
  1551. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  1552. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  1553. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  1554. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  1555. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  1556. ClassDB::bind_method(D_METHOD("get_node_list"), &AnimationNodeStateMachine::get_node_list_as_typed_array);
  1557. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  1558. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  1559. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  1560. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  1561. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  1562. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  1563. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  1564. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  1565. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  1566. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  1567. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
  1568. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  1569. ClassDB::bind_method(D_METHOD("set_state_machine_type", "state_machine_type"), &AnimationNodeStateMachine::set_state_machine_type);
  1570. ClassDB::bind_method(D_METHOD("get_state_machine_type"), &AnimationNodeStateMachine::get_state_machine_type);
  1571. ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeStateMachine::set_allow_transition_to_self);
  1572. ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeStateMachine::is_allow_transition_to_self);
  1573. ClassDB::bind_method(D_METHOD("set_reset_ends", "enable"), &AnimationNodeStateMachine::set_reset_ends);
  1574. ClassDB::bind_method(D_METHOD("are_ends_reset"), &AnimationNodeStateMachine::are_ends_reset);
  1575. ADD_PROPERTY(PropertyInfo(Variant::INT, "state_machine_type", PROPERTY_HINT_ENUM, "Root,Nested,Grouped"), "set_state_machine_type", "get_state_machine_type");
  1576. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
  1577. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_ends"), "set_reset_ends", "are_ends_reset");
  1578. BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_ROOT);
  1579. BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_NESTED);
  1580. BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_GROUPED);
  1581. }
  1582. Vector<StringName> AnimationNodeStateMachine::get_nodes_with_transitions_from(const StringName &p_node) const {
  1583. Vector<StringName> result;
  1584. for (const Transition &transition : transitions) {
  1585. if (transition.from == p_node) {
  1586. result.push_back(transition.to);
  1587. }
  1588. }
  1589. return result;
  1590. }
  1591. Vector<StringName> AnimationNodeStateMachine::get_nodes_with_transitions_to(const StringName &p_node) const {
  1592. Vector<StringName> result;
  1593. for (const Transition &transition : transitions) {
  1594. if (transition.to == p_node) {
  1595. result.push_back(transition.from);
  1596. }
  1597. }
  1598. return result;
  1599. }
  1600. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  1601. Ref<AnimationNodeStartState> s;
  1602. s.instantiate();
  1603. State start;
  1604. start.node = s;
  1605. start.position = Vector2(200, 100);
  1606. states[SceneStringName(Start)] = start;
  1607. Ref<AnimationNodeEndState> e;
  1608. e.instantiate();
  1609. State end;
  1610. end.node = e;
  1611. end.position = Vector2(900, 100);
  1612. states[SceneStringName(End)] = end;
  1613. }