2023-01-10 14:26:54 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* skeleton_editor_plugin.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
#include "skeleton_editor_plugin.h"
|
2018-09-27 11:11:41 +00:00
|
|
|
|
2017-10-03 16:49:32 +00:00
|
|
|
#include "scene/3d/collision_shape.h"
|
|
|
|
#include "scene/3d/physics_body.h"
|
2018-09-27 11:11:41 +00:00
|
|
|
#include "scene/3d/physics_joint.h"
|
2017-10-03 16:49:32 +00:00
|
|
|
#include "scene/resources/capsule_shape.h"
|
|
|
|
#include "scene/resources/sphere_shape.h"
|
|
|
|
#include "spatial_editor_plugin.h"
|
|
|
|
|
|
|
|
void SkeletonEditor::_on_click_option(int p_option) {
|
|
|
|
if (!skeleton) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (p_option) {
|
|
|
|
case MENU_OPTION_CREATE_PHYSICAL_SKELETON: {
|
|
|
|
create_physical_skeleton();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonEditor::create_physical_skeleton() {
|
|
|
|
UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
|
2022-10-20 07:06:17 +00:00
|
|
|
Node *owner = get_tree()->get_edited_scene_root();
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
const int bc = skeleton->get_bone_count();
|
|
|
|
|
|
|
|
if (!bc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<BoneInfo> bones_infos;
|
|
|
|
bones_infos.resize(bc);
|
|
|
|
|
|
|
|
for (int bone_id = 0; bc > bone_id; ++bone_id) {
|
|
|
|
const int parent = skeleton->get_bone_parent(bone_id);
|
|
|
|
|
|
|
|
if (parent < 0) {
|
2018-07-25 01:11:03 +00:00
|
|
|
bones_infos.write[bone_id].relative_rest = skeleton->get_bone_rest(bone_id);
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
} else {
|
2019-11-30 19:27:12 +00:00
|
|
|
const int parent_parent = skeleton->get_bone_parent(parent);
|
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
bones_infos.write[bone_id].relative_rest = bones_infos[parent].relative_rest * skeleton->get_bone_rest(bone_id);
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
/// create physical bone on parent
|
|
|
|
if (!bones_infos[parent].physical_bone) {
|
2018-07-25 01:11:03 +00:00
|
|
|
bones_infos.write[parent].physical_bone = create_physical_bone(parent, bone_id, bones_infos);
|
2017-10-03 16:49:32 +00:00
|
|
|
|
2022-01-30 17:28:44 +00:00
|
|
|
ur->create_action(TTR("Create physical bones"), UndoRedo::MERGE_ALL);
|
2017-10-03 16:49:32 +00:00
|
|
|
ur->add_do_method(skeleton, "add_child", bones_infos[parent].physical_bone);
|
|
|
|
ur->add_do_reference(bones_infos[parent].physical_bone);
|
|
|
|
ur->add_undo_method(skeleton, "remove_child", bones_infos[parent].physical_bone);
|
|
|
|
ur->commit_action();
|
|
|
|
|
|
|
|
bones_infos[parent].physical_bone->set_bone_name(skeleton->get_bone_name(parent));
|
|
|
|
bones_infos[parent].physical_bone->set_owner(owner);
|
|
|
|
bones_infos[parent].physical_bone->get_child(0)->set_owner(owner); // set shape owner
|
|
|
|
|
|
|
|
/// Create joint between parent of parent
|
|
|
|
if (-1 != parent_parent) {
|
|
|
|
bones_infos[parent].physical_bone->set_joint_type(PhysicalBone::JOINT_TYPE_PIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicalBone *SkeletonEditor::create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos) {
|
2021-04-29 22:20:57 +00:00
|
|
|
const Transform child_rest = skeleton->get_bone_rest(bone_child_id);
|
|
|
|
|
|
|
|
real_t half_height(child_rest.origin.length() * 0.5);
|
2017-10-03 16:49:32 +00:00
|
|
|
real_t radius(half_height * 0.2);
|
|
|
|
|
|
|
|
CapsuleShape *bone_shape_capsule = memnew(CapsuleShape);
|
|
|
|
bone_shape_capsule->set_height((half_height - radius) * 2);
|
|
|
|
bone_shape_capsule->set_radius(radius);
|
|
|
|
|
|
|
|
CollisionShape *bone_shape = memnew(CollisionShape);
|
|
|
|
bone_shape->set_shape(bone_shape_capsule);
|
|
|
|
|
2020-11-26 00:35:27 +00:00
|
|
|
Transform capsule_transform;
|
|
|
|
bone_shape->set_transform(capsule_transform);
|
|
|
|
|
2021-04-29 22:20:57 +00:00
|
|
|
Vector3 up = Vector3(0, 1, 0);
|
|
|
|
if (up.cross(child_rest.origin).is_equal_approx(Vector3())) {
|
|
|
|
up = Vector3(0, 0, 1);
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:49:32 +00:00
|
|
|
Transform body_transform;
|
2021-04-29 22:20:57 +00:00
|
|
|
body_transform.set_look_at(Vector3(0, 0, 0), child_rest.origin, up);
|
|
|
|
body_transform.origin = body_transform.basis.xform(Vector3(0, 0, -half_height));
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
Transform joint_transform;
|
|
|
|
joint_transform.origin = Vector3(0, 0, half_height);
|
|
|
|
|
|
|
|
PhysicalBone *physical_bone = memnew(PhysicalBone);
|
|
|
|
physical_bone->add_child(bone_shape);
|
|
|
|
physical_bone->set_name("Physical Bone " + skeleton->get_bone_name(bone_id));
|
|
|
|
physical_bone->set_body_offset(body_transform);
|
|
|
|
physical_bone->set_joint_offset(joint_transform);
|
|
|
|
return physical_bone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonEditor::edit(Skeleton *p_node) {
|
|
|
|
skeleton = p_node;
|
2018-08-24 11:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonEditor::_notification(int p_what) {
|
2018-08-24 14:50:29 +00:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2018-08-24 11:28:53 +00:00
|
|
|
get_tree()->connect("node_removed", this, "_node_removed");
|
|
|
|
}
|
2017-10-03 16:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonEditor::_node_removed(Node *p_node) {
|
|
|
|
if (p_node == skeleton) {
|
2021-05-04 14:00:45 +00:00
|
|
|
skeleton = nullptr;
|
2017-10-03 16:49:32 +00:00
|
|
|
options->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonEditor::_bind_methods() {
|
|
|
|
ClassDB::bind_method("_on_click_option", &SkeletonEditor::_on_click_option);
|
2018-08-24 11:28:53 +00:00
|
|
|
ClassDB::bind_method("_node_removed", &SkeletonEditor::_node_removed);
|
2017-10-03 16:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkeletonEditor::SkeletonEditor() {
|
2021-05-04 14:00:45 +00:00
|
|
|
skeleton = nullptr;
|
2017-10-03 16:49:32 +00:00
|
|
|
options = memnew(MenuButton);
|
|
|
|
SpatialEditor::get_singleton()->add_control_to_menu_panel(options);
|
|
|
|
|
|
|
|
options->set_text(TTR("Skeleton"));
|
|
|
|
options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Skeleton", "EditorIcons"));
|
|
|
|
|
|
|
|
options->get_popup()->add_item(TTR("Create physical skeleton"), MENU_OPTION_CREATE_PHYSICAL_SKELETON);
|
|
|
|
|
|
|
|
options->get_popup()->connect("id_pressed", this, "_on_click_option");
|
|
|
|
options->hide();
|
|
|
|
}
|
|
|
|
|
2018-05-07 22:59:22 +00:00
|
|
|
SkeletonEditor::~SkeletonEditor() {}
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
void SkeletonEditorPlugin::edit(Object *p_object) {
|
|
|
|
skeleton_editor->edit(Object::cast_to<Skeleton>(p_object));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkeletonEditorPlugin::handles(Object *p_object) const {
|
|
|
|
return p_object->is_class("Skeleton");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonEditorPlugin::make_visible(bool p_visible) {
|
|
|
|
if (p_visible) {
|
|
|
|
skeleton_editor->options->show();
|
|
|
|
} else {
|
|
|
|
skeleton_editor->options->hide();
|
2021-05-04 14:00:45 +00:00
|
|
|
skeleton_editor->edit(nullptr);
|
2017-10-03 16:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkeletonEditorPlugin::SkeletonEditorPlugin(EditorNode *p_node) {
|
|
|
|
editor = p_node;
|
|
|
|
skeleton_editor = memnew(SkeletonEditor);
|
|
|
|
editor->get_viewport()->add_child(skeleton_editor);
|
|
|
|
}
|
|
|
|
|
2018-05-07 22:59:22 +00:00
|
|
|
SkeletonEditorPlugin::~SkeletonEditorPlugin() {}
|