diff --git a/Assets/Scripts/Domain/CustomInput.cs b/Assets/Scripts/Domain/CustomInput.cs index 337806b..3e57876 100644 --- a/Assets/Scripts/Domain/CustomInput.cs +++ b/Assets/Scripts/Domain/CustomInput.cs @@ -3,40 +3,6 @@ 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 532077b..e89c64b 100644 --- a/Assets/Scripts/Domain/Input/AObertaReader.cs +++ b/Assets/Scripts/Domain/Input/AObertaReader.cs @@ -3,6 +3,7 @@ using System.Linq; namespace Domain.Input { public class AObertaReader : InputReader { - public AObertaReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.AccentOpen, (int)VKeyCode.A }, (int)VKeyCode.Shift, false) { } + public AObertaReader(KeyHistory history) : base(history, new List { VKeyCode.AccentOpen, VKeyCode.A }, + 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 64540c7..3ff6a8e 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{ (int)VKeyCode.Cedilla }) { } + public CedillaReader(KeyHistory history) : base(history, new List { 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 815b841..4c7f3dd 100644 --- a/Assets/Scripts/Domain/Input/EObertaReader.cs +++ b/Assets/Scripts/Domain/Input/EObertaReader.cs @@ -3,6 +3,7 @@ using System.Linq; namespace Domain.Input { public class EObertaReader : InputReader { - public EObertaReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.AccentOpen, (int)VKeyCode.E }, (int)VKeyCode.Shift, false) { } + public EObertaReader(KeyHistory history) : base(history, new List { VKeyCode.AccentOpen, VKeyCode.E }, + 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 1cddb5e..7ccef46 100644 --- a/Assets/Scripts/Domain/Input/GeminadaReader.cs +++ b/Assets/Scripts/Domain/Input/GeminadaReader.cs @@ -3,6 +3,7 @@ using System.Linq; namespace Domain.Input { public class GeminadaReader : InputReader { - public GeminadaReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.L, (int)VKeyCode.Interpunct, (int)VKeyCode.L }) { } + public GeminadaReader(KeyHistory history) : base(history, + new List { VKeyCode.L, VKeyCode.Interpunct, 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 d7ec9a8..249d85b 100644 --- a/Assets/Scripts/Domain/Input/IDieresiReader.cs +++ b/Assets/Scripts/Domain/Input/IDieresiReader.cs @@ -3,6 +3,7 @@ using System.Linq; namespace Domain.Input { public class IDieresiReader : InputReader { - public IDieresiReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.AccentClosed, (int)VKeyCode.I }, (int)VKeyCode.Shift, true) { } + public IDieresiReader(KeyHistory history) : base(history, new List { VKeyCode.AccentClosed, VKeyCode.I }, + 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 ab2a428..1a5a33c 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{ (int)VKeyCode.I, (int)VKeyCode.X }) { } + public IxReader(KeyHistory history) : base(history, new List { VKeyCode.I, 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 bac6464..aa00f53 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{ (int)VKeyCode.L, (int)VKeyCode.L }) { } + public LlReader(KeyHistory history) : base(history, new List { VKeyCode.L, 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 9c771dc..7627028 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{ (int)VKeyCode.N, (int)VKeyCode.Y }) { } + public NyReader(KeyHistory history) : base(history, new List { VKeyCode.N, 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 0c460a1..6b7a026 100644 --- a/Assets/Scripts/Domain/Input/OObertaReader.cs +++ b/Assets/Scripts/Domain/Input/OObertaReader.cs @@ -3,6 +3,7 @@ using System.Linq; namespace Domain.Input { public class OObertaReader : InputReader { - public OObertaReader(KeyHistory history) : base(history, new List{ (int)VKeyCode.AccentOpen, (int)VKeyCode.O }, (int)VKeyCode.Shift, false) { } + public OObertaReader(KeyHistory history) : base(history, new List { VKeyCode.AccentOpen, VKeyCode.O }, + 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 58f970d..74d0a8a 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{ (int)VKeyCode.S, (int)VKeyCode.S }) { } + public SsReader(KeyHistory history) : base(history, new List { VKeyCode.S, 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 5ce7622..245b666 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{ (int)VKeyCode.T, (int)VKeyCode.G }) { } + public TgReader(KeyHistory history) : base(history, new List { VKeyCode.T, 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 08eb4fe..82e73fe 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{ (int)VKeyCode.T, (int)VKeyCode.J }) { } + public TjReader(KeyHistory history) : base(history, new List { VKeyCode.T, 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 3d91a89..44f606c 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{ (int)VKeyCode.T, (int)VKeyCode.X }) { } + public TxReader(KeyHistory history) : base(history, new List { VKeyCode.T, VKeyCode.X }) { } } } \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/VKeyCode.cs b/Assets/Scripts/Domain/Input/VKeyCode.cs new file mode 100644 index 0000000..a52ea95 --- /dev/null +++ b/Assets/Scripts/Domain/Input/VKeyCode.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Extensions; + +namespace Domain.Input { + public class VKeyCode : SmartEnum { + public static VKeyCode A => new("A", 0x41); + public static VKeyCode B => new("B", 0x42); + public static VKeyCode C => new("C", 0x43); + public static VKeyCode D => new("D", 0x44); + public static VKeyCode E => new("E", 0x45); + public static VKeyCode F => new("F", 0x46); + public static VKeyCode G => new("G", 0x47); + public static VKeyCode H => new("H", 0x48); + public static VKeyCode I => new("I", 0x49); + public static VKeyCode J => new("J", 0x4A); + public static VKeyCode K => new("K", 0x4B); + public static VKeyCode L => new("L", 0x4C); + public static VKeyCode M => new("M", 0x4D); + public static VKeyCode N => new("N", 0x4E); + public static VKeyCode O => new("O", 0x4F); + public static VKeyCode P => new("P", 0x50); + public static VKeyCode Q => new("Q", 0x51); + public static VKeyCode R => new("R", 0x52); + public static VKeyCode S => new("S", 0x53); + public static VKeyCode T => new("T", 0x54); + public static VKeyCode U => new("U", 0x55); + public static VKeyCode V => new("V", 0x56); + public static VKeyCode W => new("W", 0x57); + public static VKeyCode X => new("X", 0x58); + public static VKeyCode Y => new("Y", 0x59); + public static VKeyCode Z => new("Z", 0x5A); + public static VKeyCode Interpunct => new("Interpunct", 0x33); + public static VKeyCode Cedilla => new("Cedilla", 0xBF); + public static VKeyCode AccentOpen => new("Accent Open", 0xBA); + public static VKeyCode AccentClosed => new("Accent Closed", 0xDE); + public static VKeyCode Shift => new("Shift", 0x10); + + private VKeyCode(string name, int value) : base(name, value) { } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/VKeyCode.cs.meta b/Assets/Scripts/Domain/Input/VKeyCode.cs.meta new file mode 100644 index 0000000..c2afd07 --- /dev/null +++ b/Assets/Scripts/Domain/Input/VKeyCode.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 26d2a6386e9140e9a9afb79b32140e3b +timeCreated: 1713367740 \ No newline at end of file diff --git a/Assets/Scripts/Domain/KeyHistory.cs b/Assets/Scripts/Domain/KeyHistory.cs index af1b2ff..33ce2ae 100644 --- a/Assets/Scripts/Domain/KeyHistory.cs +++ b/Assets/Scripts/Domain/KeyHistory.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Domain.Input; using Extensions; using UnityEngine; @@ -8,16 +9,16 @@ namespace Domain { private readonly LimitedSizeList _lastPressesWithSpecial = new(20); private const int AlphabetSize = 26; - private const int CustomKeysSize = 5; + private const int CustomKeysSize = 4; private const int SpecialKeysSize = 1; - private readonly VKeyCode[] _customKeys = { + private readonly int[] _customKeys = { VKeyCode.Cedilla, VKeyCode.Interpunct, VKeyCode.AccentClosed, VKeyCode.AccentOpen }; - private readonly VKeyCode[] _specialKeys = { VKeyCode.Shift }; + private readonly int[] _specialKeys = { VKeyCode.Shift }; private readonly bool[] _isPressed = new bool[AlphabetSize + CustomKeysSize + SpecialKeysSize]; private readonly bool[] _wasPressed = new bool[AlphabetSize + CustomKeysSize + SpecialKeysSize]; @@ -46,11 +47,11 @@ namespace Domain { for (int i = 0; i < _customKeys.Length; i++) { int pressIndex = AlphabetSize + i; _wasPressed[pressIndex] = _isPressed[pressIndex]; - short state = Win32API.GetAsyncKeyState((int)_customKeys[i]); + short state = Win32API.GetAsyncKeyState(_customKeys[i]); if (!_wasPressed[pressIndex] && state != 0) { _isPressed[pressIndex] = true; - KeyPressed((int)_customKeys[i]); + KeyPressed(_customKeys[i]); }else if (_isPressed[pressIndex] && state == 0) { _isPressed[pressIndex] = false; } @@ -59,11 +60,11 @@ namespace Domain { for (int i = 0; i < _specialKeys.Length; i++) { int pressIndex = AlphabetSize + CustomKeysSize + i; _wasPressed[pressIndex] = _isPressed[pressIndex]; - short state = Win32API.GetAsyncKeyState((int)_specialKeys[i]); + short state = Win32API.GetAsyncKeyState(_specialKeys[i]); if (!_wasPressed[pressIndex] && state != 0) { _isPressed[pressIndex] = true; - KeyPressed((int)_specialKeys[i], true); + KeyPressed(_specialKeys[i], true); }else if (_isPressed[pressIndex] && state == 0) { _isPressed[pressIndex] = false; } diff --git a/Assets/Scripts/Extensions/SmartEnum.cs b/Assets/Scripts/Extensions/SmartEnum.cs new file mode 100644 index 0000000..ecf065b --- /dev/null +++ b/Assets/Scripts/Extensions/SmartEnum.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace Extensions { + public abstract class SmartEnum where TEnum : SmartEnum { + private static readonly List _list = new(); + + // Despite analysis tool warnings, we want this static bool to be on this generic type (so that each TEnum has its own bool). + private static bool _invoked; + + public static List List { + get { + if (!_invoked) { + _invoked = true; + // Force invocaiton/initialization by calling one of the derived members. + typeof(TEnum).GetProperties(BindingFlags.Public | BindingFlags.Static) + .FirstOrDefault(p => p.PropertyType == typeof(TEnum))?.GetValue(null, null); + } + + return _list; + } + } + + public string Name { get; } + public TValue Value { get; } + + protected SmartEnum(string name, TValue value) { + Name = name; + Value = value; + + TEnum item = this as TEnum; + List.Add(item); + } + + public static TEnum FromName(string name) { + return List.Single(item => string.Equals(item.Name, name, StringComparison.OrdinalIgnoreCase)); + } + + public static TEnum FromValue(TValue value) { + // Can't use == to compare generics unless we constrain TValue to "class", which we don't want because then we couldn't use int. + return List.Single(item => EqualityComparer.Default.Equals(item.Value, value)); + } + + public override string ToString() => $"{Name} ({Value})"; + public static implicit operator TValue(SmartEnum key) => key.Value; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/SmartEnum.cs.meta b/Assets/Scripts/Extensions/SmartEnum.cs.meta new file mode 100644 index 0000000..84eb89a --- /dev/null +++ b/Assets/Scripts/Extensions/SmartEnum.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d14368a1c4e54cfb9e201b2d9801b602 +timeCreated: 1713368573 \ No newline at end of file