add support for 2 cursors

This commit is contained in:
Crispy 2021-10-16 00:47:29 +02:00
parent dc6a9929b7
commit 20bf94d7ab
10 changed files with 131 additions and 81 deletions

View file

@ -1,7 +1,6 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=2 format=2]
[ext_resource path="res://addons/openvr_overlay/interaction/HandCollider.tscn" type="PackedScene" id=1]
[ext_resource path="res://addons/openvr_overlay/interaction/TouchCollider.tscn" type="PackedScene" id=2]
[node name="OverlayInteractionRoot" type="Node"]
@ -12,14 +11,7 @@
[node name="HandCollider" parent="VR/left" groups=[
"left",
] instance=ExtResource( 1 )]
collision_layer = 4
collision_mask = 4
[node name="TouchCollider" parent="VR/left" groups=[
"left",
] instance=ExtResource( 2 )]
collision_layer = 16
collision_mask = 16
collision_layer = 20
[node name="right" type="ARVRController" parent="VR"]
controller_id = 2
@ -27,14 +19,7 @@ controller_id = 2
[node name="HandCollider" parent="VR/right" groups=[
"right",
] instance=ExtResource( 1 )]
collision_layer = 2
collision_mask = 2
[node name="TouchCollider" parent="VR/right" groups=[
"right",
] instance=ExtResource( 2 )]
collision_layer = 8
collision_mask = 8
collision_layer = 10
[node name="head" type="ARVRCamera" parent="VR"]
visible = false

View file

@ -2,9 +2,12 @@
[sub_resource type="SphereShape" id=1]
margin = 0.001
radius = 0.03
radius = 0.01
[node name="HandCollider" type="StaticBody"]
[node name="HandCollider" type="Area"]
monitoring = false
collision_layer = 30
collision_mask = 0
[node name="Collision" type="CollisionShape" parent="."]
shape = SubResource( 1 )

View file

@ -1,12 +1,17 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=3 format=2]
[sub_resource type="BoxShape" id=1]
resource_local_to_scene = true
extents = Vector3( 0.2, 0.2, 0.1 )
[sub_resource type="BoxShape" id=2]
resource_local_to_scene = true
extents = Vector3( 0.2, 0.2, 0.01 )
[node name="OverlayArea" type="Spatial"]
[node name="AreaNear" type="Area" parent="."]
monitorable = false
collision_layer = 0
collision_mask = 6
@ -14,8 +19,9 @@ collision_mask = 6
shape = SubResource( 1 )
[node name="AreaTouch" type="Area" parent="."]
monitorable = false
collision_layer = 0
collision_mask = 24
[node name="CollisionShape" type="CollisionShape" parent="AreaTouch"]
shape = SubResource( 1 )
shape = SubResource( 2 )

View file

@ -1,10 +0,0 @@
[gd_scene load_steps=2 format=2]
[sub_resource type="SphereShape" id=1]
margin = 0.001
radius = 0.0
[node name="TouchCollider" type="StaticBody"]
[node name="Collision" type="CollisionShape" parent="."]
shape = SubResource( 1 )

View file

@ -1,39 +1,53 @@
extends Node
var cursor_node = preload("res://addons/openvr_overlay/interaction/Cursor.tscn").instance()
#var cursor_node = preload("res://addons/openvr_overlay/interaction/Cursor.tscn").instance()
onready var viewport: Viewport = get_node("../../OverlayViewport")
onready var _i = get_parent()
var curr_pos: Vector2
var prev_pos := Vector2(-1000, 1000)
var cursor_pos := {
"right": Vector2(),
"left": Vector2(),
"prev": {
"right": Vector2(),
"left": Vector2(),
}
}
var click := {
"right": false,
"left": false,
}
var cursor_nodes := {
"right": preload("res://addons/openvr_overlay/interaction/Cursor.tscn").instance(),
"left": preload("res://addons/openvr_overlay/interaction/Cursor.tscn").instance(),
}
func _ready() -> void:
viewport.add_child(cursor_node)
viewport.add_child(cursor_nodes.right)
viewport.add_child(cursor_nodes.left)
get_parent().connect("trigger_on", self, "_trigger_on")
get_parent().connect("trigger_off", self, "_trigger_off")
func _process(_delta: float) -> void:
curr_pos = get_canvas_pos()
_update_cursor()
cursor_pos.right= get_canvas_pos("right")
cursor_pos.left= get_canvas_pos("left")
_update_cursors()
_send_move_event()
prev_pos = curr_pos
cursor_pos.prev.right = cursor_pos.right
cursor_pos.prev.left = cursor_pos.left
#get canvas position of active controller
func get_canvas_pos() -> Vector2:
var active
if _i.state.right.near:
active = "right"
elif _i.state.left.near:
active = "left"
else:
return Vector2(-1000, 1000) # offscreen
#if not (_i.state.right.near or _i.state.left.near):
func get_canvas_pos(controller) -> Vector2:
#if not _i.state[controller].near:
# return Vector2(-100, 100) # offscreen
var controller_local_pos = _i._overlay_area.global_transform.xform_inv(\
_i.tracker_nodes[active].translation)
_i.tracker_nodes[controller].translation)
var pos = Vector2(controller_local_pos.x, controller_local_pos.y)
var overlay_size = Vector2(2048, 2048)
@ -46,31 +60,46 @@ func get_canvas_pos() -> Vector2:
return pos
func _update_cursor():
cursor_node.rect_position = get_canvas_pos()
func _update_cursors():
cursor_nodes.right.visible = _i.state.right.near
cursor_nodes.left.visible = _i.state.left.near
cursor_nodes.right.rect_position = cursor_pos.right
cursor_nodes.left.rect_position = cursor_pos.left
func _send_move_event():
var active: String
if click.right:
active = "right"
elif click.left:
active = "left"
else:
return# only send move events while a cursor is held down
var event = InputEventMouseMotion.new()
event.position = prev_pos
event.relative = curr_pos - prev_pos
event.position = cursor_pos.prev[active]
event.relative = cursor_pos[active] - cursor_pos.prev[active]
event.speed = event.relative
viewport.input(event)
func _send_click_event(state: bool):
func _send_click_event(state: bool, controller: String):
if click[controller] == state:
return # already in that state
click[controller] = state
var click_event = InputEventMouseButton.new()
click_event.position = curr_pos
click_event.position = cursor_pos[controller]
click_event.pressed = state
click_event.button_index = 1
viewport.input(click_event)
func _trigger_on(controller):
_send_click_event(true)
if click.right or click.left:
return
_send_click_event(true, controller)
func _trigger_off(controller):
_send_click_event(false)
_send_click_event(false, controller)

View file

@ -44,10 +44,10 @@ onready var tracker_nodes = {
func _ready() -> void:
#add_child(_overlay_area)
_overlay_area = $OverlayArea
_overlay_area.get_node("AreaNear").connect("body_entered", self, "_on_Near_entered")
_overlay_area.get_node("AreaNear").connect("body_exited", self, "_on_Near_exited")
_overlay_area.get_node("AreaTouch").connect("body_entered", self, "_on_Touch_entered")
_overlay_area.get_node("AreaTouch").connect("body_exited", self, "_on_Touch_exited")
_overlay_area.get_node("AreaNear"). connect("area_entered", self, "_on_Near_entered")
_overlay_area.get_node("AreaNear"). connect("area_exited", self, "_on_Near_exited")
_overlay_area.get_node("AreaTouch").connect("area_entered", self, "_on_Touch_entered")
_overlay_area.get_node("AreaTouch").connect("area_exited", self, "_on_Touch_exited")
get_parent().connect("width_changed", self, "_update_width")
get_parent().connect("offset_changed", self, "_update_offset")
@ -125,7 +125,10 @@ func set_grab_mode(state: bool) -> void:
func _update_width():
var ratio = OverlayInit.ovr_interface.get_render_targetsize()
var extents = get_parent().width_meters * 0.5
_overlay_area.get_node("AreaNear/CollisionShape").shape.set_extents(Vector3(extents, extents * ratio.y/ratio.x, 0.05))
_overlay_area.get_node("AreaNear/CollisionShape").shape.set_extents(
Vector3(extents, extents * ratio.y/ratio.x, 0.1))
_overlay_area.get_node("AreaTouch/CollisionShape").shape.set_extents(
Vector3(extents, extents * ratio.y/ratio.x, 0.01))
func _update_offset():

View file

@ -13,8 +13,7 @@ export (String, "head", "left", "right", "world") var target = "left" setget se
export var width_meters := 0.4 setget set_width_in_meters
export var alpha := 1.0 setget set_alpha
#export var add_grabbing := true # add grabbing module
#export var add_cursor := false # add cursor module
var _tracker_id := 0
var _offsets:Dictionary = {
@ -46,23 +45,10 @@ func _ready() -> void:
$VROverlayViewport.size = OverlayInit.ovr_interface.get_render_targetsize()
set_notify_transform(true)
# if add_cursor:
# add_cursor()
# if add_grabbing:
# add_grab()
update_tracker_id()
call_deferred("update_offset")
#func add_cursor():
# interaction_handler.add_child(load("res://addons/openvr_overlay/OverlayCursor.tscn").instance())
#func add_grab():
# interaction_handler.add_child(load("res://addons/openvr_overlay/OverlayGrab.tscn").instance())
func update_tracker_id():
_tracker_id = -1
match current_target:
@ -163,7 +149,6 @@ func set_path(new: String) -> void:
container.add_child(overlay_scene)
func set_alpha(val: float):
alpha = val
$VROverlayViewport/TextureRect.modulate.a = val

19
src/overlays/Cross.tscn Normal file
View file

@ -0,0 +1,19 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://addons/openvr_overlay/interaction/cursor.png" type="Texture" id=1]
[node name="Cursor" type="TextureRect"]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -128.0
margin_top = -128.0
margin_right = 128.0
margin_bottom = 128.0
mouse_filter = 2
texture = ExtResource( 1 )
expand = true
__meta__ = {
"_edit_use_anchors_": false
}

View file

@ -1,8 +1,9 @@
[gd_scene load_steps=9 format=2]
[gd_scene load_steps=10 format=2]
[ext_resource path="res://overlay_scripts/ui_demo.gd" type="Script" id=1]
[ext_resource path="res://fonts/OpenSans-Regular.ttf" type="DynamicFontData" id=2]
[ext_resource path="res://fonts/OpenSans-Bold.ttf" type="DynamicFontData" id=3]
[ext_resource path="res://styles/menu.theme" type="Theme" id=4]
[sub_resource type="DynamicFont" id=1]
size = 100
@ -29,6 +30,7 @@ font_data = ExtResource( 3 )
[node name="Control" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
theme = ExtResource( 4 )
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
@ -64,6 +66,34 @@ __meta__ = {
"_edit_use_anchors_": false
}
[node name="Button" type="Button" parent="."]
margin_left = 80.8171
margin_top = 242.452
margin_right = 481.817
margin_bottom = 433.452
text = "aaaaa"
[node name="Button2" type="Button" parent="."]
margin_left = 80.8171
margin_top = 433.452
margin_right = 481.817
margin_bottom = 624.452
text = "EEEEE"
[node name="Button3" type="Button" parent="."]
margin_left = 650.0
margin_top = 242.452
margin_right = 1051.0
margin_bottom = 433.452
text = "aaaaa"
[node name="Button4" type="Button" parent="."]
margin_left = 650.0
margin_top = 433.452
margin_right = 1051.0
margin_bottom = 624.452
text = "EEEEE"
[connection signal="item_selected" from="OptionButton" to="." method="_on_OptionButton_item_selected"]
[connection signal="button_down" from="DragButton" to="." method="_on_DragButton_button_down"]
[connection signal="button_up" from="DragButton" to="." method="_on_DragButton_button_up"]

View file

@ -49,7 +49,7 @@ singletons=[ "res://addons/godot-openvr/godot_openvr.gdnlib" ]
[physics]
common/enable_pause_aware_picking=true
3d/active_soft_world=false
[rendering]