1 | /**************************************************************************/ |
2 | /* sprite_frames.cpp */ |
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 | #include "sprite_frames.h" |
32 | |
33 | #include "scene/scene_string_names.h" |
34 | |
35 | void SpriteFrames::add_frame(const StringName &p_anim, const Ref<Texture2D> &p_texture, float p_duration, int p_at_pos) { |
36 | HashMap<StringName, Anim>::Iterator E = animations.find(p_anim); |
37 | ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist." ); |
38 | |
39 | p_duration = MAX(SPRITE_FRAME_MINIMUM_DURATION, p_duration); |
40 | |
41 | Frame frame = { p_texture, p_duration }; |
42 | |
43 | if (p_at_pos >= 0 && p_at_pos < E->value.frames.size()) { |
44 | E->value.frames.insert(p_at_pos, frame); |
45 | } else { |
46 | E->value.frames.push_back(frame); |
47 | } |
48 | |
49 | emit_changed(); |
50 | } |
51 | |
52 | void SpriteFrames::set_frame(const StringName &p_anim, int p_idx, const Ref<Texture2D> &p_texture, float p_duration) { |
53 | HashMap<StringName, Anim>::Iterator E = animations.find(p_anim); |
54 | ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist." ); |
55 | ERR_FAIL_COND(p_idx < 0); |
56 | if (p_idx >= E->value.frames.size()) { |
57 | return; |
58 | } |
59 | |
60 | p_duration = MAX(SPRITE_FRAME_MINIMUM_DURATION, p_duration); |
61 | |
62 | Frame frame = { p_texture, p_duration }; |
63 | |
64 | E->value.frames.write[p_idx] = frame; |
65 | |
66 | emit_changed(); |
67 | } |
68 | |
69 | int SpriteFrames::get_frame_count(const StringName &p_anim) const { |
70 | HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim); |
71 | ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist." ); |
72 | |
73 | return E->value.frames.size(); |
74 | } |
75 | |
76 | void SpriteFrames::remove_frame(const StringName &p_anim, int p_idx) { |
77 | HashMap<StringName, Anim>::Iterator E = animations.find(p_anim); |
78 | ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist." ); |
79 | |
80 | E->value.frames.remove_at(p_idx); |
81 | |
82 | emit_changed(); |
83 | } |
84 | |
85 | void SpriteFrames::clear(const StringName &p_anim) { |
86 | HashMap<StringName, Anim>::Iterator E = animations.find(p_anim); |
87 | ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist." ); |
88 | |
89 | E->value.frames.clear(); |
90 | |
91 | emit_changed(); |
92 | } |
93 | |
94 | void SpriteFrames::clear_all() { |
95 | animations.clear(); |
96 | add_animation("default" ); |
97 | } |
98 | |
99 | void SpriteFrames::add_animation(const StringName &p_anim) { |
100 | ERR_FAIL_COND_MSG(animations.has(p_anim), "SpriteFrames already has animation '" + p_anim + "'." ); |
101 | |
102 | animations[p_anim] = Anim(); |
103 | } |
104 | |
105 | bool SpriteFrames::has_animation(const StringName &p_anim) const { |
106 | return animations.has(p_anim); |
107 | } |
108 | |
109 | void SpriteFrames::remove_animation(const StringName &p_anim) { |
110 | animations.erase(p_anim); |
111 | } |
112 | |
113 | void SpriteFrames::rename_animation(const StringName &p_prev, const StringName &p_next) { |
114 | ERR_FAIL_COND_MSG(!animations.has(p_prev), "SpriteFrames doesn't have animation '" + String(p_prev) + "'." ); |
115 | ERR_FAIL_COND_MSG(animations.has(p_next), "Animation '" + String(p_next) + "' already exists." ); |
116 | |
117 | Anim anim = animations[p_prev]; |
118 | animations.erase(p_prev); |
119 | animations[p_next] = anim; |
120 | } |
121 | |
122 | void SpriteFrames::get_animation_list(List<StringName> *r_animations) const { |
123 | for (const KeyValue<StringName, Anim> &E : animations) { |
124 | r_animations->push_back(E.key); |
125 | } |
126 | } |
127 | |
128 | Vector<String> SpriteFrames::get_animation_names() const { |
129 | Vector<String> names; |
130 | for (const KeyValue<StringName, Anim> &E : animations) { |
131 | names.push_back(E.key); |
132 | } |
133 | names.sort(); |
134 | return names; |
135 | } |
136 | |
137 | void SpriteFrames::set_animation_speed(const StringName &p_anim, double p_fps) { |
138 | ERR_FAIL_COND_MSG(p_fps < 0, "Animation speed cannot be negative (" + itos(p_fps) + ")." ); |
139 | HashMap<StringName, Anim>::Iterator E = animations.find(p_anim); |
140 | ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist." ); |
141 | E->value.speed = p_fps; |
142 | } |
143 | |
144 | double SpriteFrames::get_animation_speed(const StringName &p_anim) const { |
145 | HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim); |
146 | ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist." ); |
147 | return E->value.speed; |
148 | } |
149 | |
150 | void SpriteFrames::set_animation_loop(const StringName &p_anim, bool p_loop) { |
151 | HashMap<StringName, Anim>::Iterator E = animations.find(p_anim); |
152 | ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist." ); |
153 | E->value.loop = p_loop; |
154 | } |
155 | |
156 | bool SpriteFrames::get_animation_loop(const StringName &p_anim) const { |
157 | HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim); |
158 | ERR_FAIL_COND_V_MSG(!E, false, "Animation '" + String(p_anim) + "' doesn't exist." ); |
159 | return E->value.loop; |
160 | } |
161 | |
162 | Array SpriteFrames::_get_animations() const { |
163 | Array anims; |
164 | |
165 | List<StringName> sorted_names; |
166 | get_animation_list(&sorted_names); |
167 | sorted_names.sort_custom<StringName::AlphCompare>(); |
168 | |
169 | for (const StringName &anim_name : sorted_names) { |
170 | const Anim &anim = animations[anim_name]; |
171 | Dictionary d; |
172 | d["name" ] = anim_name; |
173 | d["speed" ] = anim.speed; |
174 | d["loop" ] = anim.loop; |
175 | Array frames; |
176 | for (int i = 0; i < anim.frames.size(); i++) { |
177 | Dictionary f; |
178 | f["texture" ] = anim.frames[i].texture; |
179 | f["duration" ] = anim.frames[i].duration; |
180 | frames.push_back(f); |
181 | } |
182 | d["frames" ] = frames; |
183 | anims.push_back(d); |
184 | } |
185 | |
186 | return anims; |
187 | } |
188 | |
189 | void SpriteFrames::_set_animations(const Array &p_animations) { |
190 | animations.clear(); |
191 | for (int i = 0; i < p_animations.size(); i++) { |
192 | Dictionary d = p_animations[i]; |
193 | |
194 | ERR_CONTINUE(!d.has("name" )); |
195 | ERR_CONTINUE(!d.has("speed" )); |
196 | ERR_CONTINUE(!d.has("loop" )); |
197 | ERR_CONTINUE(!d.has("frames" )); |
198 | |
199 | Anim anim; |
200 | anim.speed = d["speed" ]; |
201 | anim.loop = d["loop" ]; |
202 | Array frames = d["frames" ]; |
203 | for (int j = 0; j < frames.size(); j++) { |
204 | #ifndef DISABLE_DEPRECATED |
205 | // For compatibility. |
206 | Ref<Resource> res = frames[j]; |
207 | if (res.is_valid()) { |
208 | Frame frame = { res, 1.0 }; |
209 | anim.frames.push_back(frame); |
210 | continue; |
211 | } |
212 | #endif |
213 | |
214 | Dictionary f = frames[j]; |
215 | |
216 | ERR_CONTINUE(!f.has("texture" )); |
217 | ERR_CONTINUE(!f.has("duration" )); |
218 | |
219 | Frame frame = { f["texture" ], MAX(SPRITE_FRAME_MINIMUM_DURATION, (float)f["duration" ]) }; |
220 | anim.frames.push_back(frame); |
221 | } |
222 | |
223 | animations[d["name" ]] = anim; |
224 | } |
225 | } |
226 | |
227 | void SpriteFrames::_bind_methods() { |
228 | ClassDB::bind_method(D_METHOD("add_animation" , "anim" ), &SpriteFrames::add_animation); |
229 | ClassDB::bind_method(D_METHOD("has_animation" , "anim" ), &SpriteFrames::has_animation); |
230 | ClassDB::bind_method(D_METHOD("remove_animation" , "anim" ), &SpriteFrames::remove_animation); |
231 | ClassDB::bind_method(D_METHOD("rename_animation" , "anim" , "newname" ), &SpriteFrames::rename_animation); |
232 | |
233 | ClassDB::bind_method(D_METHOD("get_animation_names" ), &SpriteFrames::get_animation_names); |
234 | |
235 | ClassDB::bind_method(D_METHOD("set_animation_speed" , "anim" , "fps" ), &SpriteFrames::set_animation_speed); |
236 | ClassDB::bind_method(D_METHOD("get_animation_speed" , "anim" ), &SpriteFrames::get_animation_speed); |
237 | |
238 | ClassDB::bind_method(D_METHOD("set_animation_loop" , "anim" , "loop" ), &SpriteFrames::set_animation_loop); |
239 | ClassDB::bind_method(D_METHOD("get_animation_loop" , "anim" ), &SpriteFrames::get_animation_loop); |
240 | |
241 | ClassDB::bind_method(D_METHOD("add_frame" , "anim" , "texture" , "duration" , "at_position" ), &SpriteFrames::add_frame, DEFVAL(1.0), DEFVAL(-1)); |
242 | ClassDB::bind_method(D_METHOD("set_frame" , "anim" , "idx" , "texture" , "duration" ), &SpriteFrames::set_frame, DEFVAL(1.0)); |
243 | ClassDB::bind_method(D_METHOD("remove_frame" , "anim" , "idx" ), &SpriteFrames::remove_frame); |
244 | |
245 | ClassDB::bind_method(D_METHOD("get_frame_count" , "anim" ), &SpriteFrames::get_frame_count); |
246 | ClassDB::bind_method(D_METHOD("get_frame_texture" , "anim" , "idx" ), &SpriteFrames::get_frame_texture); |
247 | ClassDB::bind_method(D_METHOD("get_frame_duration" , "anim" , "idx" ), &SpriteFrames::get_frame_duration); |
248 | |
249 | ClassDB::bind_method(D_METHOD("clear" , "anim" ), &SpriteFrames::clear); |
250 | ClassDB::bind_method(D_METHOD("clear_all" ), &SpriteFrames::clear_all); |
251 | |
252 | // `animations` property is for serialization. |
253 | |
254 | ClassDB::bind_method(D_METHOD("_set_animations" , "animations" ), &SpriteFrames::_set_animations); |
255 | ClassDB::bind_method(D_METHOD("_get_animations" ), &SpriteFrames::_get_animations); |
256 | |
257 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_animations" , "_get_animations" ); |
258 | } |
259 | |
260 | SpriteFrames::SpriteFrames() { |
261 | add_animation(SceneStringNames::get_singleton()->_default); |
262 | } |
263 | |