Debian Codegraph @

scene/animation/animation_node_state_machine.cpp

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