Skip to content

Commit

Permalink
Add duplicate animation
Browse files Browse the repository at this point in the history
Co-authored-by: Nonunknown <[email protected]>
  • Loading branch information
EAinsley and Nonunknown committed Jun 26, 2024
1 parent 95110dd commit f5f50f4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/classes/SpriteFrames.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
Removes all animations. An empty [code]default[/code] animation will be created.
</description>
</method>
<method name="duplicate_animation">
<return type="void" />
<param index="0" name="anim_from" type="StringName" />
<param index="1" name="anim_to" type="StringName" />
<description>
Add a new [param anim_to] animation and duplicate all frames from [param anim_from] to [param anim_to].
</description>
</method>
<method name="get_animation_loop" qualifiers="const">
<return type="bool" />
<param index="0" name="anim" type="StringName" />
Expand Down
49 changes: 49 additions & 0 deletions editor/plugins/sprite_frames_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ void SpriteFramesEditor::_notification(int p_what) {
zoom_reset->set_icon(get_editor_theme_icon(SNAME("ZoomReset")));
zoom_in->set_icon(get_editor_theme_icon(SNAME("ZoomMore")));
add_anim->set_icon(get_editor_theme_icon(SNAME("New")));
duplicate_anim->set_icon(get_editor_theme_icon(SNAME("Duplicate")));
delete_anim->set_icon(get_editor_theme_icon(SNAME("Remove")));
anim_search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
split_sheet_zoom_out->set_icon(get_editor_theme_icon(SNAME("ZoomLess")));
Expand Down Expand Up @@ -1098,6 +1099,46 @@ void SpriteFramesEditor::_animation_add() {
animations->grab_focus();
}

void SpriteFramesEditor::_animation_duplicate() {
if (updating) {
return;
}

if (!frames->has_animation(edited_anim)) {
return;
}

int counter = 1;
String new_name = String(edited_anim);
String base_name = new_name;
PackedStringArray name_component = new_name.split("_");
int last_index = name_component.size() - 1;
if (last_index > 0 && name_component[last_index].is_valid_int() && name_component[last_index].to_int() >= 0) {
counter = name_component[last_index].to_int();
name_component.remove_at(last_index);
base_name = String("_").join(name_component);
}
new_name = base_name + "_" + itos(counter);
while (frames->has_animation(new_name)) {
counter++;
new_name = base_name + "_" + itos(counter);
}

_select_animation(new_name);

EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Duplicate Animation"), UndoRedo::MERGE_DISABLE, EditorNode::get_singleton()->get_edited_scene());
undo_redo->add_do_method(frames.ptr(), "duplicate_animation", edited_anim, new_name);
undo_redo->add_undo_method(frames.ptr(), "remove_animation", new_name);
undo_redo->add_do_method(this, "_select_animation", new_name);
undo_redo->add_undo_method(this, "_select_animation", edited_anim);
undo_redo->add_do_method(this, "_update_library");
undo_redo->add_undo_method(this, "_update_library");
undo_redo->commit_action();

animations->grab_focus();
}

void SpriteFramesEditor::_animation_remove() {
if (updating) {
return;
Expand Down Expand Up @@ -1460,6 +1501,7 @@ void SpriteFramesEditor::edit(Ref<SpriteFrames> p_frames) {
_zoom_reset();

add_anim->set_disabled(read_only);
duplicate_anim->set_disabled(read_only);
delete_anim->set_disabled(read_only);
anim_speed->set_editable(!read_only);
anim_loop->set_disabled(read_only);
Expand Down Expand Up @@ -1784,6 +1826,11 @@ SpriteFramesEditor::SpriteFramesEditor() {
hbc_animlist->add_child(add_anim);
add_anim->connect(SceneStringName(pressed), callable_mp(this, &SpriteFramesEditor::_animation_add));

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

delete_anim = memnew(Button);
delete_anim->set_theme_type_variation("FlatButton");
hbc_animlist->add_child(delete_anim);
Expand Down Expand Up @@ -1837,6 +1884,8 @@ SpriteFramesEditor::SpriteFramesEditor() {

add_anim->set_shortcut_context(animations);
add_anim->set_shortcut(ED_SHORTCUT("sprite_frames/new_animation", TTR("Add Animation"), KeyModifierMask::CMD_OR_CTRL | Key::N));
duplicate_anim->set_shortcut_context(animations);
duplicate_anim->set_shortcut(ED_SHORTCUT("sprite_frames/duplicate_animation", TTR("Duplicate Animation"), KeyModifierMask::CMD_OR_CTRL | Key::D));
delete_anim->set_shortcut_context(animations);
delete_anim->set_shortcut(ED_SHORTCUT("sprite_frames/delete_animation", TTR("Delete Animation"), Key::KEY_DELETE));

Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/sprite_frames_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class SpriteFramesEditor : public HSplitContainer {
Vector<int> selection;

Button *add_anim = nullptr;
Button *duplicate_anim = nullptr;
Button *delete_anim = nullptr;
SpinBox *anim_speed = nullptr;
Button *anim_loop = nullptr;
Expand Down Expand Up @@ -210,6 +211,7 @@ class SpriteFramesEditor : public HSplitContainer {
void _animation_selected();
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
7 changes: 7 additions & 0 deletions scene/resources/sprite_frames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ bool SpriteFrames::has_animation(const StringName &p_anim) const {
return animations.has(p_anim);
}

void SpriteFrames::duplicate_animation(const StringName &p_from, const StringName &p_to) {
ERR_FAIL_COND_MSG(!animations.has(p_from), "SpriteFrames doesn't have animation '" + String(p_from) + "'.");
ERR_FAIL_COND_MSG(animations.has(p_to), "Animation '" + String(p_to) + "' already exists.");
animations[p_to] = animations[p_from];
}

void SpriteFrames::remove_animation(const StringName &p_anim) {
animations.erase(p_anim);
}
Expand Down Expand Up @@ -246,6 +252,7 @@ void SpriteFrames::get_argument_options(const StringName &p_function, int p_idx,
void SpriteFrames::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_animation", "anim"), &SpriteFrames::add_animation);
ClassDB::bind_method(D_METHOD("has_animation", "anim"), &SpriteFrames::has_animation);
ClassDB::bind_method(D_METHOD("duplicate_animation", "anim_from", "anim_to"), &SpriteFrames::duplicate_animation);
ClassDB::bind_method(D_METHOD("remove_animation", "anim"), &SpriteFrames::remove_animation);
ClassDB::bind_method(D_METHOD("rename_animation", "anim", "newname"), &SpriteFrames::rename_animation);

Expand Down
1 change: 1 addition & 0 deletions scene/resources/sprite_frames.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class SpriteFrames : public Resource {
public:
void add_animation(const StringName &p_anim);
bool has_animation(const StringName &p_anim) const;
void duplicate_animation(const StringName &p_from, const StringName &p_to);
void remove_animation(const StringName &p_anim);
void rename_animation(const StringName &p_prev, const StringName &p_next);

Expand Down

0 comments on commit f5f50f4

Please sign in to comment.