sprite_frames.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************************/
  2. /* sprite_frames.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 SPRITE_FRAMES_H
  31. #define SPRITE_FRAMES_H
  32. #include "scene/resources/texture.h"
  33. class SpriteFrames : public Resource {
  34. GDCLASS(SpriteFrames, Resource);
  35. struct Anim {
  36. double speed = 5.0;
  37. bool loop = true;
  38. Vector<Ref<Texture2D>> frames;
  39. };
  40. HashMap<StringName, Anim> animations;
  41. Array _get_animations() const;
  42. void _set_animations(const Array &p_animations);
  43. protected:
  44. static void _bind_methods();
  45. public:
  46. void add_animation(const StringName &p_anim);
  47. bool has_animation(const StringName &p_anim) const;
  48. void remove_animation(const StringName &p_anim);
  49. void rename_animation(const StringName &p_prev, const StringName &p_next);
  50. void get_animation_list(List<StringName> *r_animations) const;
  51. Vector<String> get_animation_names() const;
  52. void set_animation_speed(const StringName &p_anim, double p_fps);
  53. double get_animation_speed(const StringName &p_anim) const;
  54. void set_animation_loop(const StringName &p_anim, bool p_loop);
  55. bool get_animation_loop(const StringName &p_anim) const;
  56. void add_frame(const StringName &p_anim, const Ref<Texture2D> &p_frame, int p_at_pos = -1);
  57. int get_frame_count(const StringName &p_anim) const;
  58. _FORCE_INLINE_ Ref<Texture2D> get_frame(const StringName &p_anim, int p_idx) const {
  59. HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
  60. ERR_FAIL_COND_V_MSG(!E, Ref<Texture2D>(), "Animation '" + String(p_anim) + "' doesn't exist.");
  61. ERR_FAIL_COND_V(p_idx < 0, Ref<Texture2D>());
  62. if (p_idx >= E->value.frames.size()) {
  63. return Ref<Texture2D>();
  64. }
  65. return E->value.frames[p_idx];
  66. }
  67. void set_frame(const StringName &p_anim, int p_idx, const Ref<Texture2D> &p_frame) {
  68. HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
  69. ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
  70. ERR_FAIL_COND(p_idx < 0);
  71. if (p_idx >= E->value.frames.size()) {
  72. return;
  73. }
  74. E->value.frames.write[p_idx] = p_frame;
  75. }
  76. void remove_frame(const StringName &p_anim, int p_idx);
  77. void clear(const StringName &p_anim);
  78. void clear_all();
  79. SpriteFrames();
  80. };
  81. #endif // SPRITE_FRAMES_H