Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/animation/animation_player.h
21520 views
1
/**************************************************************************/
2
/* animation_player.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
class AnimationPlayer : public AnimationMixer {
37
GDCLASS(AnimationPlayer, AnimationMixer);
38
39
#ifndef DISABLE_DEPRECATED
40
public:
41
enum AnimationProcessCallback {
42
ANIMATION_PROCESS_PHYSICS,
43
ANIMATION_PROCESS_IDLE,
44
ANIMATION_PROCESS_MANUAL,
45
};
46
enum AnimationMethodCallMode {
47
ANIMATION_METHOD_CALL_DEFERRED,
48
ANIMATION_METHOD_CALL_IMMEDIATE,
49
};
50
#endif // DISABLE_DEPRECATED
51
52
private:
53
AHashMap<StringName, StringName> animation_next_set; // For auto advance.
54
55
StringName finished_anim;
56
57
float speed_scale = 1.0;
58
double default_blend_time = 0.0;
59
60
bool auto_capture = true;
61
double auto_capture_duration = -1.0;
62
Tween::TransitionType auto_capture_transition_type = Tween::TRANS_LINEAR;
63
Tween::EaseType auto_capture_ease_type = Tween::EASE_IN;
64
65
bool is_stopping = false;
66
67
struct PlaybackData {
68
bool is_enabled = false;
69
String animation_name;
70
double animation_length = 0.0;
71
double pos = 0.0;
72
float speed_scale = 1.0;
73
double start_time = 0.0;
74
double end_time = 0.0;
75
double get_start_time() const {
76
if (is_enabled && (Animation::is_less_approx(start_time, 0) || Animation::is_greater_approx(start_time, animation_length))) {
77
return 0;
78
}
79
return start_time;
80
}
81
double get_end_time() const {
82
if (is_enabled && (Animation::is_less_approx(end_time, 0) || Animation::is_greater_approx(end_time, animation_length))) {
83
return animation_length;
84
}
85
return end_time;
86
}
87
};
88
89
struct Blend {
90
PlaybackData data;
91
double blend_time = 0.0;
92
double blend_left = 0.0;
93
};
94
95
struct Playback {
96
PlaybackData current;
97
StringName assigned;
98
bool seeked = false;
99
bool internal_seeked = false;
100
bool started = false;
101
List<Blend> blend;
102
} playback;
103
104
struct BlendKey {
105
StringName from;
106
StringName to;
107
static uint32_t hash(const BlendKey &p_key) {
108
return hash_one_uint64((uint64_t(p_key.from.hash()) << 32) | uint32_t(p_key.to.hash()));
109
}
110
bool operator==(const BlendKey &bk) const {
111
return from == bk.from && to == bk.to;
112
}
113
bool operator<(const BlendKey &bk) const {
114
if (from == bk.from) {
115
return StringName::AlphCompare()(to, bk.to);
116
} else {
117
return StringName::AlphCompare()(from, bk.from);
118
}
119
}
120
};
121
122
HashMap<BlendKey, double, BlendKey> blend_times;
123
124
List<StringName> playback_queue;
125
ObjectID tmp_from;
126
bool end_reached = false;
127
bool end_notify = false;
128
129
StringName autoplay;
130
131
bool movie_quit_on_finish = false;
132
133
void _play(const StringName &p_name, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
134
void _capture(const StringName &p_name, bool p_from_end = false, double p_duration = -1.0, Tween::TransitionType p_trans_type = Tween::TRANS_LINEAR, Tween::EaseType p_ease_type = Tween::EASE_IN);
135
void _process_playback_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_internal_seeked, bool p_started, bool p_is_current = false);
136
void _blend_playback_data(double p_delta, bool p_started);
137
void _stop_internal(bool p_reset, bool p_keep_state);
138
void _check_immediately_after_start();
139
140
float get_current_blend_amount();
141
142
bool playing = false;
143
144
protected:
145
bool _set(const StringName &p_name, const Variant &p_value);
146
bool _get(const StringName &p_name, Variant &r_ret) const;
147
virtual void _validate_property(PropertyInfo &p_property) const override;
148
void _get_property_list(List<PropertyInfo> *p_list) const;
149
void _notification(int p_what);
150
151
static void _bind_methods();
152
153
// Make animation instances.
154
virtual bool _blend_pre_process(double p_delta, int p_track_count, const AHashMap<NodePath, int> &p_track_map) override;
155
virtual void _blend_capture(double p_delta) override;
156
virtual void _blend_post_process() override;
157
158
virtual void _animation_removed(const StringName &p_name, const StringName &p_library) override;
159
virtual void _rename_animation(const StringName &p_from_name, const StringName &p_to_name) override;
160
161
#ifndef DISABLE_DEPRECATED
162
void _set_process_callback_bind_compat_80813(AnimationProcessCallback p_mode);
163
AnimationProcessCallback _get_process_callback_bind_compat_80813() const;
164
void _set_method_call_mode_bind_compat_80813(AnimationMethodCallMode p_mode);
165
AnimationMethodCallMode _get_method_call_mode_bind_compat_80813() const;
166
void _set_root_bind_compat_80813(const NodePath &p_root);
167
NodePath _get_root_bind_compat_80813() const;
168
void _seek_bind_compat_80813(double p_time, bool p_update = false);
169
void _play_compat_84906(const StringName &p_name = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
170
void _play_backwards_compat_84906(const StringName &p_name = StringName(), double p_custom_blend = -1);
171
172
Vector<String> _get_queue_compat_110767();
173
String _get_current_animation_compat_110767() const;
174
void _set_current_animation_compat_110767(const String &p_animation);
175
String _get_assigned_animation_compat_110767() const;
176
void _set_assigned_animation_compat_110767(const String &p_animation);
177
String _get_autoplay_compat_110767() const;
178
void _set_autoplay_compat_110767(const String &p_name);
179
180
static void _bind_compatibility_methods();
181
#endif // DISABLE_DEPRECATED
182
183
public:
184
void animation_set_next(const StringName &p_animation, const StringName &p_next);
185
StringName animation_get_next(const StringName &p_animation) const;
186
187
void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time);
188
double get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const;
189
190
void set_default_blend_time(double p_default);
191
double get_default_blend_time() const;
192
193
void set_auto_capture(bool p_auto_capture);
194
bool is_auto_capture() const;
195
void set_auto_capture_duration(double p_auto_capture_duration);
196
double get_auto_capture_duration() const;
197
void set_auto_capture_transition_type(Tween::TransitionType p_auto_capture_transition_type);
198
Tween::TransitionType get_auto_capture_transition_type() const;
199
void set_auto_capture_ease_type(Tween::EaseType p_auto_capture_ease_type);
200
Tween::EaseType get_auto_capture_ease_type() const;
201
202
#ifdef TOOLS_ENABLED
203
void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
204
#endif
205
206
void play(const StringName &p_name = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
207
void play_section_with_markers(const StringName &p_name = StringName(), const StringName &p_start_marker = StringName(), const StringName &p_end_marker = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
208
void play_section(const StringName &p_name = StringName(), double p_start_time = -1, double p_end_time = -1, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
209
void play_backwards(const StringName &p_name = StringName(), double p_custom_blend = -1);
210
void play_section_with_markers_backwards(const StringName &p_name = StringName(), const StringName &p_start_marker = StringName(), const StringName &p_end_marker = StringName(), double p_custom_blend = -1);
211
void play_section_backwards(const StringName &p_name = StringName(), double p_start_time = -1, double p_end_time = -1, double p_custom_blend = -1);
212
void play_with_capture(const StringName &p_name = StringName(), double p_duration = -1.0, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false, Tween::TransitionType p_trans_type = Tween::TRANS_LINEAR, Tween::EaseType p_ease_type = Tween::EASE_IN);
213
void queue(const StringName &p_name);
214
TypedArray<StringName> get_queue();
215
void clear_queue();
216
void pause();
217
void stop(bool p_keep_state = false);
218
bool is_playing() const;
219
StringName get_current_animation() const;
220
void set_current_animation(const StringName &p_animation);
221
StringName get_assigned_animation() const;
222
void set_assigned_animation(const StringName &p_animation);
223
bool is_valid() const;
224
225
void set_speed_scale(float p_speed);
226
float get_speed_scale() const;
227
float get_playing_speed() const;
228
229
void set_autoplay(const StringName &p_name);
230
StringName get_autoplay() const;
231
232
void set_movie_quit_on_finish_enabled(bool p_enabled);
233
bool is_movie_quit_on_finish_enabled() const;
234
235
void seek_internal(double p_time, bool p_update = false, bool p_update_only = false, bool p_is_internal_seek = false);
236
void seek(double p_time, bool p_update = false, bool p_update_only = false);
237
238
double get_current_animation_position() const;
239
double get_current_animation_length() const;
240
241
void set_section_with_markers(const StringName &p_start_marker = StringName(), const StringName &p_end_marker = StringName());
242
void set_section(double p_start_time = -1, double p_end_time = -1);
243
void reset_section();
244
245
double get_section_start_time() const;
246
double get_section_end_time() const;
247
bool has_section() const;
248
249
virtual void advance(double p_time) override;
250
251
AnimationPlayer();
252
~AnimationPlayer();
253
};
254
255
#ifndef DISABLE_DEPRECATED
256
VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessCallback);
257
VARIANT_ENUM_CAST(AnimationPlayer::AnimationMethodCallMode);
258
#endif // DISABLE_DEPRECATED
259
260