Repository: godotengine/godot
File: editor/scene/sprite_frames_editor_plugin.cpp (C++)

Code before:
void SpriteFramesEditor::_animation_paste() {
	Ref<ClipboardAnimation> clipboard_anim = EditorSettings::get_singleton()->get_resource_clipboard();
	if (!clipboard_anim.is_valid()) {
		return;
	}

	if (updating) {
		return;
	}

	if (!clipboard_anim->is_copy) {
		EditorSettings::get_singleton()->set_resource_clipboard(nullptr);
	}

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

	EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
	undo_redo->create_action(TTR("Paste Animation"), UndoRedo::MERGE_DISABLE, EditorNode::get_singleton()->get_edited_scene());
	undo_redo->add_do_method(frames.ptr(), "add_animation", new_name);
	undo_redo->add_undo_method(frames.ptr(), "remove_animation", new_name);
	undo_redo->add_do_method(frames.ptr(), "set_animation_speed", new_name, clipboard_anim->animation_speed);
	undo_redo->add_do_method(frames.ptr(), "set_animation_loop", new_name, clipboard_anim->animation_loop);

	for (ClipboardSpriteFrames::Frame &frame : clipboard_anim->frames) {
		undo_redo->add_do_method(frames.ptr(), "add_frame", new_name, frame.texture, frame.duration);
	}

	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();
}

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

	if (!frames->has_animation(edited_anim)) {

Review comment (bug):
This code incorrectly handles names like `name_subname`, where the last component isn't a number. Instead of adding the suffix `name_subname_2`, you get `name_1`. Also, as I noted in my previous comment, I personally would prefer that the pasted animation use the original name whenever possible (you save it, but don't use it), rather than using the name of the currently edited animation as a base.

Code after:

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

	delete_dialog->set_text(TTRC("Delete Animation?"));
	delete_dialog->popup_centered();
}

void SpriteFramesEditor::_animation_remove_confirmed() {
	_animation_remove_undo_redo(TTR("Remove Animation"), nullptr);
}

void SpriteFramesEditor::_animation_search_text_changed(const String &p_text) {
	_update_library();
}

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

	EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
	undo_redo->create_action(TTR("Change Animation Loop"), UndoRedo::MERGE_DISABLE, frames.ptr());
	undo_redo->add_do_method(frames.ptr(), "set_animation_loop", edited_anim, anim_loop->is_pressed());
	undo_redo->add_undo_method(frames.ptr(), "set_animation_loop", edited_anim, frames->get_animation_loop(edited_anim));
	undo_redo->add_do_method(this, "_update_library", true);
	undo_redo->add_undo_method(this, "_update_library", true);
	undo_redo->commit_action();
}

void SpriteFramesEditor::_animation_speed_resized() {
	anim_speed->update_minimum_size();
}

void SpriteFramesEditor::_animation_speed_changed(double p_value) {
	if (frames.is_null()) {
		return;
	}

	if (updating) {
		return;
	}

	EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
	undo_redo->create_action(TTR("Change Animation FPS"), UndoRedo::MERGE_ENDS, frames.ptr());
	undo_redo->add_do_method(frames.ptr(), "set_animation_speed", edited_anim, p_value);
	undo_redo->add_undo_method(frames.ptr(), "set_animation_speed", edited_anim, frames->get_animation_speed(edited_anim));
	undo_redo->add_do_method(this, "_update_library", true);
	undo_redo->add_undo_method(this, "_update_library", true);
	undo_redo->commit_action();