Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/animation/animation_blend_space_1d.cpp
9896 views
1
/**************************************************************************/
2
/* animation_blend_space_1d.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_space_1d.h"
32
33
#include "animation_blend_tree.h"
34
35
void AnimationNodeBlendSpace1D::get_parameter_list(List<PropertyInfo> *r_list) const {
36
AnimationNode::get_parameter_list(r_list);
37
r_list->push_back(PropertyInfo(Variant::FLOAT, blend_position));
38
r_list->push_back(PropertyInfo(Variant::INT, closest, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
39
}
40
41
Variant AnimationNodeBlendSpace1D::get_parameter_default_value(const StringName &p_parameter) const {
42
Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
43
if (ret != Variant()) {
44
return ret;
45
}
46
47
if (p_parameter == closest) {
48
return (int)-1;
49
} else {
50
return 0.0;
51
}
52
}
53
54
Ref<AnimationNode> AnimationNodeBlendSpace1D::get_child_by_name(const StringName &p_name) const {
55
return get_blend_point_node(p_name.operator String().to_int());
56
}
57
58
void AnimationNodeBlendSpace1D::_validate_property(PropertyInfo &p_property) const {
59
if (p_property.name.begins_with("blend_point_")) {
60
String left = p_property.name.get_slicec('/', 0);
61
int idx = left.get_slicec('_', 2).to_int();
62
if (idx >= blend_points_used) {
63
p_property.usage = PROPERTY_USAGE_NONE;
64
}
65
}
66
}
67
68
void AnimationNodeBlendSpace1D::_tree_changed() {
69
AnimationRootNode::_tree_changed();
70
}
71
72
void AnimationNodeBlendSpace1D::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
73
AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
74
}
75
76
void AnimationNodeBlendSpace1D::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
77
AnimationRootNode::_animation_node_removed(p_oid, p_node);
78
}
79
80
void AnimationNodeBlendSpace1D::_bind_methods() {
81
ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace1D::add_blend_point, DEFVAL(-1));
82
ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace1D::set_blend_point_position);
83
ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace1D::get_blend_point_position);
84
ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace1D::set_blend_point_node);
85
ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace1D::get_blend_point_node);
86
ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace1D::remove_blend_point);
87
ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace1D::get_blend_point_count);
88
89
ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace1D::set_min_space);
90
ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace1D::get_min_space);
91
92
ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace1D::set_max_space);
93
ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace1D::get_max_space);
94
95
ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace1D::set_snap);
96
ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace1D::get_snap);
97
98
ClassDB::bind_method(D_METHOD("set_value_label", "text"), &AnimationNodeBlendSpace1D::set_value_label);
99
ClassDB::bind_method(D_METHOD("get_value_label"), &AnimationNodeBlendSpace1D::get_value_label);
100
101
ClassDB::bind_method(D_METHOD("set_blend_mode", "mode"), &AnimationNodeBlendSpace1D::set_blend_mode);
102
ClassDB::bind_method(D_METHOD("get_blend_mode"), &AnimationNodeBlendSpace1D::get_blend_mode);
103
104
ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeBlendSpace1D::set_use_sync);
105
ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeBlendSpace1D::is_using_sync);
106
107
ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace1D::_add_blend_point);
108
109
for (int i = 0; i < MAX_BLEND_POINTS; i++) {
110
ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_add_blend_point", "get_blend_point_node", i);
111
ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);
112
}
113
114
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_min_space", "get_min_space");
115
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_max_space", "get_max_space");
116
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_snap", "get_snap");
117
ADD_PROPERTY(PropertyInfo(Variant::STRING, "value_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_value_label", "get_value_label");
118
ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Interpolated,Discrete,Carry", PROPERTY_USAGE_NO_EDITOR), "set_blend_mode", "get_blend_mode");
119
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_use_sync", "is_using_sync");
120
121
BIND_ENUM_CONSTANT(BLEND_MODE_INTERPOLATED);
122
BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE);
123
BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE_CARRY);
124
}
125
126
void AnimationNodeBlendSpace1D::get_child_nodes(List<ChildNode> *r_child_nodes) {
127
for (int i = 0; i < blend_points_used; i++) {
128
ChildNode cn;
129
cn.name = itos(i);
130
cn.node = blend_points[i].node;
131
r_child_nodes->push_back(cn);
132
}
133
}
134
135
void AnimationNodeBlendSpace1D::add_blend_point(const Ref<AnimationRootNode> &p_node, float p_position, int p_at_index) {
136
ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);
137
ERR_FAIL_COND(p_node.is_null());
138
139
ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used);
140
141
if (p_at_index == -1 || p_at_index == blend_points_used) {
142
p_at_index = blend_points_used;
143
} else {
144
for (int i = blend_points_used - 1; i > p_at_index; i--) {
145
blend_points[i] = blend_points[i - 1];
146
}
147
}
148
149
blend_points[p_at_index].node = p_node;
150
blend_points[p_at_index].position = p_position;
151
152
blend_points[p_at_index].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), CONNECT_REFERENCE_COUNTED);
153
blend_points[p_at_index].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
154
blend_points[p_at_index].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
155
156
blend_points_used++;
157
emit_signal(SNAME("tree_changed"));
158
}
159
160
void AnimationNodeBlendSpace1D::set_blend_point_position(int p_point, float p_position) {
161
ERR_FAIL_INDEX(p_point, blend_points_used);
162
163
blend_points[p_point].position = p_position;
164
}
165
166
void AnimationNodeBlendSpace1D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {
167
ERR_FAIL_INDEX(p_point, blend_points_used);
168
ERR_FAIL_COND(p_node.is_null());
169
170
if (blend_points[p_point].node.is_valid()) {
171
blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));
172
blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed));
173
blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed));
174
}
175
176
blend_points[p_point].node = p_node;
177
blend_points[p_point].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), CONNECT_REFERENCE_COUNTED);
178
blend_points[p_point].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
179
blend_points[p_point].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
180
181
emit_signal(SNAME("tree_changed"));
182
}
183
184
float AnimationNodeBlendSpace1D::get_blend_point_position(int p_point) const {
185
ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, 0);
186
return blend_points[p_point].position;
187
}
188
189
Ref<AnimationRootNode> AnimationNodeBlendSpace1D::get_blend_point_node(int p_point) const {
190
ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, Ref<AnimationRootNode>());
191
return blend_points[p_point].node;
192
}
193
194
void AnimationNodeBlendSpace1D::remove_blend_point(int p_point) {
195
ERR_FAIL_INDEX(p_point, blend_points_used);
196
197
ERR_FAIL_COND(blend_points[p_point].node.is_null());
198
blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));
199
blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed));
200
blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed));
201
202
for (int i = p_point; i < blend_points_used - 1; i++) {
203
blend_points[i] = blend_points[i + 1];
204
}
205
206
blend_points_used--;
207
208
emit_signal(SNAME("animation_node_removed"), get_instance_id(), itos(p_point));
209
emit_signal(SNAME("tree_changed"));
210
}
211
212
int AnimationNodeBlendSpace1D::get_blend_point_count() const {
213
return blend_points_used;
214
}
215
216
void AnimationNodeBlendSpace1D::set_min_space(float p_min) {
217
min_space = p_min;
218
219
if (min_space >= max_space) {
220
min_space = max_space - 1;
221
}
222
}
223
224
float AnimationNodeBlendSpace1D::get_min_space() const {
225
return min_space;
226
}
227
228
void AnimationNodeBlendSpace1D::set_max_space(float p_max) {
229
max_space = p_max;
230
231
if (max_space <= min_space) {
232
max_space = min_space + 1;
233
}
234
}
235
236
float AnimationNodeBlendSpace1D::get_max_space() const {
237
return max_space;
238
}
239
240
void AnimationNodeBlendSpace1D::set_snap(float p_snap) {
241
snap = p_snap;
242
}
243
244
float AnimationNodeBlendSpace1D::get_snap() const {
245
return snap;
246
}
247
248
void AnimationNodeBlendSpace1D::set_value_label(const String &p_label) {
249
value_label = p_label;
250
}
251
252
String AnimationNodeBlendSpace1D::get_value_label() const {
253
return value_label;
254
}
255
256
void AnimationNodeBlendSpace1D::set_blend_mode(BlendMode p_blend_mode) {
257
blend_mode = p_blend_mode;
258
}
259
260
AnimationNodeBlendSpace1D::BlendMode AnimationNodeBlendSpace1D::get_blend_mode() const {
261
return blend_mode;
262
}
263
264
void AnimationNodeBlendSpace1D::set_use_sync(bool p_sync) {
265
sync = p_sync;
266
}
267
268
bool AnimationNodeBlendSpace1D::is_using_sync() const {
269
return sync;
270
}
271
272
void AnimationNodeBlendSpace1D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
273
if (p_index == blend_points_used) {
274
add_blend_point(p_node, 0);
275
} else {
276
set_blend_point_node(p_index, p_node);
277
}
278
}
279
280
AnimationNode::NodeTimeInfo AnimationNodeBlendSpace1D::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
281
if (!blend_points_used) {
282
return NodeTimeInfo();
283
}
284
285
AnimationMixer::PlaybackInfo pi = p_playback_info;
286
287
if (blend_points_used == 1) {
288
// only one point available, just play that animation
289
pi.weight = 1.0;
290
return blend_node(blend_points[0].node, blend_points[0].name, pi, FILTER_IGNORE, true, p_test_only);
291
}
292
293
double blend_pos = get_parameter(blend_position);
294
int cur_closest = get_parameter(closest);
295
NodeTimeInfo mind;
296
297
if (blend_mode == BLEND_MODE_INTERPOLATED) {
298
int point_lower = -1;
299
float pos_lower = 0.0;
300
int point_higher = -1;
301
float pos_higher = 0.0;
302
303
// find the closest two points to blend between
304
for (int i = 0; i < blend_points_used; i++) {
305
float pos = blend_points[i].position;
306
307
if (pos <= blend_pos) {
308
if (point_lower == -1 || pos > pos_lower) {
309
point_lower = i;
310
pos_lower = pos;
311
}
312
} else if (point_higher == -1 || pos < pos_higher) {
313
point_higher = i;
314
pos_higher = pos;
315
}
316
}
317
318
// fill in weights
319
float weights[MAX_BLEND_POINTS] = {};
320
if (point_lower == -1 && point_higher != -1) {
321
// we are on the left side, no other point to the left
322
// we just play the next point.
323
324
weights[point_higher] = 1.0;
325
} else if (point_higher == -1) {
326
// we are on the right side, no other point to the right
327
// we just play the previous point
328
329
weights[point_lower] = 1.0;
330
} else {
331
// we are between two points.
332
// figure out weights, then blend the animations
333
334
float distance_between_points = pos_higher - pos_lower;
335
336
float current_pos_inbetween = blend_pos - pos_lower;
337
338
float blend_percentage = current_pos_inbetween / distance_between_points;
339
340
float blend_lower = 1.0 - blend_percentage;
341
float blend_higher = blend_percentage;
342
343
weights[point_lower] = blend_lower;
344
weights[point_higher] = blend_higher;
345
}
346
347
// actually blend the animations now
348
bool first = true;
349
double max_weight = 0.0;
350
for (int i = 0; i < blend_points_used; i++) {
351
if (i == point_lower || i == point_higher) {
352
pi.weight = weights[i];
353
NodeTimeInfo t = blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
354
if (first || pi.weight > max_weight) {
355
max_weight = pi.weight;
356
mind = t;
357
first = false;
358
}
359
} else if (sync) {
360
pi.weight = 0;
361
blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
362
}
363
}
364
} else {
365
int new_closest = -1;
366
double new_closest_dist = 1e20;
367
368
for (int i = 0; i < blend_points_used; i++) {
369
double d = std::abs(blend_points[i].position - blend_pos);
370
if (d < new_closest_dist) {
371
new_closest = i;
372
new_closest_dist = d;
373
}
374
}
375
376
if (new_closest != cur_closest && new_closest != -1) {
377
if (blend_mode == BLEND_MODE_DISCRETE_CARRY && cur_closest != -1) {
378
NodeTimeInfo from;
379
// For ping-pong loop.
380
Ref<AnimationNodeAnimation> na_c = static_cast<Ref<AnimationNodeAnimation>>(blend_points[cur_closest].node);
381
Ref<AnimationNodeAnimation> na_n = static_cast<Ref<AnimationNodeAnimation>>(blend_points[new_closest].node);
382
if (na_c.is_valid() && na_n.is_valid()) {
383
na_n->process_state = process_state;
384
na_c->process_state = process_state;
385
386
na_n->set_backward(na_c->is_backward());
387
388
na_n = nullptr;
389
na_c = nullptr;
390
}
391
// See how much animation remains.
392
pi.seeked = false;
393
pi.weight = 0;
394
from = blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, true);
395
pi.time = from.position;
396
}
397
pi.seeked = true;
398
pi.weight = 1.0;
399
mind = blend_node(blend_points[new_closest].node, blend_points[new_closest].name, pi, FILTER_IGNORE, true, p_test_only);
400
cur_closest = new_closest;
401
} else {
402
pi.weight = 1.0;
403
mind = blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, p_test_only);
404
}
405
406
if (sync) {
407
pi = p_playback_info;
408
pi.weight = 0;
409
for (int i = 0; i < blend_points_used; i++) {
410
if (i != cur_closest) {
411
blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
412
}
413
}
414
}
415
}
416
417
set_parameter(closest, cur_closest);
418
return mind;
419
}
420
421
String AnimationNodeBlendSpace1D::get_caption() const {
422
return "BlendSpace1D";
423
}
424
425
AnimationNodeBlendSpace1D::AnimationNodeBlendSpace1D() {
426
for (int i = 0; i < MAX_BLEND_POINTS; i++) {
427
blend_points[i].name = itos(i);
428
}
429
}
430
431
AnimationNodeBlendSpace1D::~AnimationNodeBlendSpace1D() {
432
}
433
434