Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/bone_map.cpp
20871 views
1
/**************************************************************************/
2
/* bone_map.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 "bone_map.h"
32
33
#include "core/config/engine.h"
34
35
bool BoneMap::_set(const StringName &p_path, const Variant &p_value) {
36
String path = p_path;
37
if (path.begins_with("bone_map/")) {
38
String which = path.get_slicec('/', 1);
39
set_skeleton_bone_name(which, p_value);
40
return true;
41
}
42
return false;
43
}
44
45
bool BoneMap::_get(const StringName &p_path, Variant &r_ret) const {
46
String path = p_path;
47
if (path.begins_with("bone_map/")) {
48
String which = path.get_slicec('/', 1);
49
r_ret = get_skeleton_bone_name(which);
50
return true;
51
}
52
return false;
53
}
54
55
void BoneMap::_get_property_list(List<PropertyInfo> *p_list) const {
56
HashMap<StringName, StringName>::ConstIterator E = bone_map.begin();
57
while (E) {
58
p_list->push_back(PropertyInfo(Variant::STRING_NAME, "bone_map/" + E->key, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
59
++E;
60
}
61
}
62
63
Ref<SkeletonProfile> BoneMap::get_profile() const {
64
return profile;
65
}
66
67
void BoneMap::set_profile(const Ref<SkeletonProfile> &p_profile) {
68
bool is_changed = profile != p_profile;
69
if (is_changed) {
70
if (profile.is_valid() && profile->is_connected("profile_updated", callable_mp(this, &BoneMap::_update_profile))) {
71
profile->disconnect("profile_updated", callable_mp(this, &BoneMap::_update_profile));
72
}
73
profile = p_profile;
74
if (profile.is_valid()) {
75
profile->connect("profile_updated", callable_mp(this, &BoneMap::_update_profile));
76
}
77
_update_profile();
78
}
79
notify_property_list_changed();
80
}
81
82
StringName BoneMap::get_skeleton_bone_name(const StringName &p_profile_bone_name) const {
83
ERR_FAIL_COND_V(!bone_map.has(p_profile_bone_name), StringName());
84
return bone_map.get(p_profile_bone_name);
85
}
86
87
void BoneMap::_set_skeleton_bone_name(const StringName &p_profile_bone_name, const StringName &p_skeleton_bone_name) {
88
ERR_FAIL_COND(!bone_map.has(p_profile_bone_name));
89
bone_map.insert(p_profile_bone_name, p_skeleton_bone_name);
90
}
91
92
void BoneMap::set_skeleton_bone_name(const StringName &p_profile_bone_name, const StringName &p_skeleton_bone_name) {
93
_set_skeleton_bone_name(p_profile_bone_name, p_skeleton_bone_name);
94
emit_signal("bone_map_updated");
95
}
96
97
StringName BoneMap::find_profile_bone_name(const StringName &p_skeleton_bone_name) const {
98
StringName profile_bone_name;
99
HashMap<StringName, StringName>::ConstIterator E = bone_map.begin();
100
while (E) {
101
if (E->value == p_skeleton_bone_name) {
102
profile_bone_name = E->key;
103
break;
104
}
105
++E;
106
}
107
return profile_bone_name;
108
}
109
110
int BoneMap::get_skeleton_bone_name_count(const StringName &p_skeleton_bone_name) const {
111
int count = 0;
112
HashMap<StringName, StringName>::ConstIterator E = bone_map.begin();
113
while (E) {
114
if (E->value == p_skeleton_bone_name) {
115
++count;
116
}
117
++E;
118
}
119
return count;
120
}
121
122
void BoneMap::_update_profile() {
123
_validate_bone_map();
124
emit_signal("profile_updated");
125
}
126
127
void BoneMap::_validate_bone_map() {
128
Ref<SkeletonProfile> current_profile = get_profile();
129
if (current_profile.is_valid()) {
130
// Insert missing profile bones into bone map.
131
int len = current_profile->get_bone_size();
132
StringName profile_bone_name;
133
for (int i = 0; i < len; i++) {
134
profile_bone_name = current_profile->get_bone_name(i);
135
if (!bone_map.has(profile_bone_name)) {
136
bone_map.insert(profile_bone_name, StringName());
137
}
138
}
139
// Remove bones that do not exist in the profile from the map.
140
Vector<StringName> delete_bones;
141
StringName k;
142
HashMap<StringName, StringName>::ConstIterator E = bone_map.begin();
143
while (E) {
144
k = E->key;
145
if (!current_profile->has_bone(k)) {
146
delete_bones.push_back(k);
147
}
148
++E;
149
}
150
len = delete_bones.size();
151
for (int i = 0; i < len; i++) {
152
bone_map.erase(delete_bones[i]);
153
}
154
} else {
155
bone_map.clear();
156
}
157
}
158
159
void BoneMap::_bind_methods() {
160
ClassDB::bind_method(D_METHOD("get_profile"), &BoneMap::get_profile);
161
ClassDB::bind_method(D_METHOD("set_profile", "profile"), &BoneMap::set_profile);
162
163
ClassDB::bind_method(D_METHOD("get_skeleton_bone_name", "profile_bone_name"), &BoneMap::get_skeleton_bone_name);
164
ClassDB::bind_method(D_METHOD("set_skeleton_bone_name", "profile_bone_name", "skeleton_bone_name"), &BoneMap::set_skeleton_bone_name);
165
166
ClassDB::bind_method(D_METHOD("find_profile_bone_name", "skeleton_bone_name"), &BoneMap::find_profile_bone_name);
167
168
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "profile", PROPERTY_HINT_RESOURCE_TYPE, SkeletonProfile::get_class_static()), "set_profile", "get_profile");
169
ADD_ARRAY("bonemap", "bonemap");
170
171
ADD_SIGNAL(MethodInfo("bone_map_updated"));
172
ADD_SIGNAL(MethodInfo("profile_updated"));
173
}
174
175
void BoneMap::_validate_property(PropertyInfo &property) const {
176
if (!Engine::get_singleton()->is_editor_hint()) {
177
return;
178
}
179
if (property.name == "bonemap" || property.name == "profile") {
180
property.usage = PROPERTY_USAGE_NO_EDITOR;
181
}
182
}
183
184
BoneMap::BoneMap() {
185
_validate_bone_map();
186
}
187
188
BoneMap::~BoneMap() {
189
}
190
191
//////////////////////////////////////
192
193