Skip to content

Commit

Permalink
implement duplicate animations for animated_sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonunknown committed Aug 10, 2022
1 parent cf95056 commit 7420a4d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
83 changes: 83 additions & 0 deletions editor/plugins/sprite_frames_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ void SpriteFramesEditor::_notification(int p_what) {
zoom_reset->set_icon(get_theme_icon(SNAME("ZoomReset"), SNAME("EditorIcons")));
zoom_in->set_icon(get_theme_icon(SNAME("ZoomMore"), SNAME("EditorIcons")));
new_anim->set_icon(get_theme_icon(SNAME("New"), SNAME("EditorIcons")));
duplicate_anim->set_icon(get_theme_icon(SNAME("Duplicate"), SNAME("EditorIcons")));
remove_anim->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
anim_search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
split_sheet_zoom_out->set_icon(get_theme_icon(SNAME("ZoomLess"), SNAME("EditorIcons")));
Expand Down Expand Up @@ -785,6 +786,82 @@ void SpriteFramesEditor::_animation_add() {
animations->grab_focus();
}

void SpriteFramesEditor::_animation_duplicate() {
if (!frames->has_animation(edited_anim)) {
return;
}

int counter = 1;
String name = String(edited_anim) + " " + itos(counter);
while (frames->has_animation(name)) {
counter++;
name = String(edited_anim) + " " + itos(counter);
}

undo_redo->create_action(TTR("Duplicate Animation"));
undo_redo->add_do_method(frames, "add_animation", name);
undo_redo->add_do_method(frames, "set_animation_loop", name, frames->get_animation_loop(edited_anim));
undo_redo->add_do_method(frames, "set_animation_speed", name, frames->get_animation_speed(edited_anim));
for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
undo_redo->add_do_method(frames, "add_frame", name, frames->get_frame(edited_anim, i));
}
undo_redo->add_undo_method(frames, "remove_animation", name);
undo_redo->add_do_method(this, "_update_library");
undo_redo->add_undo_method(this, "_update_library");

// Not sure whether this part is needed/wanted (`_animation_add` is doing the same thing).
List<Node *> nodes;
_find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
for (Node *E : nodes) {
String current = E->call("get_animation");
undo_redo->add_do_method(E, "set_animation", name);
undo_redo->add_undo_method(E, "set_animation", current);
}

edited_anim = name;

undo_redo->commit_action();
animations->grab_focus();
}

void SpriteFramesEditor::_animation_duplicate() {
if (!frames->has_animation(edited_anim)) {
return;
}

int counter = 1;
String name = String(edited_anim) + " " + itos(counter);
while (frames->has_animation(name)) {
counter++;
name = String(edited_anim) + " " + itos(counter);
}

undo_redo->create_action(TTR("Duplicate Animation"));
undo_redo->add_do_method(frames, "add_animation", name);
undo_redo->add_do_method(frames, "set_animation_loop", name, frames->get_animation_loop(edited_anim));
undo_redo->add_do_method(frames, "set_animation_speed", name, frames->get_animation_speed(edited_anim));
for (int i = 0; i < frames->get_frame_count(edited_anim); i++) {
undo_redo->add_do_method(frames, "add_frame", name, frames->get_frame(edited_anim, i));
}
undo_redo->add_undo_method(frames, "remove_animation", name);
undo_redo->add_do_method(this, "_update_library");
undo_redo->add_undo_method(this, "_update_library");

// Not sure whether this part is needed/wanted (`_animation_add` is doing the same thing).
List<Node *> nodes;
_find_anim_sprites(EditorNode::get_singleton()->get_edited_scene(), &nodes, Ref<SpriteFrames>(frames));
for (Node *E : nodes) {
String current = E->call("get_animation");
undo_redo->add_do_method(E, "set_animation", name);
undo_redo->add_undo_method(E, "set_animation", current);
}

edited_anim = name;

undo_redo->commit_action();
animations->grab_focus();
}

void SpriteFramesEditor::_animation_remove() {
if (updating) {
return;
Expand Down Expand Up @@ -1160,6 +1237,12 @@ SpriteFramesEditor::SpriteFramesEditor() {
hbc_animlist->add_child(new_anim);
new_anim->connect("pressed", callable_mp(this, &SpriteFramesEditor::_animation_add));

duplicate_anim = memnew(Button);
duplicate_anim->set_flat(true);
duplicate_anim->set_tooltip(TTR("Duplicate Animation"));
hbc_animlist->add_child(duplicate_anim);
duplicate_anim->connect("pressed", callable_mp(this, &SpriteFramesEditor::_animation_duplicate));

remove_anim = memnew(Button);
remove_anim->set_flat(true);
remove_anim->set_tooltip(TTR("Remove Animation"));
Expand Down
3 changes: 3 additions & 0 deletions editor/plugins/sprite_frames_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class SpriteFramesEditor : public HSplitContainer {

Button *new_anim = nullptr;
Button *remove_anim = nullptr;
Button *duplicate_anim = nullptr;

LineEdit *anim_search_box = nullptr;

Tree *animations = nullptr;
Expand Down Expand Up @@ -137,6 +139,7 @@ class SpriteFramesEditor : public HSplitContainer {
void _animation_select();
void _animation_name_edited();
void _animation_add();
void _animation_duplicate();
void _animation_remove();
void _animation_remove_confirmed();
void _animation_search_text_changed(const String &p_text);
Expand Down

0 comments on commit 7420a4d

Please sign in to comment.