| 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 | |
| 31 | #include "openxr_valve_analog_threshold_extension.h" |
| 32 | #include "../action_map/openxr_action_set.h" |
| 33 | #include "../action_map/openxr_interaction_profile.h" |
| 34 | #include "../openxr_api.h" |
| 35 | |
| 36 | // Implementation for: |
| 37 | // https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_VALVE_analog_threshold |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 40 | // OpenXRValveAnalogThresholdExtension |
| 41 | |
| 42 | OpenXRValveAnalogThresholdExtension *OpenXRValveAnalogThresholdExtension::singleton = nullptr; |
| 43 | |
| 44 | OpenXRValveAnalogThresholdExtension *OpenXRValveAnalogThresholdExtension::get_singleton() { |
| 45 | return singleton; |
| 46 | } |
| 47 | |
| 48 | OpenXRValveAnalogThresholdExtension::OpenXRValveAnalogThresholdExtension() { |
| 49 | singleton = this; |
| 50 | } |
| 51 | |
| 52 | OpenXRValveAnalogThresholdExtension::~OpenXRValveAnalogThresholdExtension() { |
| 53 | singleton = nullptr; |
| 54 | } |
| 55 | |
| 56 | HashMap<String, bool *> OpenXRValveAnalogThresholdExtension::get_requested_extensions(XrVersion p_version) { |
| 57 | HashMap<String, bool *> request_extensions; |
| 58 | |
| 59 | // Note, we're dependent on the binding modifier extension, this may be requested by multiple extension wrappers. |
| 60 | request_extensions[XR_KHR_BINDING_MODIFICATION_EXTENSION_NAME] = &binding_modifier_ext; |
| 61 | request_extensions[XR_VALVE_ANALOG_THRESHOLD_EXTENSION_NAME] = &threshold_ext; |
| 62 | |
| 63 | return request_extensions; |
| 64 | } |
| 65 | |
| 66 | bool OpenXRValveAnalogThresholdExtension::is_available() { |
| 67 | return binding_modifier_ext && threshold_ext; |
| 68 | } |
| 69 | |
| 70 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 71 | // OpenXRAnalogThresholdModifier |
| 72 | |
| 73 | void OpenXRAnalogThresholdModifier::_bind_methods() { |
| 74 | ClassDB::bind_method(D_METHOD("set_on_threshold", "on_threshold"), &OpenXRAnalogThresholdModifier::set_on_threshold); |
| 75 | ClassDB::bind_method(D_METHOD("get_on_threshold"), &OpenXRAnalogThresholdModifier::get_on_threshold); |
| 76 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "on_threshold", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_on_threshold", "get_on_threshold"); |
| 77 | |
| 78 | ClassDB::bind_method(D_METHOD("set_off_threshold", "off_threshold"), &OpenXRAnalogThresholdModifier::set_off_threshold); |
| 79 | ClassDB::bind_method(D_METHOD("get_off_threshold"), &OpenXRAnalogThresholdModifier::get_off_threshold); |
| 80 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "off_threshold", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_off_threshold", "get_off_threshold"); |
| 81 | |
| 82 | ClassDB::bind_method(D_METHOD("set_on_haptic", "haptic"), &OpenXRAnalogThresholdModifier::set_on_haptic); |
| 83 | ClassDB::bind_method(D_METHOD("get_on_haptic"), &OpenXRAnalogThresholdModifier::get_on_haptic); |
| 84 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "on_haptic", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRHapticBase"), "set_on_haptic", "get_on_haptic"); |
| 85 | |
| 86 | ClassDB::bind_method(D_METHOD("set_off_haptic", "haptic"), &OpenXRAnalogThresholdModifier::set_off_haptic); |
| 87 | ClassDB::bind_method(D_METHOD("get_off_haptic"), &OpenXRAnalogThresholdModifier::get_off_haptic); |
| 88 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "off_haptic", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRHapticBase"), "set_off_haptic", "get_off_haptic"); |
| 89 | } |
| 90 | |
| 91 | OpenXRAnalogThresholdModifier::OpenXRAnalogThresholdModifier() { |
| 92 | analog_threshold.type = XR_TYPE_INTERACTION_PROFILE_ANALOG_THRESHOLD_VALVE; |
| 93 | analog_threshold.next = nullptr; |
| 94 | |
| 95 | analog_threshold.onThreshold = 0.6; |
| 96 | analog_threshold.offThreshold = 0.4; |
| 97 | } |
| 98 | |
| 99 | void OpenXRAnalogThresholdModifier::set_on_threshold(float p_threshold) { |
| 100 | ERR_FAIL_COND(p_threshold < 0.0 || p_threshold > 1.0); |
| 101 | |
| 102 | analog_threshold.onThreshold = p_threshold; |
| 103 | emit_changed(); |
| 104 | } |
| 105 | |
| 106 | float OpenXRAnalogThresholdModifier::get_on_threshold() const { |
| 107 | return analog_threshold.onThreshold; |
| 108 | } |
| 109 | |
| 110 | void OpenXRAnalogThresholdModifier::set_off_threshold(float p_threshold) { |
| 111 | ERR_FAIL_COND(p_threshold < 0.0 || p_threshold > 1.0); |
| 112 | |
| 113 | analog_threshold.offThreshold = p_threshold; |
| 114 | emit_changed(); |
| 115 | } |
| 116 | |
| 117 | float OpenXRAnalogThresholdModifier::get_off_threshold() const { |
| 118 | return analog_threshold.offThreshold; |
| 119 | } |
| 120 | |
| 121 | void OpenXRAnalogThresholdModifier::set_on_haptic(const Ref<OpenXRHapticBase> &p_haptic) { |
| 122 | on_haptic = p_haptic; |
| 123 | emit_changed(); |
| 124 | } |
| 125 | |
| 126 | Ref<OpenXRHapticBase> OpenXRAnalogThresholdModifier::get_on_haptic() const { |
| 127 | return on_haptic; |
| 128 | } |
| 129 | |
| 130 | void OpenXRAnalogThresholdModifier::set_off_haptic(const Ref<OpenXRHapticBase> &p_haptic) { |
| 131 | off_haptic = p_haptic; |
| 132 | emit_changed(); |
| 133 | } |
| 134 | |
| 135 | Ref<OpenXRHapticBase> OpenXRAnalogThresholdModifier::get_off_haptic() const { |
| 136 | return off_haptic; |
| 137 | } |
| 138 | |
| 139 | PackedByteArray OpenXRAnalogThresholdModifier::get_ip_modification() { |
| 140 | PackedByteArray ret; |
| 141 | |
| 142 | OpenXRAPI *openxr_api = OpenXRAPI::get_singleton(); |
| 143 | ERR_FAIL_NULL_V(openxr_api, ret); |
| 144 | |
| 145 | OpenXRValveAnalogThresholdExtension *analog_threshold_ext = OpenXRValveAnalogThresholdExtension::get_singleton(); |
| 146 | if (!analog_threshold_ext || !analog_threshold_ext->is_available()) { |
| 147 | // Extension not enabled! |
| 148 | WARN_PRINT("Analog threshold extension is not enabled or available."); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | ERR_FAIL_NULL_V(ip_binding, ret); |
| 153 | |
| 154 | Ref<OpenXRAction> action = ip_binding->get_action(); |
| 155 | ERR_FAIL_COND_V(action.is_null(), ret); |
| 156 | |
| 157 | // Get our action set |
| 158 | Ref<OpenXRActionSet> action_set = action->get_action_set(); |
| 159 | ERR_FAIL_COND_V(action_set.is_null(), ret); |
| 160 | RID action_set_rid = openxr_api->find_action_set(action_set->get_name()); |
| 161 | ERR_FAIL_COND_V(!action_set_rid.is_valid(), ret); |
| 162 | |
| 163 | // Get our action |
| 164 | RID action_rid = openxr_api->find_action(action->get_name(), action_set_rid); |
| 165 | ERR_FAIL_COND_V(!action_rid.is_valid(), ret); |
| 166 | |
| 167 | analog_threshold.action = openxr_api->action_get_handle(action_rid); |
| 168 | |
| 169 | analog_threshold.binding = openxr_api->get_xr_path(ip_binding->get_binding_path()); |
| 170 | ERR_FAIL_COND_V(analog_threshold.binding == XR_NULL_PATH, ret); |
| 171 | |
| 172 | // These are set already: |
| 173 | // - analog_threshold.onThreshold |
| 174 | // - analog_threshold.offThreshold |
| 175 | |
| 176 | if (on_haptic.is_valid()) { |
| 177 | analog_threshold.onHaptic = on_haptic->get_xr_structure(); |
| 178 | } else { |
| 179 | analog_threshold.onHaptic = nullptr; |
| 180 | } |
| 181 | |
| 182 | if (off_haptic.is_valid()) { |
| 183 | analog_threshold.offHaptic = off_haptic->get_xr_structure(); |
| 184 | } else { |
| 185 | analog_threshold.offHaptic = nullptr; |
| 186 | } |
| 187 | |
| 188 | // Copy into byte array so we can return it. |
| 189 | ERR_FAIL_COND_V(ret.resize(sizeof(XrInteractionProfileAnalogThresholdVALVE)) != OK, ret); |
| 190 | memcpy(ret.ptrw(), &analog_threshold, sizeof(XrInteractionProfileAnalogThresholdVALVE)); |
| 191 | |
| 192 | return ret; |
| 193 | } |