Skip to content

Commit 2642fd9

Browse files
authored
Merge pull request #62977 from akien-mga/3.x-spriteframes-sort-anims-alphabetically
[3.x] SpriteFrames: Sort animations alphabetically
2 parents 3f02013 + 66c4047 commit 2642fd9

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

scene/2d/animated_sprite.cpp

+15-8
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,28 @@ Array SpriteFrames::_get_frames() const {
239239

240240
Array SpriteFrames::_get_animations() const {
241241
Array anims;
242-
for (Map<StringName, Anim>::Element *E = animations.front(); E; E = E->next()) {
242+
243+
List<StringName> sorted_names;
244+
get_animation_list(&sorted_names);
245+
sorted_names.sort_custom<StringName::AlphCompare>();
246+
247+
for (List<StringName>::Element *E = sorted_names.front(); E; E = E->next()) {
248+
const Anim &anim = animations[E->get()];
243249
Dictionary d;
244-
d["name"] = E->key();
245-
d["speed"] = E->get().speed;
246-
d["loop"] = E->get().loop;
250+
d["name"] = E->get();
251+
d["speed"] = anim.speed;
252+
d["loop"] = anim.loop;
247253
Array frames;
248-
for (int i = 0; i < E->get().frames.size(); i++) {
249-
frames.push_back(E->get().frames[i]);
254+
for (int i = 0; i < anim.frames.size(); i++) {
255+
frames.push_back(anim.frames[i]);
250256
}
251257
d["frames"] = frames;
252258
anims.push_back(d);
253259
}
254260

255261
return anims;
256262
}
263+
257264
void SpriteFrames::_set_animations(const Array &p_animations) {
258265
animations.clear();
259266
for (int i = 0; i < p_animations.size(); i++) {
@@ -302,12 +309,12 @@ void SpriteFrames::_bind_methods() {
302309
ClassDB::bind_method(D_METHOD("_set_frames"), &SpriteFrames::_set_frames);
303310
ClassDB::bind_method(D_METHOD("_get_frames"), &SpriteFrames::_get_frames);
304311

305-
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "frames", PROPERTY_HINT_NONE, "", 0), "_set_frames", "_get_frames"); //compatibility
312+
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "frames", PROPERTY_HINT_NONE, "", 0), "_set_frames", "_get_frames"); // Compatibility with Godot 2.1.
306313

307314
ClassDB::bind_method(D_METHOD("_set_animations"), &SpriteFrames::_set_animations);
308315
ClassDB::bind_method(D_METHOD("_get_animations"), &SpriteFrames::_get_animations);
309316

310-
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_animations", "_get_animations"); //compatibility
317+
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_animations", "_get_animations");
311318
}
312319

313320
SpriteFrames::SpriteFrames() {

0 commit comments

Comments
 (0)