Debian Codegraph @

scene/resources/sprite_frames.cpp

raw symbols SCIP
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(SceneStringName(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::duplicate_animation(const StringName &p_from, const StringName &p_to) {
110 ERR_FAIL_COND_MSG(!animations.has(p_from), vformat("SpriteFrames doesn't have animation '%s'.", p_from));
111 ERR_FAIL_COND_MSG(animations.has(p_to), vformat("Animation '%s' already exists.", p_to));
112 animations[p_to] = animations[p_from];
113 }
114
115 void SpriteFrames::remove_animation(const StringName &p_anim) {
116 animations.erase(p_anim);
117 }
118
119 void SpriteFrames::rename_animation(const StringName &p_prev, const StringName &p_next) {
120 ERR_FAIL_COND_MSG(!animations.has(p_prev), "SpriteFrames doesn't have animation '" + String(p_prev) + "'.");
121 ERR_FAIL_COND_MSG(animations.has(p_next), "Animation '" + String(p_next) + "' already exists.");
122
123 Anim anim = animations[p_prev];
124 animations.erase(p_prev);
125 animations[p_next] = anim;
126 }
127
128 void SpriteFrames::get_animation_list(List<StringName> *r_animations) const {
129 for (const KeyValue<StringName, Anim> &E : animations) {
130 r_animations->push_back(E.key);
131 }
132 }
133
134 Vector<String> SpriteFrames::get_animation_names() const {
135 Vector<String> names;
136 for (const KeyValue<StringName, Anim> &E : animations) {
137 names.push_back(E.key);
138 }
139 names.sort();
140 return names;
141 }
142
143 void SpriteFrames::set_animation_speed(const StringName &p_anim, double p_fps) {
144 ERR_FAIL_COND_MSG(p_fps < 0, "Animation speed cannot be negative (" + itos(p_fps) + ").");
145 HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
146 ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
147 E->value.speed = p_fps;
148 }
149
150 double SpriteFrames::get_animation_speed(const StringName &p_anim) const {
151 HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
152 ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
153 return E->value.speed;
154 }
155
156 void SpriteFrames::set_animation_loop(const StringName &p_anim, bool p_loop) {
157 HashMap<StringName, Anim>::Iterator E = animations.find(p_anim);
158 ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
159 E->value.loop = p_loop;
160 }
161
162 bool SpriteFrames::get_animation_loop(const StringName &p_anim) const {
163 HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim);
164 ERR_FAIL_COND_V_MSG(!E, false, "Animation '" + String(p_anim) + "' doesn't exist.");
165 return E->value.loop;
166 }
167
168 Array SpriteFrames::_get_animations() const {
169 Array anims;
170
171 List<StringName> sorted_names;
172 get_animation_list(&sorted_names);
173 sorted_names.sort_custom<StringName::AlphCompare>();
174
175 for (const StringName &anim_name : sorted_names) {
176 const Anim &anim = animations[anim_name];
177 Dictionary d;
178 d["name"] = anim_name;
179 d["speed"] = anim.speed;
180 d["loop"] = anim.loop;
181 Array frames;
182 for (int i = 0; i < anim.frames.size(); i++) {
183 Dictionary f;
184 f["texture"] = anim.frames[i].texture;
185 f["duration"] = anim.frames[i].duration;
186 frames.push_back(f);
187 }
188 d["frames"] = frames;
189 anims.push_back(d);
190 }
191
192 return anims;
193 }
194
195 void SpriteFrames::_set_animations(const Array &p_animations) {
196 animations.clear();
197 for (int i = 0; i < p_animations.size(); i++) {
198 Dictionary d = p_animations[i];
199
200 ERR_CONTINUE(!d.has("name"));
201 ERR_CONTINUE(!d.has("speed"));
202 ERR_CONTINUE(!d.has("loop"));
203 ERR_CONTINUE(!d.has("frames"));
204
205 Anim anim;
206 anim.speed = d["speed"];
207 anim.loop = d["loop"];
208 Array frames = d["frames"];
209 for (int j = 0; j < frames.size(); j++) {
210 #ifndef DISABLE_DEPRECATED
211 // For compatibility.
212 Ref<Resource> res = frames[j];
213 if (res.is_valid()) {
214 Frame frame = { res, 1.0 };
215 anim.frames.push_back(frame);
216 continue;
217 }
218 #endif
219
220 Dictionary f = frames[j];
221
222 ERR_CONTINUE(!f.has("texture"));
223 ERR_CONTINUE(!f.has("duration"));
224
225 Frame frame = { f["texture"], MAX(SPRITE_FRAME_MINIMUM_DURATION, (float)f["duration"]) };
226 anim.frames.push_back(frame);
227 }
228
229 animations[d["name"]] = anim;
230 }
231 }
232
233 #ifdef TOOLS_ENABLED
234 void SpriteFrames::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
235 const String pf = p_function;
236 if (p_idx == 0) {
237 if (pf == "has_animation" || pf == "remove_animation" || pf == "rename_animation" ||
238 pf == "set_animation_speed" || pf == "get_animation_speed" ||
239 pf == "set_animation_loop" || pf == "get_animation_loop" ||
240 pf == "add_frame" || pf == "set_frame" || pf == "remove_frame" ||
241 pf == "get_frame_count" || pf == "get_frame_texture" || pf == "get_frame_duration" ||
242 pf == "clear") {
243 for (const String &E : get_animation_names()) {
244 r_options->push_back(E.quote());
245 }
246 }
247 }
248 Resource::get_argument_options(p_function, p_idx, r_options);
249 }
250 #endif
251
252 void SpriteFrames::_bind_methods() {
253 ClassDB::bind_method(D_METHOD("add_animation", "anim"), &SpriteFrames::add_animation);
254 ClassDB::bind_method(D_METHOD("has_animation", "anim"), &SpriteFrames::has_animation);
255 ClassDB::bind_method(D_METHOD("duplicate_animation", "anim_from", "anim_to"), &SpriteFrames::duplicate_animation);
256 ClassDB::bind_method(D_METHOD("remove_animation", "anim"), &SpriteFrames::remove_animation);
257 ClassDB::bind_method(D_METHOD("rename_animation", "anim", "newname"), &SpriteFrames::rename_animation);
258
259 ClassDB::bind_method(D_METHOD("get_animation_names"), &SpriteFrames::get_animation_names);
260
261 ClassDB::bind_method(D_METHOD("set_animation_speed", "anim", "fps"), &SpriteFrames::set_animation_speed);
262 ClassDB::bind_method(D_METHOD("get_animation_speed", "anim"), &SpriteFrames::get_animation_speed);
263
264 ClassDB::bind_method(D_METHOD("set_animation_loop", "anim", "loop"), &SpriteFrames::set_animation_loop);
265 ClassDB::bind_method(D_METHOD("get_animation_loop", "anim"), &SpriteFrames::get_animation_loop);
266
267 ClassDB::bind_method(D_METHOD("add_frame", "anim", "texture", "duration", "at_position"), &SpriteFrames::add_frame, DEFVAL(1.0), DEFVAL(-1));
268 ClassDB::bind_method(D_METHOD("set_frame", "anim", "idx", "texture", "duration"), &SpriteFrames::set_frame, DEFVAL(1.0));
269 ClassDB::bind_method(D_METHOD("remove_frame", "anim", "idx"), &SpriteFrames::remove_frame);
270
271 ClassDB::bind_method(D_METHOD("get_frame_count", "anim"), &SpriteFrames::get_frame_count);
272 ClassDB::bind_method(D_METHOD("get_frame_texture", "anim", "idx"), &SpriteFrames::get_frame_texture);
273 ClassDB::bind_method(D_METHOD("get_frame_duration", "anim", "idx"), &SpriteFrames::get_frame_duration);
274
275 ClassDB::bind_method(D_METHOD("clear", "anim"), &SpriteFrames::clear);
276 ClassDB::bind_method(D_METHOD("clear_all"), &SpriteFrames::clear_all);
277
278 // `animations` property is for serialization.
279
280 ClassDB::bind_method(D_METHOD("_set_animations", "animations"), &SpriteFrames::_set_animations);
281 ClassDB::bind_method(D_METHOD("_get_animations"), &SpriteFrames::_get_animations);
282
283 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_animations", "_get_animations");
284 }
285
286 SpriteFrames::SpriteFrames() {
287 add_animation(SceneStringName(default_));
288 }