refactor: converted vkeycode enum into a class
This commit is contained in:
parent
e3f6db7ae5
commit
68d3d8065e
19 changed files with 123 additions and 54 deletions
|
@ -3,40 +3,6 @@ using System.Linq;
|
||||||
using Domain.Input;
|
using Domain.Input;
|
||||||
|
|
||||||
namespace Domain {
|
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 {
|
public class CustomInput {
|
||||||
private readonly List<InputReader> _readers = new();
|
private readonly List<InputReader> _readers = new();
|
||||||
private readonly KeyHistory _history;
|
private readonly KeyHistory _history;
|
||||||
|
|
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class AObertaReader : InputReader {
|
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) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,6 @@
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class CedillaReader : InputReader {
|
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 }) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class EObertaReader : InputReader {
|
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) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class GeminadaReader : InputReader {
|
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 }) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class IDieresiReader : InputReader {
|
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) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,6 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class IxReader : InputReader {
|
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 }) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,6 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class LlReader : InputReader {
|
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 }) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,6 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class NyReader : InputReader {
|
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 }) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class OObertaReader : InputReader {
|
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) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,6 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class SsReader : InputReader {
|
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 }) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,6 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class TgReader : InputReader {
|
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 }) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,6 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class TjReader : InputReader {
|
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 }) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,6 +3,6 @@ using System.Linq;
|
||||||
|
|
||||||
namespace Domain.Input {
|
namespace Domain.Input {
|
||||||
public class TxReader : InputReader {
|
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 }) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
42
Assets/Scripts/Domain/Input/VKeyCode.cs
Normal file
42
Assets/Scripts/Domain/Input/VKeyCode.cs
Normal 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) { }
|
||||||
|
}
|
||||||
|
}
|
3
Assets/Scripts/Domain/Input/VKeyCode.cs.meta
Normal file
3
Assets/Scripts/Domain/Input/VKeyCode.cs.meta
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 26d2a6386e9140e9a9afb79b32140e3b
|
||||||
|
timeCreated: 1713367740
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Domain.Input;
|
||||||
using Extensions;
|
using Extensions;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
|
@ -8,16 +9,16 @@ namespace Domain {
|
||||||
private readonly LimitedSizeList<int> _lastPressesWithSpecial = new(20);
|
private readonly LimitedSizeList<int> _lastPressesWithSpecial = new(20);
|
||||||
|
|
||||||
private const int AlphabetSize = 26;
|
private const int AlphabetSize = 26;
|
||||||
private const int CustomKeysSize = 5;
|
private const int CustomKeysSize = 4;
|
||||||
private const int SpecialKeysSize = 1;
|
private const int SpecialKeysSize = 1;
|
||||||
|
|
||||||
private readonly VKeyCode[] _customKeys = {
|
private readonly int[] _customKeys = {
|
||||||
VKeyCode.Cedilla,
|
VKeyCode.Cedilla,
|
||||||
VKeyCode.Interpunct,
|
VKeyCode.Interpunct,
|
||||||
VKeyCode.AccentClosed,
|
VKeyCode.AccentClosed,
|
||||||
VKeyCode.AccentOpen
|
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[] _isPressed = new bool[AlphabetSize + CustomKeysSize + SpecialKeysSize];
|
||||||
private readonly bool[] _wasPressed = 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++) {
|
for (int i = 0; i < _customKeys.Length; i++) {
|
||||||
int pressIndex = AlphabetSize + i;
|
int pressIndex = AlphabetSize + i;
|
||||||
_wasPressed[pressIndex] = _isPressed[pressIndex];
|
_wasPressed[pressIndex] = _isPressed[pressIndex];
|
||||||
short state = Win32API.GetAsyncKeyState((int)_customKeys[i]);
|
short state = Win32API.GetAsyncKeyState(_customKeys[i]);
|
||||||
|
|
||||||
if (!_wasPressed[pressIndex] && state != 0) {
|
if (!_wasPressed[pressIndex] && state != 0) {
|
||||||
_isPressed[pressIndex] = true;
|
_isPressed[pressIndex] = true;
|
||||||
KeyPressed((int)_customKeys[i]);
|
KeyPressed(_customKeys[i]);
|
||||||
}else if (_isPressed[pressIndex] && state == 0) {
|
}else if (_isPressed[pressIndex] && state == 0) {
|
||||||
_isPressed[pressIndex] = false;
|
_isPressed[pressIndex] = false;
|
||||||
}
|
}
|
||||||
|
@ -59,11 +60,11 @@ namespace Domain {
|
||||||
for (int i = 0; i < _specialKeys.Length; i++) {
|
for (int i = 0; i < _specialKeys.Length; i++) {
|
||||||
int pressIndex = AlphabetSize + CustomKeysSize + i;
|
int pressIndex = AlphabetSize + CustomKeysSize + i;
|
||||||
_wasPressed[pressIndex] = _isPressed[pressIndex];
|
_wasPressed[pressIndex] = _isPressed[pressIndex];
|
||||||
short state = Win32API.GetAsyncKeyState((int)_specialKeys[i]);
|
short state = Win32API.GetAsyncKeyState(_specialKeys[i]);
|
||||||
|
|
||||||
if (!_wasPressed[pressIndex] && state != 0) {
|
if (!_wasPressed[pressIndex] && state != 0) {
|
||||||
_isPressed[pressIndex] = true;
|
_isPressed[pressIndex] = true;
|
||||||
KeyPressed((int)_specialKeys[i], true);
|
KeyPressed(_specialKeys[i], true);
|
||||||
}else if (_isPressed[pressIndex] && state == 0) {
|
}else if (_isPressed[pressIndex] && state == 0) {
|
||||||
_isPressed[pressIndex] = false;
|
_isPressed[pressIndex] = false;
|
||||||
}
|
}
|
||||||
|
|
49
Assets/Scripts/Extensions/SmartEnum.cs
Normal file
49
Assets/Scripts/Extensions/SmartEnum.cs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Extensions {
|
||||||
|
public abstract class SmartEnum<TEnum, TValue> where TEnum : SmartEnum<TEnum, TValue> {
|
||||||
|
private static readonly List<TEnum> _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<TEnum> 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<TValue>.Default.Equals(item.Value, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => $"{Name} ({Value})";
|
||||||
|
public static implicit operator TValue(SmartEnum<TEnum, TValue> key) => key.Value;
|
||||||
|
}
|
||||||
|
}
|
3
Assets/Scripts/Extensions/SmartEnum.cs.meta
Normal file
3
Assets/Scripts/Extensions/SmartEnum.cs.meta
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d14368a1c4e54cfb9e201b2d9801b602
|
||||||
|
timeCreated: 1713368573
|
Loading…
Add table
Add a link
Reference in a new issue