skeleton_3d_editor_plugin.h 10.0 KB

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