Path: blob/master/scene/resources/animation_library.cpp
9896 views
/**************************************************************************/1/* animation_library.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.h"3132#include "scene/scene_string_names.h"3334bool AnimationLibrary::is_valid_animation_name(const String &p_name) {35return !(p_name.is_empty() || p_name.contains_char('/') || p_name.contains_char(':') || p_name.contains_char(',') || p_name.contains_char('['));36}3738bool AnimationLibrary::is_valid_library_name(const String &p_name) {39return !(p_name.contains_char('/') || p_name.contains_char(':') || p_name.contains_char(',') || p_name.contains_char('['));40}4142String AnimationLibrary::validate_library_name(const String &p_name) {43return p_name.replace_chars("/:,[", '_');44}4546Error AnimationLibrary::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) {47ERR_FAIL_COND_V_MSG(!is_valid_animation_name(p_name), ERR_INVALID_PARAMETER, "Invalid animation name: '" + String(p_name) + "'.");48ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER);4950if (animations.has(p_name)) {51animations.get(p_name)->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed));52animations.erase(p_name);53emit_signal(SNAME("animation_removed"), p_name);54}5556animations.insert(p_name, p_animation);57animations.get(p_name)->connect_changed(callable_mp(this, &AnimationLibrary::_animation_changed).bind(p_name));58emit_signal(SNAME("animation_added"), p_name);59notify_property_list_changed();60return OK;61}6263void AnimationLibrary::remove_animation(const StringName &p_name) {64ERR_FAIL_COND_MSG(!animations.has(p_name), vformat("Animation not found: %s.", p_name));6566animations.get(p_name)->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed));67animations.erase(p_name);68emit_signal(SNAME("animation_removed"), p_name);69notify_property_list_changed();70}7172void AnimationLibrary::rename_animation(const StringName &p_name, const StringName &p_new_name) {73ERR_FAIL_COND_MSG(!animations.has(p_name), vformat("Animation not found: %s.", p_name));74ERR_FAIL_COND_MSG(!is_valid_animation_name(p_new_name), "Invalid animation name: '" + String(p_new_name) + "'.");75ERR_FAIL_COND_MSG(animations.has(p_new_name), vformat("Animation name \"%s\" already exists in library.", p_new_name));7677animations.get(p_name)->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed));78animations.get(p_name)->connect_changed(callable_mp(this, &AnimationLibrary::_animation_changed).bind(p_new_name));79animations.insert(p_new_name, animations[p_name]);80animations.erase(p_name);81emit_signal(SNAME("animation_renamed"), p_name, p_new_name);82}8384bool AnimationLibrary::has_animation(const StringName &p_name) const {85return animations.has(p_name);86}8788Ref<Animation> AnimationLibrary::get_animation(const StringName &p_name) const {89ERR_FAIL_COND_V_MSG(!animations.has(p_name), Ref<Animation>(), vformat("Animation not found: \"%s\".", p_name));9091return animations[p_name];92}9394TypedArray<StringName> AnimationLibrary::_get_animation_list() const {95TypedArray<StringName> ret;96List<StringName> names;97get_animation_list(&names);98for (const StringName &K : names) {99ret.push_back(K);100}101return ret;102}103104void AnimationLibrary::_animation_changed(const StringName &p_name) {105emit_signal(SceneStringName(animation_changed), p_name);106}107108void AnimationLibrary::get_animation_list(List<StringName> *p_animations) const {109List<StringName> anims;110111for (const KeyValue<StringName, Ref<Animation>> &E : animations) {112anims.push_back(E.key);113}114115anims.sort_custom<StringName::AlphCompare>();116117for (const StringName &E : anims) {118p_animations->push_back(E);119}120}121122int AnimationLibrary::get_animation_list_size() const {123return animations.size();124}125126void AnimationLibrary::_set_data(const Dictionary &p_data) {127for (KeyValue<StringName, Ref<Animation>> &K : animations) {128K.value->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed));129}130animations.clear();131for (const KeyValue<Variant, Variant> &kv : p_data) {132add_animation(kv.key, kv.value);133}134}135136Dictionary AnimationLibrary::_get_data() const {137Dictionary ret;138for (const KeyValue<StringName, Ref<Animation>> &K : animations) {139ret[K.key] = K.value;140}141return ret;142}143144#ifdef TOOLS_ENABLED145void AnimationLibrary::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {146const String pf = p_function;147if (p_idx == 0 && (pf == "get_animation" || pf == "has_animation" || pf == "rename_animation" || pf == "remove_animation")) {148List<StringName> names;149get_animation_list(&names);150for (const StringName &E : names) {151r_options->push_back(E.operator String().quote());152}153}154Resource::get_argument_options(p_function, p_idx, r_options);155}156#endif157158void AnimationLibrary::_bind_methods() {159ClassDB::bind_method(D_METHOD("add_animation", "name", "animation"), &AnimationLibrary::add_animation);160ClassDB::bind_method(D_METHOD("remove_animation", "name"), &AnimationLibrary::remove_animation);161ClassDB::bind_method(D_METHOD("rename_animation", "name", "newname"), &AnimationLibrary::rename_animation);162ClassDB::bind_method(D_METHOD("has_animation", "name"), &AnimationLibrary::has_animation);163ClassDB::bind_method(D_METHOD("get_animation", "name"), &AnimationLibrary::get_animation);164ClassDB::bind_method(D_METHOD("get_animation_list"), &AnimationLibrary::_get_animation_list);165ClassDB::bind_method(D_METHOD("get_animation_list_size"), &AnimationLibrary::get_animation_list_size);166167ClassDB::bind_method(D_METHOD("_set_data", "data"), &AnimationLibrary::_set_data);168ClassDB::bind_method(D_METHOD("_get_data"), &AnimationLibrary::_get_data);169170ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");171172ADD_SIGNAL(MethodInfo("animation_added", PropertyInfo(Variant::STRING_NAME, "name")));173ADD_SIGNAL(MethodInfo("animation_removed", PropertyInfo(Variant::STRING_NAME, "name")));174ADD_SIGNAL(MethodInfo("animation_renamed", PropertyInfo(Variant::STRING_NAME, "name"), PropertyInfo(Variant::STRING_NAME, "to_name")));175ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING_NAME, "name")));176}177AnimationLibrary::AnimationLibrary() {178}179180181