Skip to content

Commit

Permalink
Fixed mouse grabbing in HTML5 build
Browse files Browse the repository at this point in the history
  • Loading branch information
MenacingMecha committed Jun 7, 2022
1 parent 1a4686c commit c86748f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
17 changes: 10 additions & 7 deletions player/mouse_grabber.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export var _should_grab_on_ready := true
# Useful for triggering callbacks, such as automatically displaying a pause screen when the player alt-tabs.
export var _should_stay_released_on_refocus := true

var is_mouse_grabbed := false setget set_mouse_grabbed

var is_mouse_grabbed := false setget set_mouse_grabbed, get_mouse_grabbed

func _ready():
# setgets aren't called with onready
self.is_mouse_grabbed = _should_grab_on_ready
if self._should_grab_on_ready and not OS.get_name() == "HTML5":
self.is_mouse_grabbed = true


func _notification(what: int):
Expand All @@ -29,13 +29,12 @@ func _notification(what: int):
self.is_mouse_grabbed = false


# TODO: refactor to use _unhandled_input
func _input(_event: InputEvent):
if Input.is_mouse_button_pressed(BUTTON_LEFT) and not is_mouse_grabbed:
func _unhandled_input(_event: InputEvent):
if Input.is_mouse_button_pressed(BUTTON_LEFT) and not self.is_mouse_grabbed:
self.is_mouse_grabbed = true
get_tree().set_input_as_handled()

elif Input.is_action_just_pressed(_release_action_name) and is_mouse_grabbed:
elif Input.is_action_just_pressed(self._release_action_name) and self.is_mouse_grabbed:
self.is_mouse_grabbed = false
get_tree().set_input_as_handled()

Expand All @@ -46,6 +45,10 @@ func set_mouse_grabbed(is_grabbed: bool):
_update_mouse_mode(is_grabbed)


func get_mouse_grabbed() -> bool:
return Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED


# Stub this to avoid side-effects in testing
func _update_mouse_mode(is_grabbed: bool):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED if is_grabbed else Input.MOUSE_MODE_VISIBLE)
12 changes: 1 addition & 11 deletions player/player_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extends KinematicBody

const CameraViewbob := preload("camera_viewbob.gd")
const InputDirection := preload("input_direction.gd")
const MouseGrabber := preload("mouse_grabber.gd")

const MAX_SPEED := 10
const MOVE_ACCEL := 4.5
Expand All @@ -19,20 +18,15 @@ var velocity := Vector3.ZERO
var _turn_amount := 0.0 # TODO: This is re-initialized every tick - delete if possible
var _camera_turned_this_update := false # TODO: can we eliminate this state?

onready var can_capture_mouse_motion := ($MouseGrabber as MouseGrabber).is_mouse_grabbed setget set_can_capture_mouse_motion
onready var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
onready var _camera := $RotationHelper/Camera as Camera
onready var _camera_viewbob := $RotationHelper/Camera as CameraViewbob
onready var _rotation_helper := $RotationHelper as Spatial
onready var _input_direction := $InputDirection as InputDirection


func _ready():
($MouseGrabber as MouseGrabber).connect("mouse_grabbed", self, "set_can_capture_mouse_motion")


func _unhandled_input(event: InputEvent):
if self.can_capture_mouse_motion and event is InputEventMouseMotion:
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and event is InputEventMouseMotion:
_turn_camera(-(event as InputEventMouseMotion).relative * self.mouse_look_sensitivity)


Expand Down Expand Up @@ -62,10 +56,6 @@ func _physics_process(delta: float):
_camera_turned_this_update = false


func set_can_capture_mouse_motion(new_state: bool):
can_capture_mouse_motion = new_state


func get_rotation_helper_x_rotation() -> float:
return self._rotation_helper.rotation_degrees.x

Expand Down

0 comments on commit c86748f

Please sign in to comment.