tween.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "scene/main/node.h"
  33. class Tween : public Node {
  34. GDCLASS(Tween, Node);
  35. public:
  36. enum TweenProcessMode {
  37. TWEEN_PROCESS_PHYSICS,
  38. TWEEN_PROCESS_IDLE,
  39. };
  40. enum TransitionType {
  41. TRANS_LINEAR,
  42. TRANS_SINE,
  43. TRANS_QUINT,
  44. TRANS_QUART,
  45. TRANS_QUAD,
  46. TRANS_EXPO,
  47. TRANS_ELASTIC,
  48. TRANS_CUBIC,
  49. TRANS_CIRC,
  50. TRANS_BOUNCE,
  51. TRANS_BACK,
  52. TRANS_COUNT,
  53. };
  54. enum EaseType {
  55. EASE_IN,
  56. EASE_OUT,
  57. EASE_IN_OUT,
  58. EASE_OUT_IN,
  59. EASE_COUNT,
  60. };
  61. private:
  62. enum InterpolateType {
  63. INTER_PROPERTY,
  64. INTER_METHOD,
  65. FOLLOW_PROPERTY,
  66. FOLLOW_METHOD,
  67. TARGETING_PROPERTY,
  68. TARGETING_METHOD,
  69. INTER_CALLBACK,
  70. };
  71. struct InterpolateData {
  72. bool active;
  73. InterpolateType type;
  74. bool finish;
  75. bool call_deferred;
  76. real_t elapsed;
  77. ObjectID id;
  78. Vector<StringName> key;
  79. StringName concatenated_key;
  80. Variant initial_val;
  81. Variant delta_val;
  82. Variant final_val;
  83. ObjectID target_id;
  84. Vector<StringName> target_key;
  85. real_t duration;
  86. TransitionType trans_type;
  87. EaseType ease_type;
  88. real_t delay;
  89. int args;
  90. Variant arg[VARIANT_ARG_MAX];
  91. int uid;
  92. InterpolateData() {
  93. active = false;
  94. finish = false;
  95. call_deferred = false;
  96. uid = 0;
  97. }
  98. };
  99. String autoplay;
  100. TweenProcessMode tween_process_mode;
  101. bool repeat;
  102. float speed_scale;
  103. mutable int pending_update;
  104. int uid;
  105. bool was_stopped = false;
  106. List<InterpolateData> interpolates;
  107. struct PendingCommand {
  108. StringName key;
  109. int args;
  110. Variant arg[10];
  111. };
  112. List<PendingCommand> pending_commands;
  113. void _add_pending_command(StringName p_key, const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant(), const Variant &p_arg6 = Variant(), const Variant &p_arg7 = Variant(), const Variant &p_arg8 = Variant(), const Variant &p_arg9 = Variant(), const Variant &p_arg10 = Variant());
  114. void _process_pending_commands();
  115. typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d);
  116. static interpolater interpolaters[TRANS_COUNT][EASE_COUNT];
  117. Variant &_get_delta_val(InterpolateData &p_data);
  118. Variant _get_initial_val(const InterpolateData &p_data) const;
  119. Variant _get_final_val(const InterpolateData &p_data) const;
  120. Variant _run_equation(InterpolateData &p_data);
  121. bool _calc_delta_val(const Variant &p_initial_val, const Variant &p_final_val, Variant &p_delta_val);
  122. bool _apply_tween_value(InterpolateData &p_data, Variant &value);
  123. void _tween_process(float p_delta);
  124. void _remove_by_uid(int uid);
  125. void _push_interpolate_data(InterpolateData &p_data);
  126. bool _build_interpolation(InterpolateType p_interpolation_type, Object *p_object, NodePath *p_property, StringName *p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay);
  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. static real_t run_equation(Tween::TransitionType p_trans_type, Tween::EaseType p_ease_type, real_t p_time, real_t p_initial, real_t p_delta, real_t p_duration);
  135. bool is_active() const;
  136. void set_active(bool p_active);
  137. bool is_repeat() const;
  138. void set_repeat(bool p_repeat);
  139. void set_tween_process_mode(TweenProcessMode p_mode);
  140. TweenProcessMode get_tween_process_mode() const;
  141. void set_speed_scale(float p_speed);
  142. float get_speed_scale() const;
  143. bool start();
  144. bool reset(Object *p_object, StringName p_key);
  145. bool reset_all();
  146. bool stop(Object *p_object, StringName p_key);
  147. bool stop_all();
  148. bool resume(Object *p_object, StringName p_key);
  149. bool resume_all();
  150. bool remove(Object *p_object, StringName p_key);
  151. bool remove_all();
  152. bool seek(real_t p_time);
  153. real_t tell() const;
  154. real_t get_runtime() const;
  155. bool interpolate_property(Object *p_object, NodePath p_property, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
  156. bool interpolate_method(Object *p_object, StringName p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
  157. bool interpolate_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE);
  158. bool interpolate_deferred_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE);
  159. bool follow_property(Object *p_object, NodePath p_property, Variant p_initial_val, Object *p_target, NodePath p_target_property, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
  160. bool follow_method(Object *p_object, StringName p_method, Variant p_initial_val, Object *p_target, StringName p_target_method, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
  161. bool targeting_property(Object *p_object, NodePath p_property, Object *p_initial, NodePath p_initial_property, Variant p_final_val, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
  162. bool targeting_method(Object *p_object, StringName p_method, Object *p_initial, StringName p_initial_method, Variant p_final_val, real_t p_duration, TransitionType p_trans_type = TRANS_LINEAR, EaseType p_ease_type = EASE_IN_OUT, real_t p_delay = 0);
  163. Tween();
  164. ~Tween();
  165. };
  166. VARIANT_ENUM_CAST(Tween::TweenProcessMode);
  167. VARIANT_ENUM_CAST(Tween::TransitionType);
  168. VARIANT_ENUM_CAST(Tween::EaseType);
  169. #endif // TWEEN_H