2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-03-26 21:49:16 +00:00
|
|
|
/* bone_attachment_3d.cpp */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
#include "bone_attachment_3d.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void BoneAttachment3D::_validate_property(PropertyInfo &property) const {
|
2018-01-11 22:35:12 +00:00
|
|
|
if (property.name == "bone_name") {
|
2020-03-26 21:49:16 +00:00
|
|
|
Skeleton3D *parent = Object::cast_to<Skeleton3D>(get_parent());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-01-11 22:35:12 +00:00
|
|
|
if (parent) {
|
|
|
|
String names;
|
|
|
|
for (int i = 0; i < parent->get_bone_count(); i++) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (i > 0) {
|
2018-01-11 22:35:12 +00:00
|
|
|
names += ",";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-01-11 22:35:12 +00:00
|
|
|
names += parent->get_bone_name(i);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-01-11 22:35:12 +00:00
|
|
|
property.hint = PROPERTY_HINT_ENUM;
|
|
|
|
property.hint_string = names;
|
|
|
|
} else {
|
|
|
|
property.hint = PROPERTY_HINT_NONE;
|
|
|
|
property.hint_string = "";
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void BoneAttachment3D::_check_bind() {
|
|
|
|
Skeleton3D *sk = Object::cast_to<Skeleton3D>(get_parent());
|
2017-09-06 21:50:18 +00:00
|
|
|
if (sk) {
|
2014-02-10 01:10:30 +00:00
|
|
|
int idx = sk->find_bone(bone_name);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx != -1) {
|
|
|
|
sk->bind_child_node_to_bone(idx, this);
|
2016-04-15 10:28:07 +00:00
|
|
|
set_transform(sk->get_bone_global_pose(idx));
|
2017-03-05 15:44:50 +00:00
|
|
|
bound = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void BoneAttachment3D::_check_unbind() {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (bound) {
|
2020-03-26 21:49:16 +00:00
|
|
|
Skeleton3D *sk = Object::cast_to<Skeleton3D>(get_parent());
|
2017-09-06 21:50:18 +00:00
|
|
|
if (sk) {
|
2014-02-10 01:10:30 +00:00
|
|
|
int idx = sk->find_bone(bone_name);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx != -1) {
|
|
|
|
sk->unbind_child_node_from_bone(idx, this);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
bound = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void BoneAttachment3D::set_bone_name(const String &p_name) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_inside_tree()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_check_unbind();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bone_name = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_inside_tree()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_check_bind();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
String BoneAttachment3D::get_bone_name() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return bone_name;
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void BoneAttachment3D::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
_check_bind();
|
|
|
|
} break;
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
_check_unbind();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
BoneAttachment3D::BoneAttachment3D() {
|
2017-03-05 15:44:50 +00:00
|
|
|
bound = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-12-24 14:27:08 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void BoneAttachment3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &BoneAttachment3D::set_bone_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_name"), &BoneAttachment3D::get_bone_name);
|
2018-01-11 22:35:12 +00:00
|
|
|
|
2020-02-20 21:58:05 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name");
|
2016-12-24 14:27:08 +00:00
|
|
|
}
|