tween.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #ifndef TWEEN_H
  31. #define TWEEN_H
  32. #include "core/object/ref_counted.h"
  33. class Tween;
  34. class Node;
  35. class SceneTree;
  36. class Tweener : public RefCounted {
  37. GDCLASS(Tweener, RefCounted);
  38. ObjectID tween_id;
  39. public:
  40. virtual void set_tween(const Ref<Tween> &p_tween);
  41. virtual void start();
  42. virtual bool step(double &r_delta) = 0;
  43. protected:
  44. static void _bind_methods();
  45. Ref<Tween> _get_tween();
  46. void _finish();
  47. double elapsed_time = 0;
  48. bool finished = false;
  49. };
  50. class PropertyTweener;
  51. class IntervalTweener;
  52. class CallbackTweener;
  53. class MethodTweener;
  54. class SubtweenTweener;
  55. class Tween : public RefCounted {
  56. GDCLASS(Tween, RefCounted);
  57. friend class PropertyTweener;
  58. public:
  59. enum TweenProcessMode {
  60. TWEEN_PROCESS_PHYSICS,
  61. TWEEN_PROCESS_IDLE,
  62. };
  63. enum TweenPauseMode {
  64. TWEEN_PAUSE_BOUND,
  65. TWEEN_PAUSE_STOP,
  66. TWEEN_PAUSE_PROCESS,
  67. };
  68. enum TransitionType {
  69. TRANS_LINEAR,
  70. TRANS_SINE,
  71. TRANS_QUINT,
  72. TRANS_QUART,
  73. TRANS_QUAD,
  74. TRANS_EXPO,
  75. TRANS_ELASTIC,
  76. TRANS_CUBIC,
  77. TRANS_CIRC,
  78. TRANS_BOUNCE,
  79. TRANS_BACK,
  80. TRANS_SPRING,
  81. TRANS_MAX
  82. };
  83. enum EaseType {
  84. EASE_IN,
  85. EASE_OUT,
  86. EASE_IN_OUT,
  87. EASE_OUT_IN,
  88. EASE_MAX
  89. };
  90. private:
  91. TweenProcessMode process_mode = TweenProcessMode::TWEEN_PROCESS_IDLE;
  92. TweenPauseMode pause_mode = TweenPauseMode::TWEEN_PAUSE_BOUND;
  93. TransitionType default_transition = TransitionType::TRANS_LINEAR;
  94. EaseType default_ease = EaseType::EASE_IN_OUT;
  95. ObjectID bound_node;
  96. SceneTree *parent_tree = nullptr;
  97. Vector<List<Ref<Tweener>>> tweeners;
  98. double total_time = 0;
  99. int current_step = -1;
  100. int loops = 1;
  101. int loops_done = 0;
  102. float speed_scale = 1;
  103. bool ignore_time_scale = false;
  104. bool is_bound = false;
  105. bool started = false;
  106. bool running = true;
  107. bool dead = false;
  108. bool valid = false;
  109. bool default_parallel = false;
  110. bool parallel_enabled = false;
  111. #ifdef DEBUG_ENABLED
  112. bool is_infinite = false;
  113. #endif
  114. typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d);
  115. static interpolater interpolaters[TRANS_MAX][EASE_MAX];
  116. void _start_tweeners();
  117. void _stop_internal(bool p_reset);
  118. bool _validate_type_match(const Variant &p_from, Variant &r_to);
  119. protected:
  120. static void _bind_methods();
  121. public:
  122. virtual String to_string() override;
  123. Ref<PropertyTweener> tween_property(const Object *p_target, const NodePath &p_property, Variant p_to, double p_duration);
  124. Ref<IntervalTweener> tween_interval(double p_time);
  125. Ref<CallbackTweener> tween_callback(const Callable &p_callback);
  126. Ref<MethodTweener> tween_method(const Callable &p_callback, const Variant p_from, Variant p_to, double p_duration);
  127. Ref<SubtweenTweener> tween_subtween(const Ref<Tween> &p_subtween);
  128. void append(Ref<Tweener> p_tweener);
  129. bool custom_step(double p_delta);
  130. void stop();
  131. void pause();
  132. void play();
  133. void kill();
  134. bool is_running();
  135. bool is_valid();
  136. void clear();
  137. Ref<Tween> bind_node(const Node *p_node);
  138. Ref<Tween> set_process_mode(TweenProcessMode p_mode);
  139. TweenProcessMode get_process_mode() const;
  140. Ref<Tween> set_pause_mode(TweenPauseMode p_mode);
  141. TweenPauseMode get_pause_mode() const;
  142. Ref<Tween> set_ignore_time_scale(bool p_ignore = true);
  143. bool is_ignoring_time_scale() const;
  144. Ref<Tween> set_parallel(bool p_parallel);
  145. Ref<Tween> set_loops(int p_loops);
  146. int get_loops_left() const;
  147. Ref<Tween> set_speed_scale(float p_speed);
  148. Ref<Tween> set_trans(TransitionType p_trans);
  149. TransitionType get_trans() const;
  150. Ref<Tween> set_ease(EaseType p_ease);
  151. EaseType get_ease() const;
  152. Ref<Tween> parallel();
  153. Ref<Tween> chain();
  154. 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);
  155. 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);
  156. bool step(double p_delta);
  157. bool can_process(bool p_tree_paused) const;
  158. Node *get_bound_node() const;
  159. double get_total_time() const;
  160. Tween();
  161. Tween(SceneTree *p_parent_tree);
  162. };
  163. VARIANT_ENUM_CAST(Tween::TweenPauseMode);
  164. VARIANT_ENUM_CAST(Tween::TweenProcessMode);
  165. VARIANT_ENUM_CAST(Tween::TransitionType);
  166. VARIANT_ENUM_CAST(Tween::EaseType);
  167. class PropertyTweener : public Tweener {
  168. GDCLASS(PropertyTweener, Tweener);
  169. public:
  170. Ref<PropertyTweener> from(const Variant &p_value);
  171. Ref<PropertyTweener> from_current();
  172. Ref<PropertyTweener> as_relative();
  173. Ref<PropertyTweener> set_trans(Tween::TransitionType p_trans);
  174. Ref<PropertyTweener> set_ease(Tween::EaseType p_ease);
  175. Ref<PropertyTweener> set_custom_interpolator(const Callable &p_method);
  176. Ref<PropertyTweener> set_delay(double p_delay);
  177. void set_tween(const Ref<Tween> &p_tween) override;
  178. void start() override;
  179. bool step(double &r_delta) override;
  180. PropertyTweener(const Object *p_target, const Vector<StringName> &p_property, const Variant &p_to, double p_duration);
  181. PropertyTweener();
  182. protected:
  183. static void _bind_methods();
  184. private:
  185. ObjectID target;
  186. Vector<StringName> property;
  187. Variant initial_val;
  188. Variant base_final_val;
  189. Variant final_val;
  190. Variant delta_val;
  191. Ref<RefCounted> ref_copy; // Makes sure that RefCounted objects are not freed too early.
  192. double duration = 0;
  193. Tween::TransitionType trans_type = Tween::TRANS_MAX; // This is set inside set_tween();
  194. Tween::EaseType ease_type = Tween::EASE_MAX;
  195. Callable custom_method;
  196. double delay = 0;
  197. bool do_continue = true;
  198. bool do_continue_delayed = false;
  199. bool relative = false;
  200. };
  201. class IntervalTweener : public Tweener {
  202. GDCLASS(IntervalTweener, Tweener);
  203. public:
  204. bool step(double &r_delta) override;
  205. IntervalTweener(double p_time);
  206. IntervalTweener();
  207. private:
  208. double duration = 0;
  209. };
  210. class CallbackTweener : public Tweener {
  211. GDCLASS(CallbackTweener, Tweener);
  212. public:
  213. Ref<CallbackTweener> set_delay(double p_delay);
  214. bool step(double &r_delta) override;
  215. CallbackTweener(const Callable &p_callback);
  216. CallbackTweener();
  217. protected:
  218. static void _bind_methods();
  219. private:
  220. Callable callback;
  221. double delay = 0;
  222. Ref<RefCounted> ref_copy;
  223. };
  224. class MethodTweener : public Tweener {
  225. GDCLASS(MethodTweener, Tweener);
  226. public:
  227. Ref<MethodTweener> set_trans(Tween::TransitionType p_trans);
  228. Ref<MethodTweener> set_ease(Tween::EaseType p_ease);
  229. Ref<MethodTweener> set_delay(double p_delay);
  230. void set_tween(const Ref<Tween> &p_tween) override;
  231. bool step(double &r_delta) override;
  232. MethodTweener(const Callable &p_callback, const Variant &p_from, const Variant &p_to, double p_duration);
  233. MethodTweener();
  234. protected:
  235. static void _bind_methods();
  236. private:
  237. double duration = 0;
  238. double delay = 0;
  239. Tween::TransitionType trans_type = Tween::TRANS_MAX;
  240. Tween::EaseType ease_type = Tween::EASE_MAX;
  241. Variant initial_val;
  242. Variant delta_val;
  243. Variant final_val;
  244. Callable callback;
  245. Ref<RefCounted> ref_copy;
  246. };
  247. class SubtweenTweener : public Tweener {
  248. GDCLASS(SubtweenTweener, Tweener);
  249. public:
  250. Ref<Tween> subtween;
  251. void start() override;
  252. bool step(double &r_delta) override;
  253. Ref<SubtweenTweener> set_delay(double p_delay);
  254. SubtweenTweener(const Ref<Tween> &p_subtween);
  255. SubtweenTweener();
  256. protected:
  257. static void _bind_methods();
  258. private:
  259. double delay = 0;
  260. };
  261. #endif // TWEEN_H