Path: blob/master/editor/scene/3d/skeleton_3d_editor_plugin.h
9903 views
/**************************************************************************/1/* skeleton_3d_editor_plugin.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "editor/gui/editor_file_dialog.h"33#include "editor/inspector/add_metadata_dialog.h"34#include "editor/inspector/editor_properties.h"35#include "editor/plugins/editor_plugin.h"36#include "editor/scene/3d/node_3d_editor_plugin.h"37#include "scene/3d/camera_3d.h"38#include "scene/3d/mesh_instance_3d.h"39#include "scene/3d/skeleton_3d.h"40#include "scene/resources/immediate_mesh.h"4142class EditorInspectorPluginSkeleton;43class EditorPropertyVector3;44class Joint;45class PhysicalBone3D;46class Skeleton3DEditorPlugin;47class Button;48class Tree;49class TreeItem;50class VSeparator;5152class BonePropertiesEditor : public VBoxContainer {53GDCLASS(BonePropertiesEditor, VBoxContainer);5455EditorInspectorSection *section = nullptr;5657EditorPropertyCheck *enabled_checkbox = nullptr;58EditorPropertyVector3 *position_property = nullptr;59EditorPropertyQuaternion *rotation_property = nullptr;60EditorPropertyVector3 *scale_property = nullptr;6162EditorInspectorSection *rest_section = nullptr;63EditorPropertyTransform3D *rest_matrix = nullptr;6465EditorInspectorSection *meta_section = nullptr;66AddMetadataDialog *add_meta_dialog = nullptr;6768Rect2 background_rects[5];6970Skeleton3D *skeleton = nullptr;71// String property;7273bool toggle_enabled = false;74bool updating = false;7576String label;7778void create_editors();7980void _value_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing);8182void _property_keyed(const String &p_path, bool p_advance);8384void _meta_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing);85void _meta_deleted(const String &p_property);86void _show_add_meta_dialog();87void _add_meta_confirm();8889HashMap<StringName, EditorProperty *> meta_editors;9091protected:92void _notification(int p_what);9394public:95BonePropertiesEditor(Skeleton3D *p_skeleton);9697// Which transform target to modify.98void set_target(const String &p_prop);99void set_label(const String &p_label) { label = p_label; }100void set_keyable(const bool p_keyable);101102void _update_properties();103};104105class Skeleton3DEditor : public VBoxContainer {106GDCLASS(Skeleton3DEditor, VBoxContainer);107108static void _bind_methods();109110friend class Skeleton3DEditorPlugin;111112enum SkeletonOption {113SKELETON_OPTION_RESET_ALL_POSES,114SKELETON_OPTION_RESET_SELECTED_POSES,115SKELETON_OPTION_ALL_POSES_TO_RESTS,116SKELETON_OPTION_SELECTED_POSES_TO_RESTS,117SKELETON_OPTION_CREATE_PHYSICAL_SKELETON,118SKELETON_OPTION_EXPORT_SKELETON_PROFILE,119};120121struct BoneInfo {122PhysicalBone3D *physical_bone = nullptr;123Transform3D relative_rest; // Relative to skeleton node.124};125126EditorInspectorPluginSkeleton *editor_plugin = nullptr;127128Skeleton3D *skeleton = nullptr;129130enum {131JOINT_BUTTON_REVERT = 0,132};133134Tree *joint_tree = nullptr;135BonePropertiesEditor *rest_editor = nullptr;136BonePropertiesEditor *pose_editor = nullptr;137138HBoxContainer *topmenu_bar = nullptr;139MenuButton *skeleton_options = nullptr;140Button *edit_mode_button = nullptr;141142bool edit_mode = false;143144HBoxContainer *animation_hb = nullptr;145Button *key_loc_button = nullptr;146Button *key_rot_button = nullptr;147Button *key_scale_button = nullptr;148Button *key_insert_button = nullptr;149Button *key_insert_all_button = nullptr;150151EditorInspectorSection *bones_section = nullptr;152153EditorFileDialog *file_dialog = nullptr;154155bool keyable = false;156157static Skeleton3DEditor *singleton;158159void _on_click_skeleton_option(int p_skeleton_option);160void _file_selected(const String &p_file);161TreeItem *_find(TreeItem *p_node, const NodePath &p_path);162void edit_mode_toggled(const bool pressed);163164EditorFileDialog *file_export_lib = nullptr;165166void update_joint_tree();167void update_all();168169void create_editors();170171void reset_pose(const bool p_all_bones);172void pose_to_rest(const bool p_all_bones);173174void insert_keys(const bool p_all_bones);175176void create_physical_skeleton();177PhysicalBone3D *create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos);178179void export_skeleton_profile();180181Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);182bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;183void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);184185void set_keyable(const bool p_keyable);186void set_bone_options_enabled(const bool p_bone_options_enabled);187188// Handle.189MeshInstance3D *handles_mesh_instance = nullptr;190Ref<ImmediateMesh> handles_mesh;191Ref<ShaderMaterial> handle_material;192Ref<Shader> handle_shader;193194Vector3 bone_original_position;195Quaternion bone_original_rotation;196Vector3 bone_original_scale;197198void _update_gizmo_visible();199void _bone_enabled_changed(const int p_bone_id);200201void _hide_handles();202203void _draw_gizmo();204void _draw_handles();205206void _joint_tree_selection_changed();207void _joint_tree_rmb_select(const Vector2 &p_pos, MouseButton p_button);208void _joint_tree_button_clicked(Object *p_item, int p_column, int p_id, MouseButton p_button);209void _update_properties();210211void _subgizmo_selection_change();212213int selected_bone = -1;214215protected:216void _notification(int p_what);217void _node_removed(Node *p_node);218219public:220static Skeleton3DEditor *get_singleton() { return singleton; }221222void select_bone(int p_idx);223224int get_selected_bone() const;225226void move_skeleton_bone(NodePath p_skeleton_path, int32_t p_selected_boneidx, int32_t p_target_boneidx);227228Skeleton3D *get_skeleton() const { return skeleton; }229230bool is_edit_mode() const { return edit_mode; }231232void update_bone_original();233Vector3 get_bone_original_position() const { return bone_original_position; }234Quaternion get_bone_original_rotation() const { return bone_original_rotation; }235Vector3 get_bone_original_scale() const { return bone_original_scale; }236237Skeleton3DEditor(EditorInspectorPluginSkeleton *e_plugin, Skeleton3D *skeleton);238~Skeleton3DEditor();239};240241class EditorInspectorPluginSkeleton : public EditorInspectorPlugin {242GDCLASS(EditorInspectorPluginSkeleton, EditorInspectorPlugin);243244friend class Skeleton3DEditorPlugin;245246Skeleton3DEditor *skel_editor = nullptr;247248public:249virtual bool can_handle(Object *p_object) override;250virtual void parse_begin(Object *p_object) override;251};252253class Skeleton3DEditorPlugin : public EditorPlugin {254GDCLASS(Skeleton3DEditorPlugin, EditorPlugin);255256EditorInspectorPluginSkeleton *skeleton_plugin = nullptr;257258public:259virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override;260261bool has_main_screen() const override { return false; }262virtual bool handles(Object *p_object) const override;263264virtual String get_plugin_name() const override { return "Skeleton3D"; }265266Skeleton3DEditorPlugin();267};268269class Skeleton3DGizmoPlugin : public EditorNode3DGizmoPlugin {270GDCLASS(Skeleton3DGizmoPlugin, EditorNode3DGizmoPlugin);271272struct SelectionMaterials {273Ref<StandardMaterial3D> unselected_mat;274Ref<ShaderMaterial> selected_mat;275};276static SelectionMaterials selection_materials;277278public:279static Ref<ArrayMesh> get_bones_mesh(Skeleton3D *p_skeleton, int p_selected, bool p_is_selected);280281bool has_gizmo(Node3D *p_spatial) override;282String get_gizmo_name() const override;283int get_priority() const override;284285int subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const override;286Transform3D get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const override;287void set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) override;288void commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) override;289290void redraw(EditorNode3DGizmo *p_gizmo) override;291292Skeleton3DGizmoPlugin();293~Skeleton3DGizmoPlugin();294};295296297