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