Add simple auto mapping to BoneMapper

Co-authored-by: K. S. Ernest (iFire) Lee <ernest.lee@chibifire.com>
This commit is contained in:
Silc Renew 2022-07-31 23:35:19 +09:00
parent 3f3ade9e5b
commit c7e4eeb8a4
7 changed files with 983 additions and 33 deletions

File diff suppressed because it is too large Load diff

View file

@ -34,6 +34,12 @@
#include "editor/editor_node.h"
#include "editor/editor_plugin.h"
#include "editor/editor_properties.h"
#include "modules/modules_enabled.gen.h" // For regex.
#ifdef MODULE_REGEX_ENABLED
#include "modules/regex/regex.h"
#endif
#include "scene/3d/skeleton_3d.h"
#include "scene/gui/color_rect.h"
#include "scene/gui/dialogs.h"
@ -79,12 +85,13 @@ class BoneMapperItem : public VBoxContainer {
int button_id = -1;
StringName profile_bone_name;
PackedStringArray skeleton_bone_names;
Ref<BoneMap> bone_map;
EditorPropertyTextEnum *skeleton_bone_selector;
EditorPropertyText *skeleton_bone_selector;
Button *picker_button;
void _update_property();
void _open_picker();
protected:
void _notification(int p_what);
@ -95,20 +102,49 @@ protected:
public:
void assign_button_id(int p_button_id);
BoneMapperItem(Ref<BoneMap> &p_bone_map, PackedStringArray p_skeleton_bone_names, const StringName &p_profile_bone_name = StringName());
BoneMapperItem(Ref<BoneMap> &p_bone_map, const StringName &p_profile_bone_name = StringName());
~BoneMapperItem();
};
class BonePicker : public AcceptDialog {
GDCLASS(BonePicker, AcceptDialog);
Skeleton3D *skeleton = nullptr;
Tree *bones = nullptr;
public:
void popup_bones_tree(const Size2i &p_minsize = Size2i());
bool has_selected_bone();
StringName get_selected_bone();
protected:
void _notification(int p_what);
static void _bind_methods();
void _confirm();
private:
void create_editors();
void create_bones_tree(Skeleton3D *p_skeleton);
public:
BonePicker(Skeleton3D *p_skeleton);
~BonePicker();
};
class BoneMapper : public VBoxContainer {
GDCLASS(BoneMapper, VBoxContainer);
Skeleton3D *skeleton;
Ref<BoneMap> bone_map;
EditorPropertyResource *profile_selector;
Vector<BoneMapperItem *> bone_mapper_items;
Button *clear_mapping_button;
VBoxContainer *mapper_item_vbox;
HSeparator *separator;
int current_group_idx = 0;
int current_bone_idx = -1;
@ -126,10 +162,31 @@ class BoneMapper : public VBoxContainer {
void update_group_idx();
void _update_state();
/* Bone picker */
BonePicker *picker = nullptr;
StringName picker_key_name;
void _pick_bone(const StringName &p_bone_name);
void _apply_picker_selection();
void _clear_mapping_current_group();
#ifdef MODULE_REGEX_ENABLED
/* For auto mapping */
enum BoneSegregation {
BONE_SEGREGATION_NONE,
BONE_SEGREGATION_LEFT,
BONE_SEGREGATION_RIGHT
};
int search_bone_by_name(Skeleton3D *p_skeleton, Vector<String> p_picklist, BoneSegregation p_segregation = BONE_SEGREGATION_NONE, int p_parent = -1, int p_child = -1, int p_children_count = -1);
BoneSegregation guess_bone_segregation(String p_bone_name);
void auto_mapping_process(Ref<BoneMap> &p_bone_map);
void _run_auto_mapping();
#endif // MODULE_REGEX_ENABLED
protected:
void _notification(int p_what);
static void _bind_methods();
virtual void _value_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing);
virtual void _profile_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing);
public:
void set_current_group_idx(int p_group_idx);

View file

@ -31,7 +31,6 @@
#include "skeleton_3d_editor_plugin.h"
#include "core/io/resource_saver.h"
#include "editor/editor_file_dialog.h"
#include "editor/editor_node.h"
#include "editor/editor_properties.h"
#include "editor/editor_scale.h"

View file

@ -31,6 +31,7 @@
#ifndef SKELETON_3D_EDITOR_PLUGIN_H
#define SKELETON_3D_EDITOR_PLUGIN_H
#include "editor/editor_file_dialog.h"
#include "editor/editor_plugin.h"
#include "editor/editor_properties.h"
#include "node_3d_editor_plugin.h"

View file

@ -82,9 +82,13 @@ StringName BoneMap::get_skeleton_bone_name(StringName p_profile_bone_name) const
return bone_map.get(p_profile_bone_name);
}
void BoneMap::set_skeleton_bone_name(StringName p_profile_bone_name, const StringName p_skeleton_bone_name) {
void BoneMap::_set_skeleton_bone_name(StringName p_profile_bone_name, const StringName p_skeleton_bone_name) {
ERR_FAIL_COND(!bone_map.has(p_profile_bone_name));
bone_map.insert(p_profile_bone_name, p_skeleton_bone_name);
}
void BoneMap::set_skeleton_bone_name(StringName p_profile_bone_name, const StringName p_skeleton_bone_name) {
_set_skeleton_bone_name(p_profile_bone_name, p_skeleton_bone_name);
emit_signal("bone_map_updated");
}
@ -167,8 +171,10 @@ void BoneMap::_bind_methods() {
ADD_SIGNAL(MethodInfo("profile_updated"));
}
void BoneMap::_validate_property(PropertyInfo &p_property) const {
//
void BoneMap::_validate_property(PropertyInfo &property) const {
if (property.name == "bonemap" || property.name == "profile") {
property.usage = PROPERTY_USAGE_NO_EDITOR;
}
}
BoneMap::BoneMap() {

View file

@ -50,9 +50,6 @@ protected:
static void _bind_methods();
public:
int get_profile_type() const;
void set_profile_type(const int p_profile_type);
Ref<SkeletonProfile> get_profile() const;
void set_profile(const Ref<SkeletonProfile> &p_profile);
@ -60,6 +57,7 @@ public:
StringName get_skeleton_bone_name(StringName p_profile_bone_name) const;
void set_skeleton_bone_name(StringName p_profile_bone_name, const StringName p_skeleton_bone_name);
void _set_skeleton_bone_name(StringName p_profile_bone_name, const StringName p_skeleton_bone_name); // Avoid to emit signal for editor.
StringName find_profile_bone_name(StringName p_skeleton_bone_name) const;

View file

@ -506,7 +506,7 @@ SkeletonProfileHumanoid::SkeletonProfileHumanoid() {
bones.write[5].reference_pose = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 0);
bones.write[5].handle_offset = Vector2(0.5, 0.23);
bones.write[5].group = "Body";
bones.write[5].require = true;
bones.write[5].require = false;
bones.write[6].bone_name = "Head";
bones.write[6].bone_parent = "Neck";