From 1b1902cadd498cf67ba38c3d19558c5603b6bd4b Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Tue, 25 May 2021 20:11:06 +0200 Subject: [PATCH] apply fallbacks when tracker unavailable, cleanup --- .../addons/openvr_overlay/overlay_init.gd | 45 +++++++++++----- .../addons/openvr_overlay/overlay_instance.gd | 53 +++++++++++-------- ovr-utils/overlays/battery.gd | 12 ++--- 3 files changed, 68 insertions(+), 42 deletions(-) diff --git a/ovr-utils/addons/openvr_overlay/overlay_init.gd b/ovr-utils/addons/openvr_overlay/overlay_init.gd index 945477c..77572a6 100644 --- a/ovr-utils/addons/openvr_overlay/overlay_init.gd +++ b/ovr-utils/addons/openvr_overlay/overlay_init.gd @@ -3,8 +3,12 @@ extends Node var ovr_interface: ARVRInterface var ovr_config := preload("res://addons/godot-openvr/OpenVRConfig.gdns").new() -var left_id = 0 -var right_id = 0 +var trackers = { + "head": 0, + "left": -1, + "right": -1, + "world": 63, +} func _init() -> void: ovr_config = preload("res://addons/godot-openvr/OpenVRConfig.gdns").new() @@ -14,24 +18,39 @@ func _init() -> void: # Find the OpenVR interface and initialise it ovr_interface = ARVRServer.find_interface("OpenVR") if ovr_interface and ovr_interface.initialize(): - pass + print("OpenVR Initialized.") func _ready() -> void: - ARVRServer.connect("tracker_added", self, "_tracker_changed") - ARVRServer.connect("tracker_removed", self, "_tracker_changed") + ARVRServer.connect("tracker_added", self, "_tracker_added") + ARVRServer.connect("tracker_removed", self, "_tracker_removed") update_hand_ids() -func _tracker_changed(tracker_name: String, type: int, id: int): + +func _tracker_added(tracker_name: String, type: int, id: int): update_hand_ids() + +func _tracker_removed(tracker_name: String, type: int, id: int): + match id: + 1: + trackers.left = -1 + 2: + trackers.right = -1 + + func update_hand_ids(): + trackers.left = -1 + trackers.right = -1 for i in ARVRServer.get_tracker_count(): - var tracker = ARVRServer.get_tracker(i) -# print(tracker.get_name(), ": hand ", tracker.get_hand()) + var t = ARVRServer.get_tracker(i) + var tracking_id = get_id(t) + match t.get_hand(): + 1: + trackers.left = tracking_id + 2: + trackers.right = tracking_id - var tracking_id = tracker.get_name().split("_")[-1] - if tracker.get_hand() == 1: - left_id = tracking_id - elif tracker.get_hand() == 2: - right_id = tracking_id + +static func get_id(tracker) -> int: + return int(tracker.get_name().split("_")[-1]) diff --git a/ovr-utils/addons/openvr_overlay/overlay_instance.gd b/ovr-utils/addons/openvr_overlay/overlay_instance.gd index cddb2bf..d17bc87 100644 --- a/ovr-utils/addons/openvr_overlay/overlay_instance.gd +++ b/ovr-utils/addons/openvr_overlay/overlay_instance.gd @@ -22,6 +22,7 @@ var offsets:Dictionary = { # what's actually tracking var current_target: String = "world" setget _set_current_target# most of the time the actual target, but will fall back +var fallback = ["left", "right", "head"] var _tracker_id: int = 0 @@ -49,29 +50,32 @@ func _ready() -> void: func load_settings(): if Settings.s.overlays.has(name): - target = Settings.s.overlays[name].target - # TODO target fallback - update_current_target() + if Settings.s.overlays[name].has("fallback"): + fallback = Settings.s.overlays[name].fallback + set_target(Settings.s.overlays[name].target) + set_width_in_meters(Settings.s.overlays[name].width) + for t_key in Settings.s.overlays[name].offsets: var t_offset = Settings.s.overlays[name].offsets[t_key] offsets[t_key].pos = t_offset.pos offsets[t_key].rot = t_offset.rot update_offset() else: - #TEMP defaults + #TEMP defaults (remove when dragging any overlay is possible) offsets[current_target].pos = translation offsets[current_target].rot = transform.basis.get_rotation_quat() update_offset() #### Settings.s.overlays[name] = {} - save_settings() + save_settings() func save_settings(): Settings.s.overlays[name].target = target Settings.s.overlays[name].width = width_meters + Settings.s.overlays[name].fallback = fallback if not Settings.s.overlays[name].has("offsets"): Settings.s.overlays[name]["offsets"] = {} @@ -87,17 +91,18 @@ func save_settings(): func update_tracker_id() -> void: _tracker_id = -1 - if current_target in ["left", "right"]: # target is a controller - var target_id = TARGETS.find(current_target) + match current_target: + "left": + _tracker_id = OverlayInit.trackers.left + "right": + _tracker_id = OverlayInit.trackers.right + _: + return - for i in ARVRServer.get_tracker_count(): - var tracker = ARVRServer.get_tracker(i) - if tracker.get_hand() == target_id: - _tracker_id = int(tracker.get_name().split("_")[-1]) - - if _tracker_id == -1: - # could not find controller, overlay will revert to fallback - _tracker_id = 63 # highest tracker id (unused, at origin) + if _tracker_id == -1: + # could not find controller, fallback + print("Missing controller ", current_target, " ", target, " ", fallback, " - ", name) + _tracker_id = 63 # highest tracker id (unused, at origin) func update_offset() -> void: @@ -114,22 +119,24 @@ func update_offset() -> void: func update_current_target(): - _set_current_target(target) - # TODO fallback if not found + if OverlayInit.trackers[target] != -1: + _set_current_target(target) + else: # fallback if not found + for f in fallback: + if OverlayInit.trackers[f] != -1: + _set_current_target(f) + break + update_tracker_id() + func _tracker_changed(tracker_name: String, type: int, id: int): - update_tracker_id() + update_current_target() update_offset() -func get_tracker_id() -> int: - return _tracker_id - - func set_target(new: String): target = new - update_tracker_id() call_deferred("update_offset") update_current_target() diff --git a/ovr-utils/overlays/battery.gd b/ovr-utils/overlays/battery.gd index ff9d5f6..6dae1d1 100644 --- a/ovr-utils/overlays/battery.gd +++ b/ovr-utils/overlays/battery.gd @@ -14,16 +14,16 @@ func update_text(): var l = "??" var r = "??" - if OverlayInit.left_id: - l = OverlayInit.ovr_config.get_device_battery_percentage(OverlayInit.left_id) + if OverlayInit.trackers.left != -1: + l = OverlayInit.ovr_config.get_device_battery_percentage(OverlayInit.trackers.left) l = str(int(l * 100)) - if OverlayInit.ovr_config.is_device_charging(OverlayInit.left_id): + if OverlayInit.ovr_config.is_device_charging(OverlayInit.trackers.left): l += "+" - if OverlayInit.right_id: - r = OverlayInit.ovr_config.get_device_battery_percentage(OverlayInit.right_id) + if OverlayInit.trackers.right != -1: + r = OverlayInit.ovr_config.get_device_battery_percentage(OverlayInit.trackers.right) r = str(int(r * 100)) - if OverlayInit.ovr_config.is_device_charging(OverlayInit.right_id): + if OverlayInit.ovr_config.is_device_charging(OverlayInit.trackers.right): r += "+" text = "L: " + l + "% R: " + r + "%"