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