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
21635 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
void set_skeleton(Skeleton3D *p_skeleton);
103
104
void _update_properties();
105
};
106
107
class Skeleton3DEditor : public VBoxContainer {
108
GDCLASS(Skeleton3DEditor, VBoxContainer);
109
110
static void _bind_methods();
111
112
friend class Skeleton3DEditorPlugin;
113
114
enum SkeletonOption {
115
SKELETON_OPTION_RESET_ALL_POSES,
116
SKELETON_OPTION_RESET_SELECTED_POSES,
117
SKELETON_OPTION_ALL_POSES_TO_RESTS,
118
SKELETON_OPTION_SELECTED_POSES_TO_RESTS,
119
SKELETON_OPTION_CREATE_PHYSICAL_SKELETON,
120
SKELETON_OPTION_EXPORT_SKELETON_PROFILE,
121
};
122
123
struct BoneInfo {
124
PhysicalBone3D *physical_bone = nullptr;
125
Transform3D relative_rest; // Relative to skeleton node.
126
};
127
128
EditorInspectorPluginSkeleton *editor_plugin = nullptr;
129
130
Skeleton3D *skeleton = nullptr;
131
132
enum {
133
JOINT_BUTTON_REVERT = 0,
134
};
135
136
Tree *joint_tree = nullptr;
137
BonePropertiesEditor *rest_editor = nullptr;
138
BonePropertiesEditor *pose_editor = nullptr;
139
140
HBoxContainer *topmenu_bar = nullptr;
141
MenuButton *skeleton_options = nullptr;
142
Button *edit_mode_button = nullptr;
143
144
bool edit_mode = false;
145
146
HBoxContainer *animation_hb = nullptr;
147
Button *key_loc_button = nullptr;
148
Button *key_rot_button = nullptr;
149
Button *key_scale_button = nullptr;
150
Button *key_insert_button = nullptr;
151
Button *key_insert_new_button = nullptr;
152
Button *key_mod_insert_button = nullptr;
153
Button *key_mod_insert_new_button = nullptr;
154
155
// To maintain the status while running editor.
156
void _loc_toggled(bool p_toggled_on);
157
void _rot_toggled(bool p_toggled_on);
158
void _scl_toggled(bool p_toggled_on);
159
160
EditorInspectorSection *bones_section = nullptr;
161
162
EditorFileDialog *file_dialog = nullptr;
163
164
bool keyable = false;
165
166
static Skeleton3DEditor *singleton;
167
168
void _on_click_skeleton_option(int p_skeleton_option);
169
void _file_selected(const String &p_file);
170
TreeItem *_find(TreeItem *p_node, const NodePath &p_path);
171
void edit_mode_toggled(const bool pressed);
172
173
EditorFileDialog *file_export_lib = nullptr;
174
175
void update_joint_tree();
176
void update_all();
177
178
void create_editors();
179
180
void reset_pose(const bool p_all_bones);
181
void pose_to_rest(const bool p_all_bones);
182
183
void _insert_keys(const bool p_all_bones);
184
void insert_keys(const bool p_all_bones, const bool p_enable_modifier);
185
186
void create_physical_skeleton();
187
PhysicalBone3D *create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos);
188
189
void export_skeleton_profile();
190
191
Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
192
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
193
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
194
195
void set_keyable(const bool p_keyable);
196
void set_bone_options_enabled(const bool p_bone_options_enabled);
197
198
// Handle.
199
MeshInstance3D *handles_mesh_instance = nullptr;
200
Ref<ImmediateMesh> handles_mesh;
201
Ref<ShaderMaterial> handle_material;
202
Ref<Shader> handle_shader;
203
204
Vector3 bone_original_position;
205
Quaternion bone_original_rotation;
206
Vector3 bone_original_scale;
207
208
void _update_gizmo_visible();
209
void _bone_enabled_changed(const int p_bone_id);
210
211
void _hide_handles();
212
213
void _draw_gizmo();
214
void _draw_handles();
215
216
void _joint_tree_selection_changed();
217
void _joint_tree_rmb_select(const Vector2 &p_pos, MouseButton p_button);
218
void _joint_tree_button_clicked(Object *p_item, int p_column, int p_id, MouseButton p_button);
219
void _update_properties();
220
221
void _subgizmo_selection_change();
222
void _disconnect_from_skeleton();
223
224
int selected_bone = -1;
225
226
protected:
227
void _notification(int p_what);
228
void _node_removed(Node *p_node);
229
230
public:
231
static Skeleton3DEditor *get_singleton() { return singleton; }
232
233
void select_bone(int p_idx);
234
235
int get_selected_bone() const;
236
237
void move_skeleton_bone(NodePath p_skeleton_path, int32_t p_selected_boneidx, int32_t p_target_boneidx);
238
239
Skeleton3D *get_skeleton() const { return skeleton; }
240
241
bool is_edit_mode() const { return edit_mode; }
242
243
void update_bone_original();
244
Vector3 get_bone_original_position() const { return bone_original_position; }
245
Quaternion get_bone_original_rotation() const { return bone_original_rotation; }
246
Vector3 get_bone_original_scale() const { return bone_original_scale; }
247
248
Skeleton3DEditor(EditorInspectorPluginSkeleton *e_plugin, Skeleton3D *skeleton);
249
~Skeleton3DEditor();
250
};
251
252
class EditorInspectorPluginSkeleton : public EditorInspectorPlugin {
253
GDCLASS(EditorInspectorPluginSkeleton, EditorInspectorPlugin);
254
255
friend class Skeleton3DEditorPlugin;
256
257
Skeleton3DEditor *skel_editor = nullptr;
258
259
public:
260
bool loc_pressed = false;
261
bool rot_pressed = true;
262
bool scl_pressed = false;
263
264
virtual bool can_handle(Object *p_object) override;
265
virtual void parse_begin(Object *p_object) override;
266
};
267
268
class Skeleton3DEditorPlugin : public EditorPlugin {
269
GDCLASS(Skeleton3DEditorPlugin, EditorPlugin);
270
271
EditorInspectorPluginSkeleton *skeleton_plugin = nullptr;
272
273
public:
274
virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override;
275
276
bool has_main_screen() const override { return false; }
277
virtual bool handles(Object *p_object) const override;
278
279
virtual String get_plugin_name() const override { return "Skeleton3D"; }
280
281
Skeleton3DEditorPlugin();
282
};
283
284
class Skeleton3DGizmoPlugin : public EditorNode3DGizmoPlugin {
285
GDCLASS(Skeleton3DGizmoPlugin, EditorNode3DGizmoPlugin);
286
287
struct SelectionMaterials {
288
Ref<StandardMaterial3D> unselected_mat;
289
Ref<ShaderMaterial> selected_mat;
290
};
291
static SelectionMaterials selection_materials;
292
293
public:
294
static Ref<ArrayMesh> get_bones_mesh(Skeleton3D *p_skeleton, int p_selected, bool p_is_selected);
295
static int skeleton_intersect_ray(const Skeleton3D *p_skeleton, Camera3D *p_camera, const Vector2 &p_point);
296
297
bool has_gizmo(Node3D *p_spatial) override;
298
String get_gizmo_name() const override;
299
int get_priority() const override;
300
301
virtual int subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const override;
302
virtual Transform3D get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const override;
303
virtual void set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) override;
304
virtual void commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) override;
305
306
void redraw(EditorNode3DGizmo *p_gizmo) override;
307
308
Skeleton3DGizmoPlugin();
309
~Skeleton3DGizmoPlugin();
310
};
311
312