separate grabbing and cursor functionality to optional sub-nodes

This commit is contained in:
Crispy 2021-05-26 21:39:47 +02:00
parent 3c216bf885
commit 3999f68b5d
7 changed files with 146 additions and 98 deletions

View file

@ -1,12 +1,14 @@
[gd_scene load_steps=8 format=2] [gd_scene load_steps=10 format=2]
[ext_resource path="res://addons/openvr_overlay/OverlayInstance.tscn" type="PackedScene" id=1] [ext_resource path="res://addons/openvr_overlay/OverlayInstance.tscn" type="PackedScene" id=1]
[ext_resource path="res://overlays/FPSOverlay.tscn" type="PackedScene" id=2] [ext_resource path="res://overlays/FPSOverlay.tscn" type="PackedScene" id=2]
[ext_resource path="res://overlays/TimeOverlay.tscn" type="PackedScene" id=3] [ext_resource path="res://overlays/TimeOverlay.tscn" type="PackedScene" id=3]
[ext_resource path="res://overlays/xyz_widget/XYZ Widget.tscn" type="PackedScene" id=4] [ext_resource path="res://overlays/xyz_widget/XYZ Widget.tscn" type="PackedScene" id=4]
[ext_resource path="res://overlays/UI_demo.tscn" type="PackedScene" id=5] [ext_resource path="res://overlays/UI_demo.tscn" type="PackedScene" id=5]
[ext_resource path="res://addons/openvr_overlay/OverlayGrab.tscn" type="PackedScene" id=6]
[ext_resource path="res://overlays/BatteryOverlay.tscn" type="PackedScene" id=7] [ext_resource path="res://overlays/BatteryOverlay.tscn" type="PackedScene" id=7]
[ext_resource path="res://addons/openvr_overlay/OverlayInteraction.tscn" type="PackedScene" id=8] [ext_resource path="res://addons/openvr_overlay/OverlayInteraction.tscn" type="PackedScene" id=8]
[ext_resource path="res://addons/openvr_overlay/OverlayCursor.tscn" type="PackedScene" id=9]
[node name="Overlays" type="Node"] [node name="Overlays" type="Node"]
@ -25,13 +27,12 @@ overlay_scene = ExtResource( 5 )
[node name="OverlayInteraction" parent="OverlayInstance UI demo" instance=ExtResource( 8 )] [node name="OverlayInteraction" parent="OverlayInstance UI demo" instance=ExtResource( 8 )]
[node name="head" parent="OverlayInstance UI demo/OverlayInteraction/VR" index="2"] [node name="OverlayCursor" parent="OverlayInstance UI demo/OverlayInteraction" instance=ExtResource( 9 )]
current = true
[node name="OverlayGrab" parent="OverlayInstance UI demo/OverlayInteraction" instance=ExtResource( 6 )]
[node name="XYZ Widget" parent="." instance=ExtResource( 4 )] [node name="XYZ Widget" parent="." instance=ExtResource( 4 )]
[node name="OverlayInstance Battery" parent="." instance=ExtResource( 1 )] [node name="OverlayInstance Battery" parent="." instance=ExtResource( 1 )]
transform = Transform( -4.37114e-08, -0.707107, -0.707107, 0, -0.707107, 0.707107, -1, 3.09086e-08, 3.09086e-08, -0.12, 0, 0.1 ) transform = Transform( -4.37114e-08, -0.707107, -0.707107, 0, -0.707107, 0.707107, -1, 3.09086e-08, 3.09086e-08, -0.12, 0, 0.1 )
overlay_scene = ExtResource( 7 ) overlay_scene = ExtResource( 7 )
[editable path="OverlayInstance UI demo/OverlayInteraction"]

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://addons/openvr_overlay/interaction/overlay_cursor.gd" type="Script" id=1]
[node name="OverlayCursor" type="Node"]
script = ExtResource( 1 )

View file

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://addons/openvr_overlay/interaction/overlay_grab.gd" type="Script" id=1]
[node name="OverlayGrab" type="Node"]
script = ExtResource( 1 )

View file

@ -0,0 +1,55 @@
extends Node
var _cursor_node = preload("res://addons/openvr_overlay/interaction/Cursor.tscn").instance()
onready var viewport: Viewport = get_node("../../OverlayViewport")
onready var _i = get_parent()
func _ready() -> void:
viewport.add_child(_cursor_node)
get_parent().connect("trigger_on", self, "_trigger_on")
get_parent().connect("trigger_off", self, "_trigger_off")
func _process(_delta: float) -> void:
_update_cursor()
#get canvas position of active controller
func get_canvas_pos() -> Vector2:
if _i._active_controller == "":
return Vector2(-1000, 1000) # offscreen
var controller_local_pos = _i._overlay_area.global_transform.xform_inv(\
_i.tracker_nodes[_i._active_controller].translation)
var pos = Vector2(controller_local_pos.x, controller_local_pos.y)
var overlay_size = OverlayInit.ovr_interface.get_render_targetsize()
# scale to pixels
pos *= overlay_size.x
pos /= _i.get_parent().width_meters
# adjust to center
pos.y *= -1
pos += overlay_size * 0.5
return pos
func _update_cursor():
_cursor_node.rect_position = get_canvas_pos()
func _send_click_event(state: bool):
var click_event = InputEventMouseButton.new()
click_event.position = get_canvas_pos()
click_event.pressed = state
click_event.button_index = 1
viewport.input(click_event)
func _trigger_on():
_send_click_event(true)
func _trigger_off():
_send_click_event(false)

View file

@ -0,0 +1,64 @@
extends Node
var _pre_move_target = ""# what offset is being changed
var _mover_hand_name = ""# left or right, which hand is grabbing (/active controller)
var _mover_hand_offsets = {"pos": Vector3(), "rot": Quat()} # original offsets for grabbing hand
onready var _i = get_parent()
onready var tracker_nodes = {
"head": $"../VR/head",
"left": $"../VR/left",
"right": $"../VR/right",
"world": $"../VR"
}
onready var _overlay_area = _i._overlay_area
onready var _overlay = get_node("../..")
func _ready() -> void:
pass
func begin_move():
if not _i._active_controller:
print("Could not begin moving overlay, no controller active. ", _overlay.name)
return
_i.pause_triggers = true
# store current states to revert after move
_pre_move_target = _overlay.current_target
_mover_hand_name = _i._active_controller
_mover_hand_offsets = _overlay.get_offset_dict(_mover_hand_name)
# calculate offsets from active controller to overlay
var controller_t = tracker_nodes[_i._active_controller].transform
var overlay_t = _i._overlay_area.global_transform
var new_pos = controller_t.xform_inv(overlay_t.origin)
var new_rot = Quat(controller_t.basis).inverse() * Quat(overlay_t.basis)
_overlay.set_offset(_mover_hand_name, new_pos, new_rot)
_overlay.current_target = _mover_hand_name
func finish_move():
# calculate and apply the new offsets
var new_target_t = tracker_nodes[_pre_move_target].transform
var ovelay_t = _overlay_area.global_transform
var new_pos = new_target_t.xform_inv(ovelay_t.origin)
var new_rot = Quat(new_target_t.basis).inverse() * Quat(ovelay_t.basis)
_overlay.set_offset(_pre_move_target, new_pos, new_rot)
# revert the grabbing hands offsets in case it's used as a fallback
_overlay.set_offset_dict(_mover_hand_name, _mover_hand_offsets)
# reset current_target (parent handles fallback)
_overlay.update_current_target()
_overlay.save_settings()
_i._update_target()
_i.pause_triggers = false

View file

@ -12,17 +12,11 @@ var _trigger_state = false
var _active_controller: String = "" var _active_controller: String = ""
# reference to the area node thats used for touch # reference to the area node thats used for touch
var _overlay_area = preload("res://addons/openvr_overlay/interaction/OverlayArea.tscn").instance() var _overlay_area = preload("res://addons/openvr_overlay/interaction/OverlayArea.tscn").instance()
var _cursor_node = preload("res://addons/openvr_overlay/interaction/Cursor.tscn").instance() var _right_is_activator := false
var _right_is_activator = false var _left_is_activator := false
var _left_is_activator = false
var is_moving := false var pause_triggers := false
var _pre_move_target = ""# what offset is being changed
var _mover_hand_name = ""# left or right, which hand is grabbing (/active controller)
var _mover_hand_offsets = {"pos": Vector3(), "rot": Quat()} # original offsets for grabbing hand
onready var viewport: Viewport = get_node("../OverlayViewport")
onready var tracker_nodes = { onready var tracker_nodes = {
"head": $VR/head, "head": $VR/head,
"left": $VR/left, "left": $VR/left,
@ -32,7 +26,6 @@ onready var tracker_nodes = {
func _ready() -> void: func _ready() -> void:
viewport.add_child(_cursor_node)
add_child(_overlay_area) add_child(_overlay_area)
_overlay_area.connect("body_entered", self, "_on_OverlayArea_entered") _overlay_area.connect("body_entered", self, "_on_OverlayArea_entered")
@ -47,107 +40,28 @@ func _ready() -> void:
_update_target() _update_target()
func _process(_delta: float) -> void:
_update_cursor()
func begin_move():
if not _active_controller:
print("Could not begin moving overlay, no controller active. ", get_parent().name)
return
is_moving = true
# store current states to revert after move
_pre_move_target = get_parent().current_target
_mover_hand_name = _active_controller
_mover_hand_offsets = get_parent().get_offset_dict(_mover_hand_name)
# calculate offsets from active controller to overlay
var controller_t = tracker_nodes[_active_controller].transform
var overlay_t = _overlay_area.global_transform
var new_pos = controller_t.xform_inv(overlay_t.origin)
var new_rot = Quat(controller_t.basis).inverse() * Quat(overlay_t.basis)
get_parent().set_offset(_mover_hand_name, new_pos, new_rot)
get_parent().current_target = _mover_hand_name
func finish_move():
# calculate and apply the new offsets
var new_target_t = tracker_nodes[_pre_move_target].transform
var ovelay_t = _overlay_area.global_transform
var new_pos = new_target_t.xform_inv(ovelay_t.origin)
var new_rot = Quat(new_target_t.basis).inverse() * Quat(ovelay_t.basis)
get_parent().set_offset(_pre_move_target, new_pos, new_rot)
# revert the grabbing hands offsets in case it's used as a fallback
get_parent().set_offset_dict(_mover_hand_name, _mover_hand_offsets)
# reset current_target (parent handles fallback)
get_parent().update_current_target()
get_parent().save_settings()
_update_target()
is_moving = false
#get canvas position of active controller
func get_canvas_pos() -> Vector2:
if _active_controller == "":
return Vector2(-1000, 1000) # offscreen
var controller_local_pos = _overlay_area.global_transform.xform_inv(\
tracker_nodes[_active_controller].translation)
var pos = Vector2(controller_local_pos.x, controller_local_pos.y)
var overlay_size = OverlayInit.ovr_interface.get_render_targetsize()
# scale to pixels
pos *= overlay_size.x
pos /= get_parent().width_meters
# adjust to center
pos.y *= -1
pos += overlay_size * 0.5
return pos
func _update_cursor():
_cursor_node.rect_position = get_canvas_pos()
func _send_click_event(state: bool):
var click_event = InputEventMouseButton.new()
click_event.position = get_canvas_pos()
click_event.pressed = state
click_event.button_index = 1
viewport.input(click_event)
func _trigger_on(controller): func _trigger_on(controller):
if _touch_state: if _touch_state:
_active_controller = controller _active_controller = controller
_trigger_state = true _trigger_state = true
_send_click_event(true)
emit_signal("trigger_on") emit_signal("trigger_on")
func _trigger_off(): func _trigger_off():
_trigger_state = false _trigger_state = false
_send_click_event(false)
emit_signal("trigger_off") emit_signal("trigger_off")
func _on_OverlayArea_entered(body: Node) -> void: func _on_OverlayArea_entered(body: Node) -> void:
if body.get_node("../../..") != self or is_moving: if body.get_node("../../..") != self or pause_triggers:
return return
_touch_state = true _touch_state = true
if not is_moving:
_active_controller = body.get_parent().name _active_controller = body.get_parent().name
emit_signal("touch_on") emit_signal("touch_on")
func _on_OverlayArea_exited(body: Node) -> void: func _on_OverlayArea_exited(body: Node) -> void:
if body.get_node("../../..") != self or is_moving: if body.get_node("../../..") != self or pause_triggers:
return return
# TEMP # TEMP
_active_controller = "" # TODO revert to other controller if both were touching (edge case) _active_controller = "" # TODO revert to other controller if both were touching (edge case)

View file

@ -3,13 +3,15 @@ extends Control
var ihandler var ihandler
var oinst var oinst
func _ready() -> void: func _ready() -> void:
oinst = get_viewport().get_parent() oinst = get_viewport().get_parent()
ihandler = get_viewport().get_node("../OverlayInteraction") ihandler = get_viewport().get_node("../OverlayInteraction/OverlayGrab")
for t in oinst.TARGETS: for t in oinst.TARGETS:
$OptionButton.add_item(t) $OptionButton.add_item(t)
$OptionButton.selected = oinst.TARGETS.find(oinst.target) $OptionButton.selected = oinst.TARGETS.find(oinst.target)
func _on_DragButton_button_down() -> void: func _on_DragButton_button_down() -> void:
ihandler.begin_move() ihandler.begin_move()