animation_node_state_machine.cpp 71 KB

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