Path: blob/master/editor/animation/animation_library_editor.cpp
9902 views
/**************************************************************************/1/* animation_library_editor.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "animation_library_editor.h"3132#include "core/string/print_string.h"33#include "core/string/ustring.h"34#include "core/templates/vector.h"35#include "core/variant/variant.h"36#include "editor/editor_node.h"37#include "editor/editor_string_names.h"38#include "editor/editor_undo_redo_manager.h"39#include "editor/file_system/editor_paths.h"40#include "editor/gui/editor_file_dialog.h"41#include "editor/settings/editor_settings.h"42#include "editor/themes/editor_scale.h"43#include "scene/animation/animation_mixer.h"44#include "scene/gui/line_edit.h"45#include "scene/resources/packed_scene.h"4647void AnimationLibraryEditor::set_animation_mixer(Object *p_mixer) {48mixer = Object::cast_to<AnimationMixer>(p_mixer);49}5051void AnimationLibraryEditor::_add_library() {52add_library_dialog->set_title(TTR("Library Name:"));53add_library_name->set_text("");54add_library_dialog->popup_centered();55add_library_name->grab_focus();56adding_animation = false;57adding_animation_to_library = StringName();58_add_library_validate("");59}6061void AnimationLibraryEditor::_add_library_validate(const String &p_name) {62String error;6364if (adding_animation) {65Ref<AnimationLibrary> al = mixer->get_animation_library(adding_animation_to_library);66ERR_FAIL_COND(al.is_null());67if (p_name == "") {68error = TTR("Animation name can't be empty.");69} else if (!AnimationLibrary::is_valid_animation_name(p_name)) {70error = TTR("Animation name contains invalid characters: '/', ':', ',' or '['.");71} else if (al->has_animation(p_name)) {72error = TTR("Animation with the same name already exists.");73}74} else {75if (p_name == "" && mixer->has_animation_library("")) {76error = TTR("Enter a library name.");77} else if (!AnimationLibrary::is_valid_library_name(p_name)) {78error = TTR("Library name contains invalid characters: '/', ':', ',' or '['.");79} else if (mixer->has_animation_library(p_name)) {80error = TTR("Library with the same name already exists.");81}82}8384if (error != "") {85add_library_validate->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));86add_library_validate->set_text(error);87add_library_dialog->get_ok_button()->set_disabled(true);88} else {89if (adding_animation) {90add_library_validate->set_text(TTR("Animation name is valid."));91} else {92if (p_name == "") {93add_library_validate->set_text(TTR("Global library will be created."));94} else {95add_library_validate->set_text(TTR("Library name is valid."));96}97}98add_library_validate->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("success_color"), EditorStringName(Editor)));99add_library_dialog->get_ok_button()->set_disabled(false);100}101}102103void AnimationLibraryEditor::_add_library_confirm() {104if (adding_animation) {105String anim_name = add_library_name->get_text();106EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();107108Ref<AnimationLibrary> al = mixer->get_animation_library(adding_animation_to_library);109ERR_FAIL_COND(al.is_null());110111Ref<Animation> anim;112anim.instantiate();113114undo_redo->create_action(vformat(TTR("Add Animation to Library: %s"), anim_name));115undo_redo->add_do_method(al.ptr(), "add_animation", anim_name, anim);116undo_redo->add_undo_method(al.ptr(), "remove_animation", anim_name);117undo_redo->add_do_method(this, "_update_editor", mixer);118undo_redo->add_undo_method(this, "_update_editor", mixer);119undo_redo->commit_action();120121} else {122String lib_name = add_library_name->get_text();123EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();124125Ref<AnimationLibrary> al;126al.instantiate();127128undo_redo->create_action(vformat(TTR("Add Animation Library: %s"), lib_name));129undo_redo->add_do_method(mixer, "add_animation_library", lib_name, al);130undo_redo->add_undo_method(mixer, "remove_animation_library", lib_name);131undo_redo->add_do_method(this, "_update_editor", mixer);132undo_redo->add_undo_method(this, "_update_editor", mixer);133undo_redo->commit_action();134}135}136137void AnimationLibraryEditor::_load_library() {138List<String> extensions;139ResourceLoader::get_recognized_extensions_for_type("AnimationLibrary", &extensions);140141file_dialog->set_title(TTR("Load Animation"));142file_dialog->clear_filters();143for (const String &K : extensions) {144file_dialog->add_filter("*." + K);145}146147file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);148file_dialog->set_current_file("");149file_dialog->popup_centered_ratio();150151file_dialog_action = FILE_DIALOG_ACTION_OPEN_LIBRARY;152}153154void AnimationLibraryEditor::_file_popup_selected(int p_id) {155Ref<AnimationLibrary> al = mixer->get_animation_library(file_dialog_library);156Ref<Animation> anim;157if (file_dialog_animation != StringName()) {158anim = al->get_animation(file_dialog_animation);159ERR_FAIL_COND(anim.is_null());160}161switch (p_id) {162case FILE_MENU_SAVE_LIBRARY: {163if (al->get_path().is_resource_file() && !FileAccess::exists(al->get_path() + ".import")) {164EditorNode::get_singleton()->save_resource(al);165break;166}167[[fallthrough]];168}169case FILE_MENU_SAVE_AS_LIBRARY: {170// Check if we're allowed to save this171{172String al_path = al->get_path();173if (!al_path.is_resource_file()) {174int srpos = al_path.find("::");175if (srpos != -1) {176String base = al_path.substr(0, srpos);177if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {178error_dialog->set_text(TTR("This animation library can't be saved because it does not belong to the edited scene. Make it unique first."));179error_dialog->popup_centered();180return;181}182}183} else {184if (FileAccess::exists(al_path + ".import")) {185error_dialog->set_text(TTR("This animation library can't be saved because it was imported from another file. Make it unique first."));186error_dialog->popup_centered();187return;188}189}190}191192file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);193file_dialog->set_title(TTR("Save Library"));194if (al->get_path().is_resource_file()) {195file_dialog->set_current_path(al->get_path());196} else {197file_dialog->set_current_file(String(file_dialog_library) + ".res");198}199file_dialog->clear_filters();200List<String> exts;201ResourceLoader::get_recognized_extensions_for_type("AnimationLibrary", &exts);202for (const String &K : exts) {203file_dialog->add_filter("*." + K);204}205206file_dialog->popup_centered_ratio();207file_dialog_action = FILE_DIALOG_ACTION_SAVE_LIBRARY;208} break;209case FILE_MENU_MAKE_LIBRARY_UNIQUE: {210StringName lib_name = file_dialog_library;211List<StringName> animation_list;212213Ref<AnimationLibrary> ald = memnew(AnimationLibrary);214al->get_animation_list(&animation_list);215for (const StringName &animation_name : animation_list) {216Ref<Animation> animation = al->get_animation(animation_name);217if (EditorNode::get_singleton()->is_resource_read_only(animation)) {218animation = animation->duplicate();219}220ald->add_animation(animation_name, animation);221}222223EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();224undo_redo->create_action(vformat(TTR("Make Animation Library Unique: %s"), lib_name));225undo_redo->add_do_method(mixer, "remove_animation_library", lib_name);226undo_redo->add_do_method(mixer, "add_animation_library", lib_name, ald);227undo_redo->add_undo_method(mixer, "remove_animation_library", lib_name);228undo_redo->add_undo_method(mixer, "add_animation_library", lib_name, al);229undo_redo->add_do_method(this, "_update_editor", mixer);230undo_redo->add_undo_method(this, "_update_editor", mixer);231undo_redo->commit_action();232233update_tree();234235} break;236case FILE_MENU_EDIT_LIBRARY: {237EditorNode::get_singleton()->push_item(al.ptr());238} break;239240case FILE_MENU_SAVE_ANIMATION: {241if (anim->get_path().is_resource_file() && !FileAccess::exists(anim->get_path() + ".import")) {242EditorNode::get_singleton()->save_resource(anim);243break;244}245[[fallthrough]];246}247case FILE_MENU_SAVE_AS_ANIMATION: {248// Check if we're allowed to save this249{250String anim_path = al->get_path();251if (!anim_path.is_resource_file()) {252int srpos = anim_path.find("::");253if (srpos != -1) {254String base = anim_path.substr(0, srpos);255if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {256error_dialog->set_text(TTR("This animation can't be saved because it does not belong to the edited scene. Make it unique first."));257error_dialog->popup_centered();258return;259}260}261} else {262if (FileAccess::exists(anim_path + ".import")) {263error_dialog->set_text(TTR("This animation can't be saved because it was imported from another file. Make it unique first."));264error_dialog->popup_centered();265return;266}267}268}269270file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);271file_dialog->set_title(TTR("Save Animation"));272if (anim->get_path().is_resource_file()) {273file_dialog->set_current_path(anim->get_path());274} else {275file_dialog->set_current_file(String(file_dialog_animation) + ".res");276}277file_dialog->clear_filters();278List<String> exts;279ResourceLoader::get_recognized_extensions_for_type("Animation", &exts);280for (const String &K : exts) {281file_dialog->add_filter("*." + K);282}283284file_dialog->popup_centered_ratio();285file_dialog_action = FILE_DIALOG_ACTION_SAVE_ANIMATION;286} break;287case FILE_MENU_MAKE_ANIMATION_UNIQUE: {288StringName anim_name = file_dialog_animation;289290Ref<Animation> animd = anim->duplicate();291292EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();293undo_redo->create_action(vformat(TTR("Make Animation Unique: %s"), anim_name));294undo_redo->add_do_method(al.ptr(), "remove_animation", anim_name);295undo_redo->add_do_method(al.ptr(), "add_animation", anim_name, animd);296undo_redo->add_undo_method(al.ptr(), "remove_animation", anim_name);297undo_redo->add_undo_method(al.ptr(), "add_animation", anim_name, anim);298undo_redo->add_do_method(this, "_update_editor", mixer);299undo_redo->add_undo_method(this, "_update_editor", mixer);300undo_redo->commit_action();301302update_tree();303} break;304case FILE_MENU_EDIT_ANIMATION: {305EditorNode::get_singleton()->push_item(anim.ptr());306} break;307}308}309310void AnimationLibraryEditor::_load_file(const String &p_path) {311switch (file_dialog_action) {312case FILE_DIALOG_ACTION_SAVE_LIBRARY: {313Ref<AnimationLibrary> al = mixer->get_animation_library(file_dialog_library);314String prev_path = al->get_path();315EditorNode::get_singleton()->save_resource_in_path(al, p_path);316317if (al->get_path() != prev_path) { // Save successful.318EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();319320undo_redo->create_action(vformat(TTR("Save Animation library to File: %s"), file_dialog_library));321undo_redo->add_do_method(al.ptr(), "set_path", al->get_path());322undo_redo->add_undo_method(al.ptr(), "set_path", prev_path);323undo_redo->add_do_method(this, "_update_editor", mixer);324undo_redo->add_undo_method(this, "_update_editor", mixer);325undo_redo->commit_action();326}327328} break;329case FILE_DIALOG_ACTION_SAVE_ANIMATION: {330Ref<AnimationLibrary> al = mixer->get_animation_library(file_dialog_library);331Ref<Animation> anim;332if (file_dialog_animation != StringName()) {333anim = al->get_animation(file_dialog_animation);334ERR_FAIL_COND(anim.is_null());335}336String prev_path = anim->get_path();337EditorNode::get_singleton()->save_resource_in_path(anim, p_path);338if (anim->get_path() != prev_path) { // Save successful.339EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();340341undo_redo->create_action(vformat(TTR("Save Animation to File: %s"), file_dialog_animation));342undo_redo->add_do_method(anim.ptr(), "set_path", anim->get_path());343undo_redo->add_undo_method(anim.ptr(), "set_path", prev_path);344undo_redo->add_do_method(this, "_update_editor", mixer);345undo_redo->add_undo_method(this, "_update_editor", mixer);346undo_redo->commit_action();347}348} break;349default: {350}351}352}353354void AnimationLibraryEditor::_load_files(const PackedStringArray &p_paths) {355EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();356bool has_created_action = false;357bool show_error_diag = false;358List<String> name_list;359360switch (file_dialog_action) {361case FILE_DIALOG_ACTION_OPEN_LIBRARY: {362for (const String &path : p_paths) {363const Ref<Resource> res = ResourceLoader::load(path);364const Ref<AnimationLibrary> anim_library = res;365if (anim_library.is_null()) {366show_error_diag = true;367const Ref<PackedScene> scene = res;368if (scene.is_valid()) {369error_dialog->set_text(TTR("The file you selected is an imported scene from a 3D model such as glTF or FBX.\n\nIn Godot, 3D models can be imported as either scenes or animation libraries, which is why they show up here.\n\nIf you want to use animations from this 3D model, open the Advanced Import Settings\ndialog and save the animations using Actions... -> Set Animation Save Paths,\nor import the whole scene as a single AnimationLibrary in the Import dock."));370} else {371error_dialog->set_text(TTR("The file you selected is not a valid AnimationLibrary.\n\nIf the animations you want are inside of this file, save them to a separate file first."));372}373continue;374}375376List<StringName> libs;377mixer->get_animation_library_list(&libs);378bool is_already_added = false;379for (const StringName &K : libs) {380if (mixer->get_animation_library(K) == anim_library) {381// Prioritize the "invalid" error message.382if (!show_error_diag) {383show_error_diag = true;384error_dialog->set_text(TTR("Some of the selected libraries were already added to the mixer."));385}386387is_already_added = true;388break;389}390}391392if (is_already_added) {393continue;394}395396String name = AnimationLibrary::validate_library_name(path.get_file().get_basename());397int attempt = 1;398while (bool(mixer->has_animation_library(name)) || name_list.find(name)) {399attempt++;400name = path.get_file().get_basename() + " " + itos(attempt);401}402name_list.push_back(name);403404if (!has_created_action) {405has_created_action = true;406undo_redo->create_action(p_paths.size() > 1 ? TTR("Add Animation Libraries") : vformat(TTR("Add Animation Library: %s"), name));407}408undo_redo->add_do_method(mixer, "add_animation_library", name, anim_library);409undo_redo->add_undo_method(mixer, "remove_animation_library", name);410}411} break;412case FILE_DIALOG_ACTION_OPEN_ANIMATION: {413Ref<AnimationLibrary> al = mixer->get_animation_library(adding_animation_to_library);414for (const String &path : p_paths) {415Ref<Animation> anim = ResourceLoader::load(path);416if (anim.is_null()) {417show_error_diag = true;418error_dialog->set_text(TTR("Some Animation files were invalid."));419continue;420}421422List<StringName> anims;423al->get_animation_list(&anims);424bool is_already_added = false;425for (const StringName &K : anims) {426if (al->get_animation(K) == anim) {427// Prioritize the "invalid" error message.428if (!show_error_diag) {429show_error_diag = true;430error_dialog->set_text(TTR("Some of the selected animations were already added to the library."));431}432433is_already_added = true;434break;435}436}437438if (is_already_added) {439continue;440}441442String name = path.get_file().get_basename();443int attempt = 1;444while (al->has_animation(name) || name_list.find(name)) {445attempt++;446name = path.get_file().get_basename() + " " + itos(attempt);447}448name_list.push_back(name);449450if (!has_created_action) {451has_created_action = true;452undo_redo->create_action(p_paths.size() > 1 ? TTR("Load Animations into Library") : vformat(TTR("Load Animation into Library: %s"), name));453}454undo_redo->add_do_method(al.ptr(), "add_animation", name, anim);455undo_redo->add_undo_method(al.ptr(), "remove_animation", name);456}457} break;458default: {459}460}461462if (has_created_action) {463undo_redo->add_do_method(this, "_update_editor", mixer);464undo_redo->add_undo_method(this, "_update_editor", mixer);465undo_redo->commit_action();466}467468if (show_error_diag) {469error_dialog->popup_centered();470}471}472473void AnimationLibraryEditor::_item_renamed() {474TreeItem *ti = tree->get_edited();475String text = ti->get_text(0);476String old_text = ti->get_metadata(0);477bool restore_text = false;478EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();479480if (String(text).contains_char('/') || String(text).contains_char(':') || String(text).contains_char(',') || String(text).contains_char('[')) {481restore_text = true;482} else {483if (ti->get_parent() == tree->get_root()) {484// Renamed library485486if (mixer->has_animation_library(text)) {487restore_text = true;488} else {489undo_redo->create_action(vformat(TTR("Rename Animation Library: %s"), text));490undo_redo->add_do_method(mixer, "rename_animation_library", old_text, text);491undo_redo->add_undo_method(mixer, "rename_animation_library", text, old_text);492undo_redo->add_do_method(this, "_update_editor", mixer);493undo_redo->add_undo_method(this, "_update_editor", mixer);494updating = true;495undo_redo->commit_action();496updating = false;497ti->set_metadata(0, text);498if (text == "") {499ti->set_suffix(0, TTR("[Global]"));500} else {501ti->set_suffix(0, "");502}503}504} else {505// Renamed anim506StringName library = ti->get_parent()->get_metadata(0);507Ref<AnimationLibrary> al = mixer->get_animation_library(library);508509if (al.is_valid()) {510if (al->has_animation(text)) {511restore_text = true;512} else {513undo_redo->create_action(vformat(TTR("Rename Animation: %s"), text));514undo_redo->add_do_method(al.ptr(), "rename_animation", old_text, text);515undo_redo->add_undo_method(al.ptr(), "rename_animation", text, old_text);516undo_redo->add_do_method(this, "_update_editor", mixer);517undo_redo->add_undo_method(this, "_update_editor", mixer);518updating = true;519undo_redo->commit_action();520updating = false;521522ti->set_metadata(0, text);523}524} else {525restore_text = true;526}527}528}529530if (restore_text) {531ti->set_text(0, old_text);532}533534_save_mixer_lib_folding(ti);535}536537void AnimationLibraryEditor::_button_pressed(TreeItem *p_item, int p_column, int p_id, MouseButton p_button) {538if (p_item->get_parent() == tree->get_root()) {539// Library540StringName lib_name = p_item->get_metadata(0);541Ref<AnimationLibrary> al = mixer->get_animation_library(lib_name);542switch (p_id) {543case LIB_BUTTON_ADD: {544add_library_dialog->set_title(TTR("Animation Name:"));545add_library_name->set_text("");546add_library_dialog->popup_centered();547add_library_name->grab_focus();548adding_animation = true;549adding_animation_to_library = p_item->get_metadata(0);550_add_library_validate("");551} break;552case LIB_BUTTON_LOAD: {553adding_animation_to_library = p_item->get_metadata(0);554List<String> extensions;555ResourceLoader::get_recognized_extensions_for_type("Animation", &extensions);556557file_dialog->clear_filters();558for (const String &K : extensions) {559file_dialog->add_filter("*." + K);560}561562file_dialog->set_title(TTR("Load Animation"));563file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILES);564file_dialog->set_current_file("");565file_dialog->popup_centered_ratio();566567file_dialog_action = FILE_DIALOG_ACTION_OPEN_ANIMATION;568569} break;570case LIB_BUTTON_PASTE: {571Ref<Animation> anim = EditorSettings::get_singleton()->get_resource_clipboard();572if (anim.is_null()) {573error_dialog->set_text(TTR("No animation resource in clipboard!"));574error_dialog->popup_centered();575return;576}577578if (!anim->get_path().is_resource_file()) {579anim = anim->duplicate(); // Users simply dont care about referencing, so making a copy works better here.580}581582String base_name;583if (anim->get_name() != "") {584base_name = anim->get_name();585} else {586base_name = TTR("Pasted Animation");587}588589String name = base_name;590int attempt = 1;591while (al->has_animation(name)) {592attempt++;593name = base_name + " (" + itos(attempt) + ")";594}595596EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();597598undo_redo->create_action(vformat(TTR("Add Animation to Library: %s"), name));599undo_redo->add_do_method(al.ptr(), "add_animation", name, anim);600undo_redo->add_undo_method(al.ptr(), "remove_animation", name);601undo_redo->add_do_method(this, "_update_editor", mixer);602undo_redo->add_undo_method(this, "_update_editor", mixer);603undo_redo->commit_action();604605} break;606case LIB_BUTTON_FILE: {607file_popup->clear();608file_popup->add_item(TTR("Save"), FILE_MENU_SAVE_LIBRARY);609file_popup->add_item(TTR("Save As"), FILE_MENU_SAVE_AS_LIBRARY);610file_popup->add_separator();611file_popup->add_item(TTR("Make Unique"), FILE_MENU_MAKE_LIBRARY_UNIQUE);612file_popup->add_separator();613file_popup->add_item(TTR("Open in Inspector"), FILE_MENU_EDIT_LIBRARY);614Rect2 pos = tree->get_item_rect(p_item, 1, 0);615Vector2 popup_pos = tree->get_screen_transform().xform(pos.position + Vector2(0, pos.size.height));616file_popup->popup(Rect2(popup_pos, Size2()));617618file_dialog_animation = StringName();619file_dialog_library = lib_name;620} break;621case LIB_BUTTON_DELETE: {622EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();623undo_redo->create_action(vformat(TTR("Remove Animation Library: %s"), lib_name));624undo_redo->add_do_method(mixer, "remove_animation_library", lib_name);625undo_redo->add_undo_method(mixer, "add_animation_library", lib_name, al);626undo_redo->add_do_method(this, "_update_editor", mixer);627undo_redo->add_undo_method(this, "_update_editor", mixer);628undo_redo->commit_action();629} break;630}631632} else {633// Animation634StringName lib_name = p_item->get_parent()->get_metadata(0);635StringName anim_name = p_item->get_metadata(0);636Ref<AnimationLibrary> al = mixer->get_animation_library(lib_name);637Ref<Animation> anim = al->get_animation(anim_name);638ERR_FAIL_COND(anim.is_null());639switch (p_id) {640case ANIM_BUTTON_COPY: {641if (anim->get_name() == "") {642anim->set_name(anim_name); // Keep the name around643}644EditorSettings::get_singleton()->set_resource_clipboard(anim);645} break;646case ANIM_BUTTON_FILE: {647file_popup->clear();648file_popup->add_item(TTR("Save"), FILE_MENU_SAVE_ANIMATION);649file_popup->add_item(TTR("Save As"), FILE_MENU_SAVE_AS_ANIMATION);650file_popup->add_separator();651file_popup->add_item(TTR("Make Unique"), FILE_MENU_MAKE_ANIMATION_UNIQUE);652file_popup->add_separator();653file_popup->add_item(TTR("Open in Inspector"), FILE_MENU_EDIT_ANIMATION);654Rect2 pos = tree->get_item_rect(p_item, 1, 0);655Vector2 popup_pos = tree->get_screen_transform().xform(pos.position + Vector2(0, pos.size.height));656file_popup->popup(Rect2(popup_pos, Size2()));657658file_dialog_animation = anim_name;659file_dialog_library = lib_name;660661} break;662case ANIM_BUTTON_DELETE: {663EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();664undo_redo->create_action(vformat(TTR("Remove Animation from Library: %s"), anim_name));665undo_redo->add_do_method(al.ptr(), "remove_animation", anim_name);666undo_redo->add_undo_method(al.ptr(), "add_animation", anim_name, anim);667undo_redo->add_do_method(this, "_update_editor", mixer);668undo_redo->add_undo_method(this, "_update_editor", mixer);669undo_redo->commit_action();670} break;671}672}673}674675void AnimationLibraryEditor::update_tree() {676if (updating) {677return;678}679680tree->clear();681ERR_FAIL_NULL(mixer);682683Color ss_color = get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));684685TreeItem *root = tree->create_item();686List<StringName> libs;687Vector<uint64_t> collapsed_lib_ids = _load_mixer_libs_folding();688689mixer->get_animation_library_list(&libs);690691for (const StringName &K : libs) {692TreeItem *libitem = tree->create_item(root);693libitem->set_text(0, K);694if (K == StringName()) {695libitem->set_suffix(0, TTR("[Global]"));696} else {697libitem->set_suffix(0, "");698}699700Ref<AnimationLibrary> al = mixer->get_animation_library(K);701bool animation_library_is_foreign = false;702String al_path = al->get_path();703if (!al_path.is_resource_file()) {704libitem->set_text(1, TTR("[built-in]"));705libitem->set_tooltip_text(1, al_path);706int srpos = al_path.find("::");707if (srpos != -1) {708String base = al_path.substr(0, srpos);709if (ResourceLoader::get_resource_type(base) == "PackedScene") {710if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {711animation_library_is_foreign = true;712libitem->set_text(1, TTR("[foreign]"));713}714} else {715if (FileAccess::exists(base + ".import")) {716animation_library_is_foreign = true;717libitem->set_text(1, TTR("[imported]"));718}719}720}721} else {722if (FileAccess::exists(al_path + ".import")) {723animation_library_is_foreign = true;724libitem->set_text(1, TTR("[imported]"));725} else {726libitem->set_text(1, al_path.get_file());727}728}729730libitem->set_editable(0, true);731libitem->set_metadata(0, K);732libitem->set_icon(0, get_editor_theme_icon("AnimationLibrary"));733734libitem->add_button(0, get_editor_theme_icon("Add"), LIB_BUTTON_ADD, animation_library_is_foreign, TTR("Add animation to library."));735libitem->add_button(0, get_editor_theme_icon("Load"), LIB_BUTTON_LOAD, animation_library_is_foreign, TTR("Load animation from file and add to library."));736libitem->add_button(0, get_editor_theme_icon("ActionPaste"), LIB_BUTTON_PASTE, animation_library_is_foreign, TTR("Paste animation to library from clipboard."));737738libitem->add_button(1, get_editor_theme_icon("Save"), LIB_BUTTON_FILE, false, TTR("Save animation library to resource on disk."));739libitem->add_button(1, get_editor_theme_icon("Remove"), LIB_BUTTON_DELETE, false, TTR("Remove animation library."));740741libitem->set_custom_bg_color(0, ss_color);742743List<StringName> animations;744al->get_animation_list(&animations);745for (const StringName &L : animations) {746TreeItem *anitem = tree->create_item(libitem);747anitem->set_text(0, L);748anitem->set_editable(0, !animation_library_is_foreign);749anitem->set_metadata(0, L);750anitem->set_icon(0, get_editor_theme_icon("Animation"));751anitem->add_button(0, get_editor_theme_icon("ActionCopy"), ANIM_BUTTON_COPY, animation_library_is_foreign, TTR("Copy animation to clipboard."));752753Ref<Animation> anim = al->get_animation(L);754String anim_path = anim->get_path();755if (!anim_path.is_resource_file()) {756anitem->set_text(1, TTR("[built-in]"));757anitem->set_tooltip_text(1, anim_path);758int srpos = anim_path.find("::");759if (srpos != -1) {760String base = anim_path.substr(0, srpos);761if (ResourceLoader::get_resource_type(base) == "PackedScene") {762if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {763anitem->set_text(1, TTR("[foreign]"));764}765} else {766if (FileAccess::exists(base + ".import")) {767anitem->set_text(1, TTR("[imported]"));768}769}770}771} else {772if (FileAccess::exists(anim_path + ".import")) {773anitem->set_text(1, TTR("[imported]"));774} else {775anitem->set_text(1, anim_path.get_file());776}777}778779anitem->add_button(1, get_editor_theme_icon("Save"), ANIM_BUTTON_FILE, animation_library_is_foreign, TTR("Save animation to resource on disk."));780anitem->add_button(1, get_editor_theme_icon("Remove"), ANIM_BUTTON_DELETE, animation_library_is_foreign, TTR("Remove animation from Library."));781782for (const uint64_t &lib_id : collapsed_lib_ids) {783Object *lib_obj = ObjectDB::get_instance(ObjectID(lib_id));784AnimationLibrary *cur_lib = Object::cast_to<AnimationLibrary>(lib_obj);785StringName M = mixer->get_animation_library_name(cur_lib);786787if (M == K) {788libitem->set_collapsed_recursive(true);789}790}791}792}793}794795void AnimationLibraryEditor::_save_mixer_lib_folding(TreeItem *p_item) {796//Check if ti is a library or animation797if (p_item->get_parent()->get_parent() != nullptr) {798return;799}800801Ref<ConfigFile> config;802config.instantiate();803804String path = EditorPaths::get_singleton()->get_project_settings_dir().path_join("lib_folding.cfg");805Error err = config->load(path);806if (err != OK && err != ERR_FILE_NOT_FOUND) {807ERR_PRINT("Error loading lib_folding.cfg: " + itos(err));808}809810// Get unique identifier for this scene+mixer combination811String md = (mixer->get_tree()->get_edited_scene_root()->get_scene_file_path() + String(mixer->get_path())).md5_text();812813PackedStringArray collapsed_lib_names;814PackedStringArray collapsed_lib_ids;815816if (config->has_section(md)) {817collapsed_lib_names = String(config->get_value(md, "folding")).split("\n");818collapsed_lib_ids = String(config->get_value(md, "id")).split("\n");819}820821String lib_name = p_item->get_text(0);822823// Get library reference and check validity824Ref<AnimationLibrary> al;825uint64_t lib_id = 0;826827if (mixer->has_animation_library(lib_name)) {828al = mixer->get_animation_library(lib_name);829ERR_FAIL_COND(al.is_null());830lib_id = uint64_t(al->get_instance_id());831} else {832ERR_PRINT("Library not found: " + lib_name);833}834835int at = collapsed_lib_names.find(lib_name);836if (p_item->is_collapsed()) {837if (at != -1) {838//Entry exists and needs updating839collapsed_lib_ids.set(at, String::num_int64(lib_id + INT64_MIN));840} else {841//Check if it's a rename842int id_at = collapsed_lib_ids.find(String::num_int64(lib_id + INT64_MIN));843if (id_at != -1) {844//It's actually a rename845collapsed_lib_names.set(id_at, lib_name);846} else {847//It's a new entry848collapsed_lib_names.append(lib_name);849collapsed_lib_ids.append(String::num_int64(lib_id + INT64_MIN));850}851}852} else {853if (at != -1) {854collapsed_lib_names.remove_at(at);855collapsed_lib_ids.remove_at(at);856}857}858859//Runtime IDs860config->set_value(md, "root", uint64_t(mixer->get_tree()->get_edited_scene_root()->get_instance_id()));861config->set_value(md, "mixer", uint64_t(mixer->get_instance_id()));862863//Plan B recovery mechanism864config->set_value(md, "mixer_signature", _get_mixer_signature());865866//Save folding state as text and runtime ID867config->set_value(md, "folding", String("\n").join(collapsed_lib_names));868config->set_value(md, "id", String("\n").join(collapsed_lib_ids));869870err = config->save(path);871if (err != OK) {872ERR_PRINT("Error saving lib_folding.cfg: " + itos(err));873}874}875876Vector<uint64_t> AnimationLibraryEditor::_load_mixer_libs_folding() {877Ref<ConfigFile> config;878config.instantiate();879880String path = EditorPaths::get_singleton()->get_project_settings_dir().path_join("lib_folding.cfg");881Error err = config->load(path);882if (err != OK && err != ERR_FILE_NOT_FOUND) {883ERR_PRINT("Error loading lib_folding.cfg: " + itos(err));884return Vector<uint64_t>();885}886887// Get unique identifier for this scene+mixer combination888String md = (mixer->get_tree()->get_edited_scene_root()->get_scene_file_path() + String(mixer->get_path())).md5_text();889890Vector<uint64_t> collapsed_lib_ids;891892if (config->has_section(md)) {893_load_config_libs_folding(collapsed_lib_ids, config.ptr(), md);894895} else {896//The scene/mixer combination is no longer valid and we'll try to recover897uint64_t current_mixer_id = uint64_t(mixer->get_instance_id());898String current_mixer_signature = _get_mixer_signature();899Vector<String> sections = config->get_sections();900901for (const String §ion : sections) {902Variant mixer_id = config->get_value(section, "mixer");903if ((mixer_id.get_type() == Variant::INT && uint64_t(mixer_id) == current_mixer_id) || config->get_value(section, "mixer_signature") == current_mixer_signature) { // Ensure value exists and is correct type904// Found the mixer in a different section!905_load_config_libs_folding(collapsed_lib_ids, config.ptr(), section);906907//Cleanup old entry and copy fold data into new one!908String collapsed_lib_names_str = String(config->get_value(section, "folding"));909String collapsed_lib_ids_str = String(config->get_value(section, "id"));910config->erase_section(section);911912config->set_value(md, "root", uint64_t(mixer->get_tree()->get_edited_scene_root()->get_instance_id()));913config->set_value(md, "mixer", uint64_t(mixer->get_instance_id()));914config->set_value(md, "mixer_signature", _get_mixer_signature());915config->set_value(md, "folding", collapsed_lib_names_str);916config->set_value(md, "id", collapsed_lib_ids_str);917918err = config->save(path);919if (err != OK) {920ERR_PRINT("Error saving lib_folding.cfg: " + itos(err));921}922break;923}924}925}926927return collapsed_lib_ids;928}929930void AnimationLibraryEditor::_load_config_libs_folding(Vector<uint64_t> &p_lib_ids, ConfigFile *p_config, String p_section) {931if (uint64_t(p_config->get_value(p_section, "root", 0)) != uint64_t(mixer->get_tree()->get_edited_scene_root()->get_instance_id())) {932// Root changed - tries to match by library names933PackedStringArray collapsed_lib_names = String(p_config->get_value(p_section, "folding", "")).split("\n");934for (const String &lib_name : collapsed_lib_names) {935if (mixer->has_animation_library(lib_name)) {936p_lib_ids.append(mixer->get_animation_library(lib_name)->get_instance_id());937} else {938print_line("Can't find ", lib_name, " in mixer");939}940}941} else {942// Root same - uses saved instance IDs943for (const String &saved_id : String(p_config->get_value(p_section, "id")).split("\n")) {944p_lib_ids.append(uint64_t(saved_id.to_int() - INT64_MIN));945}946}947}948949String AnimationLibraryEditor::_get_mixer_signature() const {950String signature = String();951952// Get all libraries sorted for consistency953List<StringName> libs;954mixer->get_animation_library_list(&libs);955libs.sort_custom<StringName::AlphCompare>();956957// Add libraries and their animations to signature958for (const StringName &lib_name : libs) {959signature += "::" + String(lib_name);960Ref<AnimationLibrary> lib = mixer->get_animation_library(lib_name);961if (lib.is_valid()) {962List<StringName> anims;963lib->get_animation_list(&anims);964anims.sort_custom<StringName::AlphCompare>();965for (const StringName &anim_name : anims) {966signature += "," + String(anim_name);967}968}969}970971return signature.md5_text();972}973974void AnimationLibraryEditor::show_dialog() {975update_tree();976popup_centered_ratio(0.5);977}978979void AnimationLibraryEditor::_notification(int p_what) {980switch (p_what) {981case NOTIFICATION_THEME_CHANGED: {982new_library_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));983load_library_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));984}985}986}987988void AnimationLibraryEditor::_update_editor(Object *p_mixer) {989emit_signal("update_editor", p_mixer);990}991992void AnimationLibraryEditor::shortcut_input(const Ref<InputEvent> &p_event) {993const Ref<InputEventKey> k = p_event;994if (k.is_valid() && k->is_pressed()) {995bool handled = false;996997if (ED_IS_SHORTCUT("ui_undo", p_event)) {998EditorNode::get_singleton()->undo();999handled = true;1000}10011002if (ED_IS_SHORTCUT("ui_redo", p_event)) {1003EditorNode::get_singleton()->redo();1004handled = true;1005}10061007if (handled) {1008set_input_as_handled();1009}1010}1011}10121013void AnimationLibraryEditor::_bind_methods() {1014ClassDB::bind_method(D_METHOD("_update_editor", "mixer"), &AnimationLibraryEditor::_update_editor);1015ADD_SIGNAL(MethodInfo("update_editor"));1016}10171018AnimationLibraryEditor::AnimationLibraryEditor() {1019set_title(TTR("Edit Animation Libraries"));1020set_process_shortcut_input(true);10211022file_dialog = memnew(EditorFileDialog);1023add_child(file_dialog);1024file_dialog->connect("file_selected", callable_mp(this, &AnimationLibraryEditor::_load_file));1025file_dialog->connect("files_selected", callable_mp(this, &AnimationLibraryEditor::_load_files));10261027add_library_dialog = memnew(ConfirmationDialog);1028VBoxContainer *dialog_vb = memnew(VBoxContainer);1029add_library_name = memnew(LineEdit);1030dialog_vb->add_child(add_library_name);1031add_library_name->connect(SceneStringName(text_changed), callable_mp(this, &AnimationLibraryEditor::_add_library_validate));1032add_child(add_library_dialog);10331034add_library_validate = memnew(Label);1035dialog_vb->add_child(add_library_validate);1036add_library_dialog->add_child(dialog_vb);1037add_library_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationLibraryEditor::_add_library_confirm));1038add_library_dialog->register_text_enter(add_library_name);10391040VBoxContainer *vb = memnew(VBoxContainer);1041HBoxContainer *hb = memnew(HBoxContainer);1042hb->add_spacer(true);1043new_library_button = memnew(Button(TTR("New Library")));1044new_library_button->set_tooltip_text(TTR("Create new empty animation library."));1045new_library_button->connect(SceneStringName(pressed), callable_mp(this, &AnimationLibraryEditor::_add_library));1046hb->add_child(new_library_button);1047load_library_button = memnew(Button(TTR("Load Library")));1048load_library_button->set_tooltip_text(TTR("Load animation library from disk."));1049load_library_button->connect(SceneStringName(pressed), callable_mp(this, &AnimationLibraryEditor::_load_library));1050hb->add_child(load_library_button);1051vb->add_child(hb);1052tree = memnew(Tree);1053vb->add_child(tree);10541055tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);1056tree->set_columns(2);1057tree->set_column_titles_visible(true);1058tree->set_column_title(0, TTR("Resource"));1059tree->set_column_title(1, TTR("Storage"));1060tree->set_column_expand(0, true);1061tree->set_column_custom_minimum_width(1, EDSCALE * 250);1062tree->set_column_expand(1, false);1063tree->set_hide_root(true);1064tree->set_hide_folding(false);1065tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);10661067tree->connect("item_edited", callable_mp(this, &AnimationLibraryEditor::_item_renamed));1068tree->connect("button_clicked", callable_mp(this, &AnimationLibraryEditor::_button_pressed));1069tree->connect("item_collapsed", callable_mp(this, &AnimationLibraryEditor::_save_mixer_lib_folding));10701071file_popup = memnew(PopupMenu);1072add_child(file_popup);1073file_popup->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationLibraryEditor::_file_popup_selected));10741075add_child(vb);10761077error_dialog = memnew(AcceptDialog);1078error_dialog->set_title(TTR("Error:"));1079add_child(error_dialog);1080}108110821083