Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/animation/animation_tree.h
9903 views
1
/**************************************************************************/
2
/* animation_tree.h */
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
#pragma once
32
33
#include "animation_mixer.h"
34
#include "scene/resources/animation.h"
35
36
#define HUGE_LENGTH 31540000 // 31540000 seconds mean 1 year... is it too long? It must be longer than any Animation length and Transition xfade time to prevent time inversion for AnimationNodeStateMachine.
37
38
class AnimationNodeBlendTree;
39
class AnimationNodeStartState;
40
class AnimationNodeEndState;
41
class AnimationTree;
42
43
class AnimationNode : public Resource {
44
GDCLASS(AnimationNode, Resource);
45
46
public:
47
friend class AnimationTree;
48
49
enum FilterAction {
50
FILTER_IGNORE,
51
FILTER_PASS,
52
FILTER_STOP,
53
FILTER_BLEND
54
};
55
56
struct Input {
57
String name;
58
};
59
60
bool closable = false;
61
LocalVector<Input> inputs;
62
AHashMap<NodePath, bool> filter;
63
bool filter_enabled = false;
64
65
// To propagate information from upstream for use in estimation of playback progress.
66
// These values must be taken from the result of blend_node() or blend_input() and must be essentially read-only.
67
// For example, if you want to change the position, you need to change the pi.time value of PlaybackInfo passed to blend_input(pi) and get the result.
68
struct NodeTimeInfo {
69
// Retain the previous frame values. These are stored into the AnimationTree's Map and exposing them as read-only values.
70
double length = 0.0;
71
double position = 0.0;
72
double delta = 0.0;
73
74
// Needs internally to estimate remain time, the previous frame values are not retained.
75
Animation::LoopMode loop_mode = Animation::LOOP_NONE;
76
bool will_end = false; // For breaking loop, it is true when just looped.
77
bool is_infinity = false; // For unpredictable state machine's end.
78
79
bool is_looping() {
80
return loop_mode != Animation::LOOP_NONE;
81
}
82
double get_remain(bool p_break_loop = false) {
83
if ((is_looping() && !p_break_loop) || is_infinity) {
84
return HUGE_LENGTH;
85
}
86
if (is_looping() && p_break_loop && will_end) {
87
return 0;
88
}
89
double remain = length - position;
90
if (Math::is_zero_approx(remain)) {
91
return 0;
92
}
93
return remain;
94
}
95
};
96
97
// Temporary state for blending process which needs to be stored in each AnimationNodes.
98
struct NodeState {
99
friend AnimationNode;
100
101
private:
102
StringName base_path;
103
104
public:
105
AnimationNode *parent = nullptr;
106
Vector<StringName> connections;
107
LocalVector<real_t> track_weights;
108
109
const StringName get_base_path() const {
110
return base_path;
111
}
112
113
} node_state;
114
115
// Temporary state for blending process which needs to be started in the AnimationTree, pass through the AnimationNodes, and then return to the AnimationTree.
116
struct ProcessState {
117
AnimationTree *tree = nullptr;
118
const AHashMap<NodePath, int> *track_map; // TODO: Is there a better way to manage filter/tracks?
119
bool is_testing = false;
120
bool valid = false;
121
String invalid_reasons;
122
uint64_t last_pass = 0;
123
} *process_state = nullptr;
124
125
private:
126
mutable AHashMap<StringName, int> property_cache;
127
128
public:
129
void set_node_state_base_path(const StringName p_base_path) {
130
if (p_base_path != node_state.base_path) {
131
node_state.base_path = p_base_path;
132
make_cache_dirty();
133
}
134
}
135
136
void set_node_state_base_path(const String p_base_path) {
137
if (p_base_path != node_state.base_path) {
138
node_state.base_path = p_base_path;
139
make_cache_dirty();
140
}
141
}
142
143
const StringName get_node_state_base_path() const {
144
return node_state.get_base_path();
145
}
146
147
void make_cache_dirty() {
148
property_cache.clear();
149
}
150
Array _get_filters() const;
151
void _set_filters(const Array &p_filters);
152
friend class AnimationNodeBlendTree;
153
154
// The time information is passed from upstream to downstream by AnimationMixer::PlaybackInfo::p_playback_info until AnimationNodeAnimation processes it.
155
// Conversely, AnimationNodeAnimation returns the processed result as NodeTimeInfo from downstream to upstream.
156
NodeTimeInfo _blend_node(Ref<AnimationNode> p_node, const StringName &p_subpath, AnimationNode *p_new_parent, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false, real_t *r_activity = nullptr);
157
NodeTimeInfo _pre_process(ProcessState *p_process_state, AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false);
158
159
protected:
160
StringName current_length = "current_length";
161
StringName current_position = "current_position";
162
StringName current_delta = "current_delta";
163
164
virtual NodeTimeInfo process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false); // To organize time information. Virtualizing for especially AnimationNodeAnimation needs to take "backward" into account.
165
virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false); // Main process.
166
167
void blend_animation(const StringName &p_animation, AnimationMixer::PlaybackInfo p_playback_info);
168
NodeTimeInfo blend_node(Ref<AnimationNode> p_node, const StringName &p_subpath, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false);
169
NodeTimeInfo blend_input(int p_input, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false);
170
171
// Bind-able methods to expose for compatibility, moreover AnimationMixer::PlaybackInfo is not exposed.
172
void blend_animation_ex(const StringName &p_animation, double p_time, double p_delta, bool p_seeked, bool p_is_external_seeking, real_t p_blend, Animation::LoopedFlag p_looped_flag = Animation::LOOPED_FLAG_NONE);
173
double blend_node_ex(const StringName &p_sub_path, Ref<AnimationNode> p_node, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false);
174
double blend_input_ex(int p_input, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false);
175
176
void make_invalid(const String &p_reason);
177
AnimationTree *get_animation_tree() const;
178
179
static void _bind_methods();
180
181
void _validate_property(PropertyInfo &p_property) const;
182
183
GDVIRTUAL0RC(Dictionary, _get_child_nodes)
184
GDVIRTUAL0RC(Array, _get_parameter_list)
185
GDVIRTUAL1RC(Ref<AnimationNode>, _get_child_by_name, StringName)
186
GDVIRTUAL1RC(Variant, _get_parameter_default_value, StringName)
187
GDVIRTUAL1RC(bool, _is_parameter_read_only, StringName)
188
GDVIRTUAL4R(double, _process, double, bool, bool, bool)
189
GDVIRTUAL0RC(String, _get_caption)
190
GDVIRTUAL0RC(bool, _has_filter)
191
192
public:
193
virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
194
virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
195
virtual bool is_parameter_read_only(const StringName &p_parameter) const;
196
197
void set_parameter(const StringName &p_name, const Variant &p_value);
198
Variant get_parameter(const StringName &p_name) const;
199
200
void set_node_time_info(const NodeTimeInfo &p_node_time_info); // Wrapper of set_parameter().
201
virtual NodeTimeInfo get_node_time_info() const; // Wrapper of get_parameter().
202
203
struct ChildNode {
204
StringName name;
205
Ref<AnimationNode> node;
206
};
207
208
virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
209
210
virtual String get_caption() const;
211
212
virtual bool add_input(const String &p_name);
213
virtual void remove_input(int p_index);
214
virtual bool set_input_name(int p_input, const String &p_name);
215
virtual String get_input_name(int p_input) const;
216
int get_input_count() const;
217
int find_input(const String &p_name) const;
218
219
void set_filter_path(const NodePath &p_path, bool p_enable);
220
bool is_path_filtered(const NodePath &p_path) const;
221
222
void set_filter_enabled(bool p_enable);
223
bool is_filter_enabled() const;
224
225
void set_deletable(bool p_closable);
226
bool is_deletable() const;
227
228
ObjectID get_processing_animation_tree_instance_id() const;
229
230
bool is_process_testing() const;
231
232
virtual bool has_filter() const;
233
234
#ifdef TOOLS_ENABLED
235
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
236
#endif
237
238
virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name) const;
239
Ref<AnimationNode> find_node_by_path(const String &p_name) const;
240
241
AnimationNode();
242
};
243
244
VARIANT_ENUM_CAST(AnimationNode::FilterAction)
245
246
// Root node does not allow inputs.
247
class AnimationRootNode : public AnimationNode {
248
GDCLASS(AnimationRootNode, AnimationNode);
249
250
protected:
251
virtual void _tree_changed();
252
virtual void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name);
253
virtual void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node);
254
};
255
256
class AnimationNodeStartState : public AnimationRootNode {
257
GDCLASS(AnimationNodeStartState, AnimationRootNode);
258
};
259
260
class AnimationNodeEndState : public AnimationRootNode {
261
GDCLASS(AnimationNodeEndState, AnimationRootNode);
262
};
263
264
class AnimationTree : public AnimationMixer {
265
GDCLASS(AnimationTree, AnimationMixer);
266
267
#ifndef DISABLE_DEPRECATED
268
public:
269
enum AnimationProcessCallback {
270
ANIMATION_PROCESS_PHYSICS,
271
ANIMATION_PROCESS_IDLE,
272
ANIMATION_PROCESS_MANUAL,
273
};
274
#endif // DISABLE_DEPRECATED
275
276
private:
277
Ref<AnimationRootNode> root_animation_node;
278
NodePath advance_expression_base_node = NodePath(String("."));
279
280
AnimationNode::ProcessState process_state;
281
uint64_t process_pass = 1;
282
283
bool started = true;
284
285
friend class AnimationNode;
286
287
mutable List<PropertyInfo> properties;
288
mutable AHashMap<StringName, AHashMap<StringName, StringName>> property_parent_map;
289
mutable AHashMap<ObjectID, StringName> property_reference_map;
290
mutable AHashMap<StringName, Pair<Variant, bool>> property_map; // Property value and read-only flag.
291
292
mutable bool properties_dirty = true;
293
294
void _update_properties() const;
295
void _update_properties_for_node(const String &p_base_path, Ref<AnimationNode> p_node) const;
296
297
void _tree_changed();
298
void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name);
299
void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node);
300
301
struct Activity {
302
uint64_t last_pass = 0;
303
real_t activity = 0.0;
304
};
305
mutable AHashMap<StringName, LocalVector<Activity>> input_activity_map;
306
mutable AHashMap<StringName, int> input_activity_map_get;
307
308
NodePath animation_player;
309
310
void _setup_animation_player();
311
void _animation_player_changed();
312
313
bool _set(const StringName &p_name, const Variant &p_value);
314
bool _get(const StringName &p_name, Variant &r_ret) const;
315
void _get_property_list(List<PropertyInfo> *p_list) const;
316
virtual void _validate_property(PropertyInfo &p_property) const override;
317
void _notification(int p_what);
318
319
static void _bind_methods();
320
321
virtual void _set_active(bool p_active) override;
322
323
// Make animation instances.
324
virtual bool _blend_pre_process(double p_delta, int p_track_count, const AHashMap<NodePath, int> &p_track_map) override;
325
326
#ifndef DISABLE_DEPRECATED
327
void _set_process_callback_bind_compat_80813(AnimationProcessCallback p_mode);
328
AnimationProcessCallback _get_process_callback_bind_compat_80813() const;
329
void _set_tree_root_bind_compat_80813(const Ref<AnimationNode> &p_root);
330
Ref<AnimationNode> _get_tree_root_bind_compat_80813() const;
331
332
static void _bind_compatibility_methods();
333
#endif // DISABLE_DEPRECATED
334
335
public:
336
void set_animation_player(const NodePath &p_path);
337
NodePath get_animation_player() const;
338
339
void set_root_animation_node(const Ref<AnimationRootNode> &p_animation_node);
340
Ref<AnimationRootNode> get_root_animation_node() const;
341
342
void set_advance_expression_base_node(const NodePath &p_path);
343
NodePath get_advance_expression_base_node() const;
344
345
PackedStringArray get_configuration_warnings() const override;
346
347
bool is_state_invalid() const;
348
String get_invalid_state_reason() const;
349
350
real_t get_connection_activity(const StringName &p_path, int p_connection) const;
351
352
uint64_t get_last_process_pass() const;
353
354
AnimationTree();
355
~AnimationTree();
356
};
357
358
#ifndef DISABLE_DEPRECATED
359
VARIANT_ENUM_CAST(AnimationTree::AnimationProcessCallback);
360
#endif // DISABLE_DEPRECATED
361
362