bone_map_editor_plugin.h 7.3 KB

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