From 8cffc43f23f210f21f80c5005ae71924803dc83b Mon Sep 17 00:00:00 2001 From: Syntriax Date: Mon, 22 Jan 2024 23:38:37 +0300 Subject: [PATCH] BREAKING CHANGE: Removed MonoGame Package --- Engine.Input/Abstract/IButtonInputs.cs | 14 +++ Engine.Input/Abstract/IKeyboardInputs.cs | 16 ---- Engine.Input/Engine.Input.csproj | 4 - Engine.Input/KeyboardInputsBehaviour.cs | 104 ----------------------- 4 files changed, 14 insertions(+), 124 deletions(-) create mode 100644 Engine.Input/Abstract/IButtonInputs.cs delete mode 100644 Engine.Input/Abstract/IKeyboardInputs.cs delete mode 100644 Engine.Input/KeyboardInputsBehaviour.cs diff --git a/Engine.Input/Abstract/IButtonInputs.cs b/Engine.Input/Abstract/IButtonInputs.cs new file mode 100644 index 0000000..ab67252 --- /dev/null +++ b/Engine.Input/Abstract/IButtonInputs.cs @@ -0,0 +1,14 @@ +using Syntriax.Engine.Core.Abstract; + +namespace Syntriax.Engine.Input; + +public interface IButtonInputs : IAssignableStateEnable +{ + void RegisterOnPress(T button, Action, T> callback); + void UnregisterOnPress(T button, Action, T> callback); + void RegisterOnRelease(T button, Action, T> callback); + void UnregisterOnRelease(T button, Action, T> callback); + + bool IsPressed(T button); + bool WasPressed(T button); +} diff --git a/Engine.Input/Abstract/IKeyboardInputs.cs b/Engine.Input/Abstract/IKeyboardInputs.cs deleted file mode 100644 index 27897c8..0000000 --- a/Engine.Input/Abstract/IKeyboardInputs.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.Xna.Framework.Input; - -using Syntriax.Engine.Core.Abstract; - -namespace Syntriax.Engine.Input; - -public interface IKeyboardInputs : IAssignableStateEnable -{ - void RegisterOnPress(Keys key, Action callback); - void UnregisterOnPress(Keys key, Action callback); - void RegisterOnRelease(Keys key, Action callback); - void UnregisterOnRelease(Keys key, Action callback); - - bool IsPressed(Keys key); - bool WasPressed(Keys key); -} diff --git a/Engine.Input/Engine.Input.csproj b/Engine.Input/Engine.Input.csproj index fc29107..2ecbd5e 100644 --- a/Engine.Input/Engine.Input.csproj +++ b/Engine.Input/Engine.Input.csproj @@ -6,10 +6,6 @@ enable - - - - diff --git a/Engine.Input/KeyboardInputsBehaviour.cs b/Engine.Input/KeyboardInputsBehaviour.cs deleted file mode 100644 index 514214a..0000000 --- a/Engine.Input/KeyboardInputsBehaviour.cs +++ /dev/null @@ -1,104 +0,0 @@ -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Input; - -using Syntriax.Engine.Core; - -namespace Syntriax.Engine.Input; - -public class KeyboardInputsBehaviour : BehaviourOverride, IKeyboardInputs -{ - private readonly Dictionary> OnPressed = new(256); - private readonly Dictionary> 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 void RegisterOnPress(Keys key, Action callback) - { - if (OnPressed.TryGetValue(key, out var action)) - { - action += callback; - return; - } - - OnPressed.Add(key, callback); - } - - public void UnregisterOnPress(Keys key, Action callback) - { - if (OnPressed.TryGetValue(key, out var action)) - action -= callback; - } - - public void RegisterOnRelease(Keys key, Action callback) - { - if (OnReleased.TryGetValue(key, out var action)) - { - action += callback; - return; - } - - OnReleased.Add(key, callback); - } - - public void UnregisterOnRelease(Keys key, Action 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]; - - if (!OnPressed.TryGetValue(currentlyPressedKey, out var action)) - continue; - - if (WasPressed(currentlyPressedKey)) - continue; - - action.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); - } - - 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; - } -}