Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/animation/tween.h
9903 views
1
/**************************************************************************/
2
/* tween.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 "core/object/ref_counted.h"
34
35
class Tween;
36
class Node;
37
class SceneTree;
38
39
class Tweener : public RefCounted {
40
GDCLASS(Tweener, RefCounted);
41
42
ObjectID tween_id;
43
44
public:
45
virtual void set_tween(const Ref<Tween> &p_tween);
46
virtual void start();
47
virtual bool step(double &r_delta) = 0;
48
49
protected:
50
static void _bind_methods();
51
52
Ref<Tween> _get_tween();
53
void _finish();
54
55
double elapsed_time = 0;
56
bool finished = false;
57
};
58
59
class PropertyTweener;
60
class IntervalTweener;
61
class CallbackTweener;
62
class MethodTweener;
63
class SubtweenTweener;
64
65
class Tween : public RefCounted {
66
GDCLASS(Tween, RefCounted);
67
68
friend class PropertyTweener;
69
70
public:
71
enum TweenProcessMode {
72
TWEEN_PROCESS_PHYSICS,
73
TWEEN_PROCESS_IDLE,
74
};
75
76
enum TweenPauseMode {
77
TWEEN_PAUSE_BOUND,
78
TWEEN_PAUSE_STOP,
79
TWEEN_PAUSE_PROCESS,
80
};
81
82
enum TransitionType {
83
TRANS_LINEAR,
84
TRANS_SINE,
85
TRANS_QUINT,
86
TRANS_QUART,
87
TRANS_QUAD,
88
TRANS_EXPO,
89
TRANS_ELASTIC,
90
TRANS_CUBIC,
91
TRANS_CIRC,
92
TRANS_BOUNCE,
93
TRANS_BACK,
94
TRANS_SPRING,
95
TRANS_MAX
96
};
97
98
enum EaseType {
99
EASE_IN,
100
EASE_OUT,
101
EASE_IN_OUT,
102
EASE_OUT_IN,
103
EASE_MAX
104
};
105
106
private:
107
TweenProcessMode process_mode = TweenProcessMode::TWEEN_PROCESS_IDLE;
108
TweenPauseMode pause_mode = TweenPauseMode::TWEEN_PAUSE_BOUND;
109
TransitionType default_transition = TransitionType::TRANS_LINEAR;
110
EaseType default_ease = EaseType::EASE_IN_OUT;
111
ObjectID bound_node;
112
113
SceneTree *parent_tree = nullptr;
114
Vector<List<Ref<Tweener>>> tweeners;
115
double total_time = 0;
116
int current_step = -1;
117
int loops = 1;
118
int loops_done = 0;
119
float speed_scale = 1;
120
bool ignore_time_scale = false;
121
122
bool is_bound = false;
123
bool started = false;
124
bool running = true;
125
bool in_step = false;
126
bool dead = false;
127
bool valid = false;
128
bool default_parallel = false;
129
bool parallel_enabled = false;
130
#ifdef DEBUG_ENABLED
131
bool is_infinite = false;
132
#endif
133
134
typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d);
135
static interpolater interpolaters[TRANS_MAX][EASE_MAX];
136
137
void _start_tweeners();
138
void _stop_internal(bool p_reset);
139
140
protected:
141
static void _bind_methods();
142
143
public:
144
virtual String to_string() override;
145
146
Ref<PropertyTweener> tween_property(const Object *p_target, const NodePath &p_property, Variant p_to, double p_duration);
147
Ref<IntervalTweener> tween_interval(double p_time);
148
Ref<CallbackTweener> tween_callback(const Callable &p_callback);
149
Ref<MethodTweener> tween_method(const Callable &p_callback, const Variant p_from, Variant p_to, double p_duration);
150
Ref<SubtweenTweener> tween_subtween(const Ref<Tween> &p_subtween);
151
void append(Ref<Tweener> p_tweener);
152
153
bool custom_step(double p_delta);
154
void stop();
155
void pause();
156
void play();
157
void kill();
158
159
bool is_running();
160
bool is_valid();
161
void clear();
162
163
Ref<Tween> bind_node(const Node *p_node);
164
Ref<Tween> set_process_mode(TweenProcessMode p_mode);
165
TweenProcessMode get_process_mode() const;
166
Ref<Tween> set_pause_mode(TweenPauseMode p_mode);
167
TweenPauseMode get_pause_mode() const;
168
Ref<Tween> set_ignore_time_scale(bool p_ignore = true);
169
bool is_ignoring_time_scale() const;
170
171
Ref<Tween> set_parallel(bool p_parallel);
172
Ref<Tween> set_loops(int p_loops);
173
int get_loops_left() const;
174
Ref<Tween> set_speed_scale(float p_speed);
175
Ref<Tween> set_trans(TransitionType p_trans);
176
TransitionType get_trans() const;
177
Ref<Tween> set_ease(EaseType p_ease);
178
EaseType get_ease() const;
179
180
Ref<Tween> parallel();
181
Ref<Tween> chain();
182
183
static real_t run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t t, real_t b, real_t c, real_t d);
184
static Variant interpolate_variant(const Variant &p_initial_val, const Variant &p_delta_val, double p_time, double p_duration, Tween::TransitionType p_trans, Tween::EaseType p_ease);
185
186
bool step(double p_delta);
187
bool can_process(bool p_tree_paused) const;
188
Node *get_bound_node() const;
189
double get_total_time() const;
190
191
Tween();
192
Tween(SceneTree *p_parent_tree);
193
};
194
195
VARIANT_ENUM_CAST(Tween::TweenPauseMode);
196
VARIANT_ENUM_CAST(Tween::TweenProcessMode);
197
VARIANT_ENUM_CAST(Tween::TransitionType);
198
VARIANT_ENUM_CAST(Tween::EaseType);
199
200
class PropertyTweener : public Tweener {
201
GDCLASS(PropertyTweener, Tweener);
202
203
double _get_custom_interpolated_value(const Variant &p_value);
204
205
public:
206
Ref<PropertyTweener> from(const Variant &p_value);
207
Ref<PropertyTweener> from_current();
208
Ref<PropertyTweener> as_relative();
209
Ref<PropertyTweener> set_trans(Tween::TransitionType p_trans);
210
Ref<PropertyTweener> set_ease(Tween::EaseType p_ease);
211
Ref<PropertyTweener> set_custom_interpolator(const Callable &p_method);
212
Ref<PropertyTweener> set_delay(double p_delay);
213
214
void set_tween(const Ref<Tween> &p_tween) override;
215
void start() override;
216
bool step(double &r_delta) override;
217
218
PropertyTweener(const Object *p_target, const Vector<StringName> &p_property, const Variant &p_to, double p_duration);
219
PropertyTweener();
220
221
protected:
222
static void _bind_methods();
223
224
private:
225
ObjectID target;
226
Vector<StringName> property;
227
Variant initial_val;
228
Variant base_final_val;
229
Variant final_val;
230
Variant delta_val;
231
232
Ref<RefCounted> ref_copy; // Makes sure that RefCounted objects are not freed too early.
233
234
double duration = 0;
235
Tween::TransitionType trans_type = Tween::TRANS_MAX; // This is set inside set_tween();
236
Tween::EaseType ease_type = Tween::EASE_MAX;
237
Callable custom_method;
238
239
double delay = 0;
240
bool do_continue = true;
241
bool do_continue_delayed = false;
242
bool relative = false;
243
};
244
245
class IntervalTweener : public Tweener {
246
GDCLASS(IntervalTweener, Tweener);
247
248
public:
249
bool step(double &r_delta) override;
250
251
IntervalTweener(double p_time);
252
IntervalTweener();
253
254
private:
255
double duration = 0;
256
};
257
258
class CallbackTweener : public Tweener {
259
GDCLASS(CallbackTweener, Tweener);
260
261
public:
262
Ref<CallbackTweener> set_delay(double p_delay);
263
264
bool step(double &r_delta) override;
265
266
CallbackTweener(const Callable &p_callback);
267
CallbackTweener();
268
269
protected:
270
static void _bind_methods();
271
272
private:
273
Callable callback;
274
double delay = 0;
275
276
Ref<RefCounted> ref_copy;
277
};
278
279
class MethodTweener : public Tweener {
280
GDCLASS(MethodTweener, Tweener);
281
282
public:
283
Ref<MethodTweener> set_trans(Tween::TransitionType p_trans);
284
Ref<MethodTweener> set_ease(Tween::EaseType p_ease);
285
Ref<MethodTweener> set_delay(double p_delay);
286
287
void set_tween(const Ref<Tween> &p_tween) override;
288
bool step(double &r_delta) override;
289
290
MethodTweener(const Callable &p_callback, const Variant &p_from, const Variant &p_to, double p_duration);
291
MethodTweener();
292
293
protected:
294
static void _bind_methods();
295
296
private:
297
double duration = 0;
298
double delay = 0;
299
Tween::TransitionType trans_type = Tween::TRANS_MAX;
300
Tween::EaseType ease_type = Tween::EASE_MAX;
301
302
Variant initial_val;
303
Variant delta_val;
304
Variant final_val;
305
Callable callback;
306
307
Ref<RefCounted> ref_copy;
308
};
309
310
class SubtweenTweener : public Tweener {
311
GDCLASS(SubtweenTweener, Tweener);
312
313
public:
314
Ref<Tween> subtween;
315
void start() override;
316
bool step(double &r_delta) override;
317
318
Ref<SubtweenTweener> set_delay(double p_delay);
319
320
SubtweenTweener(const Ref<Tween> &p_subtween);
321
SubtweenTweener();
322
323
protected:
324
static void _bind_methods();
325
326
private:
327
double delay = 0;
328
};
329
330