apply fallbacks when tracker unavailable, cleanup

This commit is contained in:
Crispy 2021-05-25 20:11:06 +02:00
parent 70892dcb7b
commit 1b1902cadd
3 changed files with 68 additions and 42 deletions

View file

@ -3,8 +3,12 @@ extends Node
var ovr_interface: ARVRInterface var ovr_interface: ARVRInterface
var ovr_config := preload("res://addons/godot-openvr/OpenVRConfig.gdns").new() var ovr_config := preload("res://addons/godot-openvr/OpenVRConfig.gdns").new()
var left_id = 0 var trackers = {
var right_id = 0 "head": 0,
"left": -1,
"right": -1,
"world": 63,
}
func _init() -> void: func _init() -> void:
ovr_config = preload("res://addons/godot-openvr/OpenVRConfig.gdns").new() ovr_config = preload("res://addons/godot-openvr/OpenVRConfig.gdns").new()
@ -14,24 +18,39 @@ func _init() -> void:
# Find the OpenVR interface and initialise it # Find the OpenVR interface and initialise it
ovr_interface = ARVRServer.find_interface("OpenVR") ovr_interface = ARVRServer.find_interface("OpenVR")
if ovr_interface and ovr_interface.initialize(): if ovr_interface and ovr_interface.initialize():
pass print("OpenVR Initialized.")
func _ready() -> void: func _ready() -> void:
ARVRServer.connect("tracker_added", self, "_tracker_changed") ARVRServer.connect("tracker_added", self, "_tracker_added")
ARVRServer.connect("tracker_removed", self, "_tracker_changed") ARVRServer.connect("tracker_removed", self, "_tracker_removed")
update_hand_ids() 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() 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(): func update_hand_ids():
trackers.left = -1
trackers.right = -1
for i in ARVRServer.get_tracker_count(): for i in ARVRServer.get_tracker_count():
var tracker = ARVRServer.get_tracker(i) var t = ARVRServer.get_tracker(i)
# print(tracker.get_name(), ": hand ", tracker.get_hand()) 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: static func get_id(tracker) -> int:
left_id = tracking_id return int(tracker.get_name().split("_")[-1])
elif tracker.get_hand() == 2:
right_id = tracking_id

View file

@ -22,6 +22,7 @@ var offsets:Dictionary = {
# what's actually tracking # 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 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 var _tracker_id: int = 0
@ -49,29 +50,32 @@ func _ready() -> void:
func load_settings(): func load_settings():
if Settings.s.overlays.has(name): if Settings.s.overlays.has(name):
target = Settings.s.overlays[name].target if Settings.s.overlays[name].has("fallback"):
# TODO target fallback fallback = Settings.s.overlays[name].fallback
update_current_target() set_target(Settings.s.overlays[name].target)
set_width_in_meters(Settings.s.overlays[name].width) set_width_in_meters(Settings.s.overlays[name].width)
for t_key in Settings.s.overlays[name].offsets: for t_key in Settings.s.overlays[name].offsets:
var t_offset = Settings.s.overlays[name].offsets[t_key] var t_offset = Settings.s.overlays[name].offsets[t_key]
offsets[t_key].pos = t_offset.pos offsets[t_key].pos = t_offset.pos
offsets[t_key].rot = t_offset.rot offsets[t_key].rot = t_offset.rot
update_offset() update_offset()
else: else:
#TEMP defaults #TEMP defaults (remove when dragging any overlay is possible)
offsets[current_target].pos = translation offsets[current_target].pos = translation
offsets[current_target].rot = transform.basis.get_rotation_quat() offsets[current_target].rot = transform.basis.get_rotation_quat()
update_offset() update_offset()
#### ####
Settings.s.overlays[name] = {} Settings.s.overlays[name] = {}
save_settings() save_settings()
func save_settings(): func save_settings():
Settings.s.overlays[name].target = target Settings.s.overlays[name].target = target
Settings.s.overlays[name].width = width_meters Settings.s.overlays[name].width = width_meters
Settings.s.overlays[name].fallback = fallback
if not Settings.s.overlays[name].has("offsets"): if not Settings.s.overlays[name].has("offsets"):
Settings.s.overlays[name]["offsets"] = {} Settings.s.overlays[name]["offsets"] = {}
@ -87,17 +91,18 @@ func save_settings():
func update_tracker_id() -> void: func update_tracker_id() -> void:
_tracker_id = -1 _tracker_id = -1
if current_target in ["left", "right"]: # target is a controller match current_target:
var target_id = TARGETS.find(current_target) "left":
_tracker_id = OverlayInit.trackers.left
"right":
_tracker_id = OverlayInit.trackers.right
_:
return
for i in ARVRServer.get_tracker_count(): if _tracker_id == -1:
var tracker = ARVRServer.get_tracker(i) # could not find controller, fallback
if tracker.get_hand() == target_id: print("Missing controller ", current_target, " ", target, " ", fallback, " - ", name)
_tracker_id = int(tracker.get_name().split("_")[-1]) _tracker_id = 63 # highest tracker id (unused, at origin)
if _tracker_id == -1:
# could not find controller, overlay will revert to fallback
_tracker_id = 63 # highest tracker id (unused, at origin)
func update_offset() -> void: func update_offset() -> void:
@ -114,22 +119,24 @@ func update_offset() -> void:
func update_current_target(): func update_current_target():
_set_current_target(target) if OverlayInit.trackers[target] != -1:
# TODO fallback if not found _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): func _tracker_changed(tracker_name: String, type: int, id: int):
update_tracker_id() update_current_target()
update_offset() update_offset()
func get_tracker_id() -> int:
return _tracker_id
func set_target(new: String): func set_target(new: String):
target = new target = new
update_tracker_id()
call_deferred("update_offset") call_deferred("update_offset")
update_current_target() update_current_target()

View file

@ -14,16 +14,16 @@ func update_text():
var l = "??" var l = "??"
var r = "??" var r = "??"
if OverlayInit.left_id: if OverlayInit.trackers.left != -1:
l = OverlayInit.ovr_config.get_device_battery_percentage(OverlayInit.left_id) l = OverlayInit.ovr_config.get_device_battery_percentage(OverlayInit.trackers.left)
l = str(int(l * 100)) 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 += "+" l += "+"
if OverlayInit.right_id: if OverlayInit.trackers.right != -1:
r = OverlayInit.ovr_config.get_device_battery_percentage(OverlayInit.right_id) r = OverlayInit.ovr_config.get_device_battery_percentage(OverlayInit.trackers.right)
r = str(int(r * 100)) 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 += "+" r += "+"
text = "L: " + l + "% R: " + r + "%" text = "L: " + l + "% R: " + r + "%"