1/**************************************************************************/
2/* skeleton_3d_editor_plugin.h */
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#ifndef SKELETON_3D_EDITOR_PLUGIN_H
32#define SKELETON_3D_EDITOR_PLUGIN_H
33
34#include "editor/editor_plugin.h"
35#include "editor/editor_properties.h"
36#include "editor/gui/editor_file_dialog.h"
37#include "editor/plugins/node_3d_editor_plugin.h"
38#include "scene/3d/camera_3d.h"
39#include "scene/3d/mesh_instance_3d.h"
40#include "scene/3d/skeleton_3d.h"
41#include "scene/resources/immediate_mesh.h"
42
43class EditorInspectorPluginSkeleton;
44class EditorPropertyVector3;
45class Joint;
46class PhysicalBone3D;
47class Skeleton3DEditorPlugin;
48class Button;
49class Tree;
50class TreeItem;
51class VSeparator;
52
53class BoneTransformEditor : public VBoxContainer {
54 GDCLASS(BoneTransformEditor, VBoxContainer);
55
56 EditorInspectorSection *section = nullptr;
57
58 EditorPropertyCheck *enabled_checkbox = nullptr;
59 EditorPropertyVector3 *position_property = nullptr;
60 EditorPropertyQuaternion *rotation_property = nullptr;
61 EditorPropertyVector3 *scale_property = nullptr;
62
63 EditorInspectorSection *rest_section = nullptr;
64 EditorPropertyTransform3D *rest_matrix = nullptr;
65
66 Rect2 background_rects[5];
67
68 Skeleton3D *skeleton = nullptr;
69 // String property;
70
71 bool toggle_enabled = false;
72 bool updating = false;
73
74 String label;
75
76 void create_editors();
77
78 void _value_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing);
79
80 void _property_keyed(const String &p_path, bool p_advance);
81
82protected:
83 void _notification(int p_what);
84
85public:
86 BoneTransformEditor(Skeleton3D *p_skeleton);
87
88 // Which transform target to modify.
89 void set_target(const String &p_prop);
90 void set_label(const String &p_label) { label = p_label; }
91 void set_keyable(const bool p_keyable);
92
93 void _update_properties();
94};
95
96class Skeleton3DEditor : public VBoxContainer {
97 GDCLASS(Skeleton3DEditor, VBoxContainer);
98
99 friend class Skeleton3DEditorPlugin;
100
101 enum SkeletonOption {
102 SKELETON_OPTION_RESET_ALL_POSES,
103 SKELETON_OPTION_RESET_SELECTED_POSES,
104 SKELETON_OPTION_ALL_POSES_TO_RESTS,
105 SKELETON_OPTION_SELECTED_POSES_TO_RESTS,
106 SKELETON_OPTION_CREATE_PHYSICAL_SKELETON,
107 SKELETON_OPTION_EXPORT_SKELETON_PROFILE,
108 };
109
110 struct BoneInfo {
111 PhysicalBone3D *physical_bone = nullptr;
112 Transform3D relative_rest; // Relative to skeleton node.
113 };
114
115 EditorInspectorPluginSkeleton *editor_plugin = nullptr;
116
117 Skeleton3D *skeleton = nullptr;
118
119 Tree *joint_tree = nullptr;
120 BoneTransformEditor *rest_editor = nullptr;
121 BoneTransformEditor *pose_editor = nullptr;
122
123 VSeparator *separator = nullptr;
124 MenuButton *skeleton_options = nullptr;
125 Button *edit_mode_button = nullptr;
126
127 bool edit_mode = false;
128
129 HBoxContainer *animation_hb = nullptr;
130 Button *key_loc_button = nullptr;
131 Button *key_rot_button = nullptr;
132 Button *key_scale_button = nullptr;
133 Button *key_insert_button = nullptr;
134 Button *key_insert_all_button = nullptr;
135
136 EditorInspectorSection *bones_section = nullptr;
137
138 EditorFileDialog *file_dialog = nullptr;
139
140 bool keyable = false;
141
142 static Skeleton3DEditor *singleton;
143
144 void _on_click_skeleton_option(int p_skeleton_option);
145 void _file_selected(const String &p_file);
146 TreeItem *_find(TreeItem *p_node, const NodePath &p_path);
147 void edit_mode_toggled(const bool pressed);
148
149 EditorFileDialog *file_export_lib = nullptr;
150
151 void update_joint_tree();
152
153 void create_editors();
154
155 void reset_pose(const bool p_all_bones);
156 void pose_to_rest(const bool p_all_bones);
157
158 void insert_keys(const bool p_all_bones);
159
160 void create_physical_skeleton();
161 PhysicalBone3D *create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos);
162
163 void export_skeleton_profile();
164
165 Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
166 bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
167 void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
168
169 void set_keyable(const bool p_keyable);
170 void set_bone_options_enabled(const bool p_bone_options_enabled);
171
172 // Handle.
173 MeshInstance3D *handles_mesh_instance = nullptr;
174 Ref<ImmediateMesh> handles_mesh;
175 Ref<ShaderMaterial> handle_material;
176 Ref<Shader> handle_shader;
177
178 Vector3 bone_original_position;
179 Quaternion bone_original_rotation;
180 Vector3 bone_original_scale;
181
182 void _update_gizmo_visible();
183 void _bone_enabled_changed(const int p_bone_id);
184
185 void _hide_handles();
186
187 void _draw_gizmo();
188 void _draw_handles();
189
190 void _joint_tree_selection_changed();
191 void _joint_tree_rmb_select(const Vector2 &p_pos, MouseButton p_button);
192 void _update_properties();
193
194 void _subgizmo_selection_change();
195
196 int selected_bone = -1;
197
198protected:
199 void _notification(int p_what);
200 void _node_removed(Node *p_node);
201 static void _bind_methods();
202
203public:
204 static Skeleton3DEditor *get_singleton() { return singleton; }
205
206 void select_bone(int p_idx);
207
208 int get_selected_bone() const;
209
210 void move_skeleton_bone(NodePath p_skeleton_path, int32_t p_selected_boneidx, int32_t p_target_boneidx);
211
212 Skeleton3D *get_skeleton() const { return skeleton; };
213
214 bool is_edit_mode() const { return edit_mode; }
215
216 void update_bone_original();
217 Vector3 get_bone_original_position() const { return bone_original_position; };
218 Quaternion get_bone_original_rotation() const { return bone_original_rotation; };
219 Vector3 get_bone_original_scale() const { return bone_original_scale; };
220
221 Skeleton3DEditor(EditorInspectorPluginSkeleton *e_plugin, Skeleton3D *skeleton);
222 ~Skeleton3DEditor();
223};
224
225class EditorInspectorPluginSkeleton : public EditorInspectorPlugin {
226 GDCLASS(EditorInspectorPluginSkeleton, EditorInspectorPlugin);
227
228 friend class Skeleton3DEditorPlugin;
229
230 Skeleton3DEditor *skel_editor = nullptr;
231
232public:
233 virtual bool can_handle(Object *p_object) override;
234 virtual void parse_begin(Object *p_object) override;
235};
236
237class Skeleton3DEditorPlugin : public EditorPlugin {
238 GDCLASS(Skeleton3DEditorPlugin, EditorPlugin);
239
240 EditorInspectorPluginSkeleton *skeleton_plugin = nullptr;
241
242public:
243 virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override;
244
245 bool has_main_screen() const override { return false; }
246 virtual bool handles(Object *p_object) const override;
247
248 virtual String get_name() const override { return "Skeleton3D"; }
249
250 Skeleton3DEditorPlugin();
251};
252
253class Skeleton3DGizmoPlugin : public EditorNode3DGizmoPlugin {
254 GDCLASS(Skeleton3DGizmoPlugin, EditorNode3DGizmoPlugin);
255
256 Ref<StandardMaterial3D> unselected_mat;
257 Ref<ShaderMaterial> selected_mat;
258 Ref<Shader> selected_sh;
259
260public:
261 bool has_gizmo(Node3D *p_spatial) override;
262 String get_gizmo_name() const override;
263 int get_priority() const override;
264
265 int subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const override;
266 Transform3D get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const override;
267 void set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) override;
268 void commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) override;
269
270 void redraw(EditorNode3DGizmo *p_gizmo) override;
271
272 Skeleton3DGizmoPlugin();
273};
274
275#endif // SKELETON_3D_EDITOR_PLUGIN_H
276