2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* tween.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-08-20 06:01:41 +02:00
|
|
|
#ifndef TWEEN_H
|
|
|
|
#define TWEEN_H
|
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
#include "core/object/ref_counted.h"
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
class Tween;
|
|
|
|
class Node;
|
|
|
|
|
|
|
|
class Tweener : public RefCounted {
|
|
|
|
GDCLASS(Tweener, RefCounted);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void set_tween(Ref<Tween> p_tween);
|
|
|
|
virtual void start() = 0;
|
2022-09-22 15:54:15 +02:00
|
|
|
virtual bool step(double &r_delta) = 0;
|
2021-08-05 13:05:14 +02:00
|
|
|
void clear_tween();
|
2020-09-05 03:05:30 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
Ref<Tween> tween;
|
2022-09-22 15:54:15 +02:00
|
|
|
double elapsed_time = 0;
|
2020-09-05 03:05:30 +02:00
|
|
|
bool finished = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PropertyTweener;
|
|
|
|
class IntervalTweener;
|
|
|
|
class CallbackTweener;
|
|
|
|
class MethodTweener;
|
|
|
|
|
|
|
|
class Tween : public RefCounted {
|
|
|
|
GDCLASS(Tween, RefCounted);
|
2014-08-20 06:01:41 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
enum TweenProcessMode {
|
2017-09-30 16:19:07 +02:00
|
|
|
TWEEN_PROCESS_PHYSICS,
|
2014-08-20 06:01:41 +02:00
|
|
|
TWEEN_PROCESS_IDLE,
|
|
|
|
};
|
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
enum TweenPauseMode {
|
|
|
|
TWEEN_PAUSE_BOUND,
|
|
|
|
TWEEN_PAUSE_STOP,
|
|
|
|
TWEEN_PAUSE_PROCESS,
|
|
|
|
};
|
|
|
|
|
2014-08-20 06:01:41 +02:00
|
|
|
enum TransitionType {
|
|
|
|
TRANS_LINEAR,
|
|
|
|
TRANS_SINE,
|
|
|
|
TRANS_QUINT,
|
|
|
|
TRANS_QUART,
|
|
|
|
TRANS_QUAD,
|
|
|
|
TRANS_EXPO,
|
|
|
|
TRANS_ELASTIC,
|
|
|
|
TRANS_CUBIC,
|
|
|
|
TRANS_CIRC,
|
|
|
|
TRANS_BOUNCE,
|
|
|
|
TRANS_BACK,
|
2020-09-05 03:05:30 +02:00
|
|
|
TRANS_MAX
|
2014-08-20 06:01:41 +02:00
|
|
|
};
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-08-20 06:01:41 +02:00
|
|
|
enum EaseType {
|
|
|
|
EASE_IN,
|
|
|
|
EASE_OUT,
|
|
|
|
EASE_IN_OUT,
|
2014-12-19 10:13:20 +01:00
|
|
|
EASE_OUT_IN,
|
2020-09-05 03:05:30 +02:00
|
|
|
EASE_MAX
|
2014-08-20 06:01:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2020-09-05 03:05:30 +02:00
|
|
|
TweenProcessMode process_mode = TweenProcessMode::TWEEN_PROCESS_IDLE;
|
2022-01-09 18:23:53 +01:00
|
|
|
TweenPauseMode pause_mode = TweenPauseMode::TWEEN_PAUSE_BOUND;
|
2020-09-05 03:05:30 +02:00
|
|
|
TransitionType default_transition = TransitionType::TRANS_LINEAR;
|
|
|
|
EaseType default_ease = EaseType::EASE_IN_OUT;
|
|
|
|
ObjectID bound_node;
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
Vector<List<Ref<Tweener>>> tweeners;
|
2022-09-22 15:54:15 +02:00
|
|
|
double total_time = 0;
|
2020-09-05 03:05:30 +02:00
|
|
|
int current_step = -1;
|
|
|
|
int loops = 1;
|
|
|
|
int loops_done = 0;
|
|
|
|
float speed_scale = 1;
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
bool is_bound = false;
|
|
|
|
bool started = false;
|
|
|
|
bool running = true;
|
|
|
|
bool dead = false;
|
2021-08-05 13:05:14 +02:00
|
|
|
bool valid = false;
|
2020-09-05 03:05:30 +02:00
|
|
|
bool default_parallel = false;
|
|
|
|
bool parallel_enabled = false;
|
2022-04-30 23:19:20 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
bool is_infinite = false;
|
|
|
|
#endif
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d);
|
|
|
|
static interpolater interpolaters[TRANS_MAX][EASE_MAX];
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2023-01-11 14:08:14 +01:00
|
|
|
void _start_tweeners();
|
|
|
|
void _stop_internal(bool p_reset);
|
2015-05-05 05:12:17 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
2015-05-05 05:12:17 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
public:
|
2022-12-13 22:37:41 +01:00
|
|
|
virtual String to_string() override;
|
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
Ref<PropertyTweener> tween_property(Object *p_target, NodePath p_property, Variant p_to, double p_duration);
|
|
|
|
Ref<IntervalTweener> tween_interval(double p_time);
|
2020-09-05 03:05:30 +02:00
|
|
|
Ref<CallbackTweener> tween_callback(Callable p_callback);
|
2022-09-22 15:54:15 +02:00
|
|
|
Ref<MethodTweener> tween_method(Callable p_callback, Variant p_from, Variant p_to, double p_duration);
|
2021-08-05 13:05:14 +02:00
|
|
|
void append(Ref<Tweener> p_tweener);
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
bool custom_step(double p_delta);
|
2020-09-05 03:05:30 +02:00
|
|
|
void stop();
|
|
|
|
void pause();
|
|
|
|
void play();
|
|
|
|
void kill();
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
bool is_running();
|
|
|
|
bool is_valid();
|
2021-08-05 13:05:14 +02:00
|
|
|
void clear();
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
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();
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
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();
|
2014-08-20 06:01:41 +02:00
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
Ref<Tween> parallel();
|
|
|
|
Ref<Tween> chain();
|
|
|
|
|
2022-04-17 23:13:39 +02:00
|
|
|
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);
|
2022-09-22 15:54:15 +02:00
|
|
|
static Variant interpolate_variant(Variant p_initial_val, Variant p_delta_val, double p_time, double p_duration, Tween::TransitionType p_trans, Tween::EaseType p_ease);
|
2020-09-05 03:05:30 +02:00
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
bool step(double p_delta);
|
2022-01-09 18:23:53 +01:00
|
|
|
bool can_process(bool p_tree_paused) const;
|
|
|
|
Node *get_bound_node() const;
|
2022-09-22 15:54:15 +02:00
|
|
|
double get_total_time() const;
|
2020-09-05 03:05:30 +02:00
|
|
|
|
2022-04-17 23:13:39 +02:00
|
|
|
Tween();
|
|
|
|
Tween(bool p_valid);
|
2014-08-20 06:01:41 +02:00
|
|
|
};
|
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
VARIANT_ENUM_CAST(Tween::TweenPauseMode);
|
2014-08-20 06:01:41 +02:00
|
|
|
VARIANT_ENUM_CAST(Tween::TweenProcessMode);
|
|
|
|
VARIANT_ENUM_CAST(Tween::TransitionType);
|
|
|
|
VARIANT_ENUM_CAST(Tween::EaseType);
|
|
|
|
|
2020-09-05 03:05:30 +02:00
|
|
|
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);
|
2022-09-22 15:54:15 +02:00
|
|
|
Ref<PropertyTweener> set_delay(double p_delay);
|
2020-09-05 03:05:30 +02:00
|
|
|
|
|
|
|
void set_tween(Ref<Tween> p_tween) override;
|
|
|
|
void start() override;
|
2022-09-22 15:54:15 +02:00
|
|
|
bool step(double &r_delta) override;
|
2020-09-05 03:05:30 +02:00
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
PropertyTweener(Object *p_target, NodePath p_property, Variant p_to, double p_duration);
|
2020-09-05 03:05:30 +02:00
|
|
|
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;
|
|
|
|
|
2023-01-01 11:27:53 +01:00
|
|
|
Ref<RefCounted> ref_copy; // Makes sure that RefCounted objects are not freed too early.
|
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
double duration = 0;
|
2020-09-05 03:05:30 +02:00
|
|
|
Tween::TransitionType trans_type = Tween::TRANS_MAX; // This is set inside set_tween();
|
|
|
|
Tween::EaseType ease_type = Tween::EASE_MAX;
|
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
double delay = 0;
|
2020-09-05 03:05:30 +02:00
|
|
|
bool do_continue = true;
|
|
|
|
bool relative = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
class IntervalTweener : public Tweener {
|
|
|
|
GDCLASS(IntervalTweener, Tweener);
|
|
|
|
|
|
|
|
public:
|
|
|
|
void start() override;
|
2022-09-22 15:54:15 +02:00
|
|
|
bool step(double &r_delta) override;
|
2020-09-05 03:05:30 +02:00
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
IntervalTweener(double p_time);
|
2020-09-05 03:05:30 +02:00
|
|
|
IntervalTweener();
|
|
|
|
|
|
|
|
private:
|
2022-09-22 15:54:15 +02:00
|
|
|
double duration = 0;
|
2020-09-05 03:05:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class CallbackTweener : public Tweener {
|
|
|
|
GDCLASS(CallbackTweener, Tweener);
|
|
|
|
|
|
|
|
public:
|
2022-09-22 15:54:15 +02:00
|
|
|
Ref<CallbackTweener> set_delay(double p_delay);
|
2020-09-05 03:05:30 +02:00
|
|
|
|
|
|
|
void start() override;
|
2022-09-22 15:54:15 +02:00
|
|
|
bool step(double &r_delta) override;
|
2020-09-05 03:05:30 +02:00
|
|
|
|
|
|
|
CallbackTweener(Callable p_callback);
|
|
|
|
CallbackTweener();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Callable callback;
|
2022-09-22 15:54:15 +02:00
|
|
|
double delay = 0;
|
2023-01-01 11:27:53 +01:00
|
|
|
|
|
|
|
Ref<RefCounted> ref_copy;
|
2020-09-05 03:05:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class MethodTweener : public Tweener {
|
|
|
|
GDCLASS(MethodTweener, Tweener);
|
|
|
|
|
|
|
|
public:
|
|
|
|
Ref<MethodTweener> set_trans(Tween::TransitionType p_trans);
|
|
|
|
Ref<MethodTweener> set_ease(Tween::EaseType p_ease);
|
2022-09-22 15:54:15 +02:00
|
|
|
Ref<MethodTweener> set_delay(double p_delay);
|
2020-09-05 03:05:30 +02:00
|
|
|
|
|
|
|
void set_tween(Ref<Tween> p_tween) override;
|
|
|
|
void start() override;
|
2022-09-22 15:54:15 +02:00
|
|
|
bool step(double &r_delta) override;
|
2020-09-05 03:05:30 +02:00
|
|
|
|
2022-09-22 15:54:15 +02:00
|
|
|
MethodTweener(Callable p_callback, Variant p_from, Variant p_to, double p_duration);
|
2020-09-05 03:05:30 +02:00
|
|
|
MethodTweener();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
private:
|
2022-09-22 15:54:15 +02:00
|
|
|
double duration = 0;
|
|
|
|
double delay = 0;
|
2020-09-05 03:05:30 +02:00
|
|
|
Tween::TransitionType trans_type = Tween::TRANS_MAX;
|
|
|
|
Tween::EaseType ease_type = Tween::EASE_MAX;
|
|
|
|
|
|
|
|
Ref<Tween> tween;
|
|
|
|
Variant initial_val;
|
|
|
|
Variant delta_val;
|
2022-03-27 03:09:12 +02:00
|
|
|
Variant final_val;
|
2020-09-05 03:05:30 +02:00
|
|
|
Callable callback;
|
2023-01-01 11:27:53 +01:00
|
|
|
|
|
|
|
Ref<RefCounted> ref_copy;
|
2020-09-05 03:05:30 +02:00
|
|
|
};
|
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#endif // TWEEN_H
|