animated_sprite.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*************************************************************************/
  2. /* animated_sprite.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 ANIMATED_SPRITE_H
  31. #define ANIMATED_SPRITE_H
  32. #include "scene/2d/node_2d.h"
  33. #include "scene/resources/texture.h"
  34. class SpriteFrames : public Resource {
  35. GDCLASS(SpriteFrames, Resource);
  36. struct Anim {
  37. float speed;
  38. bool loop;
  39. Vector<Ref<Texture>> frames;
  40. Anim() {
  41. loop = true;
  42. speed = 5;
  43. }
  44. StringName normal_name;
  45. };
  46. Map<StringName, Anim> animations;
  47. Array _get_frames() const;
  48. void _set_frames(const Array &p_frames);
  49. Array _get_animations() const;
  50. void _set_animations(const Array &p_animations);
  51. Vector<String> _get_animation_list() const;
  52. protected:
  53. static void _bind_methods();
  54. public:
  55. void add_animation(const StringName &p_anim);
  56. bool has_animation(const StringName &p_anim) const;
  57. void remove_animation(const StringName &p_anim);
  58. void rename_animation(const StringName &p_prev, const StringName &p_next);
  59. void get_animation_list(List<StringName> *r_animations) const;
  60. Vector<String> get_animation_names() const;
  61. void set_animation_speed(const StringName &p_anim, float p_fps);
  62. float get_animation_speed(const StringName &p_anim) const;
  63. void set_animation_loop(const StringName &p_anim, bool p_loop);
  64. bool get_animation_loop(const StringName &p_anim) const;
  65. void add_frame(const StringName &p_anim, const Ref<Texture> &p_frame, int p_at_pos = -1);
  66. int get_frame_count(const StringName &p_anim) const;
  67. _FORCE_INLINE_ Ref<Texture> get_frame(const StringName &p_anim, int p_idx) const {
  68. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  69. ERR_FAIL_COND_V_MSG(!E, Ref<Texture>(), "Animation '" + String(p_anim) + "' doesn't exist.");
  70. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture>());
  71. if (p_idx >= E->get().frames.size()) {
  72. return Ref<Texture>();
  73. }
  74. return E->get().frames[p_idx];
  75. }
  76. _FORCE_INLINE_ Ref<Texture> get_normal_frame(const StringName &p_anim, int p_idx) const {
  77. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  78. ERR_FAIL_COND_V_MSG(!E, Ref<Texture>(), "Animation '" + String(p_anim) + "' doesn't exist.");
  79. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture>());
  80. const Map<StringName, Anim>::Element *EN = animations.find(E->get().normal_name);
  81. if (!EN || p_idx >= EN->get().frames.size()) {
  82. return Ref<Texture>();
  83. }
  84. return EN->get().frames[p_idx];
  85. }
  86. void set_frame(const StringName &p_anim, int p_idx, const Ref<Texture> &p_frame) {
  87. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  88. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  89. ERR_FAIL_COND(p_idx < 0);
  90. if (p_idx >= E->get().frames.size()) {
  91. return;
  92. }
  93. E->get().frames.write[p_idx] = p_frame;
  94. }
  95. void remove_frame(const StringName &p_anim, int p_idx);
  96. void clear(const StringName &p_anim);
  97. void clear_all();
  98. SpriteFrames();
  99. };
  100. class AnimatedSprite : public Node2D {
  101. GDCLASS(AnimatedSprite, Node2D);
  102. Ref<SpriteFrames> frames;
  103. bool playing;
  104. bool backwards;
  105. StringName animation;
  106. int frame;
  107. float speed_scale;
  108. bool centered;
  109. Point2 offset;
  110. bool is_over;
  111. float timeout;
  112. bool hflip;
  113. bool vflip;
  114. void _res_changed();
  115. float _get_frame_duration();
  116. void _reset_timeout();
  117. void _set_playing(bool p_playing);
  118. bool _is_playing() const;
  119. Rect2 _get_rect() const;
  120. protected:
  121. static void _bind_methods();
  122. void _notification(int p_what);
  123. virtual void _validate_property(PropertyInfo &property) const;
  124. public:
  125. #ifdef TOOLS_ENABLED
  126. virtual Dictionary _edit_get_state() const;
  127. virtual void _edit_set_state(const Dictionary &p_state);
  128. virtual void _edit_set_pivot(const Point2 &p_pivot);
  129. virtual Point2 _edit_get_pivot() const;
  130. virtual bool _edit_use_pivot() const;
  131. virtual Rect2 _edit_get_rect() const;
  132. virtual bool _edit_use_rect() const;
  133. #endif
  134. virtual Rect2 get_anchorable_rect() const;
  135. void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
  136. Ref<SpriteFrames> get_sprite_frames() const;
  137. void play(const StringName &p_animation = StringName(), const bool p_backwards = false);
  138. void stop();
  139. bool is_playing() const;
  140. void set_animation(const StringName &p_animation);
  141. StringName get_animation() const;
  142. void set_frame(int p_frame);
  143. int get_frame() const;
  144. void set_speed_scale(float p_speed_scale);
  145. float get_speed_scale() const;
  146. void set_centered(bool p_center);
  147. bool is_centered() const;
  148. void set_offset(const Point2 &p_offset);
  149. Point2 get_offset() const;
  150. void set_flip_h(bool p_flip);
  151. bool is_flipped_h() const;
  152. void set_flip_v(bool p_flip);
  153. bool is_flipped_v() const;
  154. void set_modulate(const Color &p_color);
  155. Color get_modulate() const;
  156. virtual String get_configuration_warning() const;
  157. AnimatedSprite();
  158. };
  159. #endif // ANIMATED_SPRITE_H