bone_map_editor_plugin.h 7.4 KB

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