2023-11-24 15:34:05 +03:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
|
|
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Input;
|
|
|
|
|
|
|
|
|
|
public class KeyboardInputsBehaviour : BehaviourOverride, IKeyboardInputs
|
|
|
|
|
{
|
2024-01-22 23:30:21 +03:00
|
|
|
|
private readonly Dictionary<Keys, Action<IKeyboardInputs, Keys>> OnPressed = new(256);
|
|
|
|
|
private readonly Dictionary<Keys, Action<IKeyboardInputs, Keys>> OnReleased = new(256);
|
2023-11-24 15:34:05 +03:00
|
|
|
|
|
|
|
|
|
private int cachePressedCurrentlyCount = 0;
|
2024-01-22 23:30:21 +03:00
|
|
|
|
private readonly Keys[] cachePressedCurrently = new Keys[256];
|
2023-11-24 15:34:05 +03:00
|
|
|
|
|
|
|
|
|
private int cachePressedPreviouslyCount = 0;
|
2024-01-22 23:30:21 +03:00
|
|
|
|
private readonly Keys[] cachePressedPreviously = new Keys[256];
|
2023-11-24 15:34:05 +03:00
|
|
|
|
|
|
|
|
|
public void RegisterOnPress(Keys key, Action<IKeyboardInputs, Keys> callback)
|
|
|
|
|
{
|
|
|
|
|
if (OnPressed.TryGetValue(key, out var action))
|
|
|
|
|
{
|
|
|
|
|
action += callback;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnPressed.Add(key, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnregisterOnPress(Keys key, Action<IKeyboardInputs, Keys> callback)
|
|
|
|
|
{
|
|
|
|
|
if (OnPressed.TryGetValue(key, out var action))
|
|
|
|
|
action -= callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RegisterOnRelease(Keys key, Action<IKeyboardInputs, Keys> callback)
|
|
|
|
|
{
|
|
|
|
|
if (OnReleased.TryGetValue(key, out var action))
|
|
|
|
|
{
|
|
|
|
|
action += callback;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnReleased.Add(key, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnregisterOnRelease(Keys key, Action<IKeyboardInputs, Keys> callback)
|
|
|
|
|
{
|
|
|
|
|
if (OnReleased.TryGetValue(key, out var action))
|
|
|
|
|
action -= callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnUpdate(GameTime time)
|
|
|
|
|
{
|
|
|
|
|
KeyboardState keyboardState = Keyboard.GetState();
|
|
|
|
|
keyboardState.GetPressedKeys(cachePressedCurrently);
|
|
|
|
|
cachePressedCurrentlyCount = keyboardState.GetPressedKeyCount();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < cachePressedCurrentlyCount; i++)
|
|
|
|
|
{
|
|
|
|
|
Keys currentlyPressedKey = cachePressedCurrently[i];
|
2023-11-24 16:18:55 +03:00
|
|
|
|
|
|
|
|
|
if (!OnPressed.TryGetValue(currentlyPressedKey, out var action))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (WasPressed(currentlyPressedKey))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
action.Invoke(this, currentlyPressedKey);
|
2023-11-24 15:34:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < cachePressedPreviouslyCount; i++)
|
|
|
|
|
{
|
|
|
|
|
Keys previouslyPressedKey = cachePressedPreviously[i];
|
2023-11-24 16:18:55 +03:00
|
|
|
|
|
|
|
|
|
if (!OnReleased.TryGetValue(previouslyPressedKey, out var action))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (IsPressed(previouslyPressedKey))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
action.Invoke(this, previouslyPressedKey);
|
2023-11-24 15:34:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Array.Copy(cachePressedCurrently, cachePressedPreviously, cachePressedCurrentlyCount);
|
|
|
|
|
cachePressedPreviouslyCount = cachePressedCurrentlyCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsPressed(Keys key)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < cachePressedCurrentlyCount; i++)
|
|
|
|
|
if (cachePressedCurrently[i] == key)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WasPressed(Keys key)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < cachePressedPreviouslyCount; i++)
|
|
|
|
|
if (cachePressedPreviously[i] == key)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|