| 1 | /**************************************************************************/ |
| 2 | /* animation_library.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "animation_library.h" |
| 32 | |
| 33 | #include "scene/scene_string_names.h" |
| 34 | |
| 35 | bool AnimationLibrary::is_valid_animation_name(const String &p_name) { |
| 36 | return !(p_name.is_empty() || p_name.contains_char('/') || p_name.contains_char(':') || p_name.contains_char(',') || p_name.contains_char('[')); |
| 37 | } |
| 38 | |
| 39 | bool AnimationLibrary::is_valid_library_name(const String &p_name) { |
| 40 | return !(p_name.contains_char('/') || p_name.contains_char(':') || p_name.contains_char(',') || p_name.contains_char('[')); |
| 41 | } |
| 42 | |
| 43 | String AnimationLibrary::validate_library_name(const String &p_name) { |
| 44 | return p_name.replace_chars("/:,[", '_'); |
| 45 | } |
| 46 | |
| 47 | Error AnimationLibrary::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) { |
| 48 | ERR_FAIL_COND_V_MSG(!is_valid_animation_name(p_name), ERR_INVALID_PARAMETER, "Invalid animation name: '" + String(p_name) + "'."); |
| 49 | ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER); |
| 50 | |
| 51 | if (animations.has(p_name)) { |
| 52 | animations[p_name]->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed)); |
| 53 | animations.erase(p_name); |
| 54 | emit_signal(SNAME("animation_removed"), p_name); |
| 55 | } |
| 56 | |
| 57 | animations.insert(p_name, p_animation); |
| 58 | animations[p_name]->connect_changed(callable_mp(this, &AnimationLibrary::_animation_changed).bind(p_name)); |
| 59 | emit_signal(SNAME("animation_added"), p_name); |
| 60 | notify_property_list_changed(); |
| 61 | return OK; |
| 62 | } |
| 63 | |
| 64 | void AnimationLibrary::remove_animation(const StringName &p_name) { |
| 65 | ERR_FAIL_COND_MSG(!animations.has(p_name), vformat("Animation not found: %s.", p_name)); |
| 66 | |
| 67 | animations[p_name]->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed)); |
| 68 | animations.erase(p_name); |
| 69 | emit_signal(SNAME("animation_removed"), p_name); |
| 70 | notify_property_list_changed(); |
| 71 | } |
| 72 | |
| 73 | void AnimationLibrary::rename_animation(const StringName &p_name, const StringName &p_new_name) { |
| 74 | ERR_FAIL_COND_MSG(!animations.has(p_name), vformat("Animation not found: %s.", p_name)); |
| 75 | ERR_FAIL_COND_MSG(!is_valid_animation_name(p_new_name), "Invalid animation name: '" + String(p_new_name) + "'."); |
| 76 | ERR_FAIL_COND_MSG(animations.has(p_new_name), vformat("Animation name \"%s\" already exists in library.", p_new_name)); |
| 77 | |
| 78 | animations[p_name]->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed)); |
| 79 | animations[p_name]->connect_changed(callable_mp(this, &AnimationLibrary::_animation_changed).bind(p_new_name)); |
| 80 | animations.insert(p_new_name, animations[p_name]); |
| 81 | animations.erase(p_name); |
| 82 | emit_signal(SNAME("animation_renamed"), p_name, p_new_name); |
| 83 | } |
| 84 | |
| 85 | bool AnimationLibrary::has_animation(const StringName &p_name) const { |
| 86 | return animations.has(p_name); |
| 87 | } |
| 88 | |
| 89 | Ref<Animation> AnimationLibrary::get_animation(const StringName &p_name) const { |
| 90 | ERR_FAIL_COND_V_MSG(!animations.has(p_name), Ref<Animation>(), vformat("Animation not found: \"%s\".", p_name)); |
| 91 | |
| 92 | return animations[p_name]; |
| 93 | } |
| 94 | |
| 95 | TypedArray<StringName> AnimationLibrary::_get_animation_list() const { |
| 96 | TypedArray<StringName> ret; |
| 97 | List<StringName> names; |
| 98 | get_animation_list(&names); |
| 99 | for (const StringName &K : names) { |
| 100 | ret.push_back(K); |
| 101 | } |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | void AnimationLibrary::_animation_changed(const StringName &p_name) { |
| 106 | emit_signal(SceneStringName(animation_changed), p_name); |
| 107 | } |
| 108 | |
| 109 | void AnimationLibrary::get_animation_list(List<StringName> *p_animations) const { |
| 110 | List<StringName> anims; |
| 111 | |
| 112 | for (const KeyValue<StringName, Ref<Animation>> &E : animations) { |
| 113 | anims.push_back(E.key); |
| 114 | } |
| 115 | |
| 116 | anims.sort_custom<StringName::AlphCompare>(); |
| 117 | |
| 118 | for (const StringName &E : anims) { |
| 119 | p_animations->push_back(E); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | int AnimationLibrary::get_animation_list_size() const { |
| 124 | return animations.size(); |
| 125 | } |
| 126 | |
| 127 | void AnimationLibrary::_set_data(const Dictionary &p_data) { |
| 128 | for (KeyValue<StringName, Ref<Animation>> &K : animations) { |
| 129 | K.value->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed)); |
| 130 | } |
| 131 | animations.clear(); |
| 132 | for (const KeyValue<Variant, Variant> &kv : p_data) { |
| 133 | add_animation(kv.key, kv.value); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | Dictionary AnimationLibrary::_get_data() const { |
| 138 | Dictionary ret; |
| 139 | for (const KeyValue<StringName, Ref<Animation>> &K : animations) { |
| 140 | ret[K.key] = K.value; |
| 141 | } |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | #ifdef TOOLS_ENABLED |
| 146 | void AnimationLibrary::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { |
| 147 | const String pf = p_function; |
| 148 | if (p_idx == 0 && (pf == "get_animation" || pf == "has_animation" || pf == "rename_animation" || pf == "remove_animation")) { |
| 149 | List<StringName> names; |
| 150 | get_animation_list(&names); |
| 151 | for (const StringName &E : names) { |
| 152 | r_options->push_back(E.operator String().quote()); |
| 153 | } |
| 154 | } |
| 155 | Resource::get_argument_options(p_function, p_idx, r_options); |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | void AnimationLibrary::_bind_methods() { |
| 160 | ClassDB::bind_method(D_METHOD("add_animation", "name", "animation"), &AnimationLibrary::add_animation); |
| 161 | ClassDB::bind_method(D_METHOD("remove_animation", "name"), &AnimationLibrary::remove_animation); |
| 162 | ClassDB::bind_method(D_METHOD("rename_animation", "name", "newname"), &AnimationLibrary::rename_animation); |
| 163 | ClassDB::bind_method(D_METHOD("has_animation", "name"), &AnimationLibrary::has_animation); |
| 164 | ClassDB::bind_method(D_METHOD("get_animation", "name"), &AnimationLibrary::get_animation); |
| 165 | ClassDB::bind_method(D_METHOD("get_animation_list"), &AnimationLibrary::_get_animation_list); |
| 166 | ClassDB::bind_method(D_METHOD("get_animation_list_size"), &AnimationLibrary::get_animation_list_size); |
| 167 | |
| 168 | ClassDB::bind_method(D_METHOD("_set_data", "data"), &AnimationLibrary::_set_data); |
| 169 | ClassDB::bind_method(D_METHOD("_get_data"), &AnimationLibrary::_get_data); |
| 170 | |
| 171 | ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data"); |
| 172 | |
| 173 | ADD_SIGNAL(MethodInfo("animation_added", PropertyInfo(Variant::STRING_NAME, "name"))); |
| 174 | ADD_SIGNAL(MethodInfo("animation_removed", PropertyInfo(Variant::STRING_NAME, "name"))); |
| 175 | ADD_SIGNAL(MethodInfo("animation_renamed", PropertyInfo(Variant::STRING_NAME, "name"), PropertyInfo(Variant::STRING_NAME, "to_name"))); |
| 176 | ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING_NAME, "name"))); |
| 177 | } |
| 178 | AnimationLibrary::AnimationLibrary() { |
| 179 | } |