43 lines
1,009 B
C#
43 lines
1,009 B
C#
using Godot;
|
|
using Newlon;
|
|
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 => controlled;
|
|
private bool controlled;
|
|
public override void _EnterTree()
|
|
{
|
|
Instance = this;
|
|
}
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
if (@event is InputEventJoypadButton || @event is InputEventJoypadMotion)
|
|
{
|
|
SetControlled(true);
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|