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