113 lines
3.5 KiB
C#
113 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using Syntriax.Engine.Core;
|
|
using Syntriax.Engine.Systems.Input;
|
|
|
|
namespace Pong.Platforms.Desktop;
|
|
|
|
public class KeyboardInputsBehaviour : Behaviour, IButtonInputs<Keys>
|
|
{
|
|
private readonly Dictionary<Keys, IButtonInputs<Keys>.ButtonCallbackEventHandler> OnPressed = new(256);
|
|
private readonly Dictionary<Keys, IButtonInputs<Keys>.ButtonCallbackEventHandler> OnReleased = new(256);
|
|
|
|
private int cachePressedCurrentlyCount = 0;
|
|
private readonly Keys[] cachePressedCurrently = new Keys[256];
|
|
|
|
private int cachePressedPreviouslyCount = 0;
|
|
private readonly Keys[] cachePressedPreviously = new Keys[256];
|
|
|
|
public event IButtonInputs<Keys>.ButtonCallbackEventHandler? OnAnyButtonPressed = null;
|
|
public event IButtonInputs<Keys>.ButtonCallbackEventHandler? OnAnyButtonReleased = null;
|
|
|
|
public void RegisterOnPress(Keys key, IButtonInputs<Keys>.ButtonCallbackEventHandler callback)
|
|
{
|
|
if (OnPressed.TryGetValue(key, out var action))
|
|
{
|
|
action += callback;
|
|
return;
|
|
}
|
|
|
|
OnPressed.Add(key, callback);
|
|
}
|
|
|
|
public void UnregisterOnPress(Keys key, IButtonInputs<Keys>.ButtonCallbackEventHandler callback)
|
|
{
|
|
if (OnPressed.TryGetValue(key, out var action))
|
|
action -= callback;
|
|
}
|
|
|
|
public void RegisterOnRelease(Keys key, IButtonInputs<Keys>.ButtonCallbackEventHandler callback)
|
|
{
|
|
if (OnReleased.TryGetValue(key, out var action))
|
|
{
|
|
action += callback;
|
|
return;
|
|
}
|
|
|
|
OnReleased.Add(key, callback);
|
|
}
|
|
|
|
public void UnregisterOnRelease(Keys key, IButtonInputs<Keys>.ButtonCallbackEventHandler callback)
|
|
{
|
|
if (OnReleased.TryGetValue(key, out var action))
|
|
action -= callback;
|
|
}
|
|
|
|
protected override void OnUpdate()
|
|
{
|
|
KeyboardState keyboardState = Keyboard.GetState();
|
|
keyboardState.GetPressedKeys(cachePressedCurrently);
|
|
cachePressedCurrentlyCount = keyboardState.GetPressedKeyCount();
|
|
|
|
for (int i = 0; i < cachePressedCurrentlyCount; i++)
|
|
{
|
|
Keys currentlyPressedKey = cachePressedCurrently[i];
|
|
|
|
if (!OnPressed.TryGetValue(currentlyPressedKey, out var action))
|
|
continue;
|
|
|
|
if (WasPressed(currentlyPressedKey))
|
|
continue;
|
|
|
|
action.Invoke(this, currentlyPressedKey);
|
|
OnAnyButtonPressed?.Invoke(this, currentlyPressedKey);
|
|
}
|
|
|
|
for (int i = 0; i < cachePressedPreviouslyCount; i++)
|
|
{
|
|
Keys previouslyPressedKey = cachePressedPreviously[i];
|
|
|
|
if (!OnReleased.TryGetValue(previouslyPressedKey, out var action))
|
|
continue;
|
|
|
|
if (IsPressed(previouslyPressedKey))
|
|
continue;
|
|
|
|
action.Invoke(this, previouslyPressedKey);
|
|
OnAnyButtonReleased?.Invoke(this, previouslyPressedKey);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|