From e3f6db7ae503b588d4a6780d28355f9845b63447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Wed, 17 Apr 2024 17:21:28 +0200 Subject: [PATCH] refactor: converted keycodes into an enum --- Assets/Scripts/Domain/CustomInput.cs | 34 +++++++++++++++++++ Assets/Scripts/Domain/Input/AObertaReader.cs | 2 +- Assets/Scripts/Domain/Input/CedillaReader.cs | 2 +- Assets/Scripts/Domain/Input/EObertaReader.cs | 2 +- Assets/Scripts/Domain/Input/GeminadaReader.cs | 2 +- Assets/Scripts/Domain/Input/IDieresiReader.cs | 2 +- Assets/Scripts/Domain/Input/IxReader.cs | 2 +- Assets/Scripts/Domain/Input/LlReader.cs | 2 +- Assets/Scripts/Domain/Input/NyReader.cs | 2 +- Assets/Scripts/Domain/Input/OObertaReader.cs | 2 +- Assets/Scripts/Domain/Input/SsReader.cs | 2 +- Assets/Scripts/Domain/Input/TgReader.cs | 2 +- Assets/Scripts/Domain/Input/TjReader.cs | 2 +- Assets/Scripts/Domain/Input/TxReader.cs | 2 +- Assets/Scripts/Domain/KeyHistory.cs | 17 ++++++---- 15 files changed, 58 insertions(+), 19 deletions(-) diff --git a/Assets/Scripts/Domain/CustomInput.cs b/Assets/Scripts/Domain/CustomInput.cs index 3e57876..337806b 100644 --- a/Assets/Scripts/Domain/CustomInput.cs +++ b/Assets/Scripts/Domain/CustomInput.cs @@ -3,6 +3,40 @@ using System.Linq; using Domain.Input; namespace Domain { + public enum VKeyCode { + A = 0x41, + B = 0x42, + C = 0x43, + D = 0x44, + E = 0x45, + F = 0x46, + G = 0x47, + H = 0x48, + I = 0x49, + J = 0x4A, + K = 0x4B, + L = 0x4C, + M = 0x4D, + N = 0x4E, + O = 0x4F, + P = 0x50, + Q = 0x51, + R = 0x52, + S = 0x53, + T = 0x54, + U = 0x55, + V = 0x56, + W = 0x57, + X = 0x58, + Y = 0x59, + Z = 0x5A, + Interpunct = 0x33, + Cedilla = 0xBF, + AccentOpen = 0xBA, + AccentClosed = 0xDE, + Shift = 0x10 + } + public class CustomInput { private readonly List _readers = new(); private readonly KeyHistory _history; diff --git a/Assets/Scripts/Domain/Input/AObertaReader.cs b/Assets/Scripts/Domain/Input/AObertaReader.cs index 53748c2..532077b 100644 --- a/Assets/Scripts/Domain/Input/AObertaReader.cs +++ b/Assets/Scripts/Domain/Input/AObertaReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class AObertaReader : InputReader { - public AObertaReader(KeyHistory history) : base(history, new List{ 0xBA, 0x41 }, 0x10, false) { } + public AObertaReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.AccentOpen, (int)VKeyCode.A }, (int)VKeyCode.Shift, false) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/CedillaReader.cs b/Assets/Scripts/Domain/Input/CedillaReader.cs index dd7aeae..64540c7 100644 --- a/Assets/Scripts/Domain/Input/CedillaReader.cs +++ b/Assets/Scripts/Domain/Input/CedillaReader.cs @@ -2,6 +2,6 @@ namespace Domain.Input { public class CedillaReader : InputReader { - public CedillaReader(KeyHistory history) : base(history, new List{ 0xBF }) { } + public CedillaReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.Cedilla }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/EObertaReader.cs b/Assets/Scripts/Domain/Input/EObertaReader.cs index a5a92b0..815b841 100644 --- a/Assets/Scripts/Domain/Input/EObertaReader.cs +++ b/Assets/Scripts/Domain/Input/EObertaReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class EObertaReader : InputReader { - public EObertaReader(KeyHistory history) : base(history, new List{ 0xBA, 0x45 }, 0x10, false) { } + public EObertaReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.AccentOpen, (int)VKeyCode.E }, (int)VKeyCode.Shift, false) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/GeminadaReader.cs b/Assets/Scripts/Domain/Input/GeminadaReader.cs index f4b3c4a..1cddb5e 100644 --- a/Assets/Scripts/Domain/Input/GeminadaReader.cs +++ b/Assets/Scripts/Domain/Input/GeminadaReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class GeminadaReader : InputReader { - public GeminadaReader(KeyHistory history) : base(history, new List{ 76, 0x33, 76 }) { } + public GeminadaReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.L, (int)VKeyCode.Interpunct, (int)VKeyCode.L }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/IDieresiReader.cs b/Assets/Scripts/Domain/Input/IDieresiReader.cs index 34c1c09..d7ec9a8 100644 --- a/Assets/Scripts/Domain/Input/IDieresiReader.cs +++ b/Assets/Scripts/Domain/Input/IDieresiReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class IDieresiReader : InputReader { - public IDieresiReader(KeyHistory history) : base(history, new List{ 0xDE, 0x49 }, 0x10, true) { } + public IDieresiReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.AccentClosed, (int)VKeyCode.I }, (int)VKeyCode.Shift, true) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/IxReader.cs b/Assets/Scripts/Domain/Input/IxReader.cs index 6c2e20e..ab2a428 100644 --- a/Assets/Scripts/Domain/Input/IxReader.cs +++ b/Assets/Scripts/Domain/Input/IxReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class IxReader : InputReader { - public IxReader(KeyHistory history) : base(history, new List{ 0x49, 0x58 }) { } + public IxReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.I, (int)VKeyCode.X }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/LlReader.cs b/Assets/Scripts/Domain/Input/LlReader.cs index 888bb3c..bac6464 100644 --- a/Assets/Scripts/Domain/Input/LlReader.cs +++ b/Assets/Scripts/Domain/Input/LlReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class LlReader : InputReader { - public LlReader(KeyHistory history) : base(history, new List{ 0x4C, 0x4C }) { } + public LlReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.L, (int)VKeyCode.L }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/NyReader.cs b/Assets/Scripts/Domain/Input/NyReader.cs index d4da479..9c771dc 100644 --- a/Assets/Scripts/Domain/Input/NyReader.cs +++ b/Assets/Scripts/Domain/Input/NyReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class NyReader : InputReader { - public NyReader(KeyHistory history) : base(history, new List{ 0x4E, 0x59 }) { } + public NyReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.N, (int)VKeyCode.Y }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/OObertaReader.cs b/Assets/Scripts/Domain/Input/OObertaReader.cs index 661a37c..0c460a1 100644 --- a/Assets/Scripts/Domain/Input/OObertaReader.cs +++ b/Assets/Scripts/Domain/Input/OObertaReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class OObertaReader : InputReader { - public OObertaReader(KeyHistory history) : base(history, new List{ 0xBA, 0x4F }, 0x10, false) { } + public OObertaReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.AccentOpen, (int)VKeyCode.O }, (int)VKeyCode.Shift, false) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/SsReader.cs b/Assets/Scripts/Domain/Input/SsReader.cs index 75cd350..58f970d 100644 --- a/Assets/Scripts/Domain/Input/SsReader.cs +++ b/Assets/Scripts/Domain/Input/SsReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class SsReader : InputReader { - public SsReader(KeyHistory history) : base(history, new List{ 0x53, 0x53 }) { } + public SsReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.S, (int)VKeyCode.S }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/TgReader.cs b/Assets/Scripts/Domain/Input/TgReader.cs index 83f2026..5ce7622 100644 --- a/Assets/Scripts/Domain/Input/TgReader.cs +++ b/Assets/Scripts/Domain/Input/TgReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class TgReader : InputReader { - public TgReader(KeyHistory history) : base(history, new List{ 0x54, 0x47 }) { } + public TgReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.T, (int)VKeyCode.G }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/TjReader.cs b/Assets/Scripts/Domain/Input/TjReader.cs index 29a343d..08eb4fe 100644 --- a/Assets/Scripts/Domain/Input/TjReader.cs +++ b/Assets/Scripts/Domain/Input/TjReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class TjReader : InputReader { - public TjReader(KeyHistory history) : base(history, new List{ 0x54, 0x4A }) { } + public TjReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.T, (int)VKeyCode.J }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/TxReader.cs b/Assets/Scripts/Domain/Input/TxReader.cs index 61e6674..3d91a89 100644 --- a/Assets/Scripts/Domain/Input/TxReader.cs +++ b/Assets/Scripts/Domain/Input/TxReader.cs @@ -3,6 +3,6 @@ using System.Linq; namespace Domain.Input { public class TxReader : InputReader { - public TxReader(KeyHistory history) : base(history, new List{ 0x54, 0x58 }) { } + public TxReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.T, (int)VKeyCode.X }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/KeyHistory.cs b/Assets/Scripts/Domain/KeyHistory.cs index 17966d0..af1b2ff 100644 --- a/Assets/Scripts/Domain/KeyHistory.cs +++ b/Assets/Scripts/Domain/KeyHistory.cs @@ -11,8 +11,13 @@ namespace Domain { private const int CustomKeysSize = 5; private const int SpecialKeysSize = 1; - private readonly int[] _customKeys = { 191, 51, 222, 186 }; - private readonly int[] _specialKeys = { 0x10 }; + private readonly VKeyCode[] _customKeys = { + VKeyCode.Cedilla, + VKeyCode.Interpunct, + VKeyCode.AccentClosed, + VKeyCode.AccentOpen + }; + private readonly VKeyCode[] _specialKeys = { VKeyCode.Shift }; private readonly bool[] _isPressed = new bool[AlphabetSize + CustomKeysSize + SpecialKeysSize]; private readonly bool[] _wasPressed = new bool[AlphabetSize + CustomKeysSize + SpecialKeysSize]; @@ -41,11 +46,11 @@ namespace Domain { for (int i = 0; i < _customKeys.Length; i++) { int pressIndex = AlphabetSize + i; _wasPressed[pressIndex] = _isPressed[pressIndex]; - short state = Win32API.GetAsyncKeyState(_customKeys[i]); + short state = Win32API.GetAsyncKeyState((int)_customKeys[i]); if (!_wasPressed[pressIndex] && state != 0) { _isPressed[pressIndex] = true; - KeyPressed(_customKeys[i]); + KeyPressed((int)_customKeys[i]); }else if (_isPressed[pressIndex] && state == 0) { _isPressed[pressIndex] = false; } @@ -54,11 +59,11 @@ namespace Domain { for (int i = 0; i < _specialKeys.Length; i++) { int pressIndex = AlphabetSize + CustomKeysSize + i; _wasPressed[pressIndex] = _isPressed[pressIndex]; - short state = Win32API.GetAsyncKeyState(_specialKeys[i]); + short state = Win32API.GetAsyncKeyState((int)_specialKeys[i]); if (!_wasPressed[pressIndex] && state != 0) { _isPressed[pressIndex] = true; - KeyPressed(_specialKeys[i], true); + KeyPressed((int)_specialKeys[i], true); }else if (_isPressed[pressIndex] && state == 0) { _isPressed[pressIndex] = false; }