animated_sprite.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**************************************************************************/
  2. /* animated_sprite.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 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. protected:
  52. static void _bind_methods();
  53. public:
  54. void add_animation(const StringName &p_anim);
  55. bool has_animation(const StringName &p_anim) const;
  56. void remove_animation(const StringName &p_anim);
  57. void rename_animation(const StringName &p_prev, const StringName &p_next);
  58. void get_animation_list(List<StringName> *r_animations) const;
  59. Vector<String> get_animation_names() const;
  60. void set_animation_speed(const StringName &p_anim, float p_fps);
  61. float get_animation_speed(const StringName &p_anim) const;
  62. void set_animation_loop(const StringName &p_anim, bool p_loop);
  63. bool get_animation_loop(const StringName &p_anim) const;
  64. void add_frame(const StringName &p_anim, const Ref<Texture> &p_frame, int p_at_pos = -1);
  65. int get_frame_count(const StringName &p_anim) const;
  66. _FORCE_INLINE_ Ref<Texture> get_frame(const StringName &p_anim, int p_idx) const {
  67. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  68. ERR_FAIL_COND_V_MSG(!E, Ref<Texture>(), "Animation '" + String(p_anim) + "' doesn't exist.");
  69. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture>());
  70. if (p_idx >= E->get().frames.size()) {
  71. return Ref<Texture>();
  72. }
  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_MSG(!E, Ref<Texture>(), "Animation '" + String(p_anim) + "' doesn't exist.");
  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. }
  83. return EN->get().frames[p_idx];
  84. }
  85. void set_frame(const StringName &p_anim, int p_idx, const Ref<Texture> &p_frame) {
  86. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  87. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  88. ERR_FAIL_COND(p_idx < 0);
  89. if (p_idx >= E->get().frames.size()) {
  90. return;
  91. }
  92. E->get().frames.write[p_idx] = p_frame;
  93. }
  94. void remove_frame(const StringName &p_anim, int p_idx);
  95. void clear(const StringName &p_anim);
  96. void clear_all();
  97. SpriteFrames();
  98. };
  99. class AnimatedSprite : public Node2D {
  100. GDCLASS(AnimatedSprite, Node2D);
  101. Ref<SpriteFrames> frames;
  102. bool playing;
  103. bool backwards;
  104. StringName animation;
  105. int frame;
  106. float speed_scale;
  107. bool centered;
  108. Point2 offset;
  109. bool is_over;
  110. float timeout;
  111. bool hflip;
  112. bool vflip;
  113. void _res_changed();
  114. float _get_frame_duration();
  115. void _reset_timeout();
  116. Rect2 _get_rect() const;
  117. protected:
  118. static void _bind_methods();
  119. void _notification(int p_what);
  120. virtual void _validate_property(PropertyInfo &property) const;
  121. public:
  122. #ifdef TOOLS_ENABLED
  123. virtual Dictionary _edit_get_state() const;
  124. virtual void _edit_set_state(const Dictionary &p_state);
  125. virtual void _edit_set_pivot(const Point2 &p_pivot);
  126. virtual Point2 _edit_get_pivot() const;
  127. virtual bool _edit_use_pivot() const;
  128. virtual Rect2 _edit_get_rect() const;
  129. virtual bool _edit_use_rect() const;
  130. #endif
  131. virtual Rect2 get_anchorable_rect() const;
  132. void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
  133. Ref<SpriteFrames> get_sprite_frames() const;
  134. void play(const StringName &p_animation = StringName(), const bool p_backwards = false);
  135. void stop();
  136. void set_playing(bool p_playing);
  137. bool is_playing() const;
  138. void set_animation(const StringName &p_animation);
  139. StringName get_animation() const;
  140. void set_frame(int p_frame);
  141. int get_frame() const;
  142. void set_speed_scale(float p_speed_scale);
  143. float get_speed_scale() const;
  144. void set_centered(bool p_center);
  145. bool is_centered() const;
  146. void set_offset(const Point2 &p_offset);
  147. Point2 get_offset() const;
  148. void set_flip_h(bool p_flip);
  149. bool is_flipped_h() const;
  150. void set_flip_v(bool p_flip);
  151. bool is_flipped_v() const;
  152. virtual String get_configuration_warning() const;
  153. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  154. AnimatedSprite();
  155. };
  156. #endif // ANIMATED_SPRITE_H