animated_sprite.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*************************************************************************/
  2. /* animated_sprite.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef ANIMATED_SPRITE_H
  30. #define ANIMATED_SPRITE_H
  31. #include "scene/2d/node_2d.h"
  32. #include "scene/resources/texture.h"
  33. class SpriteFrames : public Resource {
  34. OBJ_TYPE(SpriteFrames,Resource);
  35. Vector< Ref<Texture> > frames;
  36. Array _get_frames() const;
  37. void _set_frames(const Array& p_frames);
  38. protected:
  39. static void _bind_methods();
  40. public:
  41. void add_frame(const Ref<Texture>& p_frame,int p_at_pos=-1);
  42. int get_frame_count() const;
  43. _FORCE_INLINE_ Ref<Texture> get_frame(int p_idx) const { ERR_FAIL_INDEX_V(p_idx,frames.size(),Ref<Texture>()); return frames[p_idx]; }
  44. void set_frame(int p_idx,const Ref<Texture>& p_frame){ ERR_FAIL_INDEX(p_idx,frames.size()); frames[p_idx]=p_frame; }
  45. void remove_frame(int p_idx);
  46. void clear();
  47. SpriteFrames();
  48. };
  49. class AnimatedSprite : public Node2D {
  50. OBJ_TYPE(AnimatedSprite,Node2D);
  51. Ref<SpriteFrames> frames;
  52. int frame;
  53. bool centered;
  54. Point2 offset;
  55. bool hflip;
  56. bool vflip;
  57. Color modulate;
  58. void _res_changed();
  59. protected:
  60. static void _bind_methods();
  61. void _notification(int p_what);
  62. public:
  63. virtual void edit_set_pivot(const Point2& p_pivot);
  64. virtual Point2 edit_get_pivot() const;
  65. virtual bool edit_has_pivot() const;
  66. void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
  67. Ref<SpriteFrames> get_sprite_frames() const;
  68. void set_frame(int p_frame);
  69. int get_frame() const;
  70. void set_centered(bool p_center);
  71. bool is_centered() const;
  72. void set_offset(const Point2& p_offset);
  73. Point2 get_offset() const;
  74. void set_flip_h(bool p_flip);
  75. bool is_flipped_h() const;
  76. void set_flip_v(bool p_flip);
  77. bool is_flipped_v() const;
  78. void set_modulate(const Color& p_color);
  79. Color get_modulate() const;
  80. virtual Rect2 get_item_rect() const;
  81. AnimatedSprite();
  82. };
  83. #endif // ANIMATED_SPRITE_H