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) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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
get_parameter_list(List<PropertyInfo> * r_list) const33 void AnimationNodeBlendSpace1D::get_parameter_list(List<PropertyInfo> *r_list) const {
34 r_list->push_back(PropertyInfo(Variant::REAL, blend_position));
35 }
get_parameter_default_value(const StringName & p_parameter) const36 Variant AnimationNodeBlendSpace1D::get_parameter_default_value(const StringName &p_parameter) const {
37 return 0;
38 }
39
get_child_by_name(const StringName & p_name)40 Ref<AnimationNode> AnimationNodeBlendSpace1D::get_child_by_name(const StringName &p_name) {
41 return get_blend_point_node(p_name.operator String().to_int());
42 }
43
_validate_property(PropertyInfo & property) const44 void AnimationNodeBlendSpace1D::_validate_property(PropertyInfo &property) const {
45 if (property.name.begins_with("blend_point_")) {
46 String left = property.name.get_slicec('/', 0);
47 int idx = left.get_slicec('_', 2).to_int();
48 if (idx >= blend_points_used) {
49 property.usage = 0;
50 }
51 }
52 AnimationRootNode::_validate_property(property);
53 }
54
_tree_changed()55 void AnimationNodeBlendSpace1D::_tree_changed() {
56 emit_signal("tree_changed");
57 }
58
_bind_methods()59 void AnimationNodeBlendSpace1D::_bind_methods() {
60 ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace1D::add_blend_point, DEFVAL(-1));
61 ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace1D::set_blend_point_position);
62 ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace1D::get_blend_point_position);
63 ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace1D::set_blend_point_node);
64 ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace1D::get_blend_point_node);
65 ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace1D::remove_blend_point);
66 ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace1D::get_blend_point_count);
67
68 ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace1D::set_min_space);
69 ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace1D::get_min_space);
70
71 ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace1D::set_max_space);
72 ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace1D::get_max_space);
73
74 ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace1D::set_snap);
75 ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace1D::get_snap);
76
77 ClassDB::bind_method(D_METHOD("set_value_label", "text"), &AnimationNodeBlendSpace1D::set_value_label);
78 ClassDB::bind_method(D_METHOD("get_value_label"), &AnimationNodeBlendSpace1D::get_value_label);
79
80 ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace1D::_add_blend_point);
81
82 ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeBlendSpace1D::_tree_changed);
83
84 for (int i = 0; i < MAX_BLEND_POINTS; i++) {
85 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_add_blend_point", "get_blend_point_node", i);
86 ADD_PROPERTYI(PropertyInfo(Variant::REAL, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);
87 }
88
89 ADD_PROPERTY(PropertyInfo(Variant::REAL, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_min_space", "get_min_space");
90 ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_max_space", "get_max_space");
91 ADD_PROPERTY(PropertyInfo(Variant::REAL, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_snap", "get_snap");
92 ADD_PROPERTY(PropertyInfo(Variant::STRING, "value_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_value_label", "get_value_label");
93 }
94
get_child_nodes(List<ChildNode> * r_child_nodes)95 void AnimationNodeBlendSpace1D::get_child_nodes(List<ChildNode> *r_child_nodes) {
96 for (int i = 0; i < blend_points_used; i++) {
97 ChildNode cn;
98 cn.name = itos(i);
99 cn.node = blend_points[i].node;
100 r_child_nodes->push_back(cn);
101 }
102 }
103
add_blend_point(const Ref<AnimationRootNode> & p_node,float p_position,int p_at_index)104 void AnimationNodeBlendSpace1D::add_blend_point(const Ref<AnimationRootNode> &p_node, float p_position, int p_at_index) {
105 ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);
106 ERR_FAIL_COND(p_node.is_null());
107
108 ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used);
109
110 if (p_at_index == -1 || p_at_index == blend_points_used) {
111 p_at_index = blend_points_used;
112 } else {
113 for (int i = blend_points_used - 1; i > p_at_index; i--) {
114 blend_points[i] = blend_points[i - 1];
115 }
116 }
117
118 blend_points[p_at_index].node = p_node;
119 blend_points[p_at_index].position = p_position;
120
121 blend_points[p_at_index].node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
122
123 blend_points_used++;
124 emit_signal("tree_changed");
125 }
126
set_blend_point_position(int p_point,float p_position)127 void AnimationNodeBlendSpace1D::set_blend_point_position(int p_point, float p_position) {
128 ERR_FAIL_INDEX(p_point, blend_points_used);
129
130 blend_points[p_point].position = p_position;
131 }
132
set_blend_point_node(int p_point,const Ref<AnimationRootNode> & p_node)133 void AnimationNodeBlendSpace1D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {
134 ERR_FAIL_INDEX(p_point, blend_points_used);
135 ERR_FAIL_COND(p_node.is_null());
136
137 if (blend_points[p_point].node.is_valid()) {
138 blend_points[p_point].node->disconnect("tree_changed", this, "_tree_changed");
139 }
140
141 blend_points[p_point].node = p_node;
142 blend_points[p_point].node->connect("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED);
143
144 emit_signal("tree_changed");
145 }
146
get_blend_point_position(int p_point) const147 float AnimationNodeBlendSpace1D::get_blend_point_position(int p_point) const {
148 ERR_FAIL_INDEX_V(p_point, blend_points_used, 0);
149 return blend_points[p_point].position;
150 }
151
get_blend_point_node(int p_point) const152 Ref<AnimationRootNode> AnimationNodeBlendSpace1D::get_blend_point_node(int p_point) const {
153 ERR_FAIL_INDEX_V(p_point, blend_points_used, Ref<AnimationRootNode>());
154 return blend_points[p_point].node;
155 }
156
remove_blend_point(int p_point)157 void AnimationNodeBlendSpace1D::remove_blend_point(int p_point) {
158 ERR_FAIL_INDEX(p_point, blend_points_used);
159
160 ERR_FAIL_COND(blend_points[p_point].node.is_null());
161 blend_points[p_point].node->disconnect("tree_changed", this, "_tree_changed");
162
163 for (int i = p_point; i < blend_points_used - 1; i++) {
164 blend_points[i] = blend_points[i + 1];
165 }
166
167 blend_points_used--;
168 emit_signal("tree_changed");
169 }
170
get_blend_point_count() const171 int AnimationNodeBlendSpace1D::get_blend_point_count() const {
172
173 return blend_points_used;
174 }
175
set_min_space(float p_min)176 void AnimationNodeBlendSpace1D::set_min_space(float p_min) {
177 min_space = p_min;
178
179 if (min_space >= max_space) {
180 min_space = max_space - 1;
181 }
182 }
183
get_min_space() const184 float AnimationNodeBlendSpace1D::get_min_space() const {
185 return min_space;
186 }
187
set_max_space(float p_max)188 void AnimationNodeBlendSpace1D::set_max_space(float p_max) {
189 max_space = p_max;
190
191 if (max_space <= min_space) {
192 max_space = min_space + 1;
193 }
194 }
195
get_max_space() const196 float AnimationNodeBlendSpace1D::get_max_space() const {
197 return max_space;
198 }
199
set_snap(float p_snap)200 void AnimationNodeBlendSpace1D::set_snap(float p_snap) {
201 snap = p_snap;
202 }
203
get_snap() const204 float AnimationNodeBlendSpace1D::get_snap() const {
205 return snap;
206 }
207
set_value_label(const String & p_label)208 void AnimationNodeBlendSpace1D::set_value_label(const String &p_label) {
209 value_label = p_label;
210 }
211
get_value_label() const212 String AnimationNodeBlendSpace1D::get_value_label() const {
213 return value_label;
214 }
215
_add_blend_point(int p_index,const Ref<AnimationRootNode> & p_node)216 void AnimationNodeBlendSpace1D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
217 if (p_index == blend_points_used) {
218 add_blend_point(p_node, 0);
219 } else {
220 set_blend_point_node(p_index, p_node);
221 }
222 }
223
process(float p_time,bool p_seek)224 float AnimationNodeBlendSpace1D::process(float p_time, bool p_seek) {
225
226 if (blend_points_used == 0) {
227 return 0.0;
228 }
229
230 if (blend_points_used == 1) {
231 // only one point available, just play that animation
232 return blend_node(blend_points[0].name, blend_points[0].node, p_time, p_seek, 1.0, FILTER_IGNORE, false);
233 }
234
235 float blend_pos = get_parameter(blend_position);
236
237 float weights[MAX_BLEND_POINTS] = {};
238
239 int point_lower = -1;
240 float pos_lower = 0.0;
241 int point_higher = -1;
242 float pos_higher = 0.0;
243
244 // find the closest two points to blend between
245 for (int i = 0; i < blend_points_used; i++) {
246
247 float pos = blend_points[i].position;
248
249 if (pos <= blend_pos) {
250 if (point_lower == -1) {
251 point_lower = i;
252 pos_lower = pos;
253 } else if ((blend_pos - pos) < (blend_pos - pos_lower)) {
254 point_lower = i;
255 pos_lower = pos;
256 }
257 } else {
258 if (point_higher == -1) {
259 point_higher = i;
260 pos_higher = pos;
261 } else if ((pos - blend_pos) < (pos_higher - blend_pos)) {
262 point_higher = i;
263 pos_higher = pos;
264 }
265 }
266 }
267
268 // fill in weights
269
270 if (point_lower == -1 && point_higher != -1) {
271 // we are on the left side, no other point to the left
272 // we just play the next point.
273
274 weights[point_higher] = 1.0;
275 } else if (point_higher == -1) {
276 // we are on the right side, no other point to the right
277 // we just play the previous point
278
279 weights[point_lower] = 1.0;
280 } else {
281
282 // we are between two points.
283 // figure out weights, then blend the animations
284
285 float distance_between_points = pos_higher - pos_lower;
286
287 float current_pos_inbetween = blend_pos - pos_lower;
288
289 float blend_percentage = current_pos_inbetween / distance_between_points;
290
291 float blend_lower = 1.0 - blend_percentage;
292 float blend_higher = blend_percentage;
293
294 weights[point_lower] = blend_lower;
295 weights[point_higher] = blend_higher;
296 }
297
298 // actually blend the animations now
299
300 float max_time_remaining = 0.0;
301
302 for (int i = 0; i < blend_points_used; i++) {
303 float remaining = blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, weights[i], FILTER_IGNORE, false);
304
305 max_time_remaining = MAX(max_time_remaining, remaining);
306 }
307
308 return max_time_remaining;
309 }
310
get_caption() const311 String AnimationNodeBlendSpace1D::get_caption() const {
312 return "BlendSpace1D";
313 }
314
AnimationNodeBlendSpace1D()315 AnimationNodeBlendSpace1D::AnimationNodeBlendSpace1D() {
316
317 for (int i = 0; i < MAX_BLEND_POINTS; i++) {
318 blend_points[i].name = itos(i);
319 }
320 blend_points_used = 0;
321 max_space = 1;
322 min_space = -1;
323
324 snap = 0.1;
325 value_label = "value";
326
327 blend_position = "blend_position";
328 }
329
~AnimationNodeBlendSpace1D()330 AnimationNodeBlendSpace1D::~AnimationNodeBlendSpace1D() {
331 }
332