tween.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*************************************************************************/
  2. /* tween.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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[5];
  91. int uid;
  92. };
  93. String autoplay;
  94. TweenProcessMode tween_process_mode;
  95. bool repeat;
  96. float speed_scale;
  97. mutable int pending_update;
  98. int uid;
  99. List<InterpolateData> interpolates;
  100. struct PendingCommand {
  101. StringName key;
  102. int args;
  103. Variant arg[10];
  104. };
  105. List<PendingCommand> pending_commands;
  106. 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());
  107. void _process_pending_commands();
  108. typedef real_t (*interpolater)(real_t t, real_t b, real_t c, real_t d);
  109. static interpolater interpolaters[TRANS_COUNT][EASE_COUNT];
  110. real_t _run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t t, real_t b, real_t c, real_t d);
  111. Variant &_get_delta_val(InterpolateData &p_data);
  112. Variant &_get_initial_val(InterpolateData &p_data);
  113. Variant _run_equation(InterpolateData &p_data);
  114. bool _calc_delta_val(const Variant &p_initial_val, const Variant &p_final_val, Variant &p_delta_val);
  115. bool _apply_tween_value(InterpolateData &p_data, Variant &value);
  116. void _tween_process(float p_delta);
  117. void _remove_by_uid(int uid);
  118. void _push_interpolate_data(InterpolateData &p_data);
  119. protected:
  120. bool _set(const StringName &p_name, const Variant &p_value);
  121. bool _get(const StringName &p_name, Variant &r_ret) const;
  122. void _get_property_list(List<PropertyInfo> *p_list) const;
  123. void _notification(int p_what);
  124. static void _bind_methods();
  125. public:
  126. bool is_active() const;
  127. void set_active(bool p_active);
  128. bool is_repeat() const;
  129. void set_repeat(bool p_repeat);
  130. void set_tween_process_mode(TweenProcessMode p_mode);
  131. TweenProcessMode get_tween_process_mode() const;
  132. void set_speed_scale(float p_speed);
  133. float get_speed_scale() const;
  134. bool start();
  135. bool reset(Object *p_object, StringName p_key);
  136. bool reset_all();
  137. bool stop(Object *p_object, StringName p_key);
  138. bool stop_all();
  139. bool resume(Object *p_object, StringName p_key);
  140. bool resume_all();
  141. bool remove(Object *p_object, StringName p_key);
  142. bool remove_all();
  143. bool seek(real_t p_time);
  144. real_t tell() const;
  145. real_t get_runtime() const;
  146. 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, EaseType p_ease_type, real_t p_delay = 0);
  147. 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, EaseType p_ease_type, real_t p_delay = 0);
  148. bool interpolate_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE);
  149. bool interpolate_deferred_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE);
  150. 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, EaseType p_ease_type, real_t p_delay = 0);
  151. 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, EaseType p_ease_type, real_t p_delay = 0);
  152. 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, EaseType p_ease_type, real_t p_delay = 0);
  153. 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, EaseType p_ease_type, real_t p_delay = 0);
  154. Tween();
  155. ~Tween();
  156. };
  157. VARIANT_ENUM_CAST(Tween::TweenProcessMode);
  158. VARIANT_ENUM_CAST(Tween::TransitionType);
  159. VARIANT_ENUM_CAST(Tween::EaseType);
  160. #endif