52 lines
1.1 KiB
C#
52 lines
1.1 KiB
C#
using Godot;
|
|
using Newlon;
|
|
using System;
|
|
|
|
public partial class GamepadHandler : Node
|
|
{
|
|
public static GamepadHandler Instance { get; private set; }
|
|
public int CurrentDevice { get; private set; } = 0;
|
|
public bool IsGamepadControlled => focused && controlled;
|
|
private bool focused;
|
|
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)
|
|
{
|
|
SetControlled(false);
|
|
}
|
|
}
|
|
private void SetControlled(bool to)
|
|
{
|
|
controlled = to;
|
|
if (controlled)
|
|
{
|
|
Cursor.Mode = Cursor.CursorMode.Gamepad;
|
|
}
|
|
else
|
|
{
|
|
Cursor.Mode = Cursor.CursorMode.Mouse;
|
|
}
|
|
}
|
|
|
|
}
|