tween.h 8.5 KB

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