1/**************************************************************************/
2/* skin.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 "skin.h"
32
33void Skin::set_bind_count(int p_size) {
34 ERR_FAIL_COND(p_size < 0);
35 binds.resize(p_size);
36 binds_ptr = binds.ptrw();
37 bind_count = p_size;
38 emit_changed();
39}
40
41void Skin::add_bind(int p_bone, const Transform3D &p_pose) {
42 uint32_t index = bind_count;
43 set_bind_count(bind_count + 1);
44 set_bind_bone(index, p_bone);
45 set_bind_pose(index, p_pose);
46}
47
48void Skin::add_named_bind(const String &p_name, const Transform3D &p_pose) {
49 uint32_t index = bind_count;
50 set_bind_count(bind_count + 1);
51 set_bind_name(index, p_name);
52 set_bind_pose(index, p_pose);
53}
54
55void Skin::set_bind_name(int p_index, const StringName &p_name) {
56 ERR_FAIL_INDEX(p_index, bind_count);
57 bool notify_change = (binds_ptr[p_index].name != StringName()) != (p_name != StringName());
58 binds_ptr[p_index].name = p_name;
59 emit_changed();
60 if (notify_change) {
61 notify_property_list_changed();
62 }
63}
64
65void Skin::set_bind_bone(int p_index, int p_bone) {
66 ERR_FAIL_INDEX(p_index, bind_count);
67 binds_ptr[p_index].bone = p_bone;
68 emit_changed();
69}
70
71void Skin::set_bind_pose(int p_index, const Transform3D &p_pose) {
72 ERR_FAIL_INDEX(p_index, bind_count);
73 binds_ptr[p_index].pose = p_pose;
74 emit_changed();
75}
76
77void Skin::clear_binds() {
78 binds.clear();
79 binds_ptr = nullptr;
80 bind_count = 0;
81 emit_changed();
82}
83
84void Skin::reset_state() {
85 clear_binds();
86}
87
88bool Skin::_set(const StringName &p_name, const Variant &p_value) {
89 String prop_name = p_name;
90 if (prop_name == "bind_count") {
91 set_bind_count(p_value);
92 return true;
93 } else if (prop_name.begins_with("bind/")) {
94 int index = prop_name.get_slicec('/', 1).to_int();
95 String what = prop_name.get_slicec('/', 2);
96 if (what == "bone") {
97 set_bind_bone(index, p_value);
98 return true;
99 } else if (what == "name") {
100 set_bind_name(index, p_value);
101 return true;
102 } else if (what == "pose") {
103 set_bind_pose(index, p_value);
104 return true;
105 }
106 }
107 return false;
108}
109
110bool Skin::_get(const StringName &p_name, Variant &r_ret) const {
111 String prop_name = p_name;
112 if (prop_name == "bind_count") {
113 r_ret = get_bind_count();
114 return true;
115 } else if (prop_name.begins_with("bind/")) {
116 int index = prop_name.get_slicec('/', 1).to_int();
117 String what = prop_name.get_slicec('/', 2);
118 if (what == "bone") {
119 r_ret = get_bind_bone(index);
120 return true;
121 } else if (what == "name") {
122 r_ret = get_bind_name(index);
123 return true;
124 } else if (what == "pose") {
125 r_ret = get_bind_pose(index);
126 return true;
127 }
128 }
129 return false;
130}
131
132void Skin::_get_property_list(List<PropertyInfo> *p_list) const {
133 p_list->push_back(PropertyInfo(Variant::INT, PNAME("bind_count"), PROPERTY_HINT_RANGE, "0,16384,1,or_greater"));
134 for (int i = 0; i < get_bind_count(); i++) {
135 const String prefix = vformat("%s/%d/", PNAME("bind"), i);
136 p_list->push_back(PropertyInfo(Variant::STRING_NAME, prefix + PNAME("name")));
137 p_list->push_back(PropertyInfo(Variant::INT, prefix + PNAME("bone"), PROPERTY_HINT_RANGE, "0,16384,1,or_greater", get_bind_name(i) != StringName() ? PROPERTY_USAGE_NO_EDITOR : PROPERTY_USAGE_DEFAULT));
138 p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prefix + PNAME("pose")));
139 }
140}
141
142void Skin::_bind_methods() {
143 ClassDB::bind_method(D_METHOD("set_bind_count", "bind_count"), &Skin::set_bind_count);
144 ClassDB::bind_method(D_METHOD("get_bind_count"), &Skin::get_bind_count);
145
146 ClassDB::bind_method(D_METHOD("add_bind", "bone", "pose"), &Skin::add_bind);
147 ClassDB::bind_method(D_METHOD("add_named_bind", "name", "pose"), &Skin::add_named_bind);
148
149 ClassDB::bind_method(D_METHOD("set_bind_pose", "bind_index", "pose"), &Skin::set_bind_pose);
150 ClassDB::bind_method(D_METHOD("get_bind_pose", "bind_index"), &Skin::get_bind_pose);
151
152 ClassDB::bind_method(D_METHOD("set_bind_name", "bind_index", "name"), &Skin::set_bind_name);
153 ClassDB::bind_method(D_METHOD("get_bind_name", "bind_index"), &Skin::get_bind_name);
154
155 ClassDB::bind_method(D_METHOD("set_bind_bone", "bind_index", "bone"), &Skin::set_bind_bone);
156 ClassDB::bind_method(D_METHOD("get_bind_bone", "bind_index"), &Skin::get_bind_bone);
157
158 ClassDB::bind_method(D_METHOD("clear_binds"), &Skin::clear_binds);
159}
160
161Skin::Skin() {
162}
163