refactor: converted vkeycode enum into a class

This commit is contained in:
Gerard Gascón 2024-04-17 18:01:33 +02:00
parent e3f6db7ae5
commit 68d3d8065e
19 changed files with 123 additions and 54 deletions

View file

@ -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<InputReader> _readers = new();
private readonly KeyHistory _history;

View file

@ -3,6 +3,7 @@ using System.Linq;
namespace Domain.Input {
public class AObertaReader : InputReader {
public AObertaReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.AccentOpen, (int)VKeyCode.A }, (int)VKeyCode.Shift, false) { }
public AObertaReader(KeyHistory history) : base(history, new List<int> { VKeyCode.AccentOpen, VKeyCode.A },
VKeyCode.Shift, false) { }
}
}

View file

@ -2,6 +2,6 @@
namespace Domain.Input {
public class CedillaReader : InputReader {
public CedillaReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.Cedilla }) { }
public CedillaReader(KeyHistory history) : base(history, new List<int> { VKeyCode.Cedilla }) { }
}
}

View file

@ -3,6 +3,7 @@ using System.Linq;
namespace Domain.Input {
public class EObertaReader : InputReader {
public EObertaReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.AccentOpen, (int)VKeyCode.E }, (int)VKeyCode.Shift, false) { }
public EObertaReader(KeyHistory history) : base(history, new List<int> { VKeyCode.AccentOpen, VKeyCode.E },
VKeyCode.Shift, false) { }
}
}

View file

@ -3,6 +3,7 @@ using System.Linq;
namespace Domain.Input {
public class GeminadaReader : InputReader {
public GeminadaReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.L, (int)VKeyCode.Interpunct, (int)VKeyCode.L }) { }
public GeminadaReader(KeyHistory history) : base(history,
new List<int> { VKeyCode.L, VKeyCode.Interpunct, VKeyCode.L }) { }
}
}

View file

@ -3,6 +3,7 @@ using System.Linq;
namespace Domain.Input {
public class IDieresiReader : InputReader {
public IDieresiReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.AccentClosed, (int)VKeyCode.I }, (int)VKeyCode.Shift, true) { }
public IDieresiReader(KeyHistory history) : base(history, new List<int> { VKeyCode.AccentClosed, VKeyCode.I },
VKeyCode.Shift, true) { }
}
}

View file

@ -3,6 +3,6 @@ using System.Linq;
namespace Domain.Input {
public class IxReader : InputReader {
public IxReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.I, (int)VKeyCode.X }) { }
public IxReader(KeyHistory history) : base(history, new List<int> { VKeyCode.I, VKeyCode.X }) { }
}
}

View file

@ -3,6 +3,6 @@ using System.Linq;
namespace Domain.Input {
public class LlReader : InputReader {
public LlReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.L, (int)VKeyCode.L }) { }
public LlReader(KeyHistory history) : base(history, new List<int> { VKeyCode.L, VKeyCode.L }) { }
}
}

View file

@ -3,6 +3,6 @@ using System.Linq;
namespace Domain.Input {
public class NyReader : InputReader {
public NyReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.N, (int)VKeyCode.Y }) { }
public NyReader(KeyHistory history) : base(history, new List<int> { VKeyCode.N, VKeyCode.Y }) { }
}
}

View file

@ -3,6 +3,7 @@ using System.Linq;
namespace Domain.Input {
public class OObertaReader : InputReader {
public OObertaReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.AccentOpen, (int)VKeyCode.O }, (int)VKeyCode.Shift, false) { }
public OObertaReader(KeyHistory history) : base(history, new List<int> { VKeyCode.AccentOpen, VKeyCode.O },
VKeyCode.Shift, false) { }
}
}

View file

@ -3,6 +3,6 @@ using System.Linq;
namespace Domain.Input {
public class SsReader : InputReader {
public SsReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.S, (int)VKeyCode.S }) { }
public SsReader(KeyHistory history) : base(history, new List<int> { VKeyCode.S, VKeyCode.S }) { }
}
}

View file

@ -3,6 +3,6 @@ using System.Linq;
namespace Domain.Input {
public class TgReader : InputReader {
public TgReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.T, (int)VKeyCode.G }) { }
public TgReader(KeyHistory history) : base(history, new List<int> { VKeyCode.T, VKeyCode.G }) { }
}
}

View file

@ -3,6 +3,6 @@ using System.Linq;
namespace Domain.Input {
public class TjReader : InputReader {
public TjReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.T, (int)VKeyCode.J }) { }
public TjReader(KeyHistory history) : base(history, new List<int> { VKeyCode.T, VKeyCode.J }) { }
}
}

View file

@ -3,6 +3,6 @@ using System.Linq;
namespace Domain.Input {
public class TxReader : InputReader {
public TxReader(KeyHistory history) : base(history, new List<int>{ (int)VKeyCode.T, (int)VKeyCode.X }) { }
public TxReader(KeyHistory history) : base(history, new List<int> { VKeyCode.T, VKeyCode.X }) { }
}
}

View file

@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Extensions;
namespace Domain.Input {
public class VKeyCode : SmartEnum<VKeyCode, int> {
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) { }
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 26d2a6386e9140e9a9afb79b32140e3b
timeCreated: 1713367740

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Domain.Input;
using Extensions;
using UnityEngine;
@ -8,16 +9,16 @@ namespace Domain {
private readonly LimitedSizeList<int> _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;
}