animated_sprite.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*************************************************************************/
  2. /* animated_sprite.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. 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(!E, Ref<Texture>());
  69. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture>());
  70. if (p_idx >= E->get().frames.size())
  71. return Ref<Texture>();
  72. return E->get().frames[p_idx];
  73. }
  74. _FORCE_INLINE_ Ref<Texture> get_normal_frame(const StringName &p_anim, int p_idx) const {
  75. const Map<StringName, Anim>::Element *E = animations.find(p_anim);
  76. ERR_FAIL_COND_V(!E, Ref<Texture>());
  77. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture>());
  78. const Map<StringName, Anim>::Element *EN = animations.find(E->get().normal_name);
  79. if (!EN || p_idx >= EN->get().frames.size())
  80. return Ref<Texture>();
  81. return EN->get().frames[p_idx];
  82. }
  83. void set_frame(const StringName &p_anim, int p_idx, const Ref<Texture> &p_frame) {
  84. Map<StringName, Anim>::Element *E = animations.find(p_anim);
  85. ERR_FAIL_COND(!E);
  86. ERR_FAIL_COND(p_idx < 0);
  87. if (p_idx >= E->get().frames.size())
  88. return;
  89. E->get().frames[p_idx] = p_frame;
  90. }
  91. void remove_frame(const StringName &p_anim, int p_idx);
  92. void clear(const StringName &p_anim);
  93. void clear_all();
  94. SpriteFrames();
  95. };
  96. class AnimatedSprite : public Node2D {
  97. GDCLASS(AnimatedSprite, Node2D);
  98. Ref<SpriteFrames> frames;
  99. bool playing;
  100. StringName animation;
  101. int frame;
  102. bool centered;
  103. Point2 offset;
  104. float timeout;
  105. bool hflip;
  106. bool vflip;
  107. void _res_changed();
  108. void _reset_timeout();
  109. void _set_playing(bool p_playing);
  110. bool _is_playing() const;
  111. protected:
  112. static void _bind_methods();
  113. void _notification(int p_what);
  114. virtual void _validate_property(PropertyInfo &property) const;
  115. public:
  116. virtual void edit_set_pivot(const Point2 &p_pivot);
  117. virtual Point2 edit_get_pivot() const;
  118. virtual bool edit_has_pivot() const;
  119. void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
  120. Ref<SpriteFrames> get_sprite_frames() const;
  121. void play(const StringName &p_animation = StringName());
  122. void stop();
  123. bool is_playing() const;
  124. void set_animation(const StringName &p_animation);
  125. StringName get_animation() const;
  126. void set_frame(int p_frame);
  127. int get_frame() const;
  128. void set_centered(bool p_center);
  129. bool is_centered() const;
  130. void set_offset(const Point2 &p_offset);
  131. Point2 get_offset() const;
  132. void set_flip_h(bool p_flip);
  133. bool is_flipped_h() const;
  134. void set_flip_v(bool p_flip);
  135. bool is_flipped_v() const;
  136. void set_modulate(const Color &p_color);
  137. Color get_modulate() const;
  138. virtual Rect2 get_item_rect() const;
  139. virtual String get_configuration_warning() const;
  140. AnimatedSprite();
  141. };
  142. #endif // ANIMATED_SPRITE_H