openxr_binding_modifier_editor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**************************************************************************/
  2. /* openxr_binding_modifier_editor.cpp */
  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. #include "openxr_binding_modifier_editor.h"
  31. #include "editor/editor_string_names.h"
  32. ///////////////////////////////////////////////////////////////////////////////////////////////////
  33. // EditorPropertyActionSet
  34. void EditorPropertyActionSet::_set_read_only(bool p_read_only) {
  35. options->set_disabled(p_read_only);
  36. }
  37. void EditorPropertyActionSet::_option_selected(int p_which) {
  38. Ref<OpenXRActionSet> val = options->get_item_metadata(p_which);
  39. emit_changed(get_edited_property(), val);
  40. }
  41. void EditorPropertyActionSet::update_property() {
  42. Variant current = get_edited_property_value();
  43. if (current.get_type() == Variant::NIL) {
  44. options->select(-1);
  45. return;
  46. }
  47. Ref<OpenXRActionSet> which = current;
  48. for (int i = 0; i < options->get_item_count(); i++) {
  49. if (which == (Ref<OpenXRActionSet>)options->get_item_metadata(i)) {
  50. options->select(i);
  51. return;
  52. }
  53. }
  54. // Couldn't find it? deselect..
  55. options->select(-1);
  56. }
  57. void EditorPropertyActionSet::setup(const Ref<OpenXRActionMap> &p_action_map) {
  58. options->clear();
  59. Array action_sets = p_action_map->get_action_sets();
  60. for (Ref<OpenXRActionSet> action_set : action_sets) {
  61. options->add_item(action_set->get_localized_name());
  62. options->set_item_metadata(-1, action_set);
  63. }
  64. }
  65. void EditorPropertyActionSet::set_option_button_clip(bool p_enable) {
  66. options->set_clip_text(p_enable);
  67. }
  68. EditorPropertyActionSet::EditorPropertyActionSet() {
  69. options = memnew(OptionButton);
  70. options->set_clip_text(true);
  71. options->set_flat(true);
  72. options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  73. add_child(options);
  74. add_focusable(options);
  75. options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyActionSet::_option_selected));
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////////////////////////
  78. // EditorPropertyBindingPath
  79. void EditorPropertyBindingPath::_set_read_only(bool p_read_only) {
  80. options->set_disabled(p_read_only);
  81. }
  82. void EditorPropertyBindingPath::_option_selected(int p_which) {
  83. String val = options->get_item_metadata(p_which);
  84. emit_changed(get_edited_property(), val);
  85. }
  86. void EditorPropertyBindingPath::update_property() {
  87. Variant current = get_edited_property_value();
  88. if (current.get_type() == Variant::NIL) {
  89. options->select(-1);
  90. return;
  91. }
  92. String which = current;
  93. for (int i = 0; i < options->get_item_count(); i++) {
  94. if (which == (String)options->get_item_metadata(i)) {
  95. options->select(i);
  96. return;
  97. }
  98. }
  99. // Couldn't find it? deselect..
  100. options->select(-1);
  101. }
  102. void EditorPropertyBindingPath::setup(const String &p_interaction_profile_path, Vector<OpenXRAction::ActionType> p_include_action_types) {
  103. options->clear();
  104. const OpenXRInteractionProfileMetadata::InteractionProfile *profile_def = OpenXRInteractionProfileMetadata::get_singleton()->get_profile(p_interaction_profile_path);
  105. // Determine toplevel paths
  106. Vector<String> top_level_paths;
  107. for (const OpenXRInteractionProfileMetadata::IOPath &io_path : profile_def->io_paths) {
  108. if (!top_level_paths.has(io_path.top_level_path)) {
  109. top_level_paths.push_back(io_path.top_level_path);
  110. }
  111. }
  112. for (const String &top_level_path : top_level_paths) {
  113. String top_level_name = OpenXRInteractionProfileMetadata::get_singleton()->get_top_level_name(top_level_path);
  114. for (const OpenXRInteractionProfileMetadata::IOPath &io_path : profile_def->io_paths) {
  115. if (io_path.top_level_path == top_level_path && p_include_action_types.has(io_path.action_type)) {
  116. options->add_item(top_level_name + "/" + io_path.display_name);
  117. options->set_item_metadata(-1, io_path.openxr_path);
  118. }
  119. }
  120. }
  121. }
  122. void EditorPropertyBindingPath::set_option_button_clip(bool p_enable) {
  123. options->set_clip_text(p_enable);
  124. }
  125. EditorPropertyBindingPath::EditorPropertyBindingPath() {
  126. options = memnew(OptionButton);
  127. options->set_clip_text(true);
  128. options->set_flat(true);
  129. options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  130. add_child(options);
  131. add_focusable(options);
  132. options->connect(SceneStringName(item_selected), callable_mp(this, &EditorPropertyBindingPath::_option_selected));
  133. }
  134. ///////////////////////////////////////////////////////////////////////////////////////////////////
  135. // OpenXRBindingModifierEditor
  136. bool EditorInspectorPluginBindingModifier::can_handle(Object *p_object) {
  137. Ref<OpenXRBindingModifier> binding_modifier(Object::cast_to<OpenXRBindingModifier>(p_object));
  138. return binding_modifier.is_valid();
  139. }
  140. bool EditorInspectorPluginBindingModifier::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  141. Ref<OpenXRActionBindingModifier> action_binding_modifier(Object::cast_to<OpenXRActionBindingModifier>(p_object));
  142. if (action_binding_modifier.is_valid()) {
  143. if (p_type == Variant::OBJECT && p_hint == PROPERTY_HINT_RESOURCE_TYPE && p_hint_text == OpenXRActionSet::get_class_static()) {
  144. OpenXRIPBinding *ip_binding = action_binding_modifier->get_ip_binding();
  145. ERR_FAIL_NULL_V(ip_binding, false);
  146. OpenXRActionMap *action_map = ip_binding->get_action_map();
  147. ERR_FAIL_NULL_V(action_map, false);
  148. EditorPropertyActionSet *action_set_property = memnew(EditorPropertyActionSet);
  149. action_set_property->setup(action_map);
  150. add_property_editor(p_path, action_set_property);
  151. return true;
  152. }
  153. return false;
  154. }
  155. Ref<OpenXRIPBindingModifier> ip_binding_modifier(Object::cast_to<OpenXRIPBindingModifier>(p_object));
  156. if (ip_binding_modifier.is_valid()) {
  157. if (p_type == Variant::OBJECT && p_hint == PROPERTY_HINT_RESOURCE_TYPE && p_hint_text == OpenXRActionSet::get_class_static()) {
  158. OpenXRInteractionProfile *interaction_profile = ip_binding_modifier->get_interaction_profile();
  159. ERR_FAIL_NULL_V(interaction_profile, false);
  160. OpenXRActionMap *action_map = interaction_profile->get_action_map();
  161. ERR_FAIL_NULL_V(action_map, false);
  162. EditorPropertyActionSet *action_set_property = memnew(EditorPropertyActionSet);
  163. action_set_property->setup(action_map);
  164. add_property_editor(p_path, action_set_property);
  165. return true;
  166. }
  167. if (p_type == Variant::STRING && p_hint == PROPERTY_HINT_TYPE_STRING && p_hint_text == "binding_path") {
  168. EditorPropertyBindingPath *binding_path_property = memnew(EditorPropertyBindingPath);
  169. OpenXRInteractionProfile *interaction_profile = ip_binding_modifier->get_interaction_profile();
  170. ERR_FAIL_NULL_V(interaction_profile, false);
  171. Vector<OpenXRAction::ActionType> action_types;
  172. action_types.push_back(OpenXRAction::OPENXR_ACTION_VECTOR2);
  173. binding_path_property->setup(interaction_profile->get_interaction_profile_path(), action_types);
  174. add_property_editor(p_path, binding_path_property);
  175. return true;
  176. }
  177. return false;
  178. }
  179. return false;
  180. }
  181. ///////////////////////////////////////////////////////////////////////////////////////////////////
  182. // OpenXRBindingModifierEditor
  183. void OpenXRBindingModifierEditor::_bind_methods() {
  184. ClassDB::bind_method(D_METHOD("get_binding_modifier"), &OpenXRBindingModifierEditor::get_binding_modifier);
  185. ClassDB::bind_method(D_METHOD("setup", "action_map", "binding_modifier"), &OpenXRBindingModifierEditor::setup);
  186. ADD_SIGNAL(MethodInfo("binding_modifier_removed", PropertyInfo(Variant::OBJECT, "binding_modifier_editor")));
  187. }
  188. void OpenXRBindingModifierEditor::_notification(int p_what) {
  189. switch (p_what) {
  190. case NOTIFICATION_ENTER_TREE:
  191. case NOTIFICATION_THEME_CHANGED: {
  192. rem_binding_modifier_btn->set_button_icon(get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons)));
  193. } break;
  194. }
  195. }
  196. void OpenXRBindingModifierEditor::_on_remove_binding_modifier() {
  197. // Tell parent to remove us
  198. emit_signal("binding_modifier_removed", this);
  199. }
  200. OpenXRBindingModifierEditor::OpenXRBindingModifierEditor() {
  201. undo_redo = EditorUndoRedoManager::get_singleton();
  202. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  203. main_vb = memnew(VBoxContainer);
  204. main_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  205. add_child(main_vb);
  206. header_hb = memnew(HBoxContainer);
  207. header_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  208. main_vb->add_child(header_hb);
  209. binding_modifier_title = memnew(Label);
  210. binding_modifier_title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  211. header_hb->add_child(binding_modifier_title);
  212. rem_binding_modifier_btn = memnew(Button);
  213. rem_binding_modifier_btn->set_tooltip_text(TTR("Remove binding modifier."));
  214. rem_binding_modifier_btn->connect(SceneStringName(pressed), callable_mp(this, &OpenXRBindingModifierEditor::_on_remove_binding_modifier));
  215. rem_binding_modifier_btn->set_flat(true);
  216. header_hb->add_child(rem_binding_modifier_btn);
  217. editor_inspector = memnew(EditorInspector);
  218. editor_inspector->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  219. editor_inspector->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  220. editor_inspector->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  221. main_vb->add_child(editor_inspector);
  222. }
  223. void OpenXRBindingModifierEditor::setup(Ref<OpenXRActionMap> p_action_map, Ref<OpenXRBindingModifier> p_binding_modifier) {
  224. ERR_FAIL_NULL(binding_modifier_title);
  225. ERR_FAIL_NULL(editor_inspector);
  226. action_map = p_action_map;
  227. binding_modifier = p_binding_modifier;
  228. if (p_binding_modifier.is_valid()) {
  229. binding_modifier_title->set_text(p_binding_modifier->get_description());
  230. editor_inspector->set_object_class(p_binding_modifier->get_class());
  231. editor_inspector->edit(p_binding_modifier.ptr());
  232. }
  233. }