openxr_dpad_binding_extension.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**************************************************************************/
  2. /* openxr_dpad_binding_extension.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_dpad_binding_extension.h"
  31. #include "../action_map/openxr_interaction_profile_metadata.h"
  32. #include "../openxr_api.h"
  33. #include "core/math/math_funcs.h"
  34. // Implementation for:
  35. // https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_EXT_dpad_binding
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////
  37. // OpenXRDPadBindingExtension
  38. OpenXRDPadBindingExtension *OpenXRDPadBindingExtension::singleton = nullptr;
  39. OpenXRDPadBindingExtension *OpenXRDPadBindingExtension::get_singleton() {
  40. return singleton;
  41. }
  42. OpenXRDPadBindingExtension::OpenXRDPadBindingExtension() {
  43. singleton = this;
  44. }
  45. OpenXRDPadBindingExtension::~OpenXRDPadBindingExtension() {
  46. singleton = nullptr;
  47. }
  48. HashMap<String, bool *> OpenXRDPadBindingExtension::get_requested_extensions() {
  49. HashMap<String, bool *> request_extensions;
  50. // Note, we're dependent on the binding modifier extension, this may be requested by multiple extension wrappers.
  51. request_extensions[XR_KHR_BINDING_MODIFICATION_EXTENSION_NAME] = &binding_modifier_ext;
  52. request_extensions[XR_EXT_DPAD_BINDING_EXTENSION_NAME] = &dpad_binding_ext;
  53. return request_extensions;
  54. }
  55. bool OpenXRDPadBindingExtension::is_available() {
  56. return binding_modifier_ext && dpad_binding_ext;
  57. }
  58. ///////////////////////////////////////////////////////////////////////////////////////////////////
  59. // OpenXRDpadBindingModifier
  60. void OpenXRDpadBindingModifier::_bind_methods() {
  61. ClassDB::bind_method(D_METHOD("set_action_set", "action_set"), &OpenXRDpadBindingModifier::set_action_set);
  62. ClassDB::bind_method(D_METHOD("get_action_set"), &OpenXRDpadBindingModifier::get_action_set);
  63. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "action_set", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRActionSet"), "set_action_set", "get_action_set");
  64. ClassDB::bind_method(D_METHOD("set_input_path", "input_path"), &OpenXRDpadBindingModifier::set_input_path);
  65. ClassDB::bind_method(D_METHOD("get_input_path"), &OpenXRDpadBindingModifier::get_input_path);
  66. ADD_PROPERTY(PropertyInfo(Variant::STRING, "input_path", PROPERTY_HINT_TYPE_STRING, "binding_path"), "set_input_path", "get_input_path");
  67. ClassDB::bind_method(D_METHOD("set_threshold", "threshold"), &OpenXRDpadBindingModifier::set_threshold);
  68. ClassDB::bind_method(D_METHOD("get_threshold"), &OpenXRDpadBindingModifier::get_threshold);
  69. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "threshold", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_threshold", "get_threshold");
  70. ClassDB::bind_method(D_METHOD("set_threshold_released", "threshold_released"), &OpenXRDpadBindingModifier::set_threshold_released);
  71. ClassDB::bind_method(D_METHOD("get_threshold_released"), &OpenXRDpadBindingModifier::get_threshold_released);
  72. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "threshold_released", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_threshold_released", "get_threshold_released");
  73. ClassDB::bind_method(D_METHOD("set_center_region", "center_region"), &OpenXRDpadBindingModifier::set_center_region);
  74. ClassDB::bind_method(D_METHOD("get_center_region"), &OpenXRDpadBindingModifier::get_center_region);
  75. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "center_region", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_center_region", "get_center_region");
  76. ClassDB::bind_method(D_METHOD("set_wedge_angle", "wedge_angle"), &OpenXRDpadBindingModifier::set_wedge_angle);
  77. ClassDB::bind_method(D_METHOD("get_wedge_angle"), &OpenXRDpadBindingModifier::get_wedge_angle);
  78. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wedge_angle", PROPERTY_HINT_RANGE, "1.0,180.0,0.1,radians_as_degrees"), "set_wedge_angle", "get_wedge_angle");
  79. ClassDB::bind_method(D_METHOD("set_is_sticky", "is_sticky"), &OpenXRDpadBindingModifier::set_is_sticky);
  80. ClassDB::bind_method(D_METHOD("get_is_sticky"), &OpenXRDpadBindingModifier::get_is_sticky);
  81. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_sticky"), "set_is_sticky", "get_is_sticky");
  82. ClassDB::bind_method(D_METHOD("set_on_haptic", "haptic"), &OpenXRDpadBindingModifier::set_on_haptic);
  83. ClassDB::bind_method(D_METHOD("get_on_haptic"), &OpenXRDpadBindingModifier::get_on_haptic);
  84. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "on_haptic", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRHapticBase"), "set_on_haptic", "get_on_haptic");
  85. ClassDB::bind_method(D_METHOD("set_off_haptic", "haptic"), &OpenXRDpadBindingModifier::set_off_haptic);
  86. ClassDB::bind_method(D_METHOD("get_off_haptic"), &OpenXRDpadBindingModifier::get_off_haptic);
  87. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "off_haptic", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRHapticBase"), "set_off_haptic", "get_off_haptic");
  88. }
  89. OpenXRDpadBindingModifier::OpenXRDpadBindingModifier() {
  90. ERR_FAIL_COND(dpad_bindings_data.resize_zeroed(sizeof(XrInteractionProfileDpadBindingEXT)) != OK);
  91. dpad_bindings = (XrInteractionProfileDpadBindingEXT *)dpad_bindings_data.ptrw();
  92. dpad_bindings->type = XR_TYPE_INTERACTION_PROFILE_DPAD_BINDING_EXT;
  93. dpad_bindings->next = nullptr;
  94. dpad_bindings->forceThreshold = 0.6;
  95. dpad_bindings->forceThresholdReleased = 0.4;
  96. dpad_bindings->centerRegion = 0.1;
  97. dpad_bindings->wedgeAngle = Math::deg_to_rad(90.0);
  98. dpad_bindings->isSticky = false;
  99. }
  100. void OpenXRDpadBindingModifier::set_action_set(const Ref<OpenXRActionSet> p_action_set) {
  101. action_set = p_action_set;
  102. }
  103. Ref<OpenXRActionSet> OpenXRDpadBindingModifier::get_action_set() const {
  104. return action_set;
  105. }
  106. void OpenXRDpadBindingModifier::set_input_path(const String &p_input_path) {
  107. input_path = p_input_path;
  108. emit_changed();
  109. }
  110. String OpenXRDpadBindingModifier::get_input_path() const {
  111. return input_path;
  112. }
  113. void OpenXRDpadBindingModifier::set_threshold(float p_threshold) {
  114. ERR_FAIL_NULL(dpad_bindings);
  115. ERR_FAIL_COND(p_threshold < 0.0 || p_threshold > 1.0);
  116. dpad_bindings->forceThreshold = p_threshold;
  117. emit_changed();
  118. }
  119. float OpenXRDpadBindingModifier::get_threshold() const {
  120. ERR_FAIL_NULL_V(dpad_bindings, 0.0);
  121. return dpad_bindings->forceThreshold;
  122. }
  123. void OpenXRDpadBindingModifier::set_threshold_released(float p_threshold) {
  124. ERR_FAIL_NULL(dpad_bindings);
  125. ERR_FAIL_COND(p_threshold < 0.0 || p_threshold > 1.0);
  126. dpad_bindings->forceThresholdReleased = p_threshold;
  127. emit_changed();
  128. }
  129. float OpenXRDpadBindingModifier::get_threshold_released() const {
  130. ERR_FAIL_NULL_V(dpad_bindings, 0.0);
  131. return dpad_bindings->forceThresholdReleased;
  132. }
  133. void OpenXRDpadBindingModifier::set_center_region(float p_center_region) {
  134. ERR_FAIL_NULL(dpad_bindings);
  135. ERR_FAIL_COND(p_center_region < 0.0 || p_center_region > 1.0);
  136. dpad_bindings->centerRegion = p_center_region;
  137. emit_changed();
  138. }
  139. float OpenXRDpadBindingModifier::get_center_region() const {
  140. ERR_FAIL_NULL_V(dpad_bindings, 0.0);
  141. return dpad_bindings->centerRegion;
  142. }
  143. void OpenXRDpadBindingModifier::set_wedge_angle(float p_wedge_angle) {
  144. ERR_FAIL_NULL(dpad_bindings);
  145. dpad_bindings->wedgeAngle = p_wedge_angle;
  146. emit_changed();
  147. }
  148. float OpenXRDpadBindingModifier::get_wedge_angle() const {
  149. ERR_FAIL_NULL_V(dpad_bindings, 0.0);
  150. return dpad_bindings->wedgeAngle;
  151. }
  152. void OpenXRDpadBindingModifier::set_wedge_angle_deg(float p_wedge_angle) {
  153. ERR_FAIL_NULL(dpad_bindings);
  154. dpad_bindings->wedgeAngle = Math::deg_to_rad(p_wedge_angle);
  155. emit_changed();
  156. }
  157. float OpenXRDpadBindingModifier::get_wedge_angle_deg() const {
  158. ERR_FAIL_NULL_V(dpad_bindings, 0.0);
  159. return Math::rad_to_deg(dpad_bindings->wedgeAngle);
  160. }
  161. void OpenXRDpadBindingModifier::set_is_sticky(bool p_sticky) {
  162. ERR_FAIL_NULL(dpad_bindings);
  163. dpad_bindings->isSticky = p_sticky;
  164. emit_changed();
  165. }
  166. bool OpenXRDpadBindingModifier::get_is_sticky() const {
  167. ERR_FAIL_NULL_V(dpad_bindings, false);
  168. return dpad_bindings->isSticky;
  169. }
  170. void OpenXRDpadBindingModifier::set_on_haptic(const Ref<OpenXRHapticBase> &p_haptic) {
  171. on_haptic = p_haptic;
  172. emit_changed();
  173. }
  174. Ref<OpenXRHapticBase> OpenXRDpadBindingModifier::get_on_haptic() const {
  175. return on_haptic;
  176. }
  177. void OpenXRDpadBindingModifier::set_off_haptic(const Ref<OpenXRHapticBase> &p_haptic) {
  178. off_haptic = p_haptic;
  179. emit_changed();
  180. }
  181. Ref<OpenXRHapticBase> OpenXRDpadBindingModifier::get_off_haptic() const {
  182. return off_haptic;
  183. }
  184. PackedByteArray OpenXRDpadBindingModifier::get_ip_modification() {
  185. ERR_FAIL_NULL_V(dpad_bindings, PackedByteArray());
  186. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  187. ERR_FAIL_NULL_V(openxr_api, PackedByteArray());
  188. OpenXRDPadBindingExtension *dpad_binding_ext = OpenXRDPadBindingExtension::get_singleton();
  189. if (!dpad_binding_ext || !dpad_binding_ext->is_available()) {
  190. // Extension not enabled!
  191. WARN_PRINT("DPad binding extension is not enabled or available.");
  192. return PackedByteArray();
  193. }
  194. dpad_bindings->binding = openxr_api->get_xr_path(input_path);
  195. ERR_FAIL_COND_V(dpad_bindings->binding == XR_NULL_PATH, PackedByteArray());
  196. // Get our action set
  197. ERR_FAIL_COND_V(!action_set.is_valid(), PackedByteArray());
  198. RID action_set_rid = openxr_api->find_action_set(action_set->get_name());
  199. ERR_FAIL_COND_V(!action_set_rid.is_valid(), PackedByteArray());
  200. dpad_bindings->actionSet = openxr_api->action_set_get_handle(action_set_rid);
  201. // These are set already:
  202. // - forceThreshold
  203. // - forceThresholdReleased
  204. // - centerRegion
  205. // - wedgeAngle
  206. // - isSticky
  207. if (on_haptic.is_valid()) {
  208. dpad_bindings->onHaptic = on_haptic->get_xr_structure();
  209. } else {
  210. dpad_bindings->onHaptic = nullptr;
  211. }
  212. if (off_haptic.is_valid()) {
  213. dpad_bindings->offHaptic = off_haptic->get_xr_structure();
  214. } else {
  215. dpad_bindings->offHaptic = nullptr;
  216. }
  217. return dpad_bindings_data;
  218. }