bone_map_editor_plugin.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*************************************************************************/
  2. /* bone_map_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 BONE_MAP_EDITOR_PLUGIN_H
  31. #define BONE_MAP_EDITOR_PLUGIN_H
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_plugin.h"
  34. #include "editor/editor_properties.h"
  35. #include "modules/modules_enabled.gen.h" // For regex.
  36. #ifdef MODULE_REGEX_ENABLED
  37. #include "modules/regex/regex.h"
  38. #endif
  39. #include "scene/3d/skeleton_3d.h"
  40. #include "scene/gui/color_rect.h"
  41. #include "scene/gui/dialogs.h"
  42. #include "scene/resources/bone_map.h"
  43. #include "scene/resources/texture.h"
  44. class BoneMapperButton : public TextureButton {
  45. GDCLASS(BoneMapperButton, TextureButton);
  46. public:
  47. enum BoneMapState {
  48. BONE_MAP_STATE_UNSET,
  49. BONE_MAP_STATE_SET,
  50. BONE_MAP_STATE_MISSING,
  51. BONE_MAP_STATE_ERROR
  52. };
  53. private:
  54. StringName profile_bone_name;
  55. bool selected = false;
  56. bool require = false;
  57. TextureRect *circle;
  58. void fetch_textures();
  59. protected:
  60. void _notification(int p_what);
  61. public:
  62. StringName get_profile_bone_name() const;
  63. void set_state(BoneMapState p_state);
  64. bool is_require() const;
  65. BoneMapperButton(const StringName p_profile_bone_name, bool p_require, bool p_selected);
  66. ~BoneMapperButton();
  67. };
  68. class BoneMapperItem : public VBoxContainer {
  69. GDCLASS(BoneMapperItem, VBoxContainer);
  70. int button_id = -1;
  71. StringName profile_bone_name;
  72. Ref<BoneMap> bone_map;
  73. EditorPropertyText *skeleton_bone_selector;
  74. Button *picker_button;
  75. void _update_property();
  76. void _open_picker();
  77. protected:
  78. void _notification(int p_what);
  79. static void _bind_methods();
  80. virtual void _value_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing);
  81. virtual void create_editor();
  82. public:
  83. void assign_button_id(int p_button_id);
  84. BoneMapperItem(Ref<BoneMap> &p_bone_map, const StringName &p_profile_bone_name = StringName());
  85. ~BoneMapperItem();
  86. };
  87. class BonePicker : public AcceptDialog {
  88. GDCLASS(BonePicker, AcceptDialog);
  89. Skeleton3D *skeleton = nullptr;
  90. Tree *bones = nullptr;
  91. public:
  92. void popup_bones_tree(const Size2i &p_minsize = Size2i());
  93. bool has_selected_bone();
  94. StringName get_selected_bone();
  95. protected:
  96. void _notification(int p_what);
  97. static void _bind_methods();
  98. void _confirm();
  99. private:
  100. void create_editors();
  101. void create_bones_tree(Skeleton3D *p_skeleton);
  102. public:
  103. BonePicker(Skeleton3D *p_skeleton);
  104. ~BonePicker();
  105. };
  106. class BoneMapper : public VBoxContainer {
  107. GDCLASS(BoneMapper, VBoxContainer);
  108. Skeleton3D *skeleton;
  109. Ref<BoneMap> bone_map;
  110. EditorPropertyResource *profile_selector;
  111. Vector<BoneMapperItem *> bone_mapper_items;
  112. Button *clear_mapping_button;
  113. VBoxContainer *mapper_item_vbox;
  114. int current_group_idx = 0;
  115. int current_bone_idx = -1;
  116. AspectRatioContainer *bone_mapper_field;
  117. EditorPropertyEnum *profile_group_selector;
  118. ColorRect *profile_bg;
  119. TextureRect *profile_texture;
  120. Vector<BoneMapperButton *> bone_mapper_buttons;
  121. void create_editor();
  122. void recreate_editor();
  123. void clear_items();
  124. void recreate_items();
  125. void update_group_idx();
  126. void _update_state();
  127. /* Bone picker */
  128. BonePicker *picker = nullptr;
  129. StringName picker_key_name;
  130. void _pick_bone(const StringName &p_bone_name);
  131. void _apply_picker_selection();
  132. void _clear_mapping_current_group();
  133. #ifdef MODULE_REGEX_ENABLED
  134. /* For auto mapping */
  135. enum BoneSegregation {
  136. BONE_SEGREGATION_NONE,
  137. BONE_SEGREGATION_LEFT,
  138. BONE_SEGREGATION_RIGHT
  139. };
  140. int search_bone_by_name(Skeleton3D *p_skeleton, Vector<String> p_picklist, BoneSegregation p_segregation = BONE_SEGREGATION_NONE, int p_parent = -1, int p_child = -1, int p_children_count = -1);
  141. BoneSegregation guess_bone_segregation(String p_bone_name);
  142. void auto_mapping_process(Ref<BoneMap> &p_bone_map);
  143. void _run_auto_mapping();
  144. #endif // MODULE_REGEX_ENABLED
  145. protected:
  146. void _notification(int p_what);
  147. static void _bind_methods();
  148. virtual void _value_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing);
  149. virtual void _profile_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing);
  150. public:
  151. void set_current_group_idx(int p_group_idx);
  152. int get_current_group_idx() const;
  153. void set_current_bone_idx(int p_bone_idx);
  154. int get_current_bone_idx() const;
  155. BoneMapper(Skeleton3D *p_skeleton, Ref<BoneMap> &p_bone_map);
  156. ~BoneMapper();
  157. };
  158. class BoneMapEditor : public VBoxContainer {
  159. GDCLASS(BoneMapEditor, VBoxContainer);
  160. Skeleton3D *skeleton;
  161. Ref<BoneMap> bone_map;
  162. BoneMapper *bone_mapper;
  163. void fetch_objects();
  164. void clear_editors();
  165. void create_editors();
  166. protected:
  167. void _notification(int p_what);
  168. public:
  169. BoneMapEditor(Ref<BoneMap> &p_bone_map);
  170. ~BoneMapEditor();
  171. };
  172. class EditorInspectorPluginBoneMap : public EditorInspectorPlugin {
  173. GDCLASS(EditorInspectorPluginBoneMap, EditorInspectorPlugin);
  174. BoneMapEditor *editor;
  175. public:
  176. virtual bool can_handle(Object *p_object) override;
  177. virtual void parse_begin(Object *p_object) override;
  178. };
  179. class BoneMapEditorPlugin : public EditorPlugin {
  180. GDCLASS(BoneMapEditorPlugin, EditorPlugin);
  181. public:
  182. virtual String get_name() const override { return "BoneMap"; }
  183. BoneMapEditorPlugin();
  184. };
  185. #endif // BONE_MAP_EDITOR_PLUGIN_H