Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add double click on function_pointer #558

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions addons/godot-xr-tools/events/pointer_event.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ enum Type {
## Pointer pressed target
PRESSED,

## Pointer double click target
DOUBLECLICK,

## Pointer released target
RELEASED,

Expand Down Expand Up @@ -90,6 +93,19 @@ static func pressed(
at,
at))

## Report pointer doubleclick event
static func doubleclick(
pointer : Node3D,
target : Node3D,
at : Vector3) -> void:
report(
XRToolsPointerEvent.new(
Type.DOUBLECLICK,
pointer,
target,
at,
at))


## Report pointer released event
static func released(
Expand Down
32 changes: 31 additions & 1 deletion addons/godot-xr-tools/functions/function_pointer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ const SUPPRESS_MASK := 0b0000_0000_0100_0000_0000_0000_0000_0000
## Active button action
@export var active_button_action : String = "trigger_click"

## debounce time for double click
@export var debounce_active_button_time : float = 0.25

## Double Click action
@export var double_click_action : String = "ax_button"

@export_group("Laser")

## Controls when the laser is visible
Expand Down Expand Up @@ -122,6 +128,9 @@ var _controller : XRController3D
# The currently active controller
var _active_controller : XRController3D

# flag to manage double click in active button
var _debounce_active_button = false


## Add support for is_xr_class on XRTools classes
func is_xr_class(name : String) -> bool:
Expand Down Expand Up @@ -421,6 +430,13 @@ func _button_pressed() -> void:
last_collided_at = $RayCast.get_collision_point()
XRToolsPointerEvent.pressed(self, target, last_collided_at)

# Pointer-activation button pressed handler
func _button_doubleclick() -> void:
if $RayCast.is_colliding():
# Report doubleclick
target = $RayCast.get_collider()
last_collided_at = $RayCast.get_collision_point()
XRToolsPointerEvent.doubleclick(self, target, last_collided_at)

# Pointer-activation button released handler
func _button_released() -> void:
Expand All @@ -433,9 +449,23 @@ func _button_released() -> void:

# Button pressed handler
func _on_button_pressed(p_button : String, controller : XRController3D) -> void:
#detect if trigger or double click action
if p_button == active_button_action and enabled:
if controller == _active_controller:
_button_pressed()
if _debounce_active_button:
_button_doubleclick()
_debounce_active_button = false
else:
_button_pressed()
_debounce_active_button = true
await get_tree().create_timer(debounce_active_button_time).timeout
_debounce_active_button = false
else:
_active_controller = controller

if p_button == double_click_action and enabled:
if controller == _active_controller:
_button_doubleclick()
else:
_active_controller = controller

Expand Down
19 changes: 19 additions & 0 deletions addons/godot-xr-tools/objects/viewport_2d_in_3d_body.gd
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func _on_pointer_event(event : XRToolsPointerEvent) -> void:
_presses[pointer] = true
pressed = true

XRToolsPointerEvent.Type.DOUBLECLICK:
_presses[pointer] = true
pressed = true

XRToolsPointerEvent.Type.RELEASED:
_presses.erase(pointer)
pressed = false
Expand Down Expand Up @@ -130,6 +134,10 @@ func _on_pointer_event(event : XRToolsPointerEvent) -> void:
XRToolsPointerEvent.Type.MOVED:
_report_mouse_move(pressed, last, at)

XRToolsPointerEvent.Type.DOUBLECLICK:
_report_mouse_down(at)
_report_mouse_down_double_click(at)

# Clear pointer information on exit
if type == XRToolsPointerEvent.Type.EXITED:
# Clear pointer information
Expand Down Expand Up @@ -179,6 +187,17 @@ func _report_mouse_down(at : Vector2) -> void:
event.button_mask = 1
_viewport.push_input(event)

# Report mouse-down event
func _report_mouse_down_double_click(at : Vector2) -> void:
var event := InputEventMouseButton.new()
event.button_index = 1
event.pressed = true
event.position = at
event.global_position = at
event.button_mask = 1
event.double_click = true
_viewport.push_input(event)


# Report mouse-up event
func _report_mouse_up(at : Vector2) -> void:
Expand Down
Loading