Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/animation/animation_blend_tree.cpp
9898 views
1
/**************************************************************************/
2
/* animation_blend_tree.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_blend_tree.h"
32
33
#include "scene/resources/animation.h"
34
35
void AnimationNodeAnimation::set_animation(const StringName &p_name) {
36
animation = p_name;
37
}
38
39
StringName AnimationNodeAnimation::get_animation() const {
40
return animation;
41
}
42
43
Vector<String> (*AnimationNodeAnimation::get_editable_animation_list)() = nullptr;
44
45
void AnimationNodeAnimation::get_parameter_list(List<PropertyInfo> *r_list) const {
46
AnimationNode::get_parameter_list(r_list);
47
r_list->push_back(PropertyInfo(Variant::BOOL, backward, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
48
}
49
50
Variant AnimationNodeAnimation::get_parameter_default_value(const StringName &p_parameter) const {
51
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
52
if (ret != Variant()) {
53
return ret;
54
}
55
if (p_parameter == backward) {
56
return false;
57
}
58
return 0.0;
59
}
60
61
AnimationNode::NodeTimeInfo AnimationNodeAnimation::get_node_time_info() const {
62
NodeTimeInfo nti;
63
if (!process_state->tree->has_animation(animation)) {
64
return nti;
65
}
66
67
if (use_custom_timeline) {
68
nti.length = timeline_length;
69
nti.loop_mode = loop_mode;
70
} else {
71
Ref<Animation> anim = process_state->tree->get_animation(animation);
72
nti.length = (double)anim->get_length();
73
nti.loop_mode = anim->get_loop_mode();
74
}
75
nti.position = get_parameter(current_position);
76
77
return nti;
78
}
79
80
void AnimationNodeAnimation::_validate_property(PropertyInfo &p_property) const {
81
if (Engine::get_singleton()->is_editor_hint() && p_property.name == "animation" && get_editable_animation_list) {
82
Vector<String> names = get_editable_animation_list();
83
String anims;
84
for (int i = 0; i < names.size(); i++) {
85
if (i > 0) {
86
anims += ",";
87
}
88
anims += String(names[i]);
89
}
90
if (!anims.is_empty()) {
91
p_property.hint = PROPERTY_HINT_ENUM;
92
p_property.hint_string = anims;
93
}
94
}
95
96
if (!use_custom_timeline) {
97
if (p_property.name == "timeline_length" || p_property.name == "start_offset" || p_property.name == "loop_mode" || p_property.name == "stretch_time_scale") {
98
p_property.usage = PROPERTY_USAGE_NONE;
99
}
100
}
101
}
102
103
AnimationNode::NodeTimeInfo AnimationNodeAnimation::process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
104
process_state->is_testing = p_test_only;
105
106
AnimationMixer::PlaybackInfo pi = p_playback_info;
107
if (p_playback_info.seeked) {
108
if (p_playback_info.is_external_seeking) {
109
pi.delta = get_node_time_info().position - p_playback_info.time;
110
}
111
} else {
112
pi.time = get_node_time_info().position + (get_parameter(backward) ? -p_playback_info.delta : p_playback_info.delta);
113
}
114
115
NodeTimeInfo nti = _process(pi, p_test_only);
116
117
if (!p_test_only) {
118
set_node_time_info(nti);
119
}
120
121
return nti;
122
}
123
124
AnimationNode::NodeTimeInfo AnimationNodeAnimation::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
125
if (!process_state->tree->has_animation(animation)) {
126
AnimationNodeBlendTree *tree = Object::cast_to<AnimationNodeBlendTree>(node_state.parent);
127
if (tree) {
128
String node_name = tree->get_node_name(Ref<AnimationNodeAnimation>(this));
129
make_invalid(vformat(RTR("On BlendTree node '%s', animation not found: '%s'"), node_name, animation));
130
131
} else {
132
make_invalid(vformat(RTR("Animation not found: '%s'"), animation));
133
}
134
135
return NodeTimeInfo();
136
}
137
138
Ref<Animation> anim = process_state->tree->get_animation(animation);
139
double anim_size = (double)anim->get_length();
140
141
NodeTimeInfo cur_nti = get_node_time_info();
142
double cur_len = cur_nti.length;
143
double cur_time = p_playback_info.time;
144
double cur_delta = p_playback_info.delta;
145
bool cur_backward = get_parameter(backward);
146
147
Animation::LoopMode cur_loop_mode = cur_nti.loop_mode;
148
double prev_time = cur_nti.position;
149
150
Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
151
bool node_backward = play_mode == PLAY_MODE_BACKWARD;
152
153
bool p_seek = p_playback_info.seeked;
154
bool p_is_external_seeking = p_playback_info.is_external_seeking;
155
156
// 1. Progress for AnimationNode.
157
bool will_end = Animation::is_greater_or_equal_approx(cur_time + cur_delta, cur_len);
158
bool is_started = p_seek && !p_is_external_seeking && Math::is_zero_approx(cur_time);
159
160
// 1. Progress for AnimationNode.
161
if (is_started && advance_on_start) {
162
cur_time = cur_delta;
163
}
164
if (cur_loop_mode != Animation::LOOP_NONE) {
165
if (cur_loop_mode == Animation::LOOP_LINEAR) {
166
if (!Math::is_zero_approx(cur_len)) {
167
cur_time = Math::fposmod(cur_time, cur_len);
168
}
169
cur_backward = false;
170
} else {
171
if (!Math::is_zero_approx(cur_len)) {
172
if (Animation::is_greater_or_equal_approx(prev_time, 0) && Animation::is_less_approx(cur_time, 0)) {
173
cur_backward = !cur_backward;
174
} else if (Animation::is_less_or_equal_approx(prev_time, cur_len) && Animation::is_greater_approx(cur_time, cur_len)) {
175
cur_backward = !cur_backward;
176
}
177
cur_time = Math::pingpong(cur_time, cur_len);
178
}
179
}
180
} else {
181
if (Animation::is_less_approx(cur_time, 0)) {
182
cur_delta += cur_time;
183
cur_time = 0;
184
} else if (Animation::is_greater_approx(cur_time, cur_len)) {
185
cur_delta += cur_time - cur_len;
186
cur_time = cur_len;
187
}
188
cur_backward = false;
189
// If ended, don't progress AnimationNode. So set delta to 0.
190
if (!Math::is_zero_approx(cur_delta)) {
191
if (play_mode == PLAY_MODE_FORWARD) {
192
if (Animation::is_greater_or_equal_approx(prev_time, cur_len)) {
193
cur_delta = 0;
194
}
195
} else {
196
if (Animation::is_less_or_equal_approx(prev_time, 0)) {
197
cur_delta = 0;
198
}
199
}
200
}
201
}
202
203
// 2. For return, store "AnimationNode" time info here, not "Animation" time info as below.
204
NodeTimeInfo nti;
205
nti.length = cur_len;
206
nti.position = cur_time;
207
nti.delta = cur_delta;
208
nti.loop_mode = cur_loop_mode;
209
nti.will_end = will_end;
210
211
// 3. Progress for Animation.
212
double prev_playback_time = prev_time + start_offset;
213
double cur_playback_time = cur_time + start_offset;
214
if (stretch_time_scale) {
215
double mlt = anim_size / cur_len;
216
prev_playback_time *= mlt;
217
cur_playback_time *= mlt;
218
cur_delta *= mlt;
219
}
220
if (cur_loop_mode == Animation::LOOP_LINEAR) {
221
if (!Math::is_zero_approx(anim_size)) {
222
prev_playback_time = Math::fposmod(prev_playback_time, anim_size);
223
cur_playback_time = Math::fposmod(cur_playback_time, anim_size);
224
if (Animation::is_greater_or_equal_approx(prev_playback_time, 0) && Animation::is_less_approx(cur_playback_time, 0)) {
225
looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START;
226
}
227
if (Animation::is_less_or_equal_approx(prev_playback_time, anim_size) && Animation::is_greater_approx(cur_playback_time, anim_size)) {
228
looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END;
229
}
230
}
231
} else if (cur_loop_mode == Animation::LOOP_PINGPONG) {
232
if (!Math::is_zero_approx(anim_size)) {
233
if (Animation::is_greater_or_equal_approx(Math::fposmod(cur_playback_time, anim_size * 2.0), anim_size)) {
234
cur_delta = -cur_delta; // Needed for retrieving discrete keys correctly.
235
}
236
prev_playback_time = Math::pingpong(prev_playback_time, anim_size);
237
cur_playback_time = Math::pingpong(cur_playback_time, anim_size);
238
if (Animation::is_greater_or_equal_approx(prev_playback_time, 0) && Animation::is_less_approx(cur_playback_time, 0)) {
239
looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START;
240
}
241
if (Animation::is_less_or_equal_approx(prev_playback_time, anim_size) && Animation::is_greater_approx(cur_playback_time, anim_size)) {
242
looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END;
243
}
244
}
245
} else {
246
if (Animation::is_less_approx(cur_playback_time, 0)) {
247
cur_playback_time = 0;
248
} else if (Animation::is_greater_approx(cur_playback_time, anim_size)) {
249
cur_playback_time = anim_size;
250
}
251
252
// Emit start & finish signal. Internally, the detections are the same for backward.
253
// We should use call_deferred since the track keys are still being processed.
254
if (process_state->tree && !p_test_only) {
255
// AnimationTree uses seek to 0 "internally" to process the first key of the animation, which is used as the start detection.
256
if (is_started) {
257
process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_started), animation);
258
}
259
// Finished.
260
if (Animation::is_less_approx(prev_playback_time, anim_size) && Animation::is_greater_or_equal_approx(cur_playback_time, anim_size)) {
261
cur_playback_time = anim_size;
262
process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_finished), animation);
263
}
264
}
265
}
266
267
if (!p_test_only) {
268
AnimationMixer::PlaybackInfo pi = p_playback_info;
269
pi.start = 0.0;
270
pi.end = cur_len;
271
if (play_mode == PLAY_MODE_FORWARD) {
272
pi.time = cur_playback_time;
273
pi.delta = cur_delta;
274
} else {
275
pi.time = anim_size - cur_playback_time;
276
pi.delta = -cur_delta;
277
}
278
pi.weight = 1.0;
279
pi.looped_flag = looped_flag;
280
blend_animation(animation, pi);
281
}
282
283
set_parameter(backward, cur_backward);
284
285
return nti;
286
}
287
288
String AnimationNodeAnimation::get_caption() const {
289
return "Animation";
290
}
291
292
void AnimationNodeAnimation::set_play_mode(PlayMode p_play_mode) {
293
play_mode = p_play_mode;
294
}
295
296
AnimationNodeAnimation::PlayMode AnimationNodeAnimation::get_play_mode() const {
297
return play_mode;
298
}
299
300
void AnimationNodeAnimation::set_backward(bool p_backward) {
301
set_parameter(backward, p_backward);
302
}
303
304
bool AnimationNodeAnimation::is_backward() const {
305
return get_parameter(backward);
306
}
307
308
void AnimationNodeAnimation::set_advance_on_start(bool p_advance_on_start) {
309
advance_on_start = p_advance_on_start;
310
}
311
312
bool AnimationNodeAnimation::is_advance_on_start() const {
313
return advance_on_start;
314
}
315
316
void AnimationNodeAnimation::set_use_custom_timeline(bool p_use_custom_timeline) {
317
use_custom_timeline = p_use_custom_timeline;
318
notify_property_list_changed();
319
}
320
321
bool AnimationNodeAnimation::is_using_custom_timeline() const {
322
return use_custom_timeline;
323
}
324
325
void AnimationNodeAnimation::set_timeline_length(double p_length) {
326
timeline_length = p_length;
327
}
328
329
double AnimationNodeAnimation::get_timeline_length() const {
330
return timeline_length;
331
}
332
333
void AnimationNodeAnimation::set_stretch_time_scale(bool p_stretch_time_scale) {
334
stretch_time_scale = p_stretch_time_scale;
335
notify_property_list_changed();
336
}
337
338
bool AnimationNodeAnimation::is_stretching_time_scale() const {
339
return stretch_time_scale;
340
}
341
342
void AnimationNodeAnimation::set_start_offset(double p_offset) {
343
start_offset = p_offset;
344
}
345
346
double AnimationNodeAnimation::get_start_offset() const {
347
return start_offset;
348
}
349
350
void AnimationNodeAnimation::set_loop_mode(Animation::LoopMode p_loop_mode) {
351
loop_mode = p_loop_mode;
352
}
353
354
Animation::LoopMode AnimationNodeAnimation::get_loop_mode() const {
355
return loop_mode;
356
}
357
358
void AnimationNodeAnimation::_bind_methods() {
359
ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimationNodeAnimation::set_animation);
360
ClassDB::bind_method(D_METHOD("get_animation"), &AnimationNodeAnimation::get_animation);
361
362
ClassDB::bind_method(D_METHOD("set_play_mode", "mode"), &AnimationNodeAnimation::set_play_mode);
363
ClassDB::bind_method(D_METHOD("get_play_mode"), &AnimationNodeAnimation::get_play_mode);
364
365
ClassDB::bind_method(D_METHOD("set_advance_on_start", "advance_on_start"), &AnimationNodeAnimation::set_advance_on_start);
366
ClassDB::bind_method(D_METHOD("is_advance_on_start"), &AnimationNodeAnimation::is_advance_on_start);
367
368
ClassDB::bind_method(D_METHOD("set_use_custom_timeline", "use_custom_timeline"), &AnimationNodeAnimation::set_use_custom_timeline);
369
ClassDB::bind_method(D_METHOD("is_using_custom_timeline"), &AnimationNodeAnimation::is_using_custom_timeline);
370
371
ClassDB::bind_method(D_METHOD("set_timeline_length", "timeline_length"), &AnimationNodeAnimation::set_timeline_length);
372
ClassDB::bind_method(D_METHOD("get_timeline_length"), &AnimationNodeAnimation::get_timeline_length);
373
374
ClassDB::bind_method(D_METHOD("set_stretch_time_scale", "stretch_time_scale"), &AnimationNodeAnimation::set_stretch_time_scale);
375
ClassDB::bind_method(D_METHOD("is_stretching_time_scale"), &AnimationNodeAnimation::is_stretching_time_scale);
376
377
ClassDB::bind_method(D_METHOD("set_start_offset", "start_offset"), &AnimationNodeAnimation::set_start_offset);
378
ClassDB::bind_method(D_METHOD("get_start_offset"), &AnimationNodeAnimation::get_start_offset);
379
380
ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AnimationNodeAnimation::set_loop_mode);
381
ClassDB::bind_method(D_METHOD("get_loop_mode"), &AnimationNodeAnimation::get_loop_mode);
382
383
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation");
384
ADD_PROPERTY(PropertyInfo(Variant::INT, "play_mode", PROPERTY_HINT_ENUM, "Forward,Backward"), "set_play_mode", "get_play_mode");
385
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "advance_on_start"), "set_advance_on_start", "is_advance_on_start");
386
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_custom_timeline"), "set_use_custom_timeline", "is_using_custom_timeline");
387
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "timeline_length", PROPERTY_HINT_RANGE, "0.001,60,0.001,or_greater,or_less,hide_slider,suffix:s"), "set_timeline_length", "get_timeline_length");
388
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch_time_scale"), "set_stretch_time_scale", "is_stretching_time_scale");
389
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "start_offset", PROPERTY_HINT_RANGE, "-60,60,0.001,or_greater,or_less,hide_slider,suffix:s"), "set_start_offset", "get_start_offset");
390
ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Ping-Pong"), "set_loop_mode", "get_loop_mode");
391
392
BIND_ENUM_CONSTANT(PLAY_MODE_FORWARD);
393
BIND_ENUM_CONSTANT(PLAY_MODE_BACKWARD);
394
}
395
396
AnimationNodeAnimation::AnimationNodeAnimation() {
397
}
398
399
////////////////////////////////////////////////////////
400
401
void AnimationNodeSync::_bind_methods() {
402
ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeSync::set_use_sync);
403
ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeSync::is_using_sync);
404
405
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
406
}
407
408
void AnimationNodeSync::set_use_sync(bool p_sync) {
409
sync = p_sync;
410
}
411
412
bool AnimationNodeSync::is_using_sync() const {
413
return sync;
414
}
415
416
AnimationNodeSync::AnimationNodeSync() {
417
}
418
419
////////////////////////////////////////////////////////
420
void AnimationNodeOneShot::get_parameter_list(List<PropertyInfo> *r_list) const {
421
AnimationNode::get_parameter_list(r_list);
422
r_list->push_back(PropertyInfo(Variant::BOOL, active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY));
423
r_list->push_back(PropertyInfo(Variant::BOOL, internal_active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY));
424
r_list->push_back(PropertyInfo(Variant::INT, request, PROPERTY_HINT_ENUM, ",Fire,Abort,Fade Out"));
425
r_list->push_back(PropertyInfo(Variant::FLOAT, fade_in_remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
426
r_list->push_back(PropertyInfo(Variant::FLOAT, fade_out_remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
427
r_list->push_back(PropertyInfo(Variant::FLOAT, time_to_restart, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
428
}
429
430
Variant AnimationNodeOneShot::get_parameter_default_value(const StringName &p_parameter) const {
431
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
432
if (ret != Variant()) {
433
return ret;
434
}
435
436
if (p_parameter == request) {
437
return ONE_SHOT_REQUEST_NONE;
438
} else if (p_parameter == active || p_parameter == internal_active) {
439
return false;
440
} else if (p_parameter == time_to_restart) {
441
return -1.0;
442
}
443
444
return 0.0;
445
}
446
447
bool AnimationNodeOneShot::is_parameter_read_only(const StringName &p_parameter) const {
448
if (AnimationNode::is_parameter_read_only(p_parameter)) {
449
return true;
450
}
451
452
if (p_parameter == active || p_parameter == internal_active) {
453
return true;
454
}
455
return false;
456
}
457
458
void AnimationNodeOneShot::set_fade_in_time(double p_time) {
459
fade_in = p_time;
460
}
461
462
double AnimationNodeOneShot::get_fade_in_time() const {
463
return fade_in;
464
}
465
466
void AnimationNodeOneShot::set_fade_out_time(double p_time) {
467
fade_out = p_time;
468
}
469
470
double AnimationNodeOneShot::get_fade_out_time() const {
471
return fade_out;
472
}
473
474
void AnimationNodeOneShot::set_fade_in_curve(const Ref<Curve> &p_curve) {
475
fade_in_curve = p_curve;
476
}
477
478
Ref<Curve> AnimationNodeOneShot::get_fade_in_curve() const {
479
return fade_in_curve;
480
}
481
482
void AnimationNodeOneShot::set_fade_out_curve(const Ref<Curve> &p_curve) {
483
fade_out_curve = p_curve;
484
}
485
486
Ref<Curve> AnimationNodeOneShot::get_fade_out_curve() const {
487
return fade_out_curve;
488
}
489
490
void AnimationNodeOneShot::set_auto_restart_enabled(bool p_enabled) {
491
auto_restart = p_enabled;
492
}
493
494
void AnimationNodeOneShot::set_auto_restart_delay(double p_time) {
495
auto_restart_delay = p_time;
496
}
497
498
void AnimationNodeOneShot::set_auto_restart_random_delay(double p_time) {
499
auto_restart_random_delay = p_time;
500
}
501
502
bool AnimationNodeOneShot::is_auto_restart_enabled() const {
503
return auto_restart;
504
}
505
506
double AnimationNodeOneShot::get_auto_restart_delay() const {
507
return auto_restart_delay;
508
}
509
510
double AnimationNodeOneShot::get_auto_restart_random_delay() const {
511
return auto_restart_random_delay;
512
}
513
514
void AnimationNodeOneShot::set_mix_mode(MixMode p_mix) {
515
mix = p_mix;
516
}
517
518
AnimationNodeOneShot::MixMode AnimationNodeOneShot::get_mix_mode() const {
519
return mix;
520
}
521
522
void AnimationNodeOneShot::set_break_loop_at_end(bool p_enable) {
523
break_loop_at_end = p_enable;
524
}
525
526
bool AnimationNodeOneShot::is_loop_broken_at_end() const {
527
return break_loop_at_end;
528
}
529
530
String AnimationNodeOneShot::get_caption() const {
531
return "OneShot";
532
}
533
534
bool AnimationNodeOneShot::has_filter() const {
535
return true;
536
}
537
538
AnimationNode::NodeTimeInfo AnimationNodeOneShot::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
539
OneShotRequest cur_request = static_cast<OneShotRequest>((int)get_parameter(request));
540
bool cur_active = get_parameter(active);
541
bool cur_internal_active = get_parameter(internal_active);
542
NodeTimeInfo cur_nti = get_node_time_info();
543
double cur_time_to_restart = get_parameter(time_to_restart);
544
double cur_fade_in_remaining = get_parameter(fade_in_remaining);
545
double cur_fade_out_remaining = get_parameter(fade_out_remaining);
546
547
set_parameter(request, ONE_SHOT_REQUEST_NONE);
548
549
bool is_shooting = true;
550
bool clear_remaining_fade = false;
551
bool is_fading_out = cur_active == true && cur_internal_active == false;
552
553
double p_time = p_playback_info.time;
554
double p_delta = p_playback_info.delta;
555
double abs_delta = Math::abs(p_delta);
556
bool p_seek = p_playback_info.seeked;
557
bool p_is_external_seeking = p_playback_info.is_external_seeking;
558
559
if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) {
560
clear_remaining_fade = true; // Reset occurs.
561
}
562
563
bool do_start = cur_request == ONE_SHOT_REQUEST_FIRE;
564
if (cur_request == ONE_SHOT_REQUEST_ABORT) {
565
set_parameter(internal_active, false);
566
set_parameter(active, false);
567
set_parameter(time_to_restart, -1);
568
is_shooting = false;
569
} else if (cur_request == ONE_SHOT_REQUEST_FADE_OUT && !is_fading_out) { // If fading, keep current fade.
570
if (cur_active) {
571
// Request fading.
572
is_fading_out = true;
573
cur_fade_out_remaining = fade_out;
574
cur_fade_in_remaining = 0;
575
} else {
576
// Shot is ended, do nothing.
577
is_shooting = false;
578
}
579
set_parameter(internal_active, false);
580
set_parameter(time_to_restart, -1);
581
} else if (!do_start && !cur_active) {
582
if (Animation::is_greater_or_equal_approx(cur_time_to_restart, 0) && !p_seek) {
583
cur_time_to_restart -= abs_delta;
584
if (Animation::is_less_approx(cur_time_to_restart, 0)) {
585
do_start = true; // Restart.
586
}
587
set_parameter(time_to_restart, cur_time_to_restart);
588
}
589
if (!do_start) {
590
is_shooting = false;
591
}
592
}
593
594
bool os_seek = p_seek;
595
596
if (clear_remaining_fade) {
597
os_seek = false;
598
cur_fade_out_remaining = 0;
599
set_parameter(fade_out_remaining, 0);
600
if (is_fading_out) {
601
is_fading_out = false;
602
set_parameter(internal_active, false);
603
set_parameter(active, false);
604
}
605
}
606
607
if (!is_shooting) {
608
AnimationMixer::PlaybackInfo pi = p_playback_info;
609
pi.weight = 1.0;
610
return blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
611
}
612
613
if (do_start) {
614
os_seek = true;
615
if (!cur_internal_active) {
616
cur_fade_in_remaining = fade_in; // If already active, don't fade-in again.
617
}
618
cur_internal_active = true;
619
set_parameter(request, ONE_SHOT_REQUEST_NONE);
620
set_parameter(internal_active, true);
621
set_parameter(active, true);
622
}
623
624
real_t blend = 1.0;
625
bool use_blend = sync;
626
627
if (Animation::is_greater_approx(cur_fade_in_remaining, 0)) {
628
if (Animation::is_greater_approx(fade_in, 0)) {
629
use_blend = true;
630
blend = (fade_in - cur_fade_in_remaining) / fade_in;
631
if (fade_in_curve.is_valid()) {
632
blend = fade_in_curve->sample(blend);
633
}
634
} else {
635
blend = 0; // Should not happen.
636
}
637
}
638
639
if (is_fading_out) {
640
use_blend = true;
641
if (Animation::is_greater_approx(fade_out, 0)) {
642
blend = cur_fade_out_remaining / fade_out;
643
if (fade_out_curve.is_valid()) {
644
blend = 1.0 - fade_out_curve->sample(1.0 - blend);
645
}
646
} else {
647
blend = 0;
648
}
649
}
650
651
AnimationMixer::PlaybackInfo pi = p_playback_info;
652
NodeTimeInfo main_nti;
653
if (mix == MIX_MODE_ADD) {
654
pi.weight = 1.0;
655
main_nti = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
656
} else {
657
pi.seeked &= use_blend;
658
pi.weight = 1.0 - blend;
659
main_nti = blend_input(0, pi, FILTER_BLEND, sync, p_test_only); // Unlike below, processing this edge is a corner case.
660
}
661
662
pi = p_playback_info;
663
if (do_start) {
664
pi.time = 0;
665
} else if (os_seek) {
666
pi.time = cur_nti.position;
667
}
668
pi.seeked = os_seek;
669
pi.weight = Math::is_zero_approx(blend) ? CMP_EPSILON : blend;
670
671
NodeTimeInfo os_nti = blend_input(1, pi, FILTER_PASS, true, p_test_only); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
672
673
if (Animation::is_less_or_equal_approx(cur_fade_in_remaining, 0) && !do_start && !is_fading_out) {
674
// Predict time scale by difference of delta times to estimate input animation's remain time in self time scale.
675
// TODO: Time scale should be included into NodeTimeInfo for Godot 5.0.
676
double abs_os_delta = Math::abs(os_nti.delta);
677
double tscl = Math::is_zero_approx(abs_delta) || Math::is_zero_approx(abs_os_delta) || Math::is_equal_approx(abs_delta, abs_os_delta) ? 1.0 : (abs_delta / abs_os_delta);
678
double os_rem = os_nti.get_remain(break_loop_at_end) * tscl;
679
if (Animation::is_less_or_equal_approx(os_rem, fade_out)) {
680
is_fading_out = true;
681
cur_fade_out_remaining = os_rem + abs_delta;
682
cur_fade_in_remaining = 0;
683
set_parameter(internal_active, false);
684
}
685
}
686
687
if (!p_seek) {
688
if (Animation::is_less_or_equal_approx(os_nti.get_remain(break_loop_at_end), 0) || (is_fading_out && Animation::is_less_or_equal_approx(cur_fade_out_remaining, 0))) {
689
set_parameter(internal_active, false);
690
set_parameter(active, false);
691
if (auto_restart) {
692
double restart_sec = auto_restart_delay + Math::randd() * auto_restart_random_delay;
693
set_parameter(time_to_restart, restart_sec);
694
}
695
}
696
if (!do_start) {
697
cur_fade_in_remaining = MAX(0, cur_fade_in_remaining - abs_delta); // Don't consider seeked delta by restart.
698
}
699
cur_fade_out_remaining = MAX(0, cur_fade_out_remaining - abs_delta);
700
}
701
702
set_parameter(fade_in_remaining, cur_fade_in_remaining);
703
set_parameter(fade_out_remaining, cur_fade_out_remaining);
704
705
return cur_internal_active ? os_nti : main_nti;
706
}
707
708
void AnimationNodeOneShot::_bind_methods() {
709
ClassDB::bind_method(D_METHOD("set_fadein_time", "time"), &AnimationNodeOneShot::set_fade_in_time);
710
ClassDB::bind_method(D_METHOD("get_fadein_time"), &AnimationNodeOneShot::get_fade_in_time);
711
712
ClassDB::bind_method(D_METHOD("set_fadein_curve", "curve"), &AnimationNodeOneShot::set_fade_in_curve);
713
ClassDB::bind_method(D_METHOD("get_fadein_curve"), &AnimationNodeOneShot::get_fade_in_curve);
714
715
ClassDB::bind_method(D_METHOD("set_fadeout_time", "time"), &AnimationNodeOneShot::set_fade_out_time);
716
ClassDB::bind_method(D_METHOD("get_fadeout_time"), &AnimationNodeOneShot::get_fade_out_time);
717
718
ClassDB::bind_method(D_METHOD("set_fadeout_curve", "curve"), &AnimationNodeOneShot::set_fade_out_curve);
719
ClassDB::bind_method(D_METHOD("get_fadeout_curve"), &AnimationNodeOneShot::get_fade_out_curve);
720
721
ClassDB::bind_method(D_METHOD("set_break_loop_at_end", "enable"), &AnimationNodeOneShot::set_break_loop_at_end);
722
ClassDB::bind_method(D_METHOD("is_loop_broken_at_end"), &AnimationNodeOneShot::is_loop_broken_at_end);
723
724
ClassDB::bind_method(D_METHOD("set_autorestart", "active"), &AnimationNodeOneShot::set_auto_restart_enabled);
725
ClassDB::bind_method(D_METHOD("has_autorestart"), &AnimationNodeOneShot::is_auto_restart_enabled);
726
727
ClassDB::bind_method(D_METHOD("set_autorestart_delay", "time"), &AnimationNodeOneShot::set_auto_restart_delay);
728
ClassDB::bind_method(D_METHOD("get_autorestart_delay"), &AnimationNodeOneShot::get_auto_restart_delay);
729
730
ClassDB::bind_method(D_METHOD("set_autorestart_random_delay", "time"), &AnimationNodeOneShot::set_auto_restart_random_delay);
731
ClassDB::bind_method(D_METHOD("get_autorestart_random_delay"), &AnimationNodeOneShot::get_auto_restart_random_delay);
732
733
ClassDB::bind_method(D_METHOD("set_mix_mode", "mode"), &AnimationNodeOneShot::set_mix_mode);
734
ClassDB::bind_method(D_METHOD("get_mix_mode"), &AnimationNodeOneShot::get_mix_mode);
735
736
ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_mode", PROPERTY_HINT_ENUM, "Blend,Add"), "set_mix_mode", "get_mix_mode");
737
738
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadein_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadein_time", "get_fadein_time");
739
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fadein_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_fadein_curve", "get_fadein_curve");
740
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadeout_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadeout_time", "get_fadeout_time");
741
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fadeout_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_fadeout_curve", "get_fadeout_curve");
742
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "break_loop_at_end"), "set_break_loop_at_end", "is_loop_broken_at_end");
743
744
ADD_GROUP("Auto Restart", "autorestart_");
745
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autorestart"), "set_autorestart", "has_autorestart");
746
747
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_autorestart_delay", "get_autorestart_delay");
748
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_random_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_autorestart_random_delay", "get_autorestart_random_delay");
749
750
BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_NONE);
751
BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_FIRE);
752
BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_ABORT);
753
BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_FADE_OUT);
754
755
BIND_ENUM_CONSTANT(MIX_MODE_BLEND);
756
BIND_ENUM_CONSTANT(MIX_MODE_ADD);
757
}
758
759
AnimationNodeOneShot::AnimationNodeOneShot() {
760
add_input("in");
761
add_input("shot");
762
}
763
764
////////////////////////////////////////////////
765
766
void AnimationNodeAdd2::get_parameter_list(List<PropertyInfo> *r_list) const {
767
AnimationNode::get_parameter_list(r_list);
768
r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
769
}
770
771
Variant AnimationNodeAdd2::get_parameter_default_value(const StringName &p_parameter) const {
772
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
773
if (ret != Variant()) {
774
return ret;
775
}
776
777
return 0.0;
778
}
779
780
String AnimationNodeAdd2::get_caption() const {
781
return "Add2";
782
}
783
784
bool AnimationNodeAdd2::has_filter() const {
785
return true;
786
}
787
788
AnimationNode::NodeTimeInfo AnimationNodeAdd2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
789
double amount = get_parameter(add_amount);
790
791
AnimationMixer::PlaybackInfo pi = p_playback_info;
792
pi.weight = 1.0;
793
NodeTimeInfo nti = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
794
pi.weight = amount;
795
blend_input(1, pi, FILTER_PASS, sync, p_test_only);
796
797
return nti;
798
}
799
800
AnimationNodeAdd2::AnimationNodeAdd2() {
801
add_input("in");
802
add_input("add");
803
}
804
805
////////////////////////////////////////////////
806
807
void AnimationNodeAdd3::get_parameter_list(List<PropertyInfo> *r_list) const {
808
AnimationNode::get_parameter_list(r_list);
809
r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "-1,1,0.01,or_less,or_greater"));
810
}
811
812
Variant AnimationNodeAdd3::get_parameter_default_value(const StringName &p_parameter) const {
813
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
814
if (ret != Variant()) {
815
return ret;
816
}
817
818
return 0.0;
819
}
820
821
String AnimationNodeAdd3::get_caption() const {
822
return "Add3";
823
}
824
825
bool AnimationNodeAdd3::has_filter() const {
826
return true;
827
}
828
829
AnimationNode::NodeTimeInfo AnimationNodeAdd3::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
830
double amount = get_parameter(add_amount);
831
832
AnimationMixer::PlaybackInfo pi = p_playback_info;
833
pi.weight = MAX(0, -amount);
834
blend_input(0, pi, FILTER_PASS, sync, p_test_only);
835
pi.weight = 1.0;
836
NodeTimeInfo nti = blend_input(1, pi, FILTER_IGNORE, sync, p_test_only);
837
pi.weight = MAX(0, amount);
838
blend_input(2, pi, FILTER_PASS, sync, p_test_only);
839
840
return nti;
841
}
842
843
AnimationNodeAdd3::AnimationNodeAdd3() {
844
add_input("-add");
845
add_input("in");
846
add_input("+add");
847
}
848
849
/////////////////////////////////////////////
850
851
void AnimationNodeBlend2::get_parameter_list(List<PropertyInfo> *r_list) const {
852
AnimationNode::get_parameter_list(r_list);
853
r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
854
}
855
856
Variant AnimationNodeBlend2::get_parameter_default_value(const StringName &p_parameter) const {
857
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
858
if (ret != Variant()) {
859
return ret;
860
}
861
862
return 0.0; // For blend amount.
863
}
864
865
String AnimationNodeBlend2::get_caption() const {
866
return "Blend2";
867
}
868
869
AnimationNode::NodeTimeInfo AnimationNodeBlend2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
870
double amount = get_parameter(blend_amount);
871
872
AnimationMixer::PlaybackInfo pi = p_playback_info;
873
pi.weight = 1.0 - amount;
874
NodeTimeInfo nti0 = blend_input(0, pi, FILTER_BLEND, sync, p_test_only);
875
pi.weight = amount;
876
NodeTimeInfo nti1 = blend_input(1, pi, FILTER_PASS, sync, p_test_only);
877
878
return amount > 0.5 ? nti1 : nti0; // Hacky but good enough.
879
}
880
881
bool AnimationNodeBlend2::has_filter() const {
882
return true;
883
}
884
885
AnimationNodeBlend2::AnimationNodeBlend2() {
886
add_input("in");
887
add_input("blend");
888
}
889
890
//////////////////////////////////////
891
892
void AnimationNodeBlend3::get_parameter_list(List<PropertyInfo> *r_list) const {
893
AnimationNode::get_parameter_list(r_list);
894
r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "-1,1,0.01,or_less,or_greater"));
895
}
896
897
Variant AnimationNodeBlend3::get_parameter_default_value(const StringName &p_parameter) const {
898
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
899
if (ret != Variant()) {
900
return ret;
901
}
902
903
return 0.0; // For blend amount.
904
}
905
906
String AnimationNodeBlend3::get_caption() const {
907
return "Blend3";
908
}
909
910
AnimationNode::NodeTimeInfo AnimationNodeBlend3::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
911
double amount = get_parameter(blend_amount);
912
913
AnimationMixer::PlaybackInfo pi = p_playback_info;
914
pi.weight = MAX(0, -amount);
915
NodeTimeInfo nti0 = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
916
pi.weight = 1.0 - Math::abs(amount);
917
NodeTimeInfo nti1 = blend_input(1, pi, FILTER_IGNORE, sync, p_test_only);
918
pi.weight = MAX(0, amount);
919
NodeTimeInfo nti2 = blend_input(2, pi, FILTER_IGNORE, sync, p_test_only);
920
921
return amount > 0.5 ? nti2 : (amount < -0.5 ? nti0 : nti1); // Hacky but good enough.
922
}
923
924
AnimationNodeBlend3::AnimationNodeBlend3() {
925
add_input("-blend");
926
add_input("in");
927
add_input("+blend");
928
}
929
930
////////////////////////////////////////////////
931
932
void AnimationNodeSub2::get_parameter_list(List<PropertyInfo> *r_list) const {
933
AnimationNode::get_parameter_list(r_list);
934
r_list->push_back(PropertyInfo(Variant::FLOAT, sub_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
935
}
936
937
Variant AnimationNodeSub2::get_parameter_default_value(const StringName &p_parameter) const {
938
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
939
if (ret != Variant()) {
940
return ret;
941
}
942
943
return 0.0;
944
}
945
946
String AnimationNodeSub2::get_caption() const {
947
return "Sub2";
948
}
949
950
bool AnimationNodeSub2::has_filter() const {
951
return true;
952
}
953
954
AnimationNode::NodeTimeInfo AnimationNodeSub2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
955
double amount = get_parameter(sub_amount);
956
957
AnimationMixer::PlaybackInfo pi = p_playback_info;
958
// Out = Sub.Transform3D^(-1) * In.Transform3D
959
pi.weight = -amount;
960
blend_input(1, pi, FILTER_PASS, sync, p_test_only);
961
pi.weight = 1.0;
962
963
return blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
964
}
965
966
AnimationNodeSub2::AnimationNodeSub2() {
967
add_input("in");
968
add_input("sub");
969
}
970
971
/////////////////////////////////
972
973
void AnimationNodeTimeScale::get_parameter_list(List<PropertyInfo> *r_list) const {
974
AnimationNode::get_parameter_list(r_list);
975
r_list->push_back(PropertyInfo(Variant::FLOAT, scale, PROPERTY_HINT_RANGE, "-32,32,0.01,or_less,or_greater"));
976
}
977
978
Variant AnimationNodeTimeScale::get_parameter_default_value(const StringName &p_parameter) const {
979
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
980
if (ret != Variant()) {
981
return ret;
982
}
983
984
return 1.0; // Initial timescale.
985
}
986
987
String AnimationNodeTimeScale::get_caption() const {
988
return "TimeScale";
989
}
990
991
AnimationNode::NodeTimeInfo AnimationNodeTimeScale::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
992
double cur_scale = get_parameter(scale);
993
994
AnimationMixer::PlaybackInfo pi = p_playback_info;
995
pi.weight = 1.0;
996
if (!pi.seeked) {
997
pi.delta *= cur_scale;
998
}
999
1000
return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
1001
}
1002
1003
AnimationNodeTimeScale::AnimationNodeTimeScale() {
1004
add_input("in");
1005
}
1006
1007
////////////////////////////////////
1008
1009
void AnimationNodeTimeSeek::get_parameter_list(List<PropertyInfo> *r_list) const {
1010
AnimationNode::get_parameter_list(r_list);
1011
r_list->push_back(PropertyInfo(Variant::FLOAT, seek_pos_request, PROPERTY_HINT_RANGE, "-1,3600,0.01,or_greater")); // It will be reset to -1 after seeking the position immediately.
1012
}
1013
1014
Variant AnimationNodeTimeSeek::get_parameter_default_value(const StringName &p_parameter) const {
1015
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
1016
if (ret != Variant()) {
1017
return ret;
1018
}
1019
1020
return -1.0; // Initial seek request.
1021
}
1022
1023
String AnimationNodeTimeSeek::get_caption() const {
1024
return "TimeSeek";
1025
}
1026
1027
void AnimationNodeTimeSeek::set_explicit_elapse(bool p_enable) {
1028
explicit_elapse = p_enable;
1029
}
1030
1031
bool AnimationNodeTimeSeek::is_explicit_elapse() const {
1032
return explicit_elapse;
1033
}
1034
1035
AnimationNode::NodeTimeInfo AnimationNodeTimeSeek::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
1036
double cur_seek_pos = get_parameter(seek_pos_request);
1037
1038
AnimationMixer::PlaybackInfo pi = p_playback_info;
1039
pi.weight = 1.0;
1040
if (Animation::is_greater_or_equal_approx(cur_seek_pos, 0)) {
1041
pi.time = cur_seek_pos;
1042
pi.seeked = true;
1043
pi.is_external_seeking = explicit_elapse;
1044
set_parameter(seek_pos_request, -1.0); // Reset.
1045
}
1046
1047
return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
1048
}
1049
1050
AnimationNodeTimeSeek::AnimationNodeTimeSeek() {
1051
add_input("in");
1052
}
1053
1054
void AnimationNodeTimeSeek::_bind_methods() {
1055
ClassDB::bind_method(D_METHOD("set_explicit_elapse", "enable"), &AnimationNodeTimeSeek::set_explicit_elapse);
1056
ClassDB::bind_method(D_METHOD("is_explicit_elapse"), &AnimationNodeTimeSeek::is_explicit_elapse);
1057
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "explicit_elapse"), "set_explicit_elapse", "is_explicit_elapse");
1058
}
1059
1060
/////////////////////////////////////////////////
1061
1062
bool AnimationNodeTransition::_set(const StringName &p_path, const Variant &p_value) {
1063
String path = p_path;
1064
1065
if (!path.begins_with("input_")) {
1066
return false;
1067
}
1068
1069
int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
1070
String what = path.get_slicec('/', 1);
1071
1072
if (which == get_input_count() && what == "name") {
1073
if (add_input(p_value)) {
1074
return true;
1075
}
1076
return false;
1077
}
1078
1079
ERR_FAIL_INDEX_V(which, get_input_count(), false);
1080
1081
if (what == "name") {
1082
set_input_name(which, p_value);
1083
} else if (what == "auto_advance") {
1084
set_input_as_auto_advance(which, p_value);
1085
} else if (what == "break_loop_at_end") {
1086
set_input_break_loop_at_end(which, p_value);
1087
} else if (what == "reset") {
1088
set_input_reset(which, p_value);
1089
} else {
1090
return false;
1091
}
1092
1093
return true;
1094
}
1095
1096
bool AnimationNodeTransition::_get(const StringName &p_path, Variant &r_ret) const {
1097
String path = p_path;
1098
1099
if (!path.begins_with("input_")) {
1100
return false;
1101
}
1102
1103
int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
1104
String what = path.get_slicec('/', 1);
1105
1106
ERR_FAIL_INDEX_V(which, get_input_count(), false);
1107
1108
if (what == "name") {
1109
r_ret = get_input_name(which);
1110
} else if (what == "auto_advance") {
1111
r_ret = is_input_set_as_auto_advance(which);
1112
} else if (what == "break_loop_at_end") {
1113
r_ret = is_input_loop_broken_at_end(which);
1114
} else if (what == "reset") {
1115
r_ret = is_input_reset(which);
1116
} else {
1117
return false;
1118
}
1119
1120
return true;
1121
}
1122
1123
void AnimationNodeTransition::get_parameter_list(List<PropertyInfo> *r_list) const {
1124
AnimationNode::get_parameter_list(r_list);
1125
String anims;
1126
for (int i = 0; i < get_input_count(); i++) {
1127
if (i > 0) {
1128
anims += ",";
1129
}
1130
anims += inputs[i].name;
1131
}
1132
1133
r_list->push_back(PropertyInfo(Variant::STRING, current_state, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY)); // For interface.
1134
r_list->push_back(PropertyInfo(Variant::STRING, transition_request, PROPERTY_HINT_ENUM, anims)); // For transition request. It will be cleared after setting the value immediately.
1135
r_list->push_back(PropertyInfo(Variant::INT, current_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY)); // To avoid finding the index every frame, use this internally.
1136
r_list->push_back(PropertyInfo(Variant::INT, prev_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
1137
r_list->push_back(PropertyInfo(Variant::FLOAT, prev_xfading, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
1138
}
1139
1140
Variant AnimationNodeTransition::get_parameter_default_value(const StringName &p_parameter) const {
1141
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
1142
if (ret != Variant()) {
1143
return ret;
1144
}
1145
1146
if (p_parameter == prev_xfading) {
1147
return 0.0;
1148
} else if (p_parameter == prev_index || p_parameter == current_index) {
1149
return (int)-1;
1150
} else {
1151
return String();
1152
}
1153
}
1154
1155
bool AnimationNodeTransition::is_parameter_read_only(const StringName &p_parameter) const {
1156
if (AnimationNode::is_parameter_read_only(p_parameter)) {
1157
return true;
1158
}
1159
1160
if (p_parameter == current_state || p_parameter == current_index) {
1161
return true;
1162
}
1163
return false;
1164
}
1165
1166
String AnimationNodeTransition::get_caption() const {
1167
return "Transition";
1168
}
1169
1170
void AnimationNodeTransition::set_input_count(int p_inputs) {
1171
for (int i = get_input_count(); i < p_inputs; i++) {
1172
add_input("state_" + itos(i));
1173
}
1174
while (get_input_count() > p_inputs) {
1175
remove_input(get_input_count() - 1);
1176
}
1177
1178
pending_update = true;
1179
1180
emit_signal(SNAME("tree_changed")); // For updating connect activity map.
1181
notify_property_list_changed();
1182
}
1183
1184
bool AnimationNodeTransition::add_input(const String &p_name) {
1185
if (AnimationNode::add_input(p_name)) {
1186
input_data.push_back(InputData());
1187
return true;
1188
}
1189
return false;
1190
}
1191
1192
void AnimationNodeTransition::remove_input(int p_index) {
1193
input_data.remove_at(p_index);
1194
AnimationNode::remove_input(p_index);
1195
}
1196
1197
bool AnimationNodeTransition::set_input_name(int p_input, const String &p_name) {
1198
pending_update = true;
1199
if (!AnimationNode::set_input_name(p_input, p_name)) {
1200
return false;
1201
}
1202
emit_signal(SNAME("tree_changed")); // For updating enum options.
1203
return true;
1204
}
1205
1206
void AnimationNodeTransition::set_input_as_auto_advance(int p_input, bool p_enable) {
1207
ERR_FAIL_INDEX(p_input, get_input_count());
1208
input_data[p_input].auto_advance = p_enable;
1209
}
1210
1211
bool AnimationNodeTransition::is_input_set_as_auto_advance(int p_input) const {
1212
ERR_FAIL_INDEX_V(p_input, get_input_count(), false);
1213
return input_data[p_input].auto_advance;
1214
}
1215
1216
void AnimationNodeTransition::set_input_break_loop_at_end(int p_input, bool p_enable) {
1217
ERR_FAIL_INDEX(p_input, get_input_count());
1218
input_data[p_input].break_loop_at_end = p_enable;
1219
}
1220
1221
bool AnimationNodeTransition::is_input_loop_broken_at_end(int p_input) const {
1222
ERR_FAIL_INDEX_V(p_input, get_input_count(), false);
1223
return input_data[p_input].break_loop_at_end;
1224
}
1225
1226
void AnimationNodeTransition::set_input_reset(int p_input, bool p_enable) {
1227
ERR_FAIL_INDEX(p_input, get_input_count());
1228
input_data[p_input].reset = p_enable;
1229
}
1230
1231
bool AnimationNodeTransition::is_input_reset(int p_input) const {
1232
ERR_FAIL_INDEX_V(p_input, get_input_count(), true);
1233
return input_data[p_input].reset;
1234
}
1235
1236
void AnimationNodeTransition::set_xfade_time(double p_fade) {
1237
xfade_time = p_fade;
1238
}
1239
1240
double AnimationNodeTransition::get_xfade_time() const {
1241
return xfade_time;
1242
}
1243
1244
void AnimationNodeTransition::set_xfade_curve(const Ref<Curve> &p_curve) {
1245
xfade_curve = p_curve;
1246
}
1247
1248
Ref<Curve> AnimationNodeTransition::get_xfade_curve() const {
1249
return xfade_curve;
1250
}
1251
1252
void AnimationNodeTransition::set_allow_transition_to_self(bool p_enable) {
1253
allow_transition_to_self = p_enable;
1254
}
1255
1256
bool AnimationNodeTransition::is_allow_transition_to_self() const {
1257
return allow_transition_to_self;
1258
}
1259
1260
AnimationNode::NodeTimeInfo AnimationNodeTransition::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
1261
String cur_transition_request = get_parameter(transition_request);
1262
int cur_current_index = get_parameter(current_index);
1263
int cur_prev_index = get_parameter(prev_index);
1264
1265
NodeTimeInfo cur_nti = get_node_time_info();
1266
double cur_prev_xfading = get_parameter(prev_xfading);
1267
1268
bool switched = false;
1269
bool restart = false;
1270
bool clear_remaining_fade = false;
1271
1272
if (pending_update) {
1273
if (cur_current_index < 0 || cur_current_index >= get_input_count()) {
1274
set_parameter(prev_index, -1);
1275
if (get_input_count() > 0) {
1276
set_parameter(current_index, 0);
1277
set_parameter(current_state, get_input_name(0));
1278
} else {
1279
set_parameter(current_index, -1);
1280
set_parameter(current_state, StringName());
1281
}
1282
} else {
1283
set_parameter(current_state, get_input_name(cur_current_index));
1284
}
1285
pending_update = false;
1286
}
1287
1288
double p_time = p_playback_info.time;
1289
bool p_seek = p_playback_info.seeked;
1290
bool p_is_external_seeking = p_playback_info.is_external_seeking;
1291
1292
if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) {
1293
clear_remaining_fade = true; // Reset occurs.
1294
}
1295
1296
if (!cur_transition_request.is_empty()) {
1297
int new_idx = find_input(cur_transition_request);
1298
if (new_idx >= 0) {
1299
if (cur_current_index == new_idx) {
1300
if (allow_transition_to_self) {
1301
// Transition to same state.
1302
restart = input_data[cur_current_index].reset;
1303
clear_remaining_fade = true;
1304
}
1305
} else {
1306
switched = true;
1307
cur_prev_index = cur_current_index;
1308
set_parameter(prev_index, cur_current_index);
1309
cur_current_index = new_idx;
1310
set_parameter(current_index, cur_current_index);
1311
set_parameter(current_state, cur_transition_request);
1312
}
1313
} else {
1314
ERR_PRINT("No such input: '" + cur_transition_request + "'");
1315
}
1316
cur_transition_request = String();
1317
set_parameter(transition_request, cur_transition_request);
1318
}
1319
1320
if (clear_remaining_fade) {
1321
cur_prev_xfading = 0;
1322
set_parameter(prev_xfading, 0);
1323
cur_prev_index = -1;
1324
set_parameter(prev_index, -1);
1325
}
1326
1327
AnimationMixer::PlaybackInfo pi = p_playback_info;
1328
1329
// Special case for restart.
1330
if (restart) {
1331
pi.time = 0;
1332
pi.seeked = true;
1333
pi.weight = 1.0;
1334
return blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
1335
}
1336
1337
if (switched) {
1338
cur_prev_xfading = xfade_time;
1339
}
1340
1341
if (cur_current_index < 0 || cur_current_index >= get_input_count() || cur_prev_index >= get_input_count()) {
1342
return NodeTimeInfo();
1343
}
1344
1345
if (sync) {
1346
pi.weight = 0;
1347
for (int i = 0; i < get_input_count(); i++) {
1348
if (i != cur_current_index && i != cur_prev_index) {
1349
blend_input(i, pi, FILTER_IGNORE, true, p_test_only);
1350
}
1351
}
1352
}
1353
1354
if (cur_prev_index < 0) { // Process current animation, check for transition.
1355
pi.weight = 1.0;
1356
cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
1357
if (input_data[cur_current_index].auto_advance && Animation::is_less_or_equal_approx(cur_nti.get_remain(input_data[cur_current_index].break_loop_at_end), xfade_time)) {
1358
set_parameter(transition_request, get_input_name((cur_current_index + 1) % get_input_count()));
1359
}
1360
} else { // Cross-fading from prev to current.
1361
1362
real_t blend = 0.0;
1363
real_t blend_inv = 1.0;
1364
bool use_blend = sync;
1365
if (xfade_time > 0) {
1366
use_blend = true;
1367
blend = cur_prev_xfading / xfade_time;
1368
if (xfade_curve.is_valid()) {
1369
blend = xfade_curve->sample(blend);
1370
}
1371
blend_inv = 1.0 - blend;
1372
blend = Math::is_zero_approx(blend) ? CMP_EPSILON : blend;
1373
blend_inv = Math::is_zero_approx(blend_inv) ? CMP_EPSILON : blend_inv;
1374
}
1375
1376
// Blend values must be more than CMP_EPSILON to process discrete keys in edge.
1377
pi.weight = blend_inv;
1378
if (input_data[cur_current_index].reset && !p_seek && switched) { // Just switched, seek to start of current.
1379
pi.time = 0;
1380
pi.seeked = true;
1381
}
1382
cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
1383
1384
pi = p_playback_info;
1385
pi.seeked &= use_blend;
1386
pi.weight = blend;
1387
blend_input(cur_prev_index, pi, FILTER_IGNORE, true, p_test_only);
1388
if (!p_seek) {
1389
if (Animation::is_less_or_equal_approx(cur_prev_xfading, 0)) {
1390
set_parameter(prev_index, -1);
1391
}
1392
cur_prev_xfading -= Math::abs(p_playback_info.delta);
1393
}
1394
}
1395
1396
set_parameter(prev_xfading, cur_prev_xfading);
1397
1398
return cur_nti;
1399
}
1400
1401
void AnimationNodeTransition::_get_property_list(List<PropertyInfo> *p_list) const {
1402
for (int i = 0; i < get_input_count(); i++) {
1403
p_list->push_back(PropertyInfo(Variant::STRING, "input_" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
1404
p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/auto_advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
1405
p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/break_loop_at_end", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
1406
p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/reset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
1407
}
1408
}
1409
1410
void AnimationNodeTransition::_bind_methods() {
1411
ClassDB::bind_method(D_METHOD("set_input_count", "input_count"), &AnimationNodeTransition::set_input_count);
1412
1413
ClassDB::bind_method(D_METHOD("set_input_as_auto_advance", "input", "enable"), &AnimationNodeTransition::set_input_as_auto_advance);
1414
ClassDB::bind_method(D_METHOD("is_input_set_as_auto_advance", "input"), &AnimationNodeTransition::is_input_set_as_auto_advance);
1415
1416
ClassDB::bind_method(D_METHOD("set_input_break_loop_at_end", "input", "enable"), &AnimationNodeTransition::set_input_break_loop_at_end);
1417
ClassDB::bind_method(D_METHOD("is_input_loop_broken_at_end", "input"), &AnimationNodeTransition::is_input_loop_broken_at_end);
1418
1419
ClassDB::bind_method(D_METHOD("set_input_reset", "input", "enable"), &AnimationNodeTransition::set_input_reset);
1420
ClassDB::bind_method(D_METHOD("is_input_reset", "input"), &AnimationNodeTransition::is_input_reset);
1421
1422
ClassDB::bind_method(D_METHOD("set_xfade_time", "time"), &AnimationNodeTransition::set_xfade_time);
1423
ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeTransition::get_xfade_time);
1424
1425
ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeTransition::set_xfade_curve);
1426
ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeTransition::get_xfade_curve);
1427
1428
ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeTransition::set_allow_transition_to_self);
1429
ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeTransition::is_allow_transition_to_self);
1430
1431
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,120,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");
1432
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");
1433
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
1434
ADD_PROPERTY(PropertyInfo(Variant::INT, "input_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED, "Inputs,input_"), "set_input_count", "get_input_count");
1435
}
1436
1437
AnimationNodeTransition::AnimationNodeTransition() {
1438
}
1439
1440
/////////////////////
1441
1442
String AnimationNodeOutput::get_caption() const {
1443
return "Output";
1444
}
1445
1446
AnimationNode::NodeTimeInfo AnimationNodeOutput::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
1447
AnimationMixer::PlaybackInfo pi = p_playback_info;
1448
pi.weight = 1.0;
1449
return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
1450
}
1451
1452
AnimationNodeOutput::AnimationNodeOutput() {
1453
add_input("output");
1454
}
1455
1456
///////////////////////////////////////////////////////
1457
void AnimationNodeBlendTree::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
1458
ERR_FAIL_COND(nodes.has(p_name));
1459
ERR_FAIL_COND(p_node.is_null());
1460
ERR_FAIL_COND(p_name == SceneStringName(output));
1461
ERR_FAIL_COND(String(p_name).contains_char('/'));
1462
1463
Node n;
1464
n.node = p_node;
1465
n.position = p_position;
1466
n.connections.resize(n.node->get_input_count());
1467
nodes[p_name] = n;
1468
1469
emit_changed();
1470
emit_signal(SNAME("tree_changed"));
1471
1472
p_node->connect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed), CONNECT_REFERENCE_COUNTED);
1473
p_node->connect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
1474
p_node->connect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
1475
p_node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_name), CONNECT_REFERENCE_COUNTED);
1476
}
1477
1478
Ref<AnimationNode> AnimationNodeBlendTree::get_node(const StringName &p_name) const {
1479
ERR_FAIL_COND_V(!nodes.has(p_name), Ref<AnimationNode>());
1480
1481
return nodes[p_name].node;
1482
}
1483
1484
StringName AnimationNodeBlendTree::get_node_name(const Ref<AnimationNode> &p_node) const {
1485
for (const KeyValue<StringName, Node> &E : nodes) {
1486
if (E.value.node == p_node) {
1487
return E.key;
1488
}
1489
}
1490
1491
ERR_FAIL_V(StringName());
1492
}
1493
1494
void AnimationNodeBlendTree::set_node_position(const StringName &p_node, const Vector2 &p_position) {
1495
ERR_FAIL_COND(!nodes.has(p_node));
1496
nodes[p_node].position = p_position;
1497
}
1498
1499
Vector2 AnimationNodeBlendTree::get_node_position(const StringName &p_node) const {
1500
ERR_FAIL_COND_V(!nodes.has(p_node), Vector2());
1501
return nodes[p_node].position;
1502
}
1503
1504
void AnimationNodeBlendTree::get_child_nodes(List<ChildNode> *r_child_nodes) {
1505
Vector<StringName> ns;
1506
1507
for (const KeyValue<StringName, Node> &E : nodes) {
1508
ns.push_back(E.key);
1509
}
1510
1511
for (int i = 0; i < ns.size(); i++) {
1512
ChildNode cn;
1513
cn.name = ns[i];
1514
cn.node = nodes[cn.name].node;
1515
r_child_nodes->push_back(cn);
1516
}
1517
}
1518
1519
bool AnimationNodeBlendTree::has_node(const StringName &p_name) const {
1520
return nodes.has(p_name);
1521
}
1522
1523
Vector<StringName> AnimationNodeBlendTree::get_node_connection_array(const StringName &p_name) const {
1524
ERR_FAIL_COND_V(!nodes.has(p_name), Vector<StringName>());
1525
return nodes[p_name].connections;
1526
}
1527
1528
void AnimationNodeBlendTree::remove_node(const StringName &p_name) {
1529
ERR_FAIL_COND(!nodes.has(p_name));
1530
ERR_FAIL_COND(p_name == SceneStringName(output)); //can't delete output
1531
1532
{
1533
Ref<AnimationNode> node = nodes[p_name].node;
1534
node->disconnect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed));
1535
node->disconnect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed));
1536
node->disconnect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed));
1537
node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed));
1538
}
1539
1540
nodes.erase(p_name);
1541
1542
// Erase connections to name.
1543
for (KeyValue<StringName, Node> &E : nodes) {
1544
for (int i = 0; i < E.value.connections.size(); i++) {
1545
if (E.value.connections[i] == p_name) {
1546
E.value.connections.write[i] = StringName();
1547
}
1548
}
1549
}
1550
1551
emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);
1552
emit_changed();
1553
emit_signal(SNAME("tree_changed"));
1554
}
1555
1556
void AnimationNodeBlendTree::rename_node(const StringName &p_name, const StringName &p_new_name) {
1557
ERR_FAIL_COND(!nodes.has(p_name));
1558
ERR_FAIL_COND(nodes.has(p_new_name));
1559
ERR_FAIL_COND(p_name == SceneStringName(output));
1560
ERR_FAIL_COND(p_new_name == SceneStringName(output));
1561
1562
nodes[p_name].node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed));
1563
1564
nodes[p_new_name] = nodes[p_name];
1565
nodes.erase(p_name);
1566
1567
// Rename connections.
1568
for (KeyValue<StringName, Node> &E : nodes) {
1569
for (int i = 0; i < E.value.connections.size(); i++) {
1570
if (E.value.connections[i] == p_name) {
1571
E.value.connections.write[i] = p_new_name;
1572
}
1573
}
1574
}
1575
// Connection must be done with new name.
1576
nodes[p_new_name].node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_new_name), CONNECT_REFERENCE_COUNTED);
1577
1578
emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);
1579
emit_signal(SNAME("tree_changed"));
1580
}
1581
1582
void AnimationNodeBlendTree::connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) {
1583
ERR_FAIL_COND(!nodes.has(p_output_node));
1584
ERR_FAIL_COND(!nodes.has(p_input_node));
1585
ERR_FAIL_COND(p_output_node == SceneStringName(output));
1586
ERR_FAIL_COND(p_input_node == p_output_node);
1587
1588
Ref<AnimationNode> input = nodes[p_input_node].node;
1589
ERR_FAIL_INDEX(p_input_index, nodes[p_input_node].connections.size());
1590
1591
for (KeyValue<StringName, Node> &E : nodes) {
1592
for (int i = 0; i < E.value.connections.size(); i++) {
1593
StringName output = E.value.connections[i];
1594
ERR_FAIL_COND(output == p_output_node);
1595
}
1596
}
1597
1598
nodes[p_input_node].connections.write[p_input_index] = p_output_node;
1599
1600
emit_changed();
1601
}
1602
1603
void AnimationNodeBlendTree::disconnect_node(const StringName &p_node, int p_input_index) {
1604
ERR_FAIL_COND(!nodes.has(p_node));
1605
1606
Ref<AnimationNode> input = nodes[p_node].node;
1607
ERR_FAIL_INDEX(p_input_index, nodes[p_node].connections.size());
1608
1609
nodes[p_node].connections.write[p_input_index] = StringName();
1610
}
1611
1612
AnimationNodeBlendTree::ConnectionError AnimationNodeBlendTree::can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const {
1613
if (!nodes.has(p_output_node) || p_output_node == SceneStringName(output)) {
1614
return CONNECTION_ERROR_NO_OUTPUT;
1615
}
1616
1617
if (!nodes.has(p_input_node)) {
1618
return CONNECTION_ERROR_NO_INPUT;
1619
}
1620
1621
if (p_input_node == p_output_node) {
1622
return CONNECTION_ERROR_SAME_NODE;
1623
}
1624
1625
Ref<AnimationNode> input = nodes[p_input_node].node;
1626
1627
if (p_input_index < 0 || p_input_index >= nodes[p_input_node].connections.size()) {
1628
return CONNECTION_ERROR_NO_INPUT_INDEX;
1629
}
1630
1631
if (nodes[p_input_node].connections[p_input_index] != StringName()) {
1632
return CONNECTION_ERROR_CONNECTION_EXISTS;
1633
}
1634
1635
for (const KeyValue<StringName, Node> &E : nodes) {
1636
for (int i = 0; i < E.value.connections.size(); i++) {
1637
const StringName output = E.value.connections[i];
1638
if (output == p_output_node) {
1639
return CONNECTION_ERROR_CONNECTION_EXISTS;
1640
}
1641
}
1642
}
1643
return CONNECTION_OK;
1644
}
1645
1646
void AnimationNodeBlendTree::get_node_connections(List<NodeConnection> *r_connections) const {
1647
for (const KeyValue<StringName, Node> &E : nodes) {
1648
for (int i = 0; i < E.value.connections.size(); i++) {
1649
const StringName output = E.value.connections[i];
1650
if (output != StringName()) {
1651
NodeConnection nc;
1652
nc.input_node = E.key;
1653
nc.input_index = i;
1654
nc.output_node = output;
1655
r_connections->push_back(nc);
1656
}
1657
}
1658
}
1659
}
1660
1661
String AnimationNodeBlendTree::get_caption() const {
1662
return "BlendTree";
1663
}
1664
1665
AnimationNode::NodeTimeInfo AnimationNodeBlendTree::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
1666
Ref<AnimationNodeOutput> output = nodes[SceneStringName(output)].node;
1667
node_state.connections = nodes[SceneStringName(output)].connections;
1668
ERR_FAIL_COND_V(output.is_null(), NodeTimeInfo());
1669
1670
AnimationMixer::PlaybackInfo pi = p_playback_info;
1671
pi.weight = 1.0;
1672
1673
return _blend_node(output, "output", this, pi, FILTER_IGNORE, true, p_test_only, nullptr);
1674
}
1675
1676
LocalVector<StringName> AnimationNodeBlendTree::get_node_list() const {
1677
LocalVector<StringName> list;
1678
list.reserve(nodes.size());
1679
for (const KeyValue<StringName, Node> &E : nodes) {
1680
list.push_back(E.key);
1681
}
1682
list.sort_custom<StringName::AlphCompare>();
1683
return list;
1684
}
1685
1686
TypedArray<StringName> AnimationNodeBlendTree::get_node_list_as_typed_array() const {
1687
TypedArray<StringName> typed_arr;
1688
LocalVector<StringName> vec = get_node_list();
1689
typed_arr.resize(vec.size());
1690
for (uint32_t i = 0; i < vec.size(); i++) {
1691
typed_arr[i] = vec[i];
1692
}
1693
return typed_arr;
1694
}
1695
1696
void AnimationNodeBlendTree::set_graph_offset(const Vector2 &p_graph_offset) {
1697
graph_offset = p_graph_offset;
1698
}
1699
1700
Vector2 AnimationNodeBlendTree::get_graph_offset() const {
1701
return graph_offset;
1702
}
1703
1704
Ref<AnimationNode> AnimationNodeBlendTree::get_child_by_name(const StringName &p_name) const {
1705
return get_node(p_name);
1706
}
1707
1708
bool AnimationNodeBlendTree::_set(const StringName &p_name, const Variant &p_value) {
1709
String prop_name = p_name;
1710
if (prop_name.begins_with("nodes/")) {
1711
String node_name = prop_name.get_slicec('/', 1);
1712
String what = prop_name.get_slicec('/', 2);
1713
1714
if (what == "node") {
1715
Ref<AnimationNode> anode = p_value;
1716
if (anode.is_valid()) {
1717
add_node(node_name, p_value);
1718
}
1719
return true;
1720
}
1721
1722
if (what == "position") {
1723
if (nodes.has(node_name)) {
1724
nodes[node_name].position = p_value;
1725
}
1726
return true;
1727
}
1728
} else if (prop_name == "node_connections") {
1729
Array conns = p_value;
1730
ERR_FAIL_COND_V(conns.size() % 3 != 0, false);
1731
1732
for (int i = 0; i < conns.size(); i += 3) {
1733
connect_node(conns[i], conns[i + 1], conns[i + 2]);
1734
}
1735
return true;
1736
}
1737
1738
return false;
1739
}
1740
1741
bool AnimationNodeBlendTree::_get(const StringName &p_name, Variant &r_ret) const {
1742
String prop_name = p_name;
1743
if (prop_name.begins_with("nodes/")) {
1744
String node_name = prop_name.get_slicec('/', 1);
1745
String what = prop_name.get_slicec('/', 2);
1746
1747
if (what == "node") {
1748
if (nodes.has(node_name)) {
1749
r_ret = nodes[node_name].node;
1750
return true;
1751
}
1752
}
1753
1754
if (what == "position") {
1755
if (nodes.has(node_name)) {
1756
r_ret = nodes[node_name].position;
1757
return true;
1758
}
1759
}
1760
} else if (prop_name == "node_connections") {
1761
List<NodeConnection> nc;
1762
get_node_connections(&nc);
1763
Array conns;
1764
conns.resize(nc.size() * 3);
1765
1766
int idx = 0;
1767
for (const NodeConnection &E : nc) {
1768
conns[idx * 3 + 0] = E.input_node;
1769
conns[idx * 3 + 1] = E.input_index;
1770
conns[idx * 3 + 2] = E.output_node;
1771
idx++;
1772
}
1773
1774
r_ret = conns;
1775
return true;
1776
}
1777
1778
return false;
1779
}
1780
1781
void AnimationNodeBlendTree::_get_property_list(List<PropertyInfo> *p_list) const {
1782
List<StringName> names;
1783
for (const KeyValue<StringName, Node> &E : nodes) {
1784
names.push_back(E.key);
1785
}
1786
1787
for (const StringName &E : names) {
1788
String prop_name = E;
1789
if (prop_name != "output") {
1790
p_list->push_back(PropertyInfo(Variant::OBJECT, "nodes/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
1791
}
1792
p_list->push_back(PropertyInfo(Variant::VECTOR2, "nodes/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
1793
}
1794
1795
p_list->push_back(PropertyInfo(Variant::ARRAY, "node_connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
1796
}
1797
1798
void AnimationNodeBlendTree::_tree_changed() {
1799
AnimationRootNode::_tree_changed();
1800
}
1801
1802
void AnimationNodeBlendTree::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
1803
AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
1804
}
1805
1806
void AnimationNodeBlendTree::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
1807
AnimationRootNode::_animation_node_removed(p_oid, p_node);
1808
}
1809
1810
void AnimationNodeBlendTree::reset_state() {
1811
graph_offset = Vector2();
1812
nodes.clear();
1813
_initialize_node_tree();
1814
emit_changed();
1815
emit_signal(SNAME("tree_changed"));
1816
}
1817
1818
void AnimationNodeBlendTree::_node_changed(const StringName &p_node) {
1819
ERR_FAIL_COND(!nodes.has(p_node));
1820
nodes[p_node].connections.resize(nodes[p_node].node->get_input_count());
1821
emit_signal(SNAME("node_changed"), p_node);
1822
}
1823
1824
#ifdef TOOLS_ENABLED
1825
void AnimationNodeBlendTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
1826
const String pf = p_function;
1827
bool add_node_options = false;
1828
if (p_idx == 0) {
1829
add_node_options = (pf == "get_node" || pf == "has_node" || pf == "rename_node" || pf == "remove_node" || pf == "set_node_position" || pf == "get_node_position" || pf == "connect_node" || pf == "disconnect_node");
1830
} else if (p_idx == 2) {
1831
add_node_options = (pf == "connect_node" || pf == "disconnect_node");
1832
}
1833
if (add_node_options) {
1834
for (const KeyValue<StringName, Node> &E : nodes) {
1835
r_options->push_back(String(E.key).quote());
1836
}
1837
}
1838
AnimationRootNode::get_argument_options(p_function, p_idx, r_options);
1839
}
1840
#endif
1841
1842
void AnimationNodeBlendTree::_bind_methods() {
1843
ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeBlendTree::add_node, DEFVAL(Vector2()));
1844
ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeBlendTree::get_node);
1845
ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeBlendTree::remove_node);
1846
ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeBlendTree::rename_node);
1847
ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeBlendTree::has_node);
1848
ClassDB::bind_method(D_METHOD("connect_node", "input_node", "input_index", "output_node"), &AnimationNodeBlendTree::connect_node);
1849
ClassDB::bind_method(D_METHOD("disconnect_node", "input_node", "input_index"), &AnimationNodeBlendTree::disconnect_node);
1850
ClassDB::bind_method(D_METHOD("get_node_list"), &AnimationNodeBlendTree::get_node_list_as_typed_array);
1851
1852
ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeBlendTree::set_node_position);
1853
ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeBlendTree::get_node_position);
1854
1855
ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeBlendTree::set_graph_offset);
1856
ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeBlendTree::get_graph_offset);
1857
1858
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset");
1859
1860
BIND_CONSTANT(CONNECTION_OK);
1861
BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT);
1862
BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT_INDEX);
1863
BIND_CONSTANT(CONNECTION_ERROR_NO_OUTPUT);
1864
BIND_CONSTANT(CONNECTION_ERROR_SAME_NODE);
1865
BIND_CONSTANT(CONNECTION_ERROR_CONNECTION_EXISTS);
1866
1867
ADD_SIGNAL(MethodInfo(SNAME("node_changed"), PropertyInfo(Variant::STRING_NAME, "node_name")));
1868
}
1869
1870
void AnimationNodeBlendTree::_initialize_node_tree() {
1871
Ref<AnimationNodeOutput> output;
1872
output.instantiate();
1873
Node n;
1874
n.node = output;
1875
n.position = Vector2(300, 150);
1876
n.connections.resize(1);
1877
nodes["output"] = n;
1878
}
1879
1880
AnimationNodeBlendTree::AnimationNodeBlendTree() {
1881
_initialize_node_tree();
1882
}
1883
1884
AnimationNodeBlendTree::~AnimationNodeBlendTree() {
1885
}
1886
1887