tween.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #pragma once
  31. #include "core/object/ref_counted.h"
  32. class Tween;
  33. class Node;
  34. class SceneTree;
  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();
  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 SubtweenTweener;
  54. class Tween : public RefCounted {
  55. GDCLASS(Tween, RefCounted);
  56. friend class PropertyTweener;
  57. public:
  58. enum TweenProcessMode {
  59. TWEEN_PROCESS_PHYSICS,
  60. TWEEN_PROCESS_IDLE,
  61. };
  62. enum TweenPauseMode {
  63. TWEEN_PAUSE_BOUND,
  64. TWEEN_PAUSE_STOP,
  65. TWEEN_PAUSE_PROCESS,
  66. };
  67. enum TransitionType {
  68. TRANS_LINEAR,
  69. TRANS_SINE,
  70. TRANS_QUINT,
  71. TRANS_QUART,
  72. TRANS_QUAD,
  73. TRANS_EXPO,
  74. TRANS_ELASTIC,
  75. TRANS_CUBIC,
  76. TRANS_CIRC,
  77. TRANS_BOUNCE,
  78. TRANS_BACK,
  79. TRANS_SPRING,
  80. TRANS_MAX
  81. };
  82. enum EaseType {
  83. EASE_IN,
  84. EASE_OUT,
  85. EASE_IN_OUT,
  86. EASE_OUT_IN,
  87. EASE_MAX
  88. };
  89. private:
  90. TweenProcessMode process_mode = TweenProcessMode::TWEEN_PROCESS_IDLE;
  91. TweenPauseMode pause_mode = TweenPauseMode::TWEEN_PAUSE_BOUND;
  92. TransitionType default_transition = TransitionType::TRANS_LINEAR;
  93. EaseType default_ease = EaseType::EASE_IN_OUT;
  94. ObjectID bound_node;
  95. SceneTree *parent_tree = nullptr;
  96. Vector<List<Ref<Tweener>>> tweeners;
  97. double total_time = 0;
  98. int current_step = -1;
  99. int loops = 1;
  100. int loops_done = 0;
  101. float speed_scale = 1;
  102. bool ignore_time_scale = false;
  103. bool is_bound = false;
  104. bool started = false;
  105. bool running = true;
  106. bool dead = false;
  107. bool valid = false;
  108. bool default_parallel = false;
  109. bool parallel_enabled = false;
  110. #ifdef DEBUG_ENABLED
  111. bool is_infinite = false;
  112. #endif
  113. typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d);
  114. static interpolater interpolaters[TRANS_MAX][EASE_MAX];
  115. void _start_tweeners();
  116. void _stop_internal(bool p_reset);
  117. bool _validate_type_match(const Variant &p_from, Variant &r_to);
  118. protected:
  119. static void _bind_methods();
  120. public:
  121. virtual String to_string() override;
  122. Ref<PropertyTweener> tween_property(const Object *p_target, const NodePath &p_property, Variant p_to, double p_duration);
  123. Ref<IntervalTweener> tween_interval(double p_time);
  124. Ref<CallbackTweener> tween_callback(const Callable &p_callback);
  125. Ref<MethodTweener> tween_method(const Callable &p_callback, const Variant p_from, Variant p_to, double p_duration);
  126. Ref<SubtweenTweener> tween_subtween(const Ref<Tween> &p_subtween);
  127. void append(Ref<Tweener> p_tweener);
  128. bool custom_step(double p_delta);
  129. void stop();
  130. void pause();
  131. void play();
  132. void kill();
  133. bool is_running();
  134. bool is_valid();
  135. void clear();
  136. Ref<Tween> bind_node(const Node *p_node);
  137. Ref<Tween> set_process_mode(TweenProcessMode p_mode);
  138. TweenProcessMode get_process_mode() const;
  139. Ref<Tween> set_pause_mode(TweenPauseMode p_mode);
  140. TweenPauseMode get_pause_mode() const;
  141. Ref<Tween> set_ignore_time_scale(bool p_ignore = true);
  142. bool is_ignoring_time_scale() const;
  143. Ref<Tween> set_parallel(bool p_parallel);
  144. Ref<Tween> set_loops(int p_loops);
  145. int get_loops_left() const;
  146. Ref<Tween> set_speed_scale(float p_speed);
  147. Ref<Tween> set_trans(TransitionType p_trans);
  148. TransitionType get_trans() const;
  149. Ref<Tween> set_ease(EaseType p_ease);
  150. EaseType get_ease() const;
  151. Ref<Tween> parallel();
  152. Ref<Tween> chain();
  153. 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);
  154. 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);
  155. bool step(double p_delta);
  156. bool can_process(bool p_tree_paused) const;
  157. Node *get_bound_node() const;
  158. double get_total_time() const;
  159. Tween();
  160. Tween(SceneTree *p_parent_tree);
  161. };
  162. VARIANT_ENUM_CAST(Tween::TweenPauseMode);
  163. VARIANT_ENUM_CAST(Tween::TweenProcessMode);
  164. VARIANT_ENUM_CAST(Tween::TransitionType);
  165. VARIANT_ENUM_CAST(Tween::EaseType);
  166. class PropertyTweener : public Tweener {
  167. GDCLASS(PropertyTweener, Tweener);
  168. double _get_custom_interpolated_value(const Variant &p_value);
  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. };