Skip to content

Commit

Permalink
Merge pull request #58080 from timothyqiu/anim-dup-name-3.x
Browse files Browse the repository at this point in the history
[3.x] Make duplicate animation prompt for new name
  • Loading branch information
akien-mga authored Feb 14, 2022
2 parents d0b446c + aaf29c2 commit f68a6da
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 58 deletions.
122 changes: 65 additions & 57 deletions editor/plugins/animation_player_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void AnimationPlayerEditor::_animation_selected(int p_which) {
}

void AnimationPlayerEditor::_animation_new() {
renaming = false;
name_dialog_op = TOOL_NEW_ANIM;
name_title->set_text(TTR("New Animation Name:"));

int count = 1;
Expand Down Expand Up @@ -338,7 +338,7 @@ void AnimationPlayerEditor::_animation_rename() {

name_title->set_text(TTR("Change Animation Name:"));
name->set_text(selected_name);
renaming = true;
name_dialog_op = TOOL_RENAME_ANIM;
name_dialog->popup_centered(Size2(300, 90));
name->select_all();
name->grab_focus();
Expand Down Expand Up @@ -490,7 +490,7 @@ void AnimationPlayerEditor::_animation_name_edited() {
return;
}

if (renaming && animation->get_item_count() > 0 && animation->get_item_text(animation->get_selected()) == new_name) {
if (name_dialog_op == TOOL_RENAME_ANIM && animation->get_item_count() > 0 && animation->get_item_text(animation->get_selected()) == new_name) {
name_dialog->hide();
return;
}
Expand All @@ -501,37 +501,65 @@ void AnimationPlayerEditor::_animation_name_edited() {
return;
}

if (renaming) {
String current = animation->get_item_text(animation->get_selected());
Ref<Animation> anim = player->get_animation(current);
switch (name_dialog_op) {
case TOOL_RENAME_ANIM: {
String current = animation->get_item_text(animation->get_selected());
Ref<Animation> anim = player->get_animation(current);

undo_redo->create_action(TTR("Rename Animation"));
undo_redo->add_do_method(player, "rename_animation", current, new_name);
undo_redo->add_do_method(anim.ptr(), "set_name", new_name);
undo_redo->add_undo_method(player, "rename_animation", new_name, current);
undo_redo->add_undo_method(anim.ptr(), "set_name", current);
undo_redo->add_do_method(this, "_animation_player_changed", player);
undo_redo->add_undo_method(this, "_animation_player_changed", player);
undo_redo->commit_action();
undo_redo->create_action(TTR("Rename Animation"));
undo_redo->add_do_method(player, "rename_animation", current, new_name);
undo_redo->add_do_method(anim.ptr(), "set_name", new_name);
undo_redo->add_undo_method(player, "rename_animation", new_name, current);
undo_redo->add_undo_method(anim.ptr(), "set_name", current);
undo_redo->add_do_method(this, "_animation_player_changed", player);
undo_redo->add_undo_method(this, "_animation_player_changed", player);
undo_redo->commit_action();

_select_anim_by_name(new_name);
_select_anim_by_name(new_name);
} break;

} else {
Ref<Animation> new_anim = Ref<Animation>(memnew(Animation));
new_anim->set_name(new_name);
case TOOL_NEW_ANIM: {
Ref<Animation> new_anim = Ref<Animation>(memnew(Animation));
new_anim->set_name(new_name);

undo_redo->create_action(TTR("Add Animation"));
undo_redo->add_do_method(player, "add_animation", new_name, new_anim);
undo_redo->add_undo_method(player, "remove_animation", new_name);
undo_redo->add_do_method(this, "_animation_player_changed", player);
undo_redo->add_undo_method(this, "_animation_player_changed", player);
if (animation->get_item_count() == 0) {
undo_redo->add_do_method(this, "_start_onion_skinning");
undo_redo->add_undo_method(this, "_stop_onion_skinning");
}
undo_redo->commit_action();
undo_redo->create_action(TTR("Add Animation"));
undo_redo->add_do_method(player, "add_animation", new_name, new_anim);
undo_redo->add_undo_method(player, "remove_animation", new_name);
undo_redo->add_do_method(this, "_animation_player_changed", player);
undo_redo->add_undo_method(this, "_animation_player_changed", player);
if (animation->get_item_count() == 0) {
undo_redo->add_do_method(this, "_start_onion_skinning");
undo_redo->add_undo_method(this, "_stop_onion_skinning");
}
undo_redo->commit_action();

_select_anim_by_name(new_name);
} break;

case TOOL_DUPLICATE_ANIM: {
String current = animation->get_item_text(animation->get_selected());
Ref<Animation> anim = player->get_animation(current);

Ref<Animation> new_anim = Ref<Animation>(memnew(Animation));
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("");

undo_redo->create_action(TTR("Duplicate Animation"));
undo_redo->add_do_method(player, "add_animation", new_name, new_anim);
undo_redo->add_undo_method(player, "remove_animation", new_name);
undo_redo->add_do_method(player, "animation_set_next", new_name, player->animation_get_next(current));
undo_redo->add_do_method(this, "_animation_player_changed", player);
undo_redo->add_undo_method(this, "_animation_player_changed", player);
undo_redo->commit_action();

_select_anim_by_name(new_name);
_select_anim_by_name(new_name);
} break;
}

name_dialog->hide();
Expand Down Expand Up @@ -963,37 +991,17 @@ void AnimationPlayerEditor::_animation_duplicate() {
return;
}

Ref<Animation> new_anim = memnew(Animation);
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("");

String new_name = current;
while (player->has_animation(new_name)) {
new_name = new_name + " (copy)";
}
new_anim->set_name(new_name);

undo_redo->create_action(TTR("Duplicate Animation"));
undo_redo->add_do_method(player, "add_animation", new_name, new_anim);
undo_redo->add_undo_method(player, "remove_animation", new_name);
undo_redo->add_do_method(player, "animation_set_next", new_name, player->animation_get_next(current));
undo_redo->add_do_method(this, "_animation_player_changed", player);
undo_redo->add_undo_method(this, "_animation_player_changed", player);
undo_redo->commit_action();

for (int i = 0; i < animation->get_item_count(); i++) {
if (animation->get_item_text(i) == new_name) {
animation->select(i);
_animation_selected(i);
return;
}
}
name_title->set_text(TTR("New Animation Name:"));
name->set_text(new_name);
name_dialog_op = TOOL_DUPLICATE_ANIM;
name_dialog->popup_centered(Size2(300, 90));
name->select_all();
name->grab_focus();
}

void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_set) {
Expand Down Expand Up @@ -1597,7 +1605,7 @@ AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor, AnimationPlay
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_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_shortcut(ED_SHORTCUT("animation_player_editor/rename_animation", TTR("Rename...")), TOOL_RENAME_ANIM);
tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/edit_transitions", TTR("Edit Transitions...")), TOOL_EDIT_TRANSITIONS);
Expand Down Expand Up @@ -1709,7 +1717,7 @@ AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor, AnimationPlay
frame->connect("value_changed", this, "_seek_value_changed", Vector<Variant>(), true);
scale->connect("text_entered", this, "_scale_changed", Vector<Variant>(), true);

renaming = false;
name_dialog_op = TOOL_NEW_ANIM;
last_active = false;
timeline_position = 0;

Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/animation_player_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class AnimationPlayerEditor : public VBoxContainer {

ConfirmationDialog *name_dialog;
ConfirmationDialog *error_dialog;
bool renaming;
int name_dialog_op;

bool updating;
bool updating_blends;
Expand Down

0 comments on commit f68a6da

Please sign in to comment.