tween.h 8.3 KB

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