tween.h 8.6 KB

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