From abb8ad867c9a05b288bc20090349f8a2e8738e80 Mon Sep 17 00:00:00 2001 From: Parick Down Date: Sun, 12 Nov 2023 09:55:54 -0500 Subject: [PATCH] Add support for trigger button click In added a trigger_click button input for the wand in addition to analog trigger input --- example/Controls.tscn | 8 +++++++- example/addons/tiltfive/T5Interface.gd | 1 + example/addons/tiltfive/T5ProjectSettings.gd | 5 +++++ example/pointer_control.gd | 6 ++++++ extension/src/GodotT5Glasses.cpp | 7 +++++++ extension/src/GodotT5Glasses.h | 9 +++++++++ extension/src/TiltFiveXRInterface.cpp | 20 ++++++++++++++++++++ extension/src/TiltFiveXRInterface.h | 4 ++++ 8 files changed, 59 insertions(+), 1 deletion(-) diff --git a/example/Controls.tscn b/example/Controls.tscn index f54b52b..f393380 100644 --- a/example/Controls.tscn +++ b/example/Controls.tscn @@ -1,7 +1,9 @@ -[gd_scene load_steps=3 format=3 uid="uid://dnx42xctfl3mx"] +[gd_scene load_steps=4 format=3 uid="uid://dnx42xctfl3mx"] [sub_resource type="CapsuleMesh" id="4"] +[sub_resource type="BoxMesh" id="BoxMesh_dc5d6"] + [sub_resource type="SphereMesh" id="3"] [node name="Controls" type="Node3D"] @@ -11,6 +13,10 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0748157, -0.00207818, 0.009 transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, -0.000888377, -0.0415742, -0.0168045) mesh = SubResource("4") +[node name="TriggerClick" type="MeshInstance3D" parent="."] +transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, -0.000888377, -0.00848173, -0.0840266) +mesh = SubResource("BoxMesh_dc5d6") + [node name="Three" type="MeshInstance3D" parent="."] transform = Transform3D(0.01, 0, 0, 0, 0.01, 0, 0, 0, 0.01, 0, 0, 0) mesh = SubResource("3") diff --git a/example/addons/tiltfive/T5Interface.gd b/example/addons/tiltfive/T5Interface.gd index 6af5788..c7ee77c 100644 --- a/example/addons/tiltfive/T5Interface.gd +++ b/example/addons/tiltfive/T5Interface.gd @@ -54,6 +54,7 @@ func _enter_tree(): if tilt_five_xr_interface: tilt_five_xr_interface.application_id = T5ProjectSettings.application_id tilt_five_xr_interface.application_version = T5ProjectSettings.application_version + tilt_five_xr_interface.trigger_click_threshold = T5ProjectSettings.trigger_click_threshhold XRServer.add_interface(tilt_five_xr_interface) tilt_five_xr_interface.glasses_event.connect(_on_glasses_event) diff --git a/example/addons/tiltfive/T5ProjectSettings.gd b/example/addons/tiltfive/T5ProjectSettings.gd index 6fe1f40..2875bf4 100644 --- a/example/addons/tiltfive/T5ProjectSettings.gd +++ b/example/addons/tiltfive/T5ProjectSettings.gd @@ -30,6 +30,7 @@ static func setup_properties(): _define_project_setting("xr/tilt_five/application_id", TYPE_STRING, PROPERTY_HINT_NONE, "", "my.game.com") _define_project_setting("xr/tilt_five/application_version", TYPE_STRING, PROPERTY_HINT_NONE, "", "0.1.0") _define_project_setting("xr/tilt_five/default_display_name", TYPE_STRING, PROPERTY_HINT_NONE, "", "Game: Player One") + _define_project_setting("xr/tilt_five/trigger_click_threshhold", TYPE_FLOAT, PROPERTY_HINT_RANGE, "0,1,0.01", 0.3) _initialized = true static var application_id : String: @@ -47,3 +48,7 @@ static var default_display_name : String: setup_properties() return ProjectSettings.get_setting_with_override("xr/tilt_five/default_display_name") +static var trigger_click_threshhold : float: + get: + setup_properties() + return ProjectSettings.get_setting_with_override("xr/tilt_five/trigger_click_threshhold") diff --git a/example/pointer_control.gd b/example/pointer_control.gd index 25ce18d..1916a07 100644 --- a/example/pointer_control.gd +++ b/example/pointer_control.gd @@ -17,6 +17,7 @@ const WAND_BUTTON_1 := "button_1" const WAND_BUTTON_2 := "button_2" const WAND_BUTTON_STICK := "button_3" const WAND_BUTTON_T5 := "button_t5" +const WAND_BUTTON_TRIGGER := "trigger_click" # Axis const WAND_ANALOG_STICK := "stick" const WAND_ANALOG_TRIGGER := "trigger" @@ -34,6 +35,7 @@ func _ready(): $Controls/Three.material_override = unselected_mat $Controls/T5.material_override = unselected_mat $Controls/Trigger.material_override = unselected_mat + $Controls/TriggerClick.material_override = unselected_mat # Called every frame. 'delta' is the elapsed time since the previous frame. @@ -62,6 +64,8 @@ func _on_button_pressed(button): $Controls/Three.material_override = selected_mat WAND_BUTTON_T5: $Controls/T5.material_override = selected_mat + WAND_BUTTON_TRIGGER: + $Controls/TriggerClick.material_override = selected_mat func _on_button_released(button): @@ -82,3 +86,5 @@ func _on_button_released(button): $Controls/Three.material_override = unselected_mat WAND_BUTTON_T5: $Controls/T5.material_override = unselected_mat + WAND_BUTTON_TRIGGER: + $Controls/TriggerClick.material_override = unselected_mat diff --git a/extension/src/GodotT5Glasses.cpp b/extension/src/GodotT5Glasses.cpp index fb87887..dc5fe08 100644 --- a/extension/src/GodotT5Glasses.cpp +++ b/extension/src/GodotT5Glasses.cpp @@ -193,6 +193,13 @@ void GodotT5Glasses::update_wand(size_t wand_idx) { Vector2 stick; get_wand_stick(wand_idx, stick.x, stick.y); tracker->set_input("stick", Variant(stick)); + + if(trigger_value > _trigger_click_threshold + g_trigger_hysteresis_range) { + tracker->set_input("trigger_click", Variant(true)); + } + else if(trigger_value < (_trigger_click_threshold - g_trigger_hysteresis_range)) { + tracker->set_input("trigger_click", Variant(false)); + } } if(is_wand_state_set(wand_idx, WandState::BUTTONS_VALID)) { WandButtons buttons; diff --git a/extension/src/GodotT5Glasses.h b/extension/src/GodotT5Glasses.h index 9f141a3..ec0b0f2 100644 --- a/extension/src/GodotT5Glasses.h +++ b/extension/src/GodotT5Glasses.h @@ -20,6 +20,7 @@ using T5Integration::Glasses; namespace GodotT5Integration { constexpr int g_swap_chain_length = 3; + constexpr float g_trigger_hysteresis_range = 0.002; // Sort of arbitrary assume 8 bit DAC +/-(1/256)/2 class GodotT5Service; @@ -47,6 +48,8 @@ namespace GodotT5Integration { StringName get_wand_tracker_name(size_t wand_idx); + void set_trigger_click_threshold(float threshold); + protected: virtual void on_glasses_reserved() override; virtual void on_glasses_released() override; @@ -61,6 +64,8 @@ namespace GodotT5Integration { Ref _head; std::vector> _wand_trackers; + + float _trigger_click_threshold; }; inline bool GodotT5Glasses::is_reserved() { @@ -73,6 +78,10 @@ namespace GodotT5Integration { return _wand_trackers[wand_idx]->get_tracker_name(); } + inline void GodotT5Glasses::set_trigger_click_threshold(float threshold) { + _trigger_click_threshold = threshold; + } + } diff --git a/extension/src/TiltFiveXRInterface.cpp b/extension/src/TiltFiveXRInterface.cpp index 7688c20..626b5b2 100644 --- a/extension/src/TiltFiveXRInterface.cpp +++ b/extension/src/TiltFiveXRInterface.cpp @@ -30,6 +30,10 @@ void TiltFiveXRInterface::_bind_methods() { ClassDB::bind_method(D_METHOD("get_application_version"), &TiltFiveXRInterface::get_application_version); ADD_PROPERTY(PropertyInfo(Variant::STRING, "application_version"), "set_application_version", "get_application_version"); + ClassDB::bind_method(D_METHOD("set_trigger_click_threshold", "threshold"), &TiltFiveXRInterface::set_trigger_click_threshold); + ClassDB::bind_method(D_METHOD("get_trigger_click_threshold"), &TiltFiveXRInterface::get_trigger_click_threshold); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "trigger_click_threshold"), "set_trigger_click_threshold", "get_trigger_click_threshold"); + // Signals. ADD_SIGNAL(MethodInfo("service_event", PropertyInfo(Variant::INT, "event"))); ADD_SIGNAL(MethodInfo("glasses_event", PropertyInfo(Variant::STRING, "glasses_id"), PropertyInfo(Variant::INT, "event"))); @@ -72,6 +76,20 @@ void TiltFiveXRInterface::set_application_version(const String &p_string) { application_version = p_string; } +float TiltFiveXRInterface::get_trigger_click_threshold() { + return _trigger_click_threshold; +} + +void TiltFiveXRInterface::set_trigger_click_threshold(float threshold) { + _trigger_click_threshold = threshold; + + for(auto& entry : _glasses_index) { + if(!entry.glasses.expired()) { + entry.glasses.lock()->set_trigger_click_threshold(_trigger_click_threshold); + } + } +} + TiltFiveXRInterface::GlassesIndexEntry* TiltFiveXRInterface::lookup_glasses_entry(StringName glasses_id) { for(auto& entry : _glasses_index) { if(glasses_id == entry.id) { @@ -436,6 +454,8 @@ void TiltFiveXRInterface::_process() { } _glasses_index.resize(glasses_idx + 1); auto glasses = t5_service->get_glasses(glasses_idx); + glasses->set_trigger_click_threshold(_trigger_click_threshold); + _glasses_index[glasses_idx].glasses = glasses; _glasses_index[glasses_idx].id = glasses->get_id().c_str(); _glasses_index[glasses_idx].idx = glasses_idx; diff --git a/extension/src/TiltFiveXRInterface.h b/extension/src/TiltFiveXRInterface.h index 0d67193..02e4078 100644 --- a/extension/src/TiltFiveXRInterface.h +++ b/extension/src/TiltFiveXRInterface.h @@ -82,6 +82,9 @@ class TiltFiveXRInterface : public XRInterfaceExtension { String get_application_version() const; void set_application_version(const String &p_string); + float get_trigger_click_threshold(); + void set_trigger_click_threshold(float threshold); + // Functions. void reserve_glasses(const StringName glasses_id, const String display_name); @@ -150,6 +153,7 @@ class TiltFiveXRInterface : public XRInterfaceExtension { String application_id; String application_version; + float _trigger_click_threshold = 0.5; std::vector _glasses_index; std::vector _glasses_events;