tween.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*************************************************************************/
  2. /* tween.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef TWEEN_H
  30. #define TWEEN_H
  31. #include "scene/main/node.h"
  32. class Tween : public Node {
  33. OBJ_TYPE( Tween, Node );
  34. public:
  35. enum TweenProcessMode {
  36. TWEEN_PROCESS_FIXED,
  37. TWEEN_PROCESS_IDLE,
  38. };
  39. enum TransitionType {
  40. TRANS_LINEAR,
  41. TRANS_SINE,
  42. TRANS_QUINT,
  43. TRANS_QUART,
  44. TRANS_QUAD,
  45. TRANS_EXPO,
  46. TRANS_ELASTIC,
  47. TRANS_CUBIC,
  48. TRANS_CIRC,
  49. TRANS_BOUNCE,
  50. TRANS_BACK,
  51. TRANS_COUNT,
  52. };
  53. enum EaseType {
  54. EASE_IN,
  55. EASE_OUT,
  56. EASE_IN_OUT,
  57. EASE_OUT_IN,
  58. EASE_COUNT,
  59. };
  60. private:
  61. enum InterpolateType {
  62. INTER_PROPERTY,
  63. INTER_METHOD,
  64. FOLLOW_PROPERTY,
  65. FOLLOW_METHOD,
  66. TARGETING_PROPERTY,
  67. TARGETING_METHOD,
  68. INTER_CALLBACK,
  69. };
  70. struct InterpolateData {
  71. bool active;
  72. InterpolateType type;
  73. bool finish;
  74. bool call_deferred;
  75. real_t elapsed;
  76. ObjectID id;
  77. StringName key;
  78. Variant initial_val;
  79. Variant delta_val;
  80. Variant final_val;
  81. ObjectID target_id;
  82. StringName target_key;
  83. real_t times_in_sec;
  84. TransitionType trans_type;
  85. EaseType ease_type;
  86. real_t delay;
  87. int args;
  88. Variant arg[5];
  89. };
  90. String autoplay;
  91. TweenProcessMode tween_process_mode;
  92. bool processing;
  93. bool active;
  94. bool repeat;
  95. float speed_scale;
  96. mutable int pending_update;
  97. List<InterpolateData> interpolates;
  98. struct PendingCommand {
  99. StringName key;
  100. int args;
  101. Variant arg[10];
  102. };
  103. List<PendingCommand> pending_commands;
  104. void _add_pending_command(StringName p_key
  105. ,const Variant& p_arg1=Variant()
  106. ,const Variant& p_arg2=Variant()
  107. ,const Variant& p_arg3=Variant()
  108. ,const Variant& p_arg4=Variant()
  109. ,const Variant& p_arg5=Variant()
  110. ,const Variant& p_arg6=Variant()
  111. ,const Variant& p_arg7=Variant()
  112. ,const Variant& p_arg8=Variant()
  113. ,const Variant& p_arg9=Variant()
  114. ,const Variant& p_arg10=Variant()
  115. );
  116. void _process_pending_commands();
  117. typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d);
  118. static interpolater interpolaters[TRANS_COUNT][EASE_COUNT];
  119. real_t _run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t t, real_t b, real_t c, real_t d);
  120. Variant& _get_delta_val(InterpolateData& p_data);
  121. Variant& _get_initial_val(InterpolateData& p_data);
  122. Variant _run_equation(InterpolateData& p_data);
  123. bool _calc_delta_val(const Variant& p_initial_val, const Variant& p_final_val, Variant& p_delta_val);
  124. bool _apply_tween_value(InterpolateData& p_data, Variant& value);
  125. void _tween_process(float p_delta);
  126. void _set_process(bool p_process,bool p_force=false);
  127. protected:
  128. bool _set(const StringName& p_name, const Variant& p_value);
  129. bool _get(const StringName& p_name,Variant &r_ret) const;
  130. void _get_property_list(List<PropertyInfo> *p_list) const;
  131. void _notification(int p_what);
  132. static void _bind_methods();
  133. public:
  134. bool is_active() const;
  135. void set_active(bool p_active);
  136. bool is_repeat() const;
  137. void set_repeat(bool p_repeat);
  138. void set_tween_process_mode(TweenProcessMode p_mode);
  139. TweenProcessMode get_tween_process_mode() const;
  140. void set_speed(float p_speed);
  141. float get_speed() const;
  142. bool start();
  143. bool reset(Object *p_node, String p_key);
  144. bool reset_all();
  145. bool stop(Object *p_node, String p_key);
  146. bool stop_all();
  147. bool resume(Object *p_node, String p_key);
  148. bool resume_all();
  149. bool remove(Object *p_node, String p_key);
  150. bool remove_all();
  151. bool seek(real_t p_time);
  152. real_t tell() const;
  153. real_t get_runtime() const;
  154. bool interpolate_property(Object *p_node
  155. , String p_property
  156. , Variant p_initial_val
  157. , Variant p_final_val
  158. , real_t p_times_in_sec
  159. , TransitionType p_trans_type
  160. , EaseType p_ease_type
  161. , real_t p_delay = 0
  162. );
  163. bool interpolate_method(Object *p_node
  164. , String p_method
  165. , Variant p_initial_val
  166. , Variant p_final_val
  167. , real_t p_times_in_sec
  168. , TransitionType p_trans_type
  169. , EaseType p_ease_type
  170. , real_t p_delay = 0
  171. );
  172. bool interpolate_callback(Object *p_object
  173. , real_t p_times_in_sec
  174. , String p_callback
  175. , VARIANT_ARG_DECLARE
  176. );
  177. bool interpolate_deferred_callback(Object *p_object
  178. , real_t p_times_in_sec
  179. , String p_callback
  180. , VARIANT_ARG_DECLARE
  181. );
  182. bool follow_property(Object *p_node
  183. , String p_property
  184. , Variant p_initial_val
  185. , Object *p_target
  186. , String p_target_property
  187. , real_t p_times_in_sec
  188. , TransitionType p_trans_type
  189. , EaseType p_ease_type
  190. , real_t p_delay = 0
  191. );
  192. bool follow_method(Object *p_node
  193. , String p_method
  194. , Variant p_initial_val
  195. , Object *p_target
  196. , String p_target_method
  197. , real_t p_times_in_sec
  198. , TransitionType p_trans_type
  199. , EaseType p_ease_type
  200. , real_t p_delay = 0
  201. );
  202. bool targeting_property(Object *p_node
  203. , String p_property
  204. , Object *p_initial
  205. , String p_initial_property
  206. , Variant p_final_val
  207. , real_t p_times_in_sec
  208. , TransitionType p_trans_type
  209. , EaseType p_ease_type
  210. , real_t p_delay = 0
  211. );
  212. bool targeting_method(Object *p_node
  213. , String p_method
  214. , Object *p_initial
  215. , String p_initial_method
  216. , Variant p_final_val
  217. , real_t p_times_in_sec
  218. , TransitionType p_trans_type
  219. , EaseType p_ease_type
  220. , real_t p_delay = 0
  221. );
  222. Tween();
  223. ~Tween();
  224. };
  225. VARIANT_ENUM_CAST( Tween::TweenProcessMode );
  226. VARIANT_ENUM_CAST( Tween::TransitionType );
  227. VARIANT_ENUM_CAST( Tween::EaseType );
  228. #endif