feat: add InputDeviceManager autoload for input device tracking
This commit is contained in:
50
Autoloads/InputDeviceManager.cs
Normal file
50
Autoloads/InputDeviceManager.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
public enum InputDevice { Mouse, Keyboard, Gamepad }
|
||||
|
||||
public partial class InputDeviceManager : Node
|
||||
{
|
||||
public static InputDeviceManager Instance { get; private set; }
|
||||
|
||||
public InputDevice CurrentDevice { get; private set; } = InputDevice.Mouse;
|
||||
|
||||
public bool IsPointerless => CurrentDevice is InputDevice.Keyboard or InputDevice.Gamepad;
|
||||
|
||||
[Signal] public delegate void DeviceChangedEventHandler(int device);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Instance = this;
|
||||
Input.MouseMode = Input.MouseModeEnum.Visible;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (Instance == this) Instance = null;
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
InputDevice detected;
|
||||
|
||||
if (@event is InputEventMouseMotion or InputEventMouseButton)
|
||||
detected = InputDevice.Mouse;
|
||||
else if (@event is InputEventKey)
|
||||
detected = InputDevice.Keyboard;
|
||||
else if (@event is InputEventJoypadButton or InputEventJoypadMotion)
|
||||
detected = InputDevice.Gamepad;
|
||||
else
|
||||
return;
|
||||
|
||||
if (detected == CurrentDevice) return;
|
||||
|
||||
CurrentDevice = detected;
|
||||
Input.MouseMode = CurrentDevice == InputDevice.Mouse
|
||||
? Input.MouseModeEnum.Visible
|
||||
: Input.MouseModeEnum.Hidden;
|
||||
|
||||
EmitSignal(SignalName.DeviceChanged, (int)CurrentDevice);
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ AudioController="*res://objects/audio_controller.tscn"
|
||||
UIManager="*res://Autoloads/UIManager.cs"
|
||||
ConfigFileHandler="*res://Autoloads/ConfigFileHandler.cs"
|
||||
SettingsManager="*res://Autoloads/SettingsManager.cs"
|
||||
InputDeviceManager="*res://Autoloads/InputDeviceManager.cs"
|
||||
SaveSystem="*res://Autoloads/SaveSystem.cs"
|
||||
ConsoleManager="*res://Autoloads/ConsoleManager.cs"
|
||||
LimboConsole="*uid://dyxornv8vwibg"
|
||||
|
||||
Reference in New Issue
Block a user