scene_tree_tween.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**************************************************************************/
  2. /* scene_tree_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 SCENE_TREE_TWEEN_H
  31. #define SCENE_TREE_TWEEN_H
  32. #include "core/reference.h"
  33. #include "scene/animation/tween.h"
  34. class SceneTreeTween;
  35. class Tweener : public Reference {
  36. GDCLASS(Tweener, Reference);
  37. public:
  38. virtual void set_tween(Ref<SceneTreeTween> p_tween);
  39. virtual void start() = 0;
  40. virtual bool step(float &r_delta) = 0;
  41. void clear_tween();
  42. protected:
  43. static void _bind_methods();
  44. Ref<SceneTreeTween> tween;
  45. float elapsed_time = 0;
  46. bool finished = false;
  47. };
  48. class PropertyTweener;
  49. class IntervalTweener;
  50. class CallbackTweener;
  51. class MethodTweener;
  52. class SceneTreeTween : public Reference {
  53. GDCLASS(SceneTreeTween, Reference);
  54. public:
  55. enum TweenPauseMode {
  56. TWEEN_PAUSE_BOUND,
  57. TWEEN_PAUSE_STOP,
  58. TWEEN_PAUSE_PROCESS,
  59. };
  60. private:
  61. Tween::TweenProcessMode process_mode = Tween::TWEEN_PROCESS_IDLE;
  62. TweenPauseMode pause_mode = TweenPauseMode::TWEEN_PAUSE_BOUND;
  63. Tween::TransitionType default_transition = Tween::TRANS_LINEAR;
  64. Tween::EaseType default_ease = Tween::EASE_IN_OUT;
  65. ObjectID bound_node;
  66. Vector<List<Ref<Tweener>>> tweeners;
  67. float total_time = 0;
  68. int current_step = -1;
  69. int loops = 1;
  70. int loops_done = 0;
  71. float speed_scale = 1;
  72. bool is_bound = false;
  73. bool started = false;
  74. bool running = true;
  75. bool dead = false;
  76. bool valid = false;
  77. bool default_parallel = false;
  78. bool parallel_enabled = false;
  79. #ifdef DEBUG_ENABLED
  80. bool is_infinite = false;
  81. #endif
  82. void start_tweeners();
  83. protected:
  84. static void _bind_methods();
  85. public:
  86. Ref<PropertyTweener> tween_property(Object *p_target, NodePath p_property, Variant p_to, float p_duration);
  87. Ref<IntervalTweener> tween_interval(float p_time);
  88. Ref<CallbackTweener> tween_callback(Object *p_target, StringName p_method, const Vector<Variant> &p_binds = Vector<Variant>());
  89. Ref<MethodTweener> tween_method(Object *p_target, StringName p_method, Variant p_from, Variant p_to, float p_duration, const Vector<Variant> &p_binds = Vector<Variant>());
  90. void append(Ref<Tweener> p_tweener);
  91. bool custom_step(float p_delta);
  92. void stop();
  93. void pause();
  94. void play();
  95. void kill();
  96. bool is_running() const;
  97. bool is_valid() const;
  98. void clear();
  99. Tween::TweenProcessMode get_process_mode() const;
  100. TweenPauseMode get_pause_mode() const;
  101. Tween::TransitionType get_trans() const;
  102. Tween::EaseType get_ease() const;
  103. Ref<SceneTreeTween> bind_node(Node *p_node);
  104. Ref<SceneTreeTween> set_process_mode(Tween::TweenProcessMode p_mode);
  105. Ref<SceneTreeTween> set_pause_mode(TweenPauseMode p_mode);
  106. Ref<SceneTreeTween> set_parallel(bool p_parallel);
  107. Ref<SceneTreeTween> set_loops(int p_loops);
  108. Ref<SceneTreeTween> set_speed_scale(float p_speed);
  109. Ref<SceneTreeTween> set_trans(Tween::TransitionType p_trans);
  110. Ref<SceneTreeTween> set_ease(Tween::EaseType p_ease);
  111. Ref<SceneTreeTween> parallel();
  112. Ref<SceneTreeTween> chain();
  113. Variant interpolate_variant(Variant p_initial_val, Variant p_delta_val, float p_time, float p_duration, Tween::TransitionType p_trans, Tween::EaseType p_ease) const;
  114. Variant calculate_delta_value(Variant p_intial_val, Variant p_final_val);
  115. bool step(float p_delta);
  116. bool can_process(bool p_tree_paused) const;
  117. Node *get_bound_node() const;
  118. float get_total_time() const;
  119. SceneTreeTween() = default;
  120. SceneTreeTween(bool p_valid);
  121. };
  122. VARIANT_ENUM_CAST(SceneTreeTween::TweenPauseMode);
  123. class PropertyTweener : public Tweener {
  124. GDCLASS(PropertyTweener, Tweener);
  125. public:
  126. Ref<PropertyTweener> from(Variant p_value);
  127. Ref<PropertyTweener> from_current();
  128. Ref<PropertyTweener> as_relative();
  129. Ref<PropertyTweener> set_trans(Tween::TransitionType p_trans);
  130. Ref<PropertyTweener> set_ease(Tween::EaseType p_ease);
  131. Ref<PropertyTweener> set_delay(float p_delay);
  132. virtual void set_tween(Ref<SceneTreeTween> p_tween);
  133. virtual void start();
  134. virtual bool step(float &r_delta);
  135. PropertyTweener(Object *p_target, NodePath p_property, Variant p_to, float p_duration);
  136. PropertyTweener();
  137. protected:
  138. static void _bind_methods();
  139. private:
  140. ObjectID target;
  141. Vector<StringName> property;
  142. Variant initial_val;
  143. Variant base_final_val;
  144. Variant final_val;
  145. Variant delta_val;
  146. float duration = 0;
  147. Tween::TransitionType trans_type = Tween::TRANS_COUNT; // This is set inside set_tween();
  148. Tween::EaseType ease_type = Tween::EASE_COUNT;
  149. float delay = 0;
  150. bool do_continue = true;
  151. bool relative = false;
  152. };
  153. class IntervalTweener : public Tweener {
  154. GDCLASS(IntervalTweener, Tweener);
  155. public:
  156. virtual void start();
  157. virtual bool step(float &r_delta);
  158. IntervalTweener(float p_time);
  159. IntervalTweener();
  160. private:
  161. float duration = 0;
  162. };
  163. class CallbackTweener : public Tweener {
  164. GDCLASS(CallbackTweener, Tweener);
  165. public:
  166. Ref<CallbackTweener> set_delay(float p_delay);
  167. virtual void start();
  168. virtual bool step(float &r_delta);
  169. CallbackTweener(Object *p_target, StringName p_method, const Vector<Variant> &p_binds);
  170. CallbackTweener();
  171. protected:
  172. static void _bind_methods();
  173. private:
  174. ObjectID target;
  175. StringName method;
  176. Vector<Variant> binds;
  177. int args = 0;
  178. float delay = 0;
  179. };
  180. class MethodTweener : public Tweener {
  181. GDCLASS(MethodTweener, Tweener);
  182. public:
  183. Ref<MethodTweener> set_trans(Tween::TransitionType p_trans);
  184. Ref<MethodTweener> set_ease(Tween::EaseType p_ease);
  185. Ref<MethodTweener> set_delay(float p_delay);
  186. virtual void set_tween(Ref<SceneTreeTween> p_tween);
  187. virtual void start();
  188. virtual bool step(float &r_delta);
  189. MethodTweener(Object *p_target, StringName p_method, Variant p_from, Variant p_to, float p_duration, const Vector<Variant> &p_binds);
  190. MethodTweener();
  191. protected:
  192. static void _bind_methods();
  193. private:
  194. float duration = 0;
  195. float delay = 0;
  196. Tween::TransitionType trans_type = Tween::TRANS_COUNT;
  197. Tween::EaseType ease_type = Tween::EASE_COUNT;
  198. Ref<SceneTreeTween> tween;
  199. Variant initial_val;
  200. Variant delta_val;
  201. Variant final_val;
  202. ObjectID target;
  203. StringName method;
  204. Vector<Variant> binds;
  205. };
  206. #endif // SCENE_TREE_TWEEN_H