Complete rewrite of Tweens
* Tweens were changed from Node to RefCounted. New API is inspired by DOTween. * Tweens are created and managed by SceneTree, similar to SceneTreeTimer, which makes them ultra cheap to use a lot. * Animating with Tweens is done by creating sequences of Tweeners. You create them from code and they autostart by default (fire-and-forget). * There are 4 Tweeners that cover the former Tween functionality: PropertyTweener, IntervalTweener, CallbackTweener and MethodTweener. * The methods were simplified a lot. Long argument lists are replaced with chained calls on Tweens and Tweeners. * Tweeners by default execute in sequence, so it's easy to create complex chained animations. * You can bind a Tween to a node. Tween will be removed automatically when the bound node is freed.
This commit is contained in:
parent
12e0f10c74
commit
900b2e0fdc
16 changed files with 1399 additions and 2005 deletions
|
|
@ -31,10 +31,33 @@
|
|||
#ifndef TWEEN_H
|
||||
#define TWEEN_H
|
||||
|
||||
#include "scene/main/node.h"
|
||||
#include "core/object/ref_counted.h"
|
||||
|
||||
class Tween : public Node {
|
||||
GDCLASS(Tween, Node);
|
||||
class Tween;
|
||||
class Node;
|
||||
|
||||
class Tweener : public RefCounted {
|
||||
GDCLASS(Tweener, RefCounted);
|
||||
|
||||
public:
|
||||
virtual void set_tween(Ref<Tween> p_tween);
|
||||
virtual void start() = 0;
|
||||
virtual bool step(float &r_delta) = 0;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
Ref<Tween> tween;
|
||||
float elapsed_time = 0;
|
||||
bool finished = false;
|
||||
};
|
||||
|
||||
class PropertyTweener;
|
||||
class IntervalTweener;
|
||||
class CallbackTweener;
|
||||
class MethodTweener;
|
||||
|
||||
class Tween : public RefCounted {
|
||||
GDCLASS(Tween, RefCounted);
|
||||
|
||||
public:
|
||||
enum TweenProcessMode {
|
||||
|
|
@ -42,6 +65,12 @@ public:
|
|||
TWEEN_PROCESS_IDLE,
|
||||
};
|
||||
|
||||
enum TweenPauseMode {
|
||||
TWEEN_PAUSE_BOUND,
|
||||
TWEEN_PAUSE_STOP,
|
||||
TWEEN_PAUSE_PROCESS,
|
||||
};
|
||||
|
||||
enum TransitionType {
|
||||
TRANS_LINEAR,
|
||||
TRANS_SINE,
|
||||
|
|
@ -54,8 +83,7 @@ public:
|
|||
TRANS_CIRC,
|
||||
TRANS_BOUNCE,
|
||||
TRANS_BACK,
|
||||
|
||||
TRANS_COUNT,
|
||||
TRANS_MAX
|
||||
};
|
||||
|
||||
enum EaseType {
|
||||
|
|
@ -63,130 +91,187 @@ public:
|
|||
EASE_OUT,
|
||||
EASE_IN_OUT,
|
||||
EASE_OUT_IN,
|
||||
|
||||
EASE_COUNT,
|
||||
EASE_MAX
|
||||
};
|
||||
|
||||
private:
|
||||
enum InterpolateType {
|
||||
INTER_PROPERTY,
|
||||
INTER_METHOD,
|
||||
FOLLOW_PROPERTY,
|
||||
FOLLOW_METHOD,
|
||||
TARGETING_PROPERTY,
|
||||
TARGETING_METHOD,
|
||||
INTER_CALLBACK,
|
||||
};
|
||||
TweenProcessMode process_mode = TweenProcessMode::TWEEN_PROCESS_IDLE;
|
||||
TweenPauseMode pause_mode = TweenPauseMode::TWEEN_PAUSE_STOP;
|
||||
TransitionType default_transition = TransitionType::TRANS_LINEAR;
|
||||
EaseType default_ease = EaseType::EASE_IN_OUT;
|
||||
ObjectID bound_node;
|
||||
|
||||
struct InterpolateData {
|
||||
bool active = false;
|
||||
InterpolateType type = INTER_CALLBACK;
|
||||
bool finish = false;
|
||||
bool call_deferred = false;
|
||||
real_t elapsed = 0.0;
|
||||
ObjectID id;
|
||||
Vector<StringName> key;
|
||||
StringName concatenated_key;
|
||||
Variant initial_val;
|
||||
Variant delta_val;
|
||||
Variant final_val;
|
||||
ObjectID target_id;
|
||||
Vector<StringName> target_key;
|
||||
real_t duration = 0.0;
|
||||
TransitionType trans_type = TransitionType::TRANS_BACK;
|
||||
EaseType ease_type = EaseType::EASE_COUNT;
|
||||
real_t delay = 0.0;
|
||||
int args = 0;
|
||||
Variant arg[5];
|
||||
int uid = 0;
|
||||
};
|
||||
Vector<List<Ref<Tweener>>> tweeners;
|
||||
int current_step = -1;
|
||||
int loops = 1;
|
||||
int loops_done = 0;
|
||||
float speed_scale = 1;
|
||||
|
||||
String autoplay;
|
||||
TweenProcessMode tween_process_mode = TWEEN_PROCESS_IDLE;
|
||||
bool repeat = false;
|
||||
float speed_scale = 1.0;
|
||||
mutable int pending_update = 0;
|
||||
int uid = 0;
|
||||
bool was_stopped = false;
|
||||
|
||||
List<InterpolateData> interpolates;
|
||||
|
||||
struct PendingCommand {
|
||||
StringName key;
|
||||
int args = 0;
|
||||
Variant arg[10];
|
||||
};
|
||||
List<PendingCommand> pending_commands;
|
||||
|
||||
void _add_pending_command(StringName p_key, const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant(), const Variant &p_arg6 = Variant(), const Variant &p_arg7 = Variant(), const Variant &p_arg8 = Variant(), const Variant &p_arg9 = Variant(), const Variant &p_arg10 = Variant());
|
||||
void _process_pending_commands();
|
||||
bool is_bound = false;
|
||||
bool started = false;
|
||||
bool running = true;
|
||||
bool dead = false;
|
||||
bool invalid = true;
|
||||
bool default_parallel = false;
|
||||
bool parallel_enabled = false;
|
||||
|
||||
typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d);
|
||||
static interpolater interpolaters[TRANS_COUNT][EASE_COUNT];
|
||||
static interpolater interpolaters[TRANS_MAX][EASE_MAX];
|
||||
|
||||
real_t _run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t t, real_t b, real_t c, real_t d);
|
||||
Variant &_get_delta_val(InterpolateData &p_data);
|
||||
Variant _get_initial_val(const InterpolateData &p_data) const;
|
||||
Variant _get_final_val(const InterpolateData &p_data) const;
|
||||
Variant _run_equation(InterpolateData &p_data);
|
||||
bool _calc_delta_val(const Variant &p_initial_val, const Variant &p_final_val, Variant &p_delta_val);
|
||||
bool _apply_tween_value(InterpolateData &p_data, Variant &value);
|
||||
|
||||
void _tween_process(float p_delta);
|
||||
void _remove_by_uid(int uid);
|
||||
void _push_interpolate_data(InterpolateData &p_data);
|
||||
void _build_interpolation(InterpolateType p_interpolation_type, Object *p_object, NodePath *p_property, StringName *p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay);
|
||||
void start_tweeners();
|
||||
|
||||
protected:
|
||||
bool _set(const StringName &p_name, const Variant &p_value);
|
||||
bool _get(const StringName &p_name, Variant &r_ret) const;
|
||||
void _get_property_list(List<PropertyInfo> *p_list) const;
|
||||
void _notification(int p_what);
|
||||
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
bool is_active() const;
|
||||
void set_active(bool p_active);
|
||||
Ref<PropertyTweener> tween_property(Object *p_target, NodePath p_property, Variant p_to, float p_duration);
|
||||
Ref<IntervalTweener> tween_interval(float p_time);
|
||||
Ref<CallbackTweener> tween_callback(Callable p_callback);
|
||||
Ref<MethodTweener> tween_method(Callable p_callback, float p_from, float p_to, float p_duration);
|
||||
Ref<Tween> append(Ref<Tweener> p_tweener);
|
||||
|
||||
bool is_repeat() const;
|
||||
void set_repeat(bool p_repeat);
|
||||
bool custom_step(float p_delta);
|
||||
void stop();
|
||||
void pause();
|
||||
void play();
|
||||
void kill();
|
||||
|
||||
void set_tween_process_mode(TweenProcessMode p_mode);
|
||||
TweenProcessMode get_tween_process_mode() const;
|
||||
bool is_running();
|
||||
void set_valid(bool p_valid);
|
||||
bool is_valid();
|
||||
|
||||
void set_speed_scale(float p_speed);
|
||||
float get_speed_scale() const;
|
||||
Ref<Tween> bind_node(Node *p_node);
|
||||
Ref<Tween> set_process_mode(TweenProcessMode p_mode);
|
||||
TweenProcessMode get_process_mode();
|
||||
Ref<Tween> set_pause_mode(TweenPauseMode p_mode);
|
||||
TweenPauseMode get_pause_mode();
|
||||
|
||||
void start();
|
||||
void reset(Object *p_object, StringName p_key);
|
||||
void reset_all();
|
||||
void stop(Object *p_object, StringName p_key);
|
||||
void stop_all();
|
||||
void resume(Object *p_object, StringName p_key);
|
||||
void resume_all();
|
||||
void remove(Object *p_object, StringName p_key);
|
||||
void remove_all();
|
||||
Ref<Tween> set_parallel(bool p_parallel);
|
||||
Ref<Tween> set_loops(int p_loops);
|
||||
Ref<Tween> set_speed_scale(float p_speed);
|
||||
Ref<Tween> set_trans(TransitionType p_trans);
|
||||
TransitionType get_trans();
|
||||
Ref<Tween> set_ease(EaseType p_ease);
|
||||
EaseType get_ease();
|
||||
|
||||
void seek(real_t p_time);
|
||||
real_t tell() const;
|
||||
real_t get_runtime() const;
|
||||
Ref<Tween> parallel();
|
||||
Ref<Tween> chain();
|
||||
|
||||
void interpolate_property(Object *p_object, NodePath p_property, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
|
||||
void interpolate_method(Object *p_object, StringName p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
|
||||
void interpolate_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE);
|
||||
void interpolate_deferred_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE);
|
||||
void follow_property(Object *p_object, NodePath p_property, Variant p_initial_val, Object *p_target, NodePath p_target_property, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
|
||||
void follow_method(Object *p_object, StringName p_method, Variant p_initial_val, Object *p_target, StringName p_target_method, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
|
||||
void targeting_property(Object *p_object, NodePath p_property, Object *p_initial, NodePath p_initial_property, Variant p_final_val, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
|
||||
void targeting_method(Object *p_object, StringName p_method, Object *p_initial, StringName p_initial_method, Variant p_final_val, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
|
||||
real_t run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t t, real_t b, real_t c, real_t d);
|
||||
Variant interpolate_variant(Variant p_initial_val, Variant p_delta_val, float p_time, float p_duration, Tween::TransitionType p_trans, Tween::EaseType p_ease);
|
||||
Variant calculate_delta_value(Variant p_intial_val, Variant p_final_val);
|
||||
|
||||
Tween();
|
||||
~Tween();
|
||||
bool step(float p_delta);
|
||||
bool should_pause();
|
||||
|
||||
Tween() {}
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST(Tween::TweenPauseMode);
|
||||
VARIANT_ENUM_CAST(Tween::TweenProcessMode);
|
||||
VARIANT_ENUM_CAST(Tween::TransitionType);
|
||||
VARIANT_ENUM_CAST(Tween::EaseType);
|
||||
|
||||
class PropertyTweener : public Tweener {
|
||||
GDCLASS(PropertyTweener, Tweener);
|
||||
|
||||
public:
|
||||
Ref<PropertyTweener> from(Variant p_value);
|
||||
Ref<PropertyTweener> from_current();
|
||||
Ref<PropertyTweener> as_relative();
|
||||
Ref<PropertyTweener> set_trans(Tween::TransitionType p_trans);
|
||||
Ref<PropertyTweener> set_ease(Tween::EaseType p_ease);
|
||||
Ref<PropertyTweener> set_delay(float p_delay);
|
||||
|
||||
void set_tween(Ref<Tween> p_tween) override;
|
||||
void start() override;
|
||||
bool step(float &r_delta) override;
|
||||
|
||||
PropertyTweener(Object *p_target, NodePath p_property, Variant p_to, float p_duration);
|
||||
PropertyTweener();
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
ObjectID target;
|
||||
Vector<StringName> property;
|
||||
Variant initial_val;
|
||||
Variant base_final_val;
|
||||
Variant final_val;
|
||||
Variant delta_val;
|
||||
|
||||
float duration = 0;
|
||||
Tween::TransitionType trans_type = Tween::TRANS_MAX; // This is set inside set_tween();
|
||||
Tween::EaseType ease_type = Tween::EASE_MAX;
|
||||
|
||||
float delay = 0;
|
||||
bool do_continue = true;
|
||||
bool relative = false;
|
||||
};
|
||||
|
||||
class IntervalTweener : public Tweener {
|
||||
GDCLASS(IntervalTweener, Tweener);
|
||||
|
||||
public:
|
||||
void start() override;
|
||||
bool step(float &r_delta) override;
|
||||
|
||||
IntervalTweener(float p_time);
|
||||
IntervalTweener();
|
||||
|
||||
private:
|
||||
float duration = 0;
|
||||
};
|
||||
|
||||
class CallbackTweener : public Tweener {
|
||||
GDCLASS(CallbackTweener, Tweener);
|
||||
|
||||
public:
|
||||
Ref<CallbackTweener> set_delay(float p_delay);
|
||||
|
||||
void start() override;
|
||||
bool step(float &r_delta) override;
|
||||
|
||||
CallbackTweener(Callable p_callback);
|
||||
CallbackTweener();
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
Callable callback;
|
||||
float delay = 0;
|
||||
};
|
||||
|
||||
class MethodTweener : public Tweener {
|
||||
GDCLASS(MethodTweener, Tweener);
|
||||
|
||||
public:
|
||||
Ref<MethodTweener> set_trans(Tween::TransitionType p_trans);
|
||||
Ref<MethodTweener> set_ease(Tween::EaseType p_ease);
|
||||
Ref<MethodTweener> set_delay(float p_delay);
|
||||
|
||||
void set_tween(Ref<Tween> p_tween) override;
|
||||
void start() override;
|
||||
bool step(float &r_delta) override;
|
||||
|
||||
MethodTweener(Callable p_callback, float p_from, float p_to, float p_duration);
|
||||
MethodTweener();
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
private:
|
||||
float duration = 0;
|
||||
float delay = 0;
|
||||
Tween::TransitionType trans_type = Tween::TRANS_MAX;
|
||||
Tween::EaseType ease_type = Tween::EASE_MAX;
|
||||
|
||||
Ref<Tween> tween;
|
||||
Variant initial_val;
|
||||
Variant delta_val;
|
||||
Callable callback;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue