MonoGameLibrary/MonoGameLibrary/Input/InputManager.cs

271 lines
No EOL
9.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Transactions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace MonoGameLibrary.Input;
public static class InputManager {
private static Dictionary<Keys, List<KeyboardInputEntry>> keyPressListeners = new();
private static Dictionary<Keys, List<KeyboardInputEntry>> keyHoldListeners = new(); // The bool is for enabling/disabling that method
private static Dictionary<Keys, List<KeyboardInputEntry>> keyReleaseListeners = new();
private static Dictionary<string, InputEntry> labelDict = new();
private static KeyboardState oldKBState = Keyboard.GetState();
private static KeyboardState newKBState;
public static KeyboardState KeyboardState => newKBState;
private static MouseState oldMouseState = Mouse.GetState();
private static MouseState newMouseState;
public static MouseState MouseState => newMouseState;
private static Dictionary<MouseButton, List<MouseInputEntry>> mousePressListeners = new();
private static Dictionary<MouseButton, List<MouseInputEntry>> mouseHoldListeners = new();
private static Dictionary<MouseButton, List<MouseInputEntry>> mouseReleaseListeners = new();
// public static ConcurrentQueue<Action> inputEventQueue = new();
public delegate void KeypressHandler();
public delegate void MouseHandler();
private static int nextUnnamedId = 0;
// public static void StartListening() {
// processInputThread.Start();
// Console.WriteLine("Started listening for input");
// }
public static void NextInputCycle() {
Queue<KeypressHandler> inputEventQueue = new();
// Read Keyboard
newKBState = Keyboard.GetState();
Keys[] pressed = newKBState.GetPressedKeys();
Keys[] pressedLastFrame = oldKBState.GetPressedKeys();
Array.ForEach(pressed, key => {
if (oldKBState.IsKeyUp(key)){
if (keyPressListeners.TryGetValue(key, out var pressHandlers)){
pressHandlers.ForEach(t => {
if (t.Enabled){
inputEventQueue.Enqueue(t.Action);
}
if (t.Hook.RemoveOnNextTrigger){
pressHandlers.Remove(t);
}
});
}
}
if (keyHoldListeners.TryGetValue(key, out var holdHandlers)){
holdHandlers.ForEach(t => {
if (t.Enabled){
inputEventQueue.Enqueue(t.Action);
}
if (t.Hook.RemoveOnNextTrigger){
holdHandlers.Remove(t);
}
});
}
});
Array.ForEach(pressedLastFrame, key => {
if (!keyReleaseListeners.ContainsKey(key)){
return;
}
if (newKBState.IsKeyUp(key)){
if (keyReleaseListeners.TryGetValue(key, out var releaseHandlers))
releaseHandlers.ForEach(t => {
if (t.Enabled){
inputEventQueue.Enqueue(t.Action);
}
if (t.Hook.RemoveOnNextTrigger){
releaseHandlers.Remove(t);
}
});
}
});
oldKBState = newKBState;
// Read Mouse
newMouseState = Mouse.GetState();
List<MouseInputEntry> mouseHandlers = new List<MouseInputEntry>();
foreach (var button in ((MouseButton type, ButtonState current, ButtonState old)[])[
(MouseButton.LEFT, newMouseState.LeftButton, oldMouseState.LeftButton),
(MouseButton.RIGHT, newMouseState.RightButton, oldMouseState.RightButton),
(MouseButton.MIDDLE, newMouseState.MiddleButton, oldMouseState.MiddleButton)
])
{
if (button.current == ButtonState.Pressed){
if (button.old == ButtonState.Released){
mousePressListeners.TryGetValue(button.type, out var pressHandlers);
mouseHandlers.AddRange(pressHandlers ?? new List<MouseInputEntry>());
}
mouseHoldListeners.TryGetValue(button.type, out var holdHandlers);
mouseHandlers.AddRange(holdHandlers ?? new List<MouseInputEntry>());
}
else if (button.old == ButtonState.Pressed){
if (button.current == ButtonState.Released){
mouseReleaseListeners.TryGetValue(button.type, out var releaseHandlers);
mouseHandlers.AddRange(releaseHandlers ?? new List<MouseInputEntry>());
}
}
}
mouseHandlers.ForEach(entry => inputEventQueue.Enqueue(entry.Action));
oldMouseState = newMouseState;
// Execute
foreach (KeypressHandler handler in inputEventQueue){
handler();
}
}
// private static Thread processInputThread = new(() => {
//
// });
public static void AddListener(string label, Keys key, KeypressHandler action, InputTiming timing, InputListenerHook hook) {
if (label.StartsWith("!")){
throw new ArgumentException("Label cannot start with !");
}
KeyboardInputEntry entry = new (label, key, action, timing, hook);
_addListenerKB(entry);
}
public static void AddListener(Keys key, KeypressHandler action, InputTiming timing, InputListenerHook hook) {
KeyboardInputEntry entry = new ($"!{Enum.GetName(typeof(Keys), key)}Handler{nextUnnamedId++}", key, action, timing, hook);
_addListenerKB(entry);
}
public static void AddListener(string label, MouseButton button, KeypressHandler action, InputTiming timing, InputListenerHook hook) {
if (label.StartsWith("!")){
throw new ArgumentException("Label cannot start with !");
}
MouseInputEntry entry = new (label, button, action, timing, hook);
_addListenerMouse(entry);
}
public static void AddListener(MouseButton button, KeypressHandler action, InputTiming timing,
InputListenerHook hook) {
MouseInputEntry entry = new ($"!{Enum.GetName(typeof(MouseButton), button)}Handler{nextUnnamedId++}", button, action, timing, hook);
_addListenerMouse(entry);
}
private static void _addListenerKB(KeyboardInputEntry entry) {
Dictionary<Keys, List<KeyboardInputEntry>> workingDict;
switch (entry.Timing){
case InputTiming.PRESS:
workingDict = keyPressListeners;
break;
case InputTiming.HOLD:
workingDict = keyHoldListeners;
break;
case InputTiming.RELEASE:
workingDict = keyReleaseListeners;
break;
default:
throw new ArgumentOutOfRangeException(nameof(entry.Timing), entry.Timing, null);
}
if (!workingDict.ContainsKey(entry.Key)){
workingDict.Add(entry.Key, new());
}
workingDict[entry.Key].Add(entry);
labelDict.Add(entry.Label, entry);
}
private static void _addListenerMouse(MouseInputEntry entry) {
Dictionary<MouseButton, List<MouseInputEntry>> workingDict;
switch (entry.Timing){
case InputTiming.PRESS:
workingDict = mousePressListeners;
break;
case InputTiming.HOLD:
workingDict = mouseHoldListeners;
break;
case InputTiming.RELEASE:
workingDict = mouseReleaseListeners;
break;
default:
throw new ArgumentOutOfRangeException(nameof(entry.Timing), entry.Timing, null);
}
if (!workingDict.ContainsKey(entry.Button)){
workingDict.Add(entry.Button, new());
}
workingDict[entry.Button].Add(entry);
labelDict.Add(entry.Label, entry);
}
public static InputEntry GetEntry(string label) {
return labelDict[label];
}
public abstract class InputEntry {
public string Label;
public KeypressHandler Action;
public InputTiming Timing;
public InputListenerHook Hook;
public bool Enabled => Hook.Enabled;
public InputEntry(string label, KeypressHandler action, InputTiming timing, InputListenerHook hook) {
Action = action;
Timing = timing;
Hook = hook;
Label = label;
}
}
public class KeyboardInputEntry : InputEntry {
public Keys Key;
public KeyboardInputEntry(string label, Keys key, KeypressHandler action, InputTiming timing, InputListenerHook hook) : base(label, action, timing, hook) {
Key = key;
}
}
public class MouseInputEntry : InputEntry {
public MouseButton Button;
public MouseInputEntry(string label, MouseButton button, KeypressHandler action, InputTiming timing, InputListenerHook hook) : base(label, action, timing, hook) {
Button = button;
}
}
public enum MouseButton {
RIGHT,
LEFT,
MIDDLE
}
// private static List<InputEntry> inputQueue = new();
}