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