animation_node_state_machine.cpp 71 KB

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