From 4e0547afe96099e526fad364150406d2181a7cbf Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Thu, 14 Apr 2022 10:39:12 +0800 Subject: [PATCH] Add option to paste animation as duplicate Co-authored-by: Tomasz Chabora --- .../animation_player_editor_plugin.cpp | 46 +++++++++++++++---- .../plugins/animation_player_editor_plugin.h | 2 + 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/editor/plugins/animation_player_editor_plugin.cpp b/editor/plugins/animation_player_editor_plugin.cpp index b206646b3db..1566eb51e58 100644 --- a/editor/plugins/animation_player_editor_plugin.cpp +++ b/editor/plugins/animation_player_editor_plugin.cpp @@ -542,15 +542,7 @@ void AnimationPlayerEditor::_animation_name_edited() { String current = animation->get_item_text(animation->get_selected()); Ref anim = player->get_animation(current); - Ref new_anim = Ref(memnew(Animation)); - List plist; - anim->get_property_list(&plist); - for (List::Element *E = plist.front(); E; E = E->next()) { - if (E->get().usage & PROPERTY_USAGE_STORAGE) { - new_anim->set(E->get().name, anim->get(E->get().name)); - } - } - new_anim->set_path(""); + Ref new_anim = _animation_clone(anim); new_anim->set_name(new_name); undo_redo->create_action(TTR("Duplicate Animation")); @@ -1018,6 +1010,23 @@ void AnimationPlayerEditor::_animation_duplicate() { name->grab_focus(); } +Ref AnimationPlayerEditor::_animation_clone(const Ref p_anim) { + Ref new_anim = memnew(Animation); + + List plist; + p_anim->get_property_list(&plist); + for (List::Element *E = plist.front(); E; E = E->next()) { + const PropertyInfo &property = E->get(); + if (property.usage & PROPERTY_USAGE_STORAGE) { + new_anim->set(property.name, p_anim->get(property.name)); + } + } + + new_anim->set_path(""); + + return new_anim; +} + void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_set) { if (updating || !player || player->is_playing()) { return; @@ -1114,37 +1123,47 @@ void AnimationPlayerEditor::_animation_tool_menu(int p_option) { case TOOL_NEW_ANIM: { _animation_new(); } break; + case TOOL_LOAD_ANIM: { _animation_load(); } break; + case TOOL_SAVE_ANIM: { if (anim.is_valid()) { _animation_save(anim); } } break; + case TOOL_SAVE_AS_ANIM: { if (anim.is_valid()) { _animation_save_as(anim); } } break; + case TOOL_DUPLICATE_ANIM: { _animation_duplicate(); } break; + case TOOL_RENAME_ANIM: { _animation_rename(); } break; + case TOOL_EDIT_TRANSITIONS: { _animation_blend(); } break; + case TOOL_REMOVE_ANIM: { _animation_remove(); } break; + case TOOL_COPY_ANIM: { if (anim.is_valid()) { EditorSettings::get_singleton()->set_resource_clipboard(anim); } } break; - case TOOL_PASTE_ANIM: { + + case TOOL_PASTE_ANIM: + case TOOL_PASTE_ANIM_REF: { Ref anim2 = EditorSettings::get_singleton()->get_resource_clipboard(); if (!anim2.is_valid()) { error_dialog->set_text(TTR("No animation resource on clipboard!")); @@ -1164,6 +1183,11 @@ void AnimationPlayerEditor::_animation_tool_menu(int p_option) { name = base + " " + itos(idx); } + if (p_option == TOOL_PASTE_ANIM) { + anim2 = _animation_clone(anim2); + anim2->set_name(name); + } + undo_redo->create_action(TTR("Paste Animation")); undo_redo->add_do_method(player, "add_animation", name, anim2); undo_redo->add_undo_method(player, "remove_animation", name); @@ -1173,6 +1197,7 @@ void AnimationPlayerEditor::_animation_tool_menu(int p_option) { _select_anim_by_name(name); } break; + case TOOL_EDIT_RESOURCE: { if (anim.is_valid()) { editor->edit_resource(anim); @@ -1606,6 +1631,7 @@ AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor, AnimationPlay tool_anim->get_popup()->add_separator(); tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/copy_animation", TTR("Copy")), TOOL_COPY_ANIM); tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/paste_animation", TTR("Paste")), TOOL_PASTE_ANIM); + tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/paste_animation_as_reference", TTR("Paste As Reference")), TOOL_PASTE_ANIM_REF); tool_anim->get_popup()->add_separator(); tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/duplicate_animation", TTR("Duplicate...")), TOOL_DUPLICATE_ANIM); tool_anim->get_popup()->add_separator(); diff --git a/editor/plugins/animation_player_editor_plugin.h b/editor/plugins/animation_player_editor_plugin.h index a291d59d524..5debd7c17db 100644 --- a/editor/plugins/animation_player_editor_plugin.h +++ b/editor/plugins/animation_player_editor_plugin.h @@ -60,6 +60,7 @@ class AnimationPlayerEditor : public VBoxContainer { TOOL_REMOVE_ANIM, TOOL_COPY_ANIM, TOOL_PASTE_ANIM, + TOOL_PASTE_ANIM_REF, TOOL_EDIT_RESOURCE }; @@ -184,6 +185,7 @@ class AnimationPlayerEditor : public VBoxContainer { void _animation_blend(); void _animation_edit(); void _animation_duplicate(); + Ref _animation_clone(const Ref p_anim); void _animation_resource_edit(); void _scale_changed(const String &p_scale); void _dialog_action(String p_file);