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