1 /*************************************************************************/
2 /*  tween.h                                                              */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 #ifndef TWEEN_H
32 #define TWEEN_H
33 
34 #include "scene/main/node.h"
35 
36 class Tween : public Node {
37 
38 	GDCLASS(Tween, Node);
39 
40 public:
41 	enum TweenProcessMode {
42 		TWEEN_PROCESS_PHYSICS,
43 		TWEEN_PROCESS_IDLE,
44 	};
45 
46 	enum TransitionType {
47 		TRANS_LINEAR,
48 		TRANS_SINE,
49 		TRANS_QUINT,
50 		TRANS_QUART,
51 		TRANS_QUAD,
52 		TRANS_EXPO,
53 		TRANS_ELASTIC,
54 		TRANS_CUBIC,
55 		TRANS_CIRC,
56 		TRANS_BOUNCE,
57 		TRANS_BACK,
58 
59 		TRANS_COUNT,
60 	};
61 
62 	enum EaseType {
63 		EASE_IN,
64 		EASE_OUT,
65 		EASE_IN_OUT,
66 		EASE_OUT_IN,
67 
68 		EASE_COUNT,
69 	};
70 
71 private:
72 	enum InterpolateType {
73 
74 		INTER_PROPERTY,
75 		INTER_METHOD,
76 		FOLLOW_PROPERTY,
77 		FOLLOW_METHOD,
78 		TARGETING_PROPERTY,
79 		TARGETING_METHOD,
80 		INTER_CALLBACK,
81 	};
82 
83 	struct InterpolateData {
84 		bool active;
85 		InterpolateType type;
86 		bool finish;
87 		bool call_deferred;
88 		real_t elapsed;
89 		ObjectID id;
90 		Vector<StringName> key;
91 		StringName concatenated_key;
92 		Variant initial_val;
93 		Variant delta_val;
94 		Variant final_val;
95 		ObjectID target_id;
96 		Vector<StringName> target_key;
97 		real_t duration;
98 		TransitionType trans_type;
99 		EaseType ease_type;
100 		real_t delay;
101 		int args;
102 		Variant arg[5];
103 		int uid;
InterpolateDataInterpolateData104 		InterpolateData() {
105 			active = false;
106 			finish = false;
107 			call_deferred = false;
108 			uid = 0;
109 		}
110 	};
111 
112 	String autoplay;
113 	TweenProcessMode tween_process_mode;
114 	bool repeat;
115 	float speed_scale;
116 	mutable int pending_update;
117 	int uid;
118 
119 	List<InterpolateData> interpolates;
120 
121 	struct PendingCommand {
122 		StringName key;
123 		int args;
124 		Variant arg[10];
125 	};
126 	List<PendingCommand> pending_commands;
127 
128 	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());
129 	void _process_pending_commands();
130 
131 	typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d);
132 	static interpolater interpolaters[TRANS_COUNT][EASE_COUNT];
133 
134 	real_t _run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t t, real_t b, real_t c, real_t d);
135 	Variant &_get_delta_val(InterpolateData &p_data);
136 	Variant _get_initial_val(const InterpolateData &p_data) const;
137 	Variant _get_final_val(const InterpolateData &p_data) const;
138 	Variant _run_equation(InterpolateData &p_data);
139 	bool _calc_delta_val(const Variant &p_initial_val, const Variant &p_final_val, Variant &p_delta_val);
140 	bool _apply_tween_value(InterpolateData &p_data, Variant &value);
141 
142 	void _tween_process(float p_delta);
143 	void _remove_by_uid(int uid);
144 	void _push_interpolate_data(InterpolateData &p_data);
145 	bool _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);
146 
147 protected:
148 	bool _set(const StringName &p_name, const Variant &p_value);
149 	bool _get(const StringName &p_name, Variant &r_ret) const;
150 	void _get_property_list(List<PropertyInfo> *p_list) const;
151 	void _notification(int p_what);
152 
153 	static void _bind_methods();
154 
155 public:
156 	bool is_active() const;
157 	void set_active(bool p_active);
158 
159 	bool is_repeat() const;
160 	void set_repeat(bool p_repeat);
161 
162 	void set_tween_process_mode(TweenProcessMode p_mode);
163 	TweenProcessMode get_tween_process_mode() const;
164 
165 	void set_speed_scale(float p_speed);
166 	float get_speed_scale() const;
167 
168 	bool start();
169 	bool reset(Object *p_object, StringName p_key);
170 	bool reset_all();
171 	bool stop(Object *p_object, StringName p_key);
172 	bool stop_all();
173 	bool resume(Object *p_object, StringName p_key);
174 	bool resume_all();
175 	bool remove(Object *p_object, StringName p_key);
176 	bool remove_all();
177 
178 	bool seek(real_t p_time);
179 	real_t tell() const;
180 	real_t get_runtime() const;
181 
182 	bool 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);
183 	bool 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);
184 	bool interpolate_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE);
185 	bool interpolate_deferred_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE);
186 	bool 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);
187 	bool 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);
188 	bool 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);
189 	bool 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);
190 
191 	Tween();
192 	~Tween();
193 };
194 
195 VARIANT_ENUM_CAST(Tween::TweenProcessMode);
196 VARIANT_ENUM_CAST(Tween::TransitionType);
197 VARIANT_ENUM_CAST(Tween::EaseType);
198 
199 #endif
200