Skip to content

Commit

Permalink
Merge pull request #24 from zibetnu/hide-mouse
Browse files Browse the repository at this point in the history
Hide and ignore mouse when detecting Joypad input
  • Loading branch information
zibetnu authored Oct 18, 2024
2 parents 9978d0f + ca67b62 commit 745ea20
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SceneChanger="*res://src/autoload/scene_changer/scene_changer.tscn"
FullscreenToggle="res://src/autoload/fullscreen_toggle/fullscreen_toggle.tscn"
SteamCallbacks="res://src/autoload/steam_callbacks.gd"
GlobalSounds="res://src/autoload/global_sounds/global_sounds.tscn"
MouseHider="res://src/autoload/mouse_hider.gd"

[debug]

Expand Down
17 changes: 17 additions & 0 deletions src/autoload/mouse_hider.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extends Node


const ACTIVE_MODE = Input.MOUSE_MODE_CONFINED
const INACTIVE_MODE = Input.MOUSE_MODE_HIDDEN


func _ready() -> void:
Input.set_mouse_mode(INACTIVE_MODE)


func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
Input.set_mouse_mode(ACTIVE_MODE)

elif event is InputEventJoypadButton or event is InputEventJoypadMotion:
Input.set_mouse_mode(INACTIVE_MODE)
46 changes: 22 additions & 24 deletions src/controller/keyboard_and_mouse/keyboard_and_mouse.gd
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
extends Controller


var _mouse_active := false
func _process(_delta: float) -> void:
_look_at_mouse_if_visible()
if _is_mouse_visible():
input_handled.emit()


func _unhandled_input(event: InputEvent) -> void:
func _unhandled_input(_event: InputEvent) -> void:
move_vector = Input.get_vector("move_left", "move_right", "move_up", "move_down")
if event is InputEventMouseMotion:
_mouse_active = true
$Timer.start()

if _mouse_active:
look_vector = Vector2.from_angle(
global_position.angle_to_point(get_global_mouse_position())
)

else:
look_vector = Vector2.ZERO

_look_at_mouse_if_visible()
button_1 = Input.is_action_pressed("button_1")
button_2 = Input.is_action_pressed("button_2")

Expand All @@ -26,18 +18,24 @@ func _unhandled_input(event: InputEvent) -> void:

func force_handle_input() -> void:
move_vector = Input.get_vector("move_left", "move_right", "move_up", "move_down")
if _mouse_active:
look_vector = Vector2.from_angle(
global_position.angle_to_point(get_global_mouse_position())
)

else:
look_vector = Vector2.ZERO

_look_at_mouse_if_visible()
button_1 = Input.is_action_pressed("button_1")
button_2 = Input.is_action_pressed("button_2")
input_handled.emit()


func _on_timer_timeout() -> void:
_mouse_active = false
func _is_mouse_visible() -> bool:
return not Input.get_mouse_mode() in [
Input.MOUSE_MODE_CONFINED_HIDDEN,
Input.MOUSE_MODE_HIDDEN
]


func _look_at_mouse_if_visible() -> void:
if not _is_mouse_visible():
look_vector = Vector2.ZERO
return

look_vector = Vector2.from_angle(
global_position.angle_to_point(get_global_mouse_position())
)
7 changes: 0 additions & 7 deletions src/controller/keyboard_and_mouse/keyboard_and_mouse.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,3 @@

[node name="KeyboardAndMouse" type="Node2D"]
script = ExtResource("1_vscdr")

[node name="Timer" type="Timer" parent="."]
process_callback = 0
wait_time = 3.0
one_shot = true

[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]

0 comments on commit 745ea20

Please sign in to comment.