Add option to paste animation as duplicate

Co-authored-by: Tomasz Chabora <kobewi4e@gmail.com>
This commit is contained in:
Haoyu Qiu 2022-04-14 10:39:12 +08:00 committed by Relintai
parent 95e6e47f88
commit 6191e0f0b8
2 changed files with 38 additions and 10 deletions

View File

@ -580,15 +580,7 @@ void AnimationPlayerEditor::_animation_name_edited() {
String current = animation->get_item_text(animation->get_selected()); String current = animation->get_item_text(animation->get_selected());
Ref<Animation> anim = player->get_animation(current); Ref<Animation> anim = player->get_animation(current);
Ref<Animation> new_anim = Ref<Animation>(memnew(Animation)); Ref<Animation> new_anim = _animation_clone(anim);
List<PropertyInfo> plist;
anim->get_property_list(&plist);
for (List<PropertyInfo>::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("");
new_anim->set_name(new_name); new_anim->set_name(new_name);
undo_redo->create_action(TTR("Duplicate Animation")); undo_redo->create_action(TTR("Duplicate Animation"));
@ -1056,6 +1048,23 @@ void AnimationPlayerEditor::_animation_duplicate() {
name->grab_focus(); name->grab_focus();
} }
Ref<Animation> AnimationPlayerEditor::_animation_clone(const Ref<Animation> p_anim) {
Ref<Animation> new_anim = memnew(Animation);
List<PropertyInfo> plist;
p_anim->get_property_list(&plist);
for (List<PropertyInfo>::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) { void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_set) {
if (updating || !player || player->is_playing()) { if (updating || !player || player->is_playing()) {
return; return;
@ -1152,37 +1161,47 @@ void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
case TOOL_NEW_ANIM: { case TOOL_NEW_ANIM: {
_animation_new(); _animation_new();
} break; } break;
case TOOL_LOAD_ANIM: { case TOOL_LOAD_ANIM: {
_animation_load(); _animation_load();
} break; } break;
case TOOL_SAVE_ANIM: { case TOOL_SAVE_ANIM: {
if (anim.is_valid()) { if (anim.is_valid()) {
_animation_save(anim); _animation_save(anim);
} }
} break; } break;
case TOOL_SAVE_AS_ANIM: { case TOOL_SAVE_AS_ANIM: {
if (anim.is_valid()) { if (anim.is_valid()) {
_animation_save_as(anim); _animation_save_as(anim);
} }
} break; } break;
case TOOL_DUPLICATE_ANIM: { case TOOL_DUPLICATE_ANIM: {
_animation_duplicate(); _animation_duplicate();
} break; } break;
case TOOL_RENAME_ANIM: { case TOOL_RENAME_ANIM: {
_animation_rename(); _animation_rename();
} break; } break;
case TOOL_EDIT_TRANSITIONS: { case TOOL_EDIT_TRANSITIONS: {
_animation_blend(); _animation_blend();
} break; } break;
case TOOL_REMOVE_ANIM: { case TOOL_REMOVE_ANIM: {
_animation_remove(); _animation_remove();
} break; } break;
case TOOL_COPY_ANIM: { case TOOL_COPY_ANIM: {
if (anim.is_valid()) { if (anim.is_valid()) {
EditorSettings::get_singleton()->set_resource_clipboard(anim); EditorSettings::get_singleton()->set_resource_clipboard(anim);
} }
} break; } break;
case TOOL_PASTE_ANIM: {
case TOOL_PASTE_ANIM:
case TOOL_PASTE_ANIM_REF: {
Ref<Animation> anim2 = EditorSettings::get_singleton()->get_resource_clipboard(); Ref<Animation> anim2 = EditorSettings::get_singleton()->get_resource_clipboard();
if (!anim2.is_valid()) { if (!anim2.is_valid()) {
error_dialog->set_text(TTR("No animation resource on clipboard!")); error_dialog->set_text(TTR("No animation resource on clipboard!"));
@ -1202,6 +1221,11 @@ void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
name = base + " " + itos(idx); 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->create_action(TTR("Paste Animation"));
undo_redo->add_do_method(player, "add_animation", name, anim2); undo_redo->add_do_method(player, "add_animation", name, anim2);
undo_redo->add_undo_method(player, "remove_animation", name); undo_redo->add_undo_method(player, "remove_animation", name);
@ -1211,6 +1235,7 @@ void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
_select_anim_by_name(name); _select_anim_by_name(name);
} break; } break;
case TOOL_EDIT_RESOURCE: { case TOOL_EDIT_RESOURCE: {
if (anim.is_valid()) { if (anim.is_valid()) {
editor->edit_resource(anim); editor->edit_resource(anim);
@ -1644,6 +1669,7 @@ AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor, AnimationPlay
tool_anim->get_popup()->add_separator(); 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/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", 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_separator();
tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/duplicate_animation", TTR("Duplicate...")), TOOL_DUPLICATE_ANIM); tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/duplicate_animation", TTR("Duplicate...")), TOOL_DUPLICATE_ANIM);
tool_anim->get_popup()->add_separator(); tool_anim->get_popup()->add_separator();

View File

@ -80,6 +80,7 @@ class AnimationPlayerEditor : public VBoxContainer {
TOOL_REMOVE_ANIM, TOOL_REMOVE_ANIM,
TOOL_COPY_ANIM, TOOL_COPY_ANIM,
TOOL_PASTE_ANIM, TOOL_PASTE_ANIM,
TOOL_PASTE_ANIM_REF,
TOOL_EDIT_RESOURCE TOOL_EDIT_RESOURCE
}; };
@ -204,6 +205,7 @@ class AnimationPlayerEditor : public VBoxContainer {
void _animation_blend(); void _animation_blend();
void _animation_edit(); void _animation_edit();
void _animation_duplicate(); void _animation_duplicate();
Ref<Animation> _animation_clone(const Ref<Animation> p_anim);
void _animation_resource_edit(); void _animation_resource_edit();
void _scale_changed(const String &p_scale); void _scale_changed(const String &p_scale);
void _dialog_action(String p_file); void _dialog_action(String p_file);