skeleton_3d_editor_plugin.h 9.1 KB

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