mirror of
https://github.com/CrispyPin/ovr-utils.git
synced 2024-11-10 02:40:25 +01:00
separate grabbing and cursor functionality to optional sub-nodes
This commit is contained in:
parent
3c216bf885
commit
3999f68b5d
7 changed files with 146 additions and 98 deletions
|
@ -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://overlays/FPSOverlay.tscn" type="PackedScene" id=2]
|
||||
[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/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://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"]
|
||||
|
||||
|
@ -25,13 +27,12 @@ overlay_scene = ExtResource( 5 )
|
|||
|
||||
[node name="OverlayInteraction" parent="OverlayInstance UI demo" instance=ExtResource( 8 )]
|
||||
|
||||
[node name="head" parent="OverlayInstance UI demo/OverlayInteraction/VR" index="2"]
|
||||
current = true
|
||||
[node name="OverlayCursor" parent="OverlayInstance UI demo/OverlayInteraction" instance=ExtResource( 9 )]
|
||||
|
||||
[node name="OverlayGrab" parent="OverlayInstance UI demo/OverlayInteraction" instance=ExtResource( 6 )]
|
||||
|
||||
[node name="XYZ Widget" parent="." instance=ExtResource( 4 )]
|
||||
|
||||
[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 )
|
||||
overlay_scene = ExtResource( 7 )
|
||||
|
||||
[editable path="OverlayInstance UI demo/OverlayInteraction"]
|
||||
|
|
6
ovr-utils/addons/openvr_overlay/OverlayCursor.tscn
Normal file
6
ovr-utils/addons/openvr_overlay/OverlayCursor.tscn
Normal 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 )
|
6
ovr-utils/addons/openvr_overlay/OverlayGrab.tscn
Normal file
6
ovr-utils/addons/openvr_overlay/OverlayGrab.tscn
Normal 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 )
|
|
@ -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)
|
||||
|
64
ovr-utils/addons/openvr_overlay/interaction/overlay_grab.gd
Normal file
64
ovr-utils/addons/openvr_overlay/interaction/overlay_grab.gd
Normal 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
|
|
@ -12,17 +12,11 @@ var _trigger_state = false
|
|||
var _active_controller: String = ""
|
||||
# reference to the area node thats used for touch
|
||||
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 _left_is_activator = false
|
||||
var _right_is_activator := false
|
||||
var _left_is_activator := false
|
||||
|
||||
var is_moving := 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
|
||||
var pause_triggers := false
|
||||
|
||||
|
||||
onready var viewport: Viewport = get_node("../OverlayViewport")
|
||||
onready var tracker_nodes = {
|
||||
"head": $VR/head,
|
||||
"left": $VR/left,
|
||||
|
@ -32,7 +26,6 @@ onready var tracker_nodes = {
|
|||
|
||||
|
||||
func _ready() -> void:
|
||||
viewport.add_child(_cursor_node)
|
||||
|
||||
add_child(_overlay_area)
|
||||
_overlay_area.connect("body_entered", self, "_on_OverlayArea_entered")
|
||||
|
@ -47,107 +40,28 @@ func _ready() -> void:
|
|||
_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):
|
||||
if _touch_state:
|
||||
_active_controller = controller
|
||||
_trigger_state = true
|
||||
_send_click_event(true)
|
||||
emit_signal("trigger_on")
|
||||
|
||||
|
||||
func _trigger_off():
|
||||
_trigger_state = false
|
||||
_send_click_event(false)
|
||||
emit_signal("trigger_off")
|
||||
|
||||
|
||||
func _on_OverlayArea_entered(body: Node) -> void:
|
||||
if body.get_node("../../..") != self or is_moving:
|
||||
if body.get_node("../../..") != self or pause_triggers:
|
||||
return
|
||||
_touch_state = true
|
||||
if not is_moving:
|
||||
_active_controller = body.get_parent().name
|
||||
emit_signal("touch_on")
|
||||
|
||||
|
||||
func _on_OverlayArea_exited(body: Node) -> void:
|
||||
if body.get_node("../../..") != self or is_moving:
|
||||
if body.get_node("../../..") != self or pause_triggers:
|
||||
return
|
||||
# TEMP
|
||||
_active_controller = "" # TODO revert to other controller if both were touching (edge case)
|
||||
|
|
|
@ -3,13 +3,15 @@ extends Control
|
|||
var ihandler
|
||||
var oinst
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
oinst = get_viewport().get_parent()
|
||||
ihandler = get_viewport().get_node("../OverlayInteraction")
|
||||
ihandler = get_viewport().get_node("../OverlayInteraction/OverlayGrab")
|
||||
for t in oinst.TARGETS:
|
||||
$OptionButton.add_item(t)
|
||||
$OptionButton.selected = oinst.TARGETS.find(oinst.target)
|
||||
|
||||
|
||||
func _on_DragButton_button_down() -> void:
|
||||
ihandler.begin_move()
|
||||
|
||||
|
|
Loading…
Reference in a new issue