sprite_frames.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**************************************************************************/
  2. /* sprite_frames.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 SPRITE_FRAMES_H
  31. #define SPRITE_FRAMES_H
  32. #include "scene/resources/texture.h"
  33. static const float SPRITE_FRAME_MINIMUM_DURATION = 0.01;
  34. class SpriteFrames : public Resource {
  35. GDCLASS(SpriteFrames, Resource);
  36. struct Frame {
  37. Ref<Texture2D> texture;
  38. float duration = 1.0;
  39. };
  40. struct Anim {
  41. double speed = 5.0;
  42. bool loop = true;
  43. Vector<Frame> frames;
  44. };
  45. HashMap<StringName, Anim> animations;
  46. Array _get_animations() const;
  47. void _set_animations(const Array &p_animations);
  48. protected:
  49. static void _bind_methods();
  50. public:
  51. void add_animation(const StringName &p_anim);
  52. bool has_animation(const StringName &p_anim) const;
  53. void duplicate_animation(const StringName &p_from, const StringName &p_to);
  54. void remove_animation(const StringName &p_anim);
  55. void rename_animation(const StringName &p_prev, const StringName &p_next);
  56. void get_animation_list(List<StringName> *r_animations) const;
  57. Vector<String> get_animation_names() const;
  58. void set_animation_speed(const StringName &p_anim, double p_fps);
  59. double get_animation_speed(const StringName &p_anim) const;
  60. void set_animation_loop(const StringName &p_anim, bool p_loop);
  61. bool get_animation_loop(const StringName &p_anim) const;
  62. void add_frame(const StringName &p_anim, const Ref<Texture2D> &p_texture, float p_duration = 1.0, int p_at_pos = -1);
  63. void set_frame(const StringName &p_anim, int p_idx, const Ref<Texture2D> &p_texture, float p_duration = 1.0);
  64. void remove_frame(const StringName &p_anim, int p_idx);
  65. int get_frame_count(const StringName &p_anim) const;
  66. _FORCE_INLINE_ Ref<Texture2D> get_frame_texture(const StringName &p_anim, int p_idx) const {
  67. HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
  68. ERR_FAIL_COND_V_MSG(!E, Ref<Texture2D>(), "Animation '" + String(p_anim) + "' doesn't exist.");
  69. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture2D>());
  70. if (p_idx >= E->value.frames.size()) {
  71. return Ref<Texture2D>();
  72. }
  73. return E->value.frames[p_idx].texture;
  74. }
  75. _FORCE_INLINE_ float get_frame_duration(const StringName &p_anim, int p_idx) const {
  76. HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
  77. ERR_FAIL_COND_V_MSG(!E, 1.0, "Animation '" + String(p_anim) + "' doesn't exist.");
  78. ERR_FAIL_COND_V(p_idx < 0, 1.0);
  79. if (p_idx >= E->value.frames.size()) {
  80. return 1.0;
  81. }
  82. return E->value.frames[p_idx].duration;
  83. }
  84. void clear(const StringName &p_anim);
  85. void clear_all();
  86. #ifdef TOOLS_ENABLED
  87. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  88. #endif
  89. SpriteFrames();
  90. };
  91. #endif // SPRITE_FRAMES_H