animation_node_state_machine.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_auto_advance(bool p_enable) {
  39. auto_advance = p_enable;
  40. }
  41. bool AnimationNodeStateMachineTransition::has_auto_advance() const {
  42. return auto_advance;
  43. }
  44. void AnimationNodeStateMachineTransition::set_advance_condition(const StringName &p_condition) {
  45. String cs = p_condition;
  46. ERR_FAIL_COND(cs.contains("/") || cs.contains(":"));
  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_advance_expression_base_node(const NodePath &p_expression_base_node) {
  77. advance_expression_base_node = p_expression_base_node;
  78. }
  79. NodePath AnimationNodeStateMachineTransition::get_advance_expression_base_node() const {
  80. return advance_expression_base_node;
  81. }
  82. void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) {
  83. ERR_FAIL_COND(p_xfade < 0);
  84. xfade_time = p_xfade;
  85. emit_changed();
  86. }
  87. float AnimationNodeStateMachineTransition::get_xfade_time() const {
  88. return xfade_time;
  89. }
  90. void AnimationNodeStateMachineTransition::set_xfade_curve(const Ref<Curve> &p_curve) {
  91. xfade_curve = p_curve;
  92. }
  93. Ref<Curve> AnimationNodeStateMachineTransition::get_xfade_curve() const {
  94. return xfade_curve;
  95. }
  96. void AnimationNodeStateMachineTransition::set_disabled(bool p_disabled) {
  97. disabled = p_disabled;
  98. emit_changed();
  99. }
  100. bool AnimationNodeStateMachineTransition::is_disabled() const {
  101. return disabled;
  102. }
  103. void AnimationNodeStateMachineTransition::set_priority(int p_priority) {
  104. priority = p_priority;
  105. emit_changed();
  106. }
  107. int AnimationNodeStateMachineTransition::get_priority() const {
  108. return priority;
  109. }
  110. void AnimationNodeStateMachineTransition::_bind_methods() {
  111. ClassDB::bind_method(D_METHOD("set_switch_mode", "mode"), &AnimationNodeStateMachineTransition::set_switch_mode);
  112. ClassDB::bind_method(D_METHOD("get_switch_mode"), &AnimationNodeStateMachineTransition::get_switch_mode);
  113. ClassDB::bind_method(D_METHOD("set_auto_advance", "auto_advance"), &AnimationNodeStateMachineTransition::set_auto_advance);
  114. ClassDB::bind_method(D_METHOD("has_auto_advance"), &AnimationNodeStateMachineTransition::has_auto_advance);
  115. ClassDB::bind_method(D_METHOD("set_advance_condition", "name"), &AnimationNodeStateMachineTransition::set_advance_condition);
  116. ClassDB::bind_method(D_METHOD("get_advance_condition"), &AnimationNodeStateMachineTransition::get_advance_condition);
  117. ClassDB::bind_method(D_METHOD("set_xfade_time", "secs"), &AnimationNodeStateMachineTransition::set_xfade_time);
  118. ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeStateMachineTransition::get_xfade_time);
  119. ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeStateMachineTransition::set_xfade_curve);
  120. ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeStateMachineTransition::get_xfade_curve);
  121. ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &AnimationNodeStateMachineTransition::set_disabled);
  122. ClassDB::bind_method(D_METHOD("is_disabled"), &AnimationNodeStateMachineTransition::is_disabled);
  123. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority);
  124. ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority);
  125. ClassDB::bind_method(D_METHOD("set_advance_expression", "text"), &AnimationNodeStateMachineTransition::set_advance_expression);
  126. ClassDB::bind_method(D_METHOD("get_advance_expression"), &AnimationNodeStateMachineTransition::get_advance_expression);
  127. ClassDB::bind_method(D_METHOD("set_advance_expression_base_node", "path"), &AnimationNodeStateMachineTransition::set_advance_expression_base_node);
  128. ClassDB::bind_method(D_METHOD("get_advance_expression_base_node"), &AnimationNodeStateMachineTransition::get_advance_expression_base_node);
  129. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");
  130. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");
  131. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,32,1"), "set_priority", "get_priority");
  132. ADD_GROUP("Switch", "");
  133. ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,At End"), "set_switch_mode", "get_switch_mode");
  134. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_advance"), "set_auto_advance", "has_auto_advance");
  135. ADD_GROUP("Advance", "advance_");
  136. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "advance_condition"), "set_advance_condition", "get_advance_condition");
  137. ADD_PROPERTY(PropertyInfo(Variant::STRING, "advance_expression", PROPERTY_HINT_EXPRESSION, ""), "set_advance_expression", "get_advance_expression");
  138. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "advance_expression_base_node"), "set_advance_expression_base_node", "get_advance_expression_base_node");
  139. ADD_GROUP("Disabling", "");
  140. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
  141. BIND_ENUM_CONSTANT(SWITCH_MODE_IMMEDIATE);
  142. BIND_ENUM_CONSTANT(SWITCH_MODE_SYNC);
  143. BIND_ENUM_CONSTANT(SWITCH_MODE_AT_END);
  144. ADD_SIGNAL(MethodInfo("advance_condition_changed"));
  145. }
  146. AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() {
  147. }
  148. ////////////////////////////////////////////////////////
  149. void AnimationNodeStateMachinePlayback::travel(const StringName &p_state) {
  150. start_request_travel = true;
  151. start_request = p_state;
  152. stop_request = false;
  153. }
  154. void AnimationNodeStateMachinePlayback::start(const StringName &p_state) {
  155. start_request_travel = false;
  156. start_request = p_state;
  157. stop_request = false;
  158. }
  159. void AnimationNodeStateMachinePlayback::stop() {
  160. stop_request = true;
  161. }
  162. bool AnimationNodeStateMachinePlayback::is_playing() const {
  163. return playing;
  164. }
  165. StringName AnimationNodeStateMachinePlayback::get_current_node() const {
  166. return current;
  167. }
  168. StringName AnimationNodeStateMachinePlayback::get_blend_from_node() const {
  169. return fading_from;
  170. }
  171. Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const {
  172. return path;
  173. }
  174. float AnimationNodeStateMachinePlayback::get_current_play_pos() const {
  175. return pos_current;
  176. }
  177. float AnimationNodeStateMachinePlayback::get_current_length() const {
  178. return len_current;
  179. }
  180. bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel) {
  181. ERR_FAIL_COND_V(!playing, false);
  182. ERR_FAIL_COND_V(!p_state_machine->states.has(p_travel), false);
  183. ERR_FAIL_COND_V(!p_state_machine->states.has(current), false);
  184. path.clear(); //a new one will be needed
  185. if (current == p_travel) {
  186. return true; //nothing to do
  187. }
  188. Vector2 current_pos = p_state_machine->states[current].position;
  189. Vector2 target_pos = p_state_machine->states[p_travel].position;
  190. HashMap<StringName, AStarCost> cost_map;
  191. List<int> open_list;
  192. //build open list
  193. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  194. if (p_state_machine->transitions[i].transition->is_disabled()) {
  195. continue;
  196. }
  197. if (p_state_machine->transitions[i].local_from == current) {
  198. open_list.push_back(i);
  199. float cost = p_state_machine->states[p_state_machine->transitions[i].local_to].position.distance_to(current_pos);
  200. cost *= p_state_machine->transitions[i].transition->get_priority();
  201. AStarCost ap;
  202. ap.prev = current;
  203. ap.distance = cost;
  204. cost_map[p_state_machine->transitions[i].local_to] = ap;
  205. if (p_state_machine->transitions[i].local_to == p_travel) { //prematurely found it! :D
  206. path.push_back(p_travel);
  207. return true;
  208. }
  209. }
  210. }
  211. //begin astar
  212. bool found_route = false;
  213. while (!found_route) {
  214. if (open_list.size() == 0) {
  215. return false; //no path found
  216. }
  217. //find the last cost transition
  218. List<int>::Element *least_cost_transition = nullptr;
  219. float least_cost = 1e20;
  220. for (List<int>::Element *E = open_list.front(); E; E = E->next()) {
  221. float cost = cost_map[p_state_machine->transitions[E->get()].local_to].distance;
  222. cost += p_state_machine->states[p_state_machine->transitions[E->get()].local_to].position.distance_to(target_pos);
  223. if (cost < least_cost) {
  224. least_cost_transition = E;
  225. least_cost = cost;
  226. }
  227. }
  228. StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].local_from;
  229. StringName transition = p_state_machine->transitions[least_cost_transition->get()].local_to;
  230. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  231. if (p_state_machine->transitions[i].transition->is_disabled()) {
  232. continue;
  233. }
  234. if (p_state_machine->transitions[i].local_from != transition || p_state_machine->transitions[i].local_to == transition_prev) {
  235. continue; //not interested on those
  236. }
  237. float distance = p_state_machine->states[p_state_machine->transitions[i].local_from].position.distance_to(p_state_machine->states[p_state_machine->transitions[i].local_to].position);
  238. distance *= p_state_machine->transitions[i].transition->get_priority();
  239. distance += cost_map[p_state_machine->transitions[i].local_from].distance;
  240. if (cost_map.has(p_state_machine->transitions[i].local_to)) {
  241. //oh this was visited already, can we win the cost?
  242. if (distance < cost_map[p_state_machine->transitions[i].local_to].distance) {
  243. cost_map[p_state_machine->transitions[i].local_to].distance = distance;
  244. cost_map[p_state_machine->transitions[i].local_to].prev = p_state_machine->transitions[i].local_from;
  245. }
  246. } else {
  247. //add to open list
  248. AStarCost ac;
  249. ac.prev = p_state_machine->transitions[i].local_from;
  250. ac.distance = distance;
  251. cost_map[p_state_machine->transitions[i].local_to] = ac;
  252. open_list.push_back(i);
  253. if (p_state_machine->transitions[i].local_to == p_travel) {
  254. found_route = true;
  255. break;
  256. }
  257. }
  258. }
  259. if (found_route) {
  260. break;
  261. }
  262. open_list.erase(least_cost_transition);
  263. }
  264. //make path
  265. StringName at = p_travel;
  266. while (at != current) {
  267. path.push_back(at);
  268. at = cost_map[at].prev;
  269. }
  270. path.reverse();
  271. return true;
  272. }
  273. double AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, double p_time, bool p_seek, bool p_seek_root) {
  274. if (p_time == -1) {
  275. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->states[current].node;
  276. if (anodesm.is_valid()) {
  277. p_state_machine->blend_node(current, p_state_machine->states[current].node, -1, p_seek, p_seek_root, 0, AnimationNode::FILTER_IGNORE, true);
  278. }
  279. playing = false;
  280. return 0;
  281. }
  282. //if not playing and it can restart, then restart
  283. if (!playing && start_request == StringName()) {
  284. if (!stop_request && p_state_machine->start_node) {
  285. start(p_state_machine->start_node);
  286. } else {
  287. return 0;
  288. }
  289. }
  290. if (playing && stop_request) {
  291. stop_request = false;
  292. playing = false;
  293. return 0;
  294. }
  295. bool play_start = false;
  296. if (start_request != StringName()) {
  297. if (start_request_travel) {
  298. if (!playing) {
  299. if (!stop_request && p_state_machine->start_node) {
  300. // can restart, just postpone traveling
  301. path.clear();
  302. current = p_state_machine->start_node;
  303. playing = true;
  304. play_start = true;
  305. } else {
  306. // stopped, invalid state
  307. String node_name = start_request;
  308. start_request = StringName(); //clear start request
  309. ERR_FAIL_V_MSG(0, "Can't travel to '" + node_name + "' if state machine is not playing. Maybe you need to enable Autoplay on Load for one of the nodes in your state machine or call .start() first?");
  310. }
  311. } else {
  312. if (!_travel(p_state_machine, start_request)) {
  313. // can't travel, then teleport
  314. path.clear();
  315. current = start_request;
  316. }
  317. start_request = StringName(); //clear start request
  318. }
  319. } else {
  320. // teleport to start
  321. if (p_state_machine->states.has(start_request)) {
  322. path.clear();
  323. current = start_request;
  324. playing = true;
  325. play_start = true;
  326. start_request = StringName(); //clear start request
  327. } else {
  328. StringName node = start_request;
  329. start_request = StringName(); //clear start request
  330. ERR_FAIL_V_MSG(0, "No such node: '" + node + "'");
  331. }
  332. }
  333. }
  334. bool do_start = (p_seek && p_time == 0) || play_start || current == StringName();
  335. if (do_start) {
  336. if (p_state_machine->start_node != StringName() && p_seek && p_time == 0 && current == StringName()) {
  337. current = p_state_machine->start_node;
  338. }
  339. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, p_seek_root, 1.0, AnimationNode::FILTER_IGNORE, true);
  340. pos_current = 0;
  341. }
  342. if (!p_state_machine->states.has(current)) {
  343. playing = false; //current does not exist
  344. current = StringName();
  345. return 0;
  346. }
  347. float fade_blend = 1.0;
  348. if (fading_from != StringName()) {
  349. if (!p_state_machine->states.has(fading_from)) {
  350. fading_from = StringName();
  351. } else {
  352. if (!p_seek) {
  353. fading_pos += p_time;
  354. }
  355. fade_blend = MIN(1.0, fading_pos / fading_time);
  356. if (fade_blend >= 1.0) {
  357. fading_from = StringName();
  358. }
  359. }
  360. }
  361. if (current_curve.is_valid()) {
  362. fade_blend = current_curve->sample(fade_blend);
  363. }
  364. float rem = p_state_machine->blend_node(current, p_state_machine->states[current].node, p_time, p_seek, p_seek_root, fade_blend, AnimationNode::FILTER_IGNORE, true);
  365. if (fading_from != StringName()) {
  366. p_state_machine->blend_node(fading_from, p_state_machine->states[fading_from].node, p_time, p_seek, p_seek_root, 1.0 - fade_blend, AnimationNode::FILTER_IGNORE, true);
  367. }
  368. //guess playback position
  369. if (rem > len_current) { // weird but ok
  370. len_current = rem;
  371. }
  372. { //advance and loop check
  373. float next_pos = len_current - rem;
  374. end_loop = next_pos < pos_current;
  375. pos_current = next_pos; //looped
  376. }
  377. //find next
  378. StringName next;
  379. float next_xfade = 0.0;
  380. AnimationNodeStateMachineTransition::SwitchMode switch_mode = AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE;
  381. if (path.size()) {
  382. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  383. if (p_state_machine->transitions[i].transition->is_disabled()) {
  384. continue;
  385. }
  386. if (p_state_machine->transitions[i].local_from == current && p_state_machine->transitions[i].local_to == path[0]) {
  387. next_xfade = p_state_machine->transitions[i].transition->get_xfade_time();
  388. current_curve = p_state_machine->transitions[i].transition->get_xfade_curve();
  389. switch_mode = p_state_machine->transitions[i].transition->get_switch_mode();
  390. next = path[0];
  391. }
  392. }
  393. } else {
  394. float priority_best = 1e20;
  395. int auto_advance_to = -1;
  396. for (int i = 0; i < p_state_machine->transitions.size(); i++) {
  397. if (p_state_machine->transitions[i].transition->is_disabled()) {
  398. continue;
  399. }
  400. // handles end_node: when end_node is reached in a sub state machine, find and activate the current_transition
  401. if (force_auto_advance) {
  402. if (p_state_machine->transitions[i].from == current_transition.from && p_state_machine->transitions[i].to == current_transition.to) {
  403. auto_advance_to = i;
  404. force_auto_advance = false;
  405. break;
  406. }
  407. }
  408. // handles start_node: if previous state machine is pointing to a node inside the current state machine, starts the current machine from start_node to prev_local_to
  409. if (p_state_machine->start_node == current && p_state_machine->transitions[i].local_from == current) {
  410. if (p_state_machine->prev_state_machine != nullptr) {
  411. Ref<AnimationNodeStateMachinePlayback> prev_playback = p_state_machine->prev_state_machine->get_parameter(p_state_machine->playback);
  412. if (prev_playback.is_valid()) {
  413. StringName prev_local_to = String(prev_playback->current_transition.next).replace_first(String(p_state_machine->state_machine_name) + "/", "");
  414. if (p_state_machine->transitions[i].to == prev_local_to) {
  415. auto_advance_to = i;
  416. prev_playback->current_transition.next = StringName();
  417. break;
  418. }
  419. }
  420. }
  421. }
  422. if (p_state_machine->transitions[i].from == current && _check_advance_condition(p_state_machine, p_state_machine->transitions[i].transition)) {
  423. if (p_state_machine->transitions[i].transition->get_priority() <= priority_best) {
  424. priority_best = p_state_machine->transitions[i].transition->get_priority();
  425. auto_advance_to = i;
  426. }
  427. }
  428. }
  429. if (auto_advance_to != -1) {
  430. next = p_state_machine->transitions[auto_advance_to].local_to;
  431. Transition tr;
  432. tr.from = String(p_state_machine->state_machine_name) + "/" + String(p_state_machine->transitions[auto_advance_to].from);
  433. tr.to = String(p_state_machine->transitions[auto_advance_to].to).replace_first("../", "");
  434. tr.next = p_state_machine->transitions[auto_advance_to].to;
  435. current_transition = tr;
  436. current_curve = p_state_machine->transitions[auto_advance_to].transition->get_xfade_curve();
  437. next_xfade = p_state_machine->transitions[auto_advance_to].transition->get_xfade_time();
  438. switch_mode = p_state_machine->transitions[auto_advance_to].transition->get_switch_mode();
  439. }
  440. }
  441. if (next == p_state_machine->end_node) {
  442. AnimationNodeStateMachine *prev_state_machine = p_state_machine->prev_state_machine;
  443. if (prev_state_machine != nullptr) {
  444. Ref<AnimationNodeStateMachinePlayback> prev_playback = prev_state_machine->get_parameter(p_state_machine->playback);
  445. if (prev_playback.is_valid()) {
  446. if (next_xfade) {
  447. prev_playback->current_transition = current_transition;
  448. prev_playback->force_auto_advance = true;
  449. return rem;
  450. }
  451. float priority_best = 1e20;
  452. int auto_advance_to = -1;
  453. for (int i = 0; i < prev_state_machine->transitions.size(); i++) {
  454. if (prev_state_machine->transitions[i].transition->is_disabled()) {
  455. continue;
  456. }
  457. if (current_transition.next == prev_state_machine->end_node && _check_advance_condition(prev_state_machine, prev_state_machine->transitions[i].transition)) {
  458. if (prev_state_machine->transitions[i].transition->get_priority() <= priority_best) {
  459. priority_best = prev_state_machine->transitions[i].transition->get_priority();
  460. auto_advance_to = i;
  461. }
  462. }
  463. }
  464. if (auto_advance_to != -1) {
  465. if (prev_state_machine->transitions[auto_advance_to].transition->get_xfade_time()) {
  466. return rem;
  467. }
  468. }
  469. }
  470. }
  471. }
  472. //if next, see when to transition
  473. if (next != StringName()) {
  474. bool goto_next = false;
  475. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {
  476. goto_next = next_xfade >= (len_current - pos_current) || end_loop;
  477. if (end_loop) {
  478. next_xfade = 0;
  479. }
  480. } else {
  481. goto_next = fading_from == StringName();
  482. }
  483. if (goto_next) { //end_loop should be used because fade time may be too small or zero and animation may have looped
  484. if (next_xfade) {
  485. //time to fade, baby
  486. fading_from = current;
  487. fading_time = next_xfade;
  488. fading_pos = 0;
  489. } else {
  490. fading_from = StringName();
  491. fading_pos = 0;
  492. }
  493. if (path.size()) { //if it came from path, remove path
  494. path.remove_at(0);
  495. }
  496. { // if the current node is a state machine, update the "playing" variable to false by passing -1 in p_time
  497. Ref<AnimationNodeStateMachine> anodesm = p_state_machine->states[current].node;
  498. if (anodesm.is_valid()) {
  499. p_state_machine->blend_node(current, p_state_machine->states[current].node, -1, p_seek, p_seek_root, 0, AnimationNode::FILTER_IGNORE, true);
  500. }
  501. }
  502. current = next;
  503. if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {
  504. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, p_seek_root, 0, AnimationNode::FILTER_IGNORE, true);
  505. pos_current = MIN(pos_current, len_current);
  506. p_state_machine->blend_node(current, p_state_machine->states[current].node, pos_current, true, p_seek_root, 0, AnimationNode::FILTER_IGNORE, true);
  507. } else {
  508. len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, p_seek_root, 0, AnimationNode::FILTER_IGNORE, true);
  509. pos_current = 0;
  510. }
  511. rem = len_current; //so it does not show 0 on transition
  512. }
  513. }
  514. if (current != p_state_machine->end_node) {
  515. rem = 1; // the time remaining must always be 1 because there is no way to predict how long it takes for the entire state machine to complete
  516. } else {
  517. if (p_state_machine->prev_state_machine != nullptr) {
  518. Ref<AnimationNodeStateMachinePlayback> prev_playback = p_state_machine->prev_state_machine->get_parameter(p_state_machine->playback);
  519. if (prev_playback.is_valid()) {
  520. prev_playback->current_transition = current_transition;
  521. prev_playback->force_auto_advance = true;
  522. }
  523. }
  524. }
  525. return rem;
  526. }
  527. bool AnimationNodeStateMachinePlayback::_check_advance_condition(const Ref<AnimationNodeStateMachine> state_machine, const Ref<AnimationNodeStateMachineTransition> transition) const {
  528. if (transition->has_auto_advance()) {
  529. return true;
  530. }
  531. StringName advance_condition_name = transition->get_advance_condition_name();
  532. if (advance_condition_name != StringName() && bool(state_machine->get_parameter(advance_condition_name))) {
  533. return true;
  534. }
  535. if (transition->expression.is_valid()) {
  536. AnimationTree *tree_base = state_machine->get_animation_tree();
  537. ERR_FAIL_COND_V(tree_base == nullptr, false);
  538. NodePath advance_expression_base_node_path;
  539. if (!transition->advance_expression_base_node.is_empty()) {
  540. advance_expression_base_node_path = transition->advance_expression_base_node;
  541. } else {
  542. advance_expression_base_node_path = tree_base->get_advance_expression_base_node();
  543. }
  544. Node *expression_base = tree_base->get_node_or_null(advance_expression_base_node_path);
  545. if (expression_base) {
  546. Ref<Expression> exp = transition->expression;
  547. 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.
  548. if (!exp->has_execute_failed()) {
  549. if (ret) {
  550. return true;
  551. }
  552. }
  553. }
  554. }
  555. return false;
  556. }
  557. void AnimationNodeStateMachinePlayback::_bind_methods() {
  558. ClassDB::bind_method(D_METHOD("travel", "to_node"), &AnimationNodeStateMachinePlayback::travel);
  559. ClassDB::bind_method(D_METHOD("start", "node"), &AnimationNodeStateMachinePlayback::start);
  560. ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);
  561. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);
  562. ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);
  563. ClassDB::bind_method(D_METHOD("get_current_play_position"), &AnimationNodeStateMachinePlayback::get_current_play_pos);
  564. ClassDB::bind_method(D_METHOD("get_current_length"), &AnimationNodeStateMachinePlayback::get_current_length);
  565. ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::get_travel_path);
  566. }
  567. AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
  568. set_local_to_scene(true); //only one per instantiated scene
  569. }
  570. ///////////////////////////////////////////////////////
  571. void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
  572. r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  573. List<StringName> advance_conditions;
  574. for (int i = 0; i < transitions.size(); i++) {
  575. StringName ac = transitions[i].transition->get_advance_condition_name();
  576. if (ac != StringName() && advance_conditions.find(ac) == nullptr) {
  577. advance_conditions.push_back(ac);
  578. }
  579. }
  580. advance_conditions.sort_custom<StringName::AlphCompare>();
  581. for (const StringName &E : advance_conditions) {
  582. r_list->push_back(PropertyInfo(Variant::BOOL, E));
  583. }
  584. }
  585. Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
  586. if (p_parameter == playback) {
  587. Ref<AnimationNodeStateMachinePlayback> p;
  588. p.instantiate();
  589. return p;
  590. } else {
  591. return false; //advance condition
  592. }
  593. }
  594. void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  595. ERR_FAIL_COND(states.has(p_name));
  596. ERR_FAIL_COND(p_node.is_null());
  597. ERR_FAIL_COND(String(p_name).contains("/"));
  598. State state;
  599. state.node = p_node;
  600. state.position = p_position;
  601. states[p_name] = state;
  602. Ref<AnimationNodeStateMachine> anodesm = p_node;
  603. if (anodesm.is_valid()) {
  604. anodesm->state_machine_name = p_name;
  605. anodesm->prev_state_machine = this;
  606. }
  607. emit_changed();
  608. emit_signal(SNAME("tree_changed"));
  609. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  610. }
  611. void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
  612. ERR_FAIL_COND(states.has(p_name) == false);
  613. ERR_FAIL_COND(p_node.is_null());
  614. ERR_FAIL_COND(String(p_name).contains("/"));
  615. {
  616. Ref<AnimationNode> node = states[p_name].node;
  617. if (node.is_valid()) {
  618. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  619. }
  620. }
  621. states[p_name].node = p_node;
  622. emit_changed();
  623. emit_signal(SNAME("tree_changed"));
  624. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  625. }
  626. bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
  627. if (states.has(p_name)) {
  628. return !(states[p_name].node->is_class("AnimationNodeStartState") || states[p_name].node->is_class("AnimationNodeEndState"));
  629. }
  630. return true;
  631. }
  632. Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
  633. ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>());
  634. return states[p_name].node;
  635. }
  636. StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
  637. for (const KeyValue<StringName, State> &E : states) {
  638. if (E.value.node == p_node) {
  639. return E.key;
  640. }
  641. }
  642. ERR_FAIL_V(StringName());
  643. }
  644. void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
  645. Vector<StringName> nodes;
  646. for (const KeyValue<StringName, State> &E : states) {
  647. nodes.push_back(E.key);
  648. }
  649. nodes.sort_custom<StringName::AlphCompare>();
  650. for (int i = 0; i < nodes.size(); i++) {
  651. ChildNode cn;
  652. cn.name = nodes[i];
  653. cn.node = states[cn.name].node;
  654. r_child_nodes->push_back(cn);
  655. }
  656. }
  657. bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
  658. return states.has(p_name);
  659. }
  660. void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
  661. ERR_FAIL_COND(!states.has(p_name));
  662. if (!can_edit_node(p_name)) {
  663. return;
  664. }
  665. for (int i = 0; i < transitions.size(); i++) {
  666. if (transitions[i].local_from == p_name || transitions[i].local_to == p_name) {
  667. remove_transition_by_index(i);
  668. i--;
  669. }
  670. }
  671. {
  672. Ref<AnimationNode> node = states[p_name].node;
  673. ERR_FAIL_COND(node.is_null());
  674. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  675. }
  676. states.erase(p_name);
  677. emit_changed();
  678. emit_signal(SNAME("tree_changed"));
  679. }
  680. void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
  681. ERR_FAIL_COND(!states.has(p_name));
  682. ERR_FAIL_COND(states.has(p_new_name));
  683. ERR_FAIL_COND(!can_edit_node(p_name));
  684. states[p_new_name] = states[p_name];
  685. states.erase(p_name);
  686. Ref<AnimationNodeStateMachine> anodesm = states[p_new_name].node;
  687. if (anodesm.is_valid()) {
  688. anodesm->state_machine_name = p_new_name;
  689. }
  690. _rename_transitions(p_name, p_new_name);
  691. emit_signal("tree_changed");
  692. }
  693. void AnimationNodeStateMachine::_rename_transitions(const StringName &p_name, const StringName &p_new_name) {
  694. if (updating_transitions) {
  695. return;
  696. }
  697. updating_transitions = true;
  698. for (int i = 0; i < transitions.size(); i++) {
  699. if (transitions[i].from == p_name) {
  700. Vector<String> path = String(transitions[i].to).split("/");
  701. if (path.size() > 1) {
  702. if (path[0] == "..") {
  703. prev_state_machine->_rename_transitions(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  704. } else {
  705. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_to].node)->_rename_transitions("../" + p_name, "../" + p_new_name);
  706. }
  707. }
  708. if (transitions[i].local_from == p_name) {
  709. transitions.write[i].local_from = p_new_name;
  710. }
  711. transitions.write[i].from = p_new_name;
  712. }
  713. if (transitions[i].to == p_name) {
  714. Vector<String> path = String(transitions[i].from).split("/");
  715. if (path.size() > 1) {
  716. if (path[0] == "..") {
  717. prev_state_machine->_rename_transitions(String(state_machine_name) + "/" + p_name, String(state_machine_name) + "/" + p_new_name);
  718. } else {
  719. ((Ref<AnimationNodeStateMachine>)states[transitions[i].local_from].node)->_rename_transitions("../" + p_name, "../" + p_new_name);
  720. }
  721. }
  722. if (transitions[i].local_to == p_name) {
  723. transitions.write[i].local_to = p_new_name;
  724. }
  725. transitions.write[i].to = p_new_name;
  726. }
  727. updating_transitions = false;
  728. }
  729. }
  730. void AnimationNodeStateMachine::get_node_list(List<StringName> *r_nodes) const {
  731. List<StringName> nodes;
  732. for (const KeyValue<StringName, State> &E : states) {
  733. nodes.push_back(E.key);
  734. }
  735. nodes.sort_custom<StringName::AlphCompare>();
  736. for (const StringName &E : nodes) {
  737. r_nodes->push_back(E);
  738. }
  739. }
  740. AnimationNodeStateMachine *AnimationNodeStateMachine::get_prev_state_machine() const {
  741. return prev_state_machine;
  742. }
  743. bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
  744. StringName from = _get_shortest_path(p_from);
  745. StringName to = _get_shortest_path(p_to);
  746. for (int i = 0; i < transitions.size(); i++) {
  747. if (transitions[i].from == from && transitions[i].to == to) {
  748. return true;
  749. }
  750. }
  751. return false;
  752. }
  753. int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
  754. StringName from = _get_shortest_path(p_from);
  755. StringName to = _get_shortest_path(p_to);
  756. for (int i = 0; i < transitions.size(); i++) {
  757. if (transitions[i].from == from && transitions[i].to == to) {
  758. return i;
  759. }
  760. }
  761. return -1;
  762. }
  763. bool AnimationNodeStateMachine::_can_connect(const StringName &p_name, Vector<AnimationNodeStateMachine *> p_parents) {
  764. if (p_parents.is_empty()) {
  765. AnimationNodeStateMachine *prev = this;
  766. while (prev != nullptr) {
  767. p_parents.push_back(prev);
  768. prev = prev->prev_state_machine;
  769. }
  770. }
  771. if (states.has(p_name)) {
  772. Ref<AnimationNodeStateMachine> anodesm = states[p_name].node;
  773. if (anodesm.is_valid() && p_parents.find(anodesm.ptr()) != -1) {
  774. return false;
  775. }
  776. return true;
  777. }
  778. String name = p_name;
  779. Vector<String> path = name.split("/");
  780. if (path.size() < 2) {
  781. return false;
  782. }
  783. if (path[0] == "..") {
  784. if (prev_state_machine != nullptr) {
  785. return prev_state_machine->_can_connect(name.replace_first("../", ""), p_parents);
  786. }
  787. } else if (states.has(path[0])) {
  788. Ref<AnimationNodeStateMachine> anodesm = states[path[0]].node;
  789. if (anodesm.is_valid()) {
  790. return anodesm->_can_connect(name.replace_first(path[0] + "/", ""), p_parents);
  791. }
  792. }
  793. return false;
  794. }
  795. StringName AnimationNodeStateMachine::_get_shortest_path(const StringName &p_path) const {
  796. // If p_path is something like StateMachine/../StateMachine2/State1,
  797. // the result will be StateMachine2/State1. This avoid duplicate
  798. // transitions when using add_transition. eg, this two calls is the same:
  799. //
  800. // add_transition("State1", "StateMachine/../State2", tr)
  801. // add_transition("State1", "State2", tr)
  802. //
  803. // but the second call must be invalid because the transition already exists
  804. Vector<String> path = String(p_path).split("/");
  805. Vector<String> new_path;
  806. for (int i = 0; i < path.size(); i++) {
  807. if (i > 0 && path[i] == ".." && new_path[i - 1] != "..") {
  808. new_path.remove_at(i - 1);
  809. } else {
  810. new_path.push_back(path[i]);
  811. }
  812. }
  813. String result;
  814. for (int i = 0; i < new_path.size(); i++) {
  815. result += new_path[i] + "/";
  816. }
  817. result.remove_at(result.length() - 1);
  818. return result;
  819. }
  820. void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
  821. if (updating_transitions) {
  822. return;
  823. }
  824. StringName from = _get_shortest_path(p_from);
  825. StringName to = _get_shortest_path(p_to);
  826. Vector<String> path_from = String(from).split("/");
  827. Vector<String> path_to = String(to).split("/");
  828. ERR_FAIL_COND(from == end_node || to == start_node);
  829. ERR_FAIL_COND(from == to);
  830. ERR_FAIL_COND(!_can_connect(from));
  831. ERR_FAIL_COND(!_can_connect(to));
  832. ERR_FAIL_COND(p_transition.is_null());
  833. for (int i = 0; i < transitions.size(); i++) {
  834. ERR_FAIL_COND(transitions[i].from == from && transitions[i].to == to);
  835. }
  836. if (path_from.size() > 1 || path_to.size() > 1) {
  837. ERR_FAIL_COND(path_from[0] == path_to[0]);
  838. }
  839. updating_transitions = true;
  840. StringName local_from = String(from).get_slicec('/', 0);
  841. StringName local_to = String(to).get_slicec('/', 0);
  842. local_from = local_from == ".." ? "Start" : local_from;
  843. local_to = local_to == ".." ? "End" : local_to;
  844. Transition tr;
  845. tr.from = from;
  846. tr.to = to;
  847. tr.local_from = local_from;
  848. tr.local_to = local_to;
  849. tr.transition = p_transition;
  850. tr.transition->connect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
  851. transitions.push_back(tr);
  852. // do recursive
  853. if (path_from.size() > 1) {
  854. StringName local_path = String(from).replace_first(path_from[0] + "/", "");
  855. if (path_from[0] == "..") {
  856. prev_state_machine->add_transition(local_path, String(state_machine_name) + "/" + to, p_transition);
  857. } else {
  858. ((Ref<AnimationNodeStateMachine>)states[path_from[0]].node)->add_transition(local_path, "../" + to, p_transition);
  859. }
  860. }
  861. if (path_to.size() > 1) {
  862. StringName local_path = String(to).replace_first(path_to[0] + "/", "");
  863. if (path_to[0] == "..") {
  864. prev_state_machine->add_transition(String(state_machine_name) + "/" + from, local_path, p_transition);
  865. } else {
  866. ((Ref<AnimationNodeStateMachine>)states[path_to[0]].node)->add_transition("../" + from, local_path, p_transition);
  867. }
  868. }
  869. updating_transitions = false;
  870. }
  871. Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
  872. ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
  873. return transitions[p_transition].transition;
  874. }
  875. StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
  876. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  877. return transitions[p_transition].from;
  878. }
  879. StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
  880. ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
  881. return transitions[p_transition].to;
  882. }
  883. int AnimationNodeStateMachine::get_transition_count() const {
  884. return transitions.size();
  885. }
  886. void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
  887. StringName from = _get_shortest_path(p_from);
  888. StringName to = _get_shortest_path(p_to);
  889. for (int i = 0; i < transitions.size(); i++) {
  890. if (transitions[i].from == from && transitions[i].to == to) {
  891. remove_transition_by_index(i);
  892. return;
  893. }
  894. }
  895. }
  896. void AnimationNodeStateMachine::remove_transition_by_index(const int p_transition) {
  897. ERR_FAIL_INDEX(p_transition, transitions.size());
  898. Transition tr = transitions[p_transition];
  899. transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
  900. transitions.remove_at(p_transition);
  901. Vector<String> path_from = String(tr.from).split("/");
  902. Vector<String> path_to = String(tr.to).split("/");
  903. List<Vector<String>> paths;
  904. paths.push_back(path_from);
  905. paths.push_back(path_to);
  906. for (List<Vector<String>>::Element *E = paths.front(); E; E = E->next()) {
  907. if (E->get()[0].size() > 1) {
  908. if (E->get()[0] == "..") {
  909. prev_state_machine->_remove_transition(tr.transition);
  910. } else if (states.has(E->get()[0])) {
  911. Ref<AnimationNodeStateMachine> anodesm = states[E->get()[0]].node;
  912. if (anodesm.is_valid()) {
  913. anodesm->_remove_transition(tr.transition);
  914. }
  915. }
  916. }
  917. }
  918. }
  919. void AnimationNodeStateMachine::_remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition) {
  920. for (int i = 0; i < transitions.size(); i++) {
  921. if (transitions[i].transition == p_transition) {
  922. remove_transition_by_index(i);
  923. return;
  924. }
  925. }
  926. }
  927. void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
  928. graph_offset = p_offset;
  929. }
  930. Vector2 AnimationNodeStateMachine::get_graph_offset() const {
  931. return graph_offset;
  932. }
  933. double AnimationNodeStateMachine::process(double p_time, bool p_seek, bool p_seek_root) {
  934. Ref<AnimationNodeStateMachinePlayback> playback = get_parameter(this->playback);
  935. ERR_FAIL_COND_V(playback.is_null(), 0.0);
  936. return playback->process(this, p_time, p_seek, p_seek_root);
  937. }
  938. String AnimationNodeStateMachine::get_caption() const {
  939. return "StateMachine";
  940. }
  941. bool AnimationNodeStateMachine::has_local_transition(const StringName &p_from, const StringName &p_to) const {
  942. StringName from = _get_shortest_path(p_from);
  943. StringName to = _get_shortest_path(p_to);
  944. for (int i = 0; i < transitions.size(); i++) {
  945. if (transitions[i].local_from == from && transitions[i].local_to == to) {
  946. return true;
  947. }
  948. }
  949. return false;
  950. }
  951. Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) {
  952. return get_node(p_name);
  953. }
  954. bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
  955. String name = p_name;
  956. if (name.begins_with("states/")) {
  957. String node_name = name.get_slicec('/', 1);
  958. String what = name.get_slicec('/', 2);
  959. if (what == "node") {
  960. Ref<AnimationNode> anode = p_value;
  961. if (anode.is_valid()) {
  962. add_node(node_name, p_value);
  963. }
  964. return true;
  965. }
  966. if (what == "position") {
  967. if (states.has(node_name)) {
  968. states[node_name].position = p_value;
  969. }
  970. return true;
  971. }
  972. } else if (name == "transitions") {
  973. Array trans = p_value;
  974. ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
  975. for (int i = 0; i < trans.size(); i += 3) {
  976. add_transition(trans[i], trans[i + 1], trans[i + 2]);
  977. }
  978. return true;
  979. } else if (name == "graph_offset") {
  980. set_graph_offset(p_value);
  981. return true;
  982. }
  983. return false;
  984. }
  985. bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
  986. String name = p_name;
  987. if (name.begins_with("states/")) {
  988. String node_name = name.get_slicec('/', 1);
  989. String what = name.get_slicec('/', 2);
  990. if (what == "node") {
  991. if (states.has(node_name) && can_edit_node(node_name)) {
  992. r_ret = states[node_name].node;
  993. return true;
  994. }
  995. }
  996. if (what == "position") {
  997. if (states.has(node_name)) {
  998. r_ret = states[node_name].position;
  999. return true;
  1000. }
  1001. }
  1002. } else if (name == "transitions") {
  1003. Array trans;
  1004. for (int i = 0; i < transitions.size(); i++) {
  1005. String from = transitions[i].from;
  1006. String to = transitions[i].to;
  1007. if (from.get_slicec('/', 0) == ".." || to.get_slicec('/', 0) == "..") {
  1008. continue;
  1009. }
  1010. trans.push_back(from);
  1011. trans.push_back(to);
  1012. trans.push_back(transitions[i].transition);
  1013. }
  1014. r_ret = trans;
  1015. return true;
  1016. } else if (name == "graph_offset") {
  1017. r_ret = get_graph_offset();
  1018. return true;
  1019. }
  1020. return false;
  1021. }
  1022. void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
  1023. List<StringName> names;
  1024. for (const KeyValue<StringName, State> &E : states) {
  1025. names.push_back(E.key);
  1026. }
  1027. names.sort_custom<StringName::AlphCompare>();
  1028. for (const StringName &name : names) {
  1029. p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  1030. p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1031. }
  1032. p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1033. p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1034. }
  1035. void AnimationNodeStateMachine::reset_state() {
  1036. states.clear();
  1037. transitions.clear();
  1038. playback = "playback";
  1039. start_node = "Start";
  1040. end_node = "End";
  1041. graph_offset = Vector2();
  1042. Ref<AnimationNodeStartState> s;
  1043. s.instantiate();
  1044. State start;
  1045. start.node = s;
  1046. start.position = Vector2(200, 100);
  1047. states[start_node] = start;
  1048. Ref<AnimationNodeEndState> e;
  1049. e.instantiate();
  1050. State end;
  1051. end.node = e;
  1052. end.position = Vector2(900, 100);
  1053. states[end_node] = end;
  1054. emit_changed();
  1055. emit_signal(SNAME("tree_changed"));
  1056. }
  1057. void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
  1058. ERR_FAIL_COND(!states.has(p_name));
  1059. states[p_name].position = p_position;
  1060. }
  1061. Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
  1062. ERR_FAIL_COND_V(!states.has(p_name), Vector2());
  1063. return states[p_name].position;
  1064. }
  1065. void AnimationNodeStateMachine::_tree_changed() {
  1066. emit_signal(SNAME("tree_changed"));
  1067. }
  1068. void AnimationNodeStateMachine::_bind_methods() {
  1069. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
  1070. ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
  1071. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
  1072. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
  1073. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
  1074. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
  1075. ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
  1076. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
  1077. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
  1078. ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
  1079. ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
  1080. ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
  1081. ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
  1082. ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
  1083. ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
  1084. ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
  1085. ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
  1086. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
  1087. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
  1088. }
  1089. AnimationNodeStateMachine::AnimationNodeStateMachine() {
  1090. Ref<AnimationNodeStartState> s;
  1091. s.instantiate();
  1092. State start;
  1093. start.node = s;
  1094. start.position = Vector2(200, 100);
  1095. states[start_node] = start;
  1096. Ref<AnimationNodeEndState> e;
  1097. e.instantiate();
  1098. State end;
  1099. end.node = e;
  1100. end.position = Vector2(900, 100);
  1101. states[end_node] = end;
  1102. }