Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/animation/animation_node_state_machine.cpp
9896 views
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_fade_from_play_pos() const {
338
return fadeing_from_nti.position;
339
}
340
341
float AnimationNodeStateMachinePlayback::get_fade_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_travel_path"), &AnimationNodeStateMachinePlayback::_get_travel_path);
1219
1220
ADD_SIGNAL(MethodInfo(SceneStringName(state_started), PropertyInfo(Variant::STRING_NAME, "state")));
1221
ADD_SIGNAL(MethodInfo(SceneStringName(state_finished), PropertyInfo(Variant::STRING_NAME, "state")));
1222
}
1223
1224
AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {
1225
set_local_to_scene(true); // Only one per instantiated scene.
1226
default_transition.instantiate();
1227
default_transition->set_xfade_time(0);
1228
default_transition->set_reset(true);
1229
default_transition->set_advance_mode(AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO);
1230
default_transition->set_switch_mode(AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE);
1231
}
1232
1233
///////////////////////////////////////////////////////
1234
1235
void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {
1236
AnimationNode::get_parameter_list(r_list);
1237
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.
1238
List<StringName> advance_conditions;
1239
for (int i = 0; i < transitions.size(); i++) {
1240
StringName ac = transitions[i].transition->get_advance_condition_name();
1241
if (ac != StringName() && advance_conditions.find(ac) == nullptr) {
1242
advance_conditions.push_back(ac);
1243
}
1244
}
1245
1246
advance_conditions.sort_custom<StringName::AlphCompare>();
1247
for (const StringName &E : advance_conditions) {
1248
r_list->push_back(PropertyInfo(Variant::BOOL, E));
1249
}
1250
}
1251
1252
Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {
1253
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
1254
if (ret != Variant()) {
1255
return ret;
1256
}
1257
1258
if (p_parameter == playback) {
1259
Ref<AnimationNodeStateMachinePlayback> p;
1260
p.instantiate();
1261
return p;
1262
} else {
1263
return false; // Advance condition.
1264
}
1265
}
1266
1267
bool AnimationNodeStateMachine::is_parameter_read_only(const StringName &p_parameter) const {
1268
if (AnimationNode::is_parameter_read_only(p_parameter)) {
1269
return true;
1270
}
1271
1272
if (p_parameter == playback) {
1273
return true;
1274
}
1275
return false;
1276
}
1277
1278
void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
1279
ERR_FAIL_COND(states.has(p_name));
1280
ERR_FAIL_COND(p_node.is_null());
1281
ERR_FAIL_COND(String(p_name).contains_char('/'));
1282
1283
State state_new;
1284
state_new.node = p_node;
1285
state_new.position = p_position;
1286
1287
states[p_name] = state_new;
1288
1289
emit_changed();
1290
emit_signal(SNAME("tree_changed"));
1291
1292
p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
1293
p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
1294
p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
1295
}
1296
1297
void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {
1298
ERR_FAIL_COND(states.has(p_name) == false);
1299
ERR_FAIL_COND(p_node.is_null());
1300
ERR_FAIL_COND(String(p_name).contains_char('/'));
1301
1302
{
1303
Ref<AnimationNode> node = states[p_name].node;
1304
if (node.is_valid()) {
1305
node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
1306
node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));
1307
node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));
1308
}
1309
}
1310
1311
states[p_name].node = p_node;
1312
1313
emit_changed();
1314
emit_signal(SNAME("tree_changed"));
1315
1316
p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
1317
p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
1318
p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
1319
}
1320
1321
void AnimationNodeStateMachine::set_state_machine_type(StateMachineType p_state_machine_type) {
1322
state_machine_type = p_state_machine_type;
1323
emit_changed();
1324
emit_signal(SNAME("tree_changed"));
1325
notify_property_list_changed();
1326
}
1327
1328
AnimationNodeStateMachine::StateMachineType AnimationNodeStateMachine::get_state_machine_type() const {
1329
return state_machine_type;
1330
}
1331
1332
void AnimationNodeStateMachine::set_allow_transition_to_self(bool p_enable) {
1333
allow_transition_to_self = p_enable;
1334
}
1335
1336
bool AnimationNodeStateMachine::is_allow_transition_to_self() const {
1337
return allow_transition_to_self;
1338
}
1339
1340
void AnimationNodeStateMachine::set_reset_ends(bool p_enable) {
1341
reset_ends = p_enable;
1342
}
1343
1344
bool AnimationNodeStateMachine::are_ends_reset() const {
1345
return reset_ends;
1346
}
1347
1348
bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {
1349
if (states.has(p_name)) {
1350
const AnimationNode *anode = states[p_name].node.ptr();
1351
return !(Object::cast_to<AnimationNodeStartState>(anode) || Object::cast_to<AnimationNodeEndState>(anode));
1352
}
1353
1354
return true;
1355
}
1356
1357
Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {
1358
ERR_FAIL_COND_V_EDMSG(!states.has(p_name), Ref<AnimationNode>(), String(p_name) + " is not found current state.");
1359
1360
return states[p_name].node;
1361
}
1362
1363
StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {
1364
for (const KeyValue<StringName, State> &E : states) {
1365
if (E.value.node == p_node) {
1366
return E.key;
1367
}
1368
}
1369
1370
ERR_FAIL_V(StringName());
1371
}
1372
1373
void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {
1374
Vector<StringName> nodes;
1375
1376
for (const KeyValue<StringName, State> &E : states) {
1377
nodes.push_back(E.key);
1378
}
1379
1380
nodes.sort_custom<StringName::AlphCompare>();
1381
1382
for (int i = 0; i < nodes.size(); i++) {
1383
ChildNode cn;
1384
cn.name = nodes[i];
1385
cn.node = states[cn.name].node;
1386
r_child_nodes->push_back(cn);
1387
}
1388
}
1389
1390
bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {
1391
return states.has(p_name);
1392
}
1393
1394
void AnimationNodeStateMachine::remove_node(const StringName &p_name) {
1395
ERR_FAIL_COND(!states.has(p_name));
1396
1397
if (!can_edit_node(p_name)) {
1398
return;
1399
}
1400
1401
for (int i = 0; i < transitions.size(); i++) {
1402
if (transitions[i].from == p_name || transitions[i].to == p_name) {
1403
remove_transition_by_index(i);
1404
i--;
1405
}
1406
}
1407
1408
{
1409
Ref<AnimationNode> node = states[p_name].node;
1410
ERR_FAIL_COND(node.is_null());
1411
node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
1412
node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));
1413
node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));
1414
}
1415
1416
states.erase(p_name);
1417
1418
emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);
1419
emit_changed();
1420
emit_signal(SNAME("tree_changed"));
1421
}
1422
1423
void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {
1424
ERR_FAIL_COND(!states.has(p_name));
1425
ERR_FAIL_COND(states.has(p_new_name));
1426
ERR_FAIL_COND(!can_edit_node(p_name));
1427
1428
states[p_new_name] = states[p_name];
1429
states.erase(p_name);
1430
1431
_rename_transitions(p_name, p_new_name);
1432
1433
emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);
1434
emit_changed();
1435
emit_signal(SNAME("tree_changed"));
1436
}
1437
1438
void AnimationNodeStateMachine::_rename_transitions(const StringName &p_name, const StringName &p_new_name) {
1439
if (updating_transitions) {
1440
return;
1441
}
1442
1443
updating_transitions = true;
1444
for (int i = 0; i < transitions.size(); i++) {
1445
if (transitions[i].from == p_name) {
1446
transitions.write[i].from = p_new_name;
1447
}
1448
if (transitions[i].to == p_name) {
1449
transitions.write[i].to = p_new_name;
1450
}
1451
}
1452
updating_transitions = false;
1453
}
1454
1455
LocalVector<StringName> AnimationNodeStateMachine::get_node_list() const {
1456
LocalVector<StringName> nodes;
1457
nodes.reserve(states.size());
1458
for (const KeyValue<StringName, State> &E : states) {
1459
nodes.push_back(E.key);
1460
}
1461
nodes.sort_custom<StringName::AlphCompare>();
1462
return nodes;
1463
}
1464
1465
TypedArray<StringName> AnimationNodeStateMachine::get_node_list_as_typed_array() const {
1466
TypedArray<StringName> typed_arr;
1467
LocalVector<StringName> vec = get_node_list();
1468
typed_arr.resize(vec.size());
1469
for (uint32_t i = 0; i < vec.size(); i++) {
1470
typed_arr[i] = vec[i];
1471
}
1472
return typed_arr;
1473
}
1474
1475
bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {
1476
for (int i = 0; i < transitions.size(); i++) {
1477
if (transitions[i].from == p_from && transitions[i].to == p_to) {
1478
return true;
1479
}
1480
}
1481
return false;
1482
}
1483
1484
bool AnimationNodeStateMachine::has_transition_from(const StringName &p_from) const {
1485
for (int i = 0; i < transitions.size(); i++) {
1486
if (transitions[i].from == p_from) {
1487
return true;
1488
}
1489
}
1490
return false;
1491
}
1492
1493
bool AnimationNodeStateMachine::has_transition_to(const StringName &p_to) const {
1494
for (int i = 0; i < transitions.size(); i++) {
1495
if (transitions[i].to == p_to) {
1496
return true;
1497
}
1498
}
1499
return false;
1500
}
1501
1502
int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {
1503
for (int i = 0; i < transitions.size(); i++) {
1504
if (transitions[i].from == p_from && transitions[i].to == p_to) {
1505
return i;
1506
}
1507
}
1508
return -1;
1509
}
1510
1511
Vector<int> AnimationNodeStateMachine::find_transition_from(const StringName &p_from) const {
1512
Vector<int> ret;
1513
for (int i = 0; i < transitions.size(); i++) {
1514
if (transitions[i].from == p_from) {
1515
ret.push_back(i);
1516
}
1517
}
1518
return ret;
1519
}
1520
1521
Vector<int> AnimationNodeStateMachine::find_transition_to(const StringName &p_to) const {
1522
Vector<int> ret;
1523
for (int i = 0; i < transitions.size(); i++) {
1524
if (transitions[i].to == p_to) {
1525
ret.push_back(i);
1526
}
1527
}
1528
return ret;
1529
}
1530
1531
bool AnimationNodeStateMachine::_can_connect(const StringName &p_name) {
1532
if (states.has(p_name)) {
1533
return true;
1534
}
1535
1536
String node_name = p_name;
1537
Vector<String> path = node_name.split("/");
1538
1539
if (path.size() < 2) {
1540
return false;
1541
}
1542
1543
return false;
1544
}
1545
1546
void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {
1547
if (updating_transitions) {
1548
return;
1549
}
1550
1551
ERR_FAIL_COND(p_from == SceneStringName(End) || p_to == SceneStringName(Start));
1552
ERR_FAIL_COND(p_from == p_to);
1553
ERR_FAIL_COND(!_can_connect(p_from));
1554
ERR_FAIL_COND(!_can_connect(p_to));
1555
ERR_FAIL_COND(p_transition.is_null());
1556
1557
for (int i = 0; i < transitions.size(); i++) {
1558
ERR_FAIL_COND(transitions[i].from == p_from && transitions[i].to == p_to);
1559
}
1560
1561
updating_transitions = true;
1562
1563
Transition tr;
1564
tr.from = p_from;
1565
tr.to = p_to;
1566
tr.transition = p_transition;
1567
1568
tr.transition->connect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);
1569
1570
transitions.push_back(tr);
1571
1572
updating_transitions = false;
1573
}
1574
1575
Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {
1576
ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());
1577
return transitions[p_transition].transition;
1578
}
1579
1580
StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {
1581
ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
1582
return transitions[p_transition].from;
1583
}
1584
1585
StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {
1586
ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());
1587
return transitions[p_transition].to;
1588
}
1589
1590
bool AnimationNodeStateMachine::is_transition_across_group(int p_transition) const {
1591
ERR_FAIL_INDEX_V(p_transition, transitions.size(), false);
1592
if (get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
1593
if (transitions[p_transition].from == SceneStringName(Start) || transitions[p_transition].to == SceneStringName(End)) {
1594
return true;
1595
}
1596
}
1597
return false;
1598
}
1599
1600
int AnimationNodeStateMachine::get_transition_count() const {
1601
return transitions.size();
1602
}
1603
1604
void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {
1605
for (int i = 0; i < transitions.size(); i++) {
1606
if (transitions[i].from == p_from && transitions[i].to == p_to) {
1607
remove_transition_by_index(i);
1608
return;
1609
}
1610
}
1611
}
1612
1613
void AnimationNodeStateMachine::remove_transition_by_index(const int p_transition) {
1614
ERR_FAIL_INDEX(p_transition, transitions.size());
1615
transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));
1616
transitions.remove_at(p_transition);
1617
}
1618
1619
void AnimationNodeStateMachine::_remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition) {
1620
for (int i = 0; i < transitions.size(); i++) {
1621
if (transitions[i].transition == p_transition) {
1622
remove_transition_by_index(i);
1623
return;
1624
}
1625
}
1626
}
1627
1628
void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {
1629
graph_offset = p_offset;
1630
}
1631
1632
Vector2 AnimationNodeStateMachine::get_graph_offset() const {
1633
return graph_offset;
1634
}
1635
1636
AnimationNode::NodeTimeInfo AnimationNodeStateMachine::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
1637
Ref<AnimationNodeStateMachinePlayback> playback_new = get_parameter(playback);
1638
ERR_FAIL_COND_V(playback_new.is_null(), AnimationNode::NodeTimeInfo());
1639
playback_new->_set_base_path(node_state.get_base_path());
1640
playback_new->_set_grouped(state_machine_type == STATE_MACHINE_TYPE_GROUPED);
1641
if (p_test_only) {
1642
playback_new = playback_new->duplicate(); // Don't process original when testing.
1643
}
1644
1645
return playback_new->process(this, p_playback_info, p_test_only);
1646
}
1647
1648
String AnimationNodeStateMachine::get_caption() const {
1649
return "StateMachine";
1650
}
1651
1652
Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) const {
1653
return get_node(p_name);
1654
}
1655
1656
bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {
1657
String prop_name = p_name;
1658
if (prop_name.begins_with("states/")) {
1659
String node_name = prop_name.get_slicec('/', 1);
1660
String what = prop_name.get_slicec('/', 2);
1661
1662
if (what == "node") {
1663
Ref<AnimationNode> anode = p_value;
1664
if (anode.is_valid()) {
1665
add_node(node_name, p_value);
1666
}
1667
return true;
1668
}
1669
1670
if (what == "position") {
1671
if (states.has(node_name)) {
1672
states[node_name].position = p_value;
1673
}
1674
return true;
1675
}
1676
} else if (prop_name == "transitions") {
1677
Array trans = p_value;
1678
ERR_FAIL_COND_V(trans.size() % 3 != 0, false);
1679
1680
for (int i = 0; i < trans.size(); i += 3) {
1681
add_transition(trans[i], trans[i + 1], trans[i + 2]);
1682
}
1683
return true;
1684
} else if (prop_name == "graph_offset") {
1685
set_graph_offset(p_value);
1686
return true;
1687
}
1688
1689
return false;
1690
}
1691
1692
bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {
1693
String prop_name = p_name;
1694
if (prop_name.begins_with("states/")) {
1695
String node_name = prop_name.get_slicec('/', 1);
1696
String what = prop_name.get_slicec('/', 2);
1697
1698
if (what == "node") {
1699
if (states.has(node_name) && can_edit_node(node_name)) {
1700
r_ret = states[node_name].node;
1701
return true;
1702
}
1703
}
1704
1705
if (what == "position") {
1706
if (states.has(node_name)) {
1707
r_ret = states[node_name].position;
1708
return true;
1709
}
1710
}
1711
} else if (prop_name == "transitions") {
1712
Array trans;
1713
for (int i = 0; i < transitions.size(); i++) {
1714
String from = transitions[i].from;
1715
String to = transitions[i].to;
1716
1717
trans.push_back(from);
1718
trans.push_back(to);
1719
trans.push_back(transitions[i].transition);
1720
}
1721
1722
r_ret = trans;
1723
return true;
1724
} else if (prop_name == "graph_offset") {
1725
r_ret = get_graph_offset();
1726
return true;
1727
}
1728
1729
return false;
1730
}
1731
1732
void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {
1733
LocalVector<StringName> names;
1734
names.reserve(states.size());
1735
for (const KeyValue<StringName, State> &E : states) {
1736
names.push_back(E.key);
1737
}
1738
names.sort_custom<StringName::AlphCompare>();
1739
1740
for (const StringName &prop_name : names) {
1741
p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
1742
p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
1743
}
1744
1745
p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
1746
p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
1747
}
1748
1749
void AnimationNodeStateMachine::_validate_property(PropertyInfo &p_property) const {
1750
if (p_property.name == "allow_transition_to_self" || p_property.name == "reset_ends") {
1751
if (state_machine_type == STATE_MACHINE_TYPE_GROUPED) {
1752
p_property.usage = PROPERTY_USAGE_NONE;
1753
}
1754
}
1755
}
1756
1757
void AnimationNodeStateMachine::reset_state() {
1758
states.clear();
1759
transitions.clear();
1760
playback = "playback";
1761
graph_offset = Vector2();
1762
1763
Ref<AnimationNodeStartState> s;
1764
s.instantiate();
1765
State start;
1766
start.node = s;
1767
start.position = Vector2(200, 100);
1768
states[SceneStringName(Start)] = start;
1769
1770
Ref<AnimationNodeEndState> e;
1771
e.instantiate();
1772
State end;
1773
end.node = e;
1774
end.position = Vector2(900, 100);
1775
states[SceneStringName(End)] = end;
1776
1777
emit_changed();
1778
emit_signal(SNAME("tree_changed"));
1779
}
1780
1781
void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {
1782
ERR_FAIL_COND(!states.has(p_name));
1783
states[p_name].position = p_position;
1784
}
1785
1786
Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {
1787
ERR_FAIL_COND_V(!states.has(p_name), Vector2());
1788
return states[p_name].position;
1789
}
1790
1791
void AnimationNodeStateMachine::_tree_changed() {
1792
emit_changed();
1793
AnimationRootNode::_tree_changed();
1794
}
1795
1796
void AnimationNodeStateMachine::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
1797
AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
1798
}
1799
1800
void AnimationNodeStateMachine::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
1801
AnimationRootNode::_animation_node_removed(p_oid, p_node);
1802
}
1803
1804
#ifdef TOOLS_ENABLED
1805
void AnimationNodeStateMachine::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
1806
const String pf = p_function;
1807
bool add_state_options = false;
1808
if (p_idx == 0) {
1809
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");
1810
} else if (p_idx <= 1) {
1811
add_state_options = (pf == "has_transition" || pf == "add_transition" || pf == "remove_transition");
1812
}
1813
if (add_state_options) {
1814
for (const KeyValue<StringName, State> &E : states) {
1815
r_options->push_back(String(E.key).quote());
1816
}
1817
}
1818
AnimationRootNode::get_argument_options(p_function, p_idx, r_options);
1819
}
1820
#endif
1821
1822
void AnimationNodeStateMachine::_bind_methods() {
1823
ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));
1824
ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);
1825
ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);
1826
ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);
1827
ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);
1828
ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);
1829
ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);
1830
ClassDB::bind_method(D_METHOD("get_node_list"), &AnimationNodeStateMachine::get_node_list_as_typed_array);
1831
1832
ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);
1833
ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);
1834
1835
ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);
1836
ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);
1837
ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);
1838
ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);
1839
ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);
1840
ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);
1841
ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);
1842
ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);
1843
1844
ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);
1845
ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);
1846
1847
ClassDB::bind_method(D_METHOD("set_state_machine_type", "state_machine_type"), &AnimationNodeStateMachine::set_state_machine_type);
1848
ClassDB::bind_method(D_METHOD("get_state_machine_type"), &AnimationNodeStateMachine::get_state_machine_type);
1849
1850
ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeStateMachine::set_allow_transition_to_self);
1851
ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeStateMachine::is_allow_transition_to_self);
1852
1853
ClassDB::bind_method(D_METHOD("set_reset_ends", "enable"), &AnimationNodeStateMachine::set_reset_ends);
1854
ClassDB::bind_method(D_METHOD("are_ends_reset"), &AnimationNodeStateMachine::are_ends_reset);
1855
1856
ADD_PROPERTY(PropertyInfo(Variant::INT, "state_machine_type", PROPERTY_HINT_ENUM, "Root,Nested,Grouped"), "set_state_machine_type", "get_state_machine_type");
1857
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
1858
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_ends"), "set_reset_ends", "are_ends_reset");
1859
1860
BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_ROOT);
1861
BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_NESTED);
1862
BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_GROUPED);
1863
}
1864
1865
Vector<StringName> AnimationNodeStateMachine::get_nodes_with_transitions_from(const StringName &p_node) const {
1866
Vector<StringName> result;
1867
for (const Transition &transition : transitions) {
1868
if (transition.from == p_node) {
1869
result.push_back(transition.to);
1870
}
1871
}
1872
return result;
1873
}
1874
1875
Vector<StringName> AnimationNodeStateMachine::get_nodes_with_transitions_to(const StringName &p_node) const {
1876
Vector<StringName> result;
1877
for (const Transition &transition : transitions) {
1878
if (transition.to == p_node) {
1879
result.push_back(transition.from);
1880
}
1881
}
1882
return result;
1883
}
1884
1885
AnimationNodeStateMachine::AnimationNodeStateMachine() {
1886
Ref<AnimationNodeStartState> s;
1887
s.instantiate();
1888
State start;
1889
start.node = s;
1890
start.position = Vector2(200, 100);
1891
states[SceneStringName(Start)] = start;
1892
1893
Ref<AnimationNodeEndState> e;
1894
e.instantiate();
1895
State end;
1896
end.node = e;
1897
end.position = Vector2(900, 100);
1898
states[SceneStringName(End)] = end;
1899
}
1900
1901