openxr_valve_analog_threshold_extension.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**************************************************************************/
  2. /* openxr_valve_analog_threshold_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_valve_analog_threshold_extension.h"
  31. #include "../action_map/openxr_action_set.h"
  32. #include "../action_map/openxr_interaction_profile.h"
  33. #include "../openxr_api.h"
  34. // Implementation for:
  35. // https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_VALVE_analog_threshold
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////
  37. // OpenXRValveAnalogThresholdExtension
  38. OpenXRValveAnalogThresholdExtension *OpenXRValveAnalogThresholdExtension::singleton = nullptr;
  39. OpenXRValveAnalogThresholdExtension *OpenXRValveAnalogThresholdExtension::get_singleton() {
  40. return singleton;
  41. }
  42. OpenXRValveAnalogThresholdExtension::OpenXRValveAnalogThresholdExtension() {
  43. singleton = this;
  44. }
  45. OpenXRValveAnalogThresholdExtension::~OpenXRValveAnalogThresholdExtension() {
  46. singleton = nullptr;
  47. }
  48. HashMap<String, bool *> OpenXRValveAnalogThresholdExtension::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_VALVE_ANALOG_THRESHOLD_EXTENSION_NAME] = &threshold_ext;
  53. return request_extensions;
  54. }
  55. bool OpenXRValveAnalogThresholdExtension::is_available() {
  56. return binding_modifier_ext && threshold_ext;
  57. }
  58. ///////////////////////////////////////////////////////////////////////////////////////////////////
  59. // OpenXRAnalogThresholdModifier
  60. void OpenXRAnalogThresholdModifier::_bind_methods() {
  61. ClassDB::bind_method(D_METHOD("set_on_threshold", "on_threshold"), &OpenXRAnalogThresholdModifier::set_on_threshold);
  62. ClassDB::bind_method(D_METHOD("get_on_threshold"), &OpenXRAnalogThresholdModifier::get_on_threshold);
  63. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "on_threshold", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_on_threshold", "get_on_threshold");
  64. ClassDB::bind_method(D_METHOD("set_off_threshold", "off_threshold"), &OpenXRAnalogThresholdModifier::set_off_threshold);
  65. ClassDB::bind_method(D_METHOD("get_off_threshold"), &OpenXRAnalogThresholdModifier::get_off_threshold);
  66. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "off_threshold", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_off_threshold", "get_off_threshold");
  67. ClassDB::bind_method(D_METHOD("set_on_haptic", "haptic"), &OpenXRAnalogThresholdModifier::set_on_haptic);
  68. ClassDB::bind_method(D_METHOD("get_on_haptic"), &OpenXRAnalogThresholdModifier::get_on_haptic);
  69. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "on_haptic", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRHapticBase"), "set_on_haptic", "get_on_haptic");
  70. ClassDB::bind_method(D_METHOD("set_off_haptic", "haptic"), &OpenXRAnalogThresholdModifier::set_off_haptic);
  71. ClassDB::bind_method(D_METHOD("get_off_haptic"), &OpenXRAnalogThresholdModifier::get_off_haptic);
  72. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "off_haptic", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRHapticBase"), "set_off_haptic", "get_off_haptic");
  73. }
  74. OpenXRAnalogThresholdModifier::OpenXRAnalogThresholdModifier() {
  75. analog_threshold.type = XR_TYPE_INTERACTION_PROFILE_ANALOG_THRESHOLD_VALVE;
  76. analog_threshold.next = nullptr;
  77. analog_threshold.onThreshold = 0.6;
  78. analog_threshold.offThreshold = 0.4;
  79. }
  80. void OpenXRAnalogThresholdModifier::set_on_threshold(float p_threshold) {
  81. ERR_FAIL_COND(p_threshold < 0.0 || p_threshold > 1.0);
  82. analog_threshold.onThreshold = p_threshold;
  83. emit_changed();
  84. }
  85. float OpenXRAnalogThresholdModifier::get_on_threshold() const {
  86. return analog_threshold.onThreshold;
  87. }
  88. void OpenXRAnalogThresholdModifier::set_off_threshold(float p_threshold) {
  89. ERR_FAIL_COND(p_threshold < 0.0 || p_threshold > 1.0);
  90. analog_threshold.offThreshold = p_threshold;
  91. emit_changed();
  92. }
  93. float OpenXRAnalogThresholdModifier::get_off_threshold() const {
  94. return analog_threshold.offThreshold;
  95. }
  96. void OpenXRAnalogThresholdModifier::set_on_haptic(const Ref<OpenXRHapticBase> &p_haptic) {
  97. on_haptic = p_haptic;
  98. emit_changed();
  99. }
  100. Ref<OpenXRHapticBase> OpenXRAnalogThresholdModifier::get_on_haptic() const {
  101. return on_haptic;
  102. }
  103. void OpenXRAnalogThresholdModifier::set_off_haptic(const Ref<OpenXRHapticBase> &p_haptic) {
  104. off_haptic = p_haptic;
  105. emit_changed();
  106. }
  107. Ref<OpenXRHapticBase> OpenXRAnalogThresholdModifier::get_off_haptic() const {
  108. return off_haptic;
  109. }
  110. PackedByteArray OpenXRAnalogThresholdModifier::get_ip_modification() {
  111. PackedByteArray ret;
  112. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  113. ERR_FAIL_NULL_V(openxr_api, ret);
  114. OpenXRValveAnalogThresholdExtension *analog_threshold_ext = OpenXRValveAnalogThresholdExtension::get_singleton();
  115. if (!analog_threshold_ext || !analog_threshold_ext->is_available()) {
  116. // Extension not enabled!
  117. WARN_PRINT("Analog threshold extension is not enabled or available.");
  118. return ret;
  119. }
  120. ERR_FAIL_NULL_V(ip_binding, ret);
  121. Ref<OpenXRAction> action = ip_binding->get_action();
  122. ERR_FAIL_COND_V(action.is_null(), ret);
  123. // Get our action set
  124. Ref<OpenXRActionSet> action_set = action->get_action_set();
  125. ERR_FAIL_COND_V(action_set.is_null(), ret);
  126. RID action_set_rid = openxr_api->find_action_set(action_set->get_name());
  127. ERR_FAIL_COND_V(!action_set_rid.is_valid(), ret);
  128. // Get our action
  129. RID action_rid = openxr_api->find_action(action->get_name(), action_set_rid);
  130. ERR_FAIL_COND_V(!action_rid.is_valid(), ret);
  131. analog_threshold.action = openxr_api->action_get_handle(action_rid);
  132. analog_threshold.binding = openxr_api->get_xr_path(ip_binding->get_binding_path());
  133. ERR_FAIL_COND_V(analog_threshold.binding == XR_NULL_PATH, ret);
  134. // These are set already:
  135. // - analog_threshold.onThreshold
  136. // - analog_threshold.offThreshold
  137. if (on_haptic.is_valid()) {
  138. analog_threshold.onHaptic = on_haptic->get_xr_structure();
  139. } else {
  140. analog_threshold.onHaptic = nullptr;
  141. }
  142. if (off_haptic.is_valid()) {
  143. analog_threshold.offHaptic = off_haptic->get_xr_structure();
  144. } else {
  145. analog_threshold.offHaptic = nullptr;
  146. }
  147. // Copy into byte array so we can return it.
  148. ERR_FAIL_COND_V(ret.resize(sizeof(XrInteractionProfileAnalogThresholdVALVE)) != OK, ret);
  149. memcpy(ret.ptrw(), &analog_threshold, sizeof(XrInteractionProfileAnalogThresholdVALVE));
  150. return ret;
  151. }