animated_sprite.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************************************/
  2. /* animated_sprite.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 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(!E, Ref<Texture>());
  70. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture>());
  71. if (p_idx >= E->get().frames.size())
  72. return Ref<Texture>();
  73. return E->get().frames[p_idx];
  74. }
  75. _FORCE_INLINE_ Ref<Texture> get_normal_frame(const StringName &p_anim, int p_idx) const {
  76. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  77. ERR_FAIL_COND_V(!E, Ref<Texture>());
  78. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture>());
  79. const Map<StringName, Anim>::Element *EN = animations.find(E->get().normal_name);
  80. if (!EN || p_idx >= EN->get().frames.size())
  81. return Ref<Texture>();
  82. return EN->get().frames[p_idx];
  83. }
  84. void set_frame(const StringName &p_anim, int p_idx, const Ref<Texture> &p_frame) {
  85. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  86. ERR_FAIL_COND(!E);
  87. ERR_FAIL_COND(p_idx < 0);
  88. if (p_idx >= E->get().frames.size())
  89. return;
  90. E->get().frames.write[p_idx] = p_frame;
  91. }
  92. void remove_frame(const StringName &p_anim, int p_idx);
  93. void clear(const StringName &p_anim);
  94. void clear_all();
  95. SpriteFrames();
  96. };
  97. class AnimatedSprite : public Node2D {
  98. GDCLASS(AnimatedSprite, Node2D);
  99. Ref<SpriteFrames> frames;
  100. bool playing;
  101. StringName animation;
  102. int frame;
  103. float speed_scale;
  104. bool centered;
  105. Point2 offset;
  106. bool is_over;
  107. float timeout;
  108. bool hflip;
  109. bool vflip;
  110. void _res_changed();
  111. float _get_frame_duration();
  112. void _reset_timeout();
  113. void _set_playing(bool p_playing);
  114. bool _is_playing() const;
  115. Rect2 _get_rect() const;
  116. protected:
  117. static void _bind_methods();
  118. void _notification(int p_what);
  119. virtual void _validate_property(PropertyInfo &property) const;
  120. public:
  121. virtual Dictionary _edit_get_state() const;
  122. virtual void _edit_set_state(const Dictionary &p_state);
  123. virtual void _edit_set_pivot(const Point2 &p_pivot);
  124. virtual Point2 _edit_get_pivot() const;
  125. virtual bool _edit_use_pivot() const;
  126. virtual Rect2 _edit_get_rect() const;
  127. virtual bool _edit_use_rect() const;
  128. virtual Rect2 get_anchorable_rect() const;
  129. void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
  130. Ref<SpriteFrames> get_sprite_frames() const;
  131. void play(const StringName &p_animation = StringName());
  132. void stop();
  133. bool is_playing() const;
  134. void set_animation(const StringName &p_animation);
  135. StringName get_animation() const;
  136. void set_frame(int p_frame);
  137. int get_frame() const;
  138. void set_speed_scale(float p_speed_scale);
  139. float get_speed_scale() const;
  140. void set_centered(bool p_center);
  141. bool is_centered() const;
  142. void set_offset(const Point2 &p_offset);
  143. Point2 get_offset() const;
  144. void set_flip_h(bool p_flip);
  145. bool is_flipped_h() const;
  146. void set_flip_v(bool p_flip);
  147. bool is_flipped_v() const;
  148. void set_modulate(const Color &p_color);
  149. Color get_modulate() const;
  150. virtual String get_configuration_warning() const;
  151. AnimatedSprite();
  152. };
  153. #endif // ANIMATED_SPRITE_H