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 | bool AnimationLibrary::is_valid_animation_name(const String &p_name) { |
34 | return !(p_name.is_empty() || p_name.contains("/" ) || p_name.contains(":" ) || p_name.contains("," ) || p_name.contains("[" )); |
35 | } |
36 | |
37 | bool AnimationLibrary::is_valid_library_name(const String &p_name) { |
38 | return !(p_name.contains("/" ) || p_name.contains(":" ) || p_name.contains("," ) || p_name.contains("[" )); |
39 | } |
40 | |
41 | String AnimationLibrary::validate_library_name(const String &p_name) { |
42 | String name = p_name; |
43 | const char *characters = "/:,[" ; |
44 | for (const char *p = characters; *p; p++) { |
45 | name = name.replace(String::chr(*p), "_" ); |
46 | } |
47 | return name; |
48 | } |
49 | |
50 | Error AnimationLibrary::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) { |
51 | ERR_FAIL_COND_V_MSG(!is_valid_animation_name(p_name), ERR_INVALID_PARAMETER, "Invalid animation name: '" + String(p_name) + "'." ); |
52 | ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER); |
53 | |
54 | if (animations.has(p_name)) { |
55 | animations.get(p_name)->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed)); |
56 | animations.erase(p_name); |
57 | emit_signal(SNAME("animation_removed" ), p_name); |
58 | } |
59 | |
60 | animations.insert(p_name, p_animation); |
61 | animations.get(p_name)->connect_changed(callable_mp(this, &AnimationLibrary::_animation_changed).bind(p_name)); |
62 | emit_signal(SNAME("animation_added" ), p_name); |
63 | notify_property_list_changed(); |
64 | return OK; |
65 | } |
66 | |
67 | void AnimationLibrary::remove_animation(const StringName &p_name) { |
68 | ERR_FAIL_COND_MSG(!animations.has(p_name), vformat("Animation not found: %s." , p_name)); |
69 | |
70 | animations.get(p_name)->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed)); |
71 | animations.erase(p_name); |
72 | emit_signal(SNAME("animation_removed" ), p_name); |
73 | notify_property_list_changed(); |
74 | } |
75 | |
76 | void AnimationLibrary::rename_animation(const StringName &p_name, const StringName &p_new_name) { |
77 | ERR_FAIL_COND_MSG(!animations.has(p_name), vformat("Animation not found: %s." , p_name)); |
78 | ERR_FAIL_COND_MSG(!is_valid_animation_name(p_new_name), "Invalid animation name: '" + String(p_new_name) + "'." ); |
79 | ERR_FAIL_COND_MSG(animations.has(p_new_name), vformat("Animation name \"%s\" already exists in library." , p_new_name)); |
80 | |
81 | animations.get(p_name)->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed)); |
82 | animations.get(p_name)->connect_changed(callable_mp(this, &AnimationLibrary::_animation_changed).bind(p_new_name)); |
83 | animations.insert(p_new_name, animations[p_name]); |
84 | animations.erase(p_name); |
85 | emit_signal(SNAME("animation_renamed" ), p_name, p_new_name); |
86 | } |
87 | |
88 | bool AnimationLibrary::has_animation(const StringName &p_name) const { |
89 | return animations.has(p_name); |
90 | } |
91 | |
92 | Ref<Animation> AnimationLibrary::get_animation(const StringName &p_name) const { |
93 | ERR_FAIL_COND_V_MSG(!animations.has(p_name), Ref<Animation>(), vformat("Animation not found: \"%s\"." , p_name)); |
94 | |
95 | return animations[p_name]; |
96 | } |
97 | |
98 | TypedArray<StringName> AnimationLibrary::_get_animation_list() const { |
99 | TypedArray<StringName> ret; |
100 | List<StringName> names; |
101 | get_animation_list(&names); |
102 | for (const StringName &K : names) { |
103 | ret.push_back(K); |
104 | } |
105 | return ret; |
106 | } |
107 | |
108 | void AnimationLibrary::_animation_changed(const StringName &p_name) { |
109 | emit_signal(SNAME("animation_changed" ), p_name); |
110 | } |
111 | |
112 | void AnimationLibrary::get_animation_list(List<StringName> *p_animations) const { |
113 | List<StringName> anims; |
114 | |
115 | for (const KeyValue<StringName, Ref<Animation>> &E : animations) { |
116 | anims.push_back(E.key); |
117 | } |
118 | |
119 | anims.sort_custom<StringName::AlphCompare>(); |
120 | |
121 | for (const StringName &E : anims) { |
122 | p_animations->push_back(E); |
123 | } |
124 | } |
125 | |
126 | void AnimationLibrary::_set_data(const Dictionary &p_data) { |
127 | for (KeyValue<StringName, Ref<Animation>> &K : animations) { |
128 | K.value->disconnect_changed(callable_mp(this, &AnimationLibrary::_animation_changed)); |
129 | } |
130 | animations.clear(); |
131 | List<Variant> keys; |
132 | p_data.get_key_list(&keys); |
133 | for (const Variant &K : keys) { |
134 | add_animation(K, p_data[K]); |
135 | } |
136 | } |
137 | |
138 | Dictionary AnimationLibrary::_get_data() const { |
139 | Dictionary ret; |
140 | for (const KeyValue<StringName, Ref<Animation>> &K : animations) { |
141 | ret[K.key] = K.value; |
142 | } |
143 | return ret; |
144 | } |
145 | |
146 | void AnimationLibrary::_bind_methods() { |
147 | ClassDB::bind_method(D_METHOD("add_animation" , "name" , "animation" ), &AnimationLibrary::add_animation); |
148 | ClassDB::bind_method(D_METHOD("remove_animation" , "name" ), &AnimationLibrary::remove_animation); |
149 | ClassDB::bind_method(D_METHOD("rename_animation" , "name" , "newname" ), &AnimationLibrary::rename_animation); |
150 | ClassDB::bind_method(D_METHOD("has_animation" , "name" ), &AnimationLibrary::has_animation); |
151 | ClassDB::bind_method(D_METHOD("get_animation" , "name" ), &AnimationLibrary::get_animation); |
152 | ClassDB::bind_method(D_METHOD("get_animation_list" ), &AnimationLibrary::_get_animation_list); |
153 | |
154 | ClassDB::bind_method(D_METHOD("_set_data" , "data" ), &AnimationLibrary::_set_data); |
155 | ClassDB::bind_method(D_METHOD("_get_data" ), &AnimationLibrary::_get_data); |
156 | |
157 | ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_data" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "_set_data" , "_get_data" ); |
158 | ADD_SIGNAL(MethodInfo("animation_added" , PropertyInfo(Variant::STRING_NAME, "name" ))); |
159 | ADD_SIGNAL(MethodInfo("animation_removed" , PropertyInfo(Variant::STRING_NAME, "name" ))); |
160 | ADD_SIGNAL(MethodInfo("animation_renamed" , PropertyInfo(Variant::STRING_NAME, "name" ), PropertyInfo(Variant::STRING_NAME, "to_name" ))); |
161 | ADD_SIGNAL(MethodInfo("animation_changed" , PropertyInfo(Variant::STRING_NAME, "name" ))); |
162 | } |
163 | AnimationLibrary::AnimationLibrary() { |
164 | } |
165 | |