feat: l geminada working

This commit is contained in:
Gerard Gascón 2024-04-14 23:15:50 +02:00
parent 1869a92580
commit 30ca50fb35
15 changed files with 189 additions and 29 deletions

View file

@ -5,12 +5,17 @@ using Domain.Input;
namespace Domain {
public class CustomInput {
private readonly Dictionary<Type, InputReader> _readers = new();
private readonly KeyHistory _history;
public CustomInput() {
_history = new KeyHistory();
_readers.Add(typeof(CedillaReader), new CedillaReader());
_readers.Add(typeof(GeminadaReader), new GeminadaReader(_history));
}
public void UpdateInput() {
_history.CheckPresses();
foreach (KeyValuePair<Type, InputReader> reader in _readers) {
reader.Value.UpdateInput();
}

View file

@ -3,7 +3,7 @@ using System.Linq;
namespace Domain.Input {
public class GeminadaReader : InputReader {
protected override int Key { get; } = 0x33;
protected sealed override int Key { get; } = 0x33;
private int LKey { get; } = 76;
private bool _lPressed;
@ -11,22 +11,24 @@ namespace Domain.Input {
private bool _dotPressed;
private bool _dotWasPressed;
private List<int> _lastPresses;
private readonly KeyHistory _history;
private readonly List<int> _desiredSequence;
public override void UpdateInput() {
if (UpdateLInput()) {
if (_lastPresses.Count == 0) {
_lastPresses.Add(LKey);
} else if (_lastPresses.Count >= 2) {
if (_lastPresses[^1] == Key && _lastPresses[^2] == LKey) {
IsPressed = true;
}
}
}
UpdateDotInput();
public GeminadaReader(KeyHistory history) {
_history = history;
_desiredSequence = new List<int> { LKey, Key, LKey };
}
private bool UpdateDotInput() {
public override void UpdateInput() {
if (UpdateDotInputDown()) {
_history.KeyPressed(Key);
}
WasPressed = IsPressed;
IsPressed = _history.ContainsSequence(_desiredSequence);
}
private bool UpdateDotInputDown() {
_dotWasPressed = _dotPressed;
short dotState = Win32API.GetAsyncKeyState(Key);
@ -40,7 +42,7 @@ namespace Domain.Input {
return false;
}
private bool UpdateLInput() {
private bool UpdateLInputDown() {
_lWasPressed = _lPressed;
short lState = Win32API.GetAsyncKeyState(LKey);

View file

@ -1,27 +1,42 @@
using System.Collections.Generic;
using Extensions;
namespace Domain {
public class KeyHistory {
private readonly List<int> _lastPresses = new();
private readonly List<int> _desiredSequence;
private readonly LimitedSizeList<int> _lastPresses = new(10);
public KeyHistory(List<int> desiredSequence) {
_desiredSequence = desiredSequence;
}
private readonly bool[] _isPressed = new bool[26];
private readonly bool[] _wasPressed = new bool[26];
public void KeyPressed(int key) => _lastPresses.Add(key);
public void ClearPressed() => _lastPresses.Clear();
public bool ContainsSequence() {
if (_lastPresses.Count < _desiredSequence.Count)
public void CheckPresses() {
const int aIndex = 0x41;
const int zIndex = 0x5A;
for (int i = aIndex, j = 0; i <= zIndex; i++, j++) {
_wasPressed[j] = _isPressed[j];
short state = Win32API.GetAsyncKeyState(i);
if (!_wasPressed[j] && state != 0) {
_isPressed[j] = true;
KeyPressed(i);
}else if (_isPressed[j] && state == 0) {
_isPressed[j] = false;
}
}
}
public bool ContainsSequence(List<int> sequence) {
if (_lastPresses.List.Count < sequence.Count)
return false;
for (int i = 0; i < _lastPresses.Count; i++) {
if (i >= _desiredSequence.Count)
for (int i = 0; i < _lastPresses.List.Count; i++) {
if (i >= sequence.Count)
break;
int keyPressed = _lastPresses[_lastPresses.Count - 1 - i];
int sequenceKey = _desiredSequence[_desiredSequence.Count - 1 - i];
int keyPressed = _lastPresses.List[_lastPresses.List.Count - 1 - i];
int sequenceKey = sequence[sequence.Count - 1 - i];
if (keyPressed != sequenceKey) {
return false;

View file

@ -1,3 +1,4 @@
{
"name": "SantJordi.Domain"
"name": "SantJordi.Domain",
"references":[ "GUID:b347d0ff97f738846abb5625028d64db" ]
}