full gamepad support

This commit is contained in:
Rendo 2025-07-28 18:03:26 +05:00
commit a57d79e84a
21 changed files with 213 additions and 46 deletions

View file

@ -4,48 +4,39 @@ using System;
public partial class GamepadHandler : Node
{
[Signal] public delegate void GamepadControlledEventHandler(bool isControlled);
public static GamepadHandler Instance { get; private set; }
public int CurrentDevice { get; private set; } = 0;
public bool IsGamepadControlled => focused && controlled;
private bool focused;
public bool IsGamepadControlled => controlled;
private bool controlled;
public override void _EnterTree()
{
Instance = this;
}
public override void _Notification(int what)
{
if (what == NotificationApplicationFocusIn)
{
focused = true;
}
if (what == NotificationApplicationFocusOut)
{
focused = false;
}
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventJoypadButton || @event is InputEventJoypadMotion)
{
SetControlled(true);
}
if ((@event is InputEventMouse && Input.GetVector("cursor_left", "cursor_right", "cursor_up", "cursor_down") == Vector2.Zero) || @event is InputEventKey)
else if (@event is InputEventMouseButton || @event is InputEventKey)
{
SetControlled(false);
}
}
private void SetControlled(bool to)
{
if (controlled == to) return;
controlled = to;
if (controlled)
{
Cursor.Mode = Cursor.CursorMode.Gamepad;
EmitSignal(SignalName.GamepadControlled, true);
}
else
{
Cursor.Mode = Cursor.CursorMode.Mouse;
EmitSignal(SignalName.GamepadControlled, false);
}
}