Commit 1869a925 authored by Gerard Gascón's avatar Gerard Gascón
Browse files

feat: key history algorithm

parent 5d2da3dd
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -17,7 +17,5 @@ namespace Domain {
		}

		public bool KeyDown(Type key) => _readers[key].KeyDown();
		public bool KeyPressed(Type key) => _readers[key].KeyPressed();
		public bool KeyUp(Type key) => _readers[key].KeyUp();
	}
}
 No newline at end of file
+57 −0
Original line number Diff line number Diff line
using System.Collections.Generic;
using System.Linq;

namespace Domain.Input {
	public class GeminadaReader : InputReader {
		protected override int Key { get; } = 0x33;
		private int LKey { get; } = 76;

		private bool _lPressed;
		private bool _lWasPressed;
		private bool _dotPressed;
		private bool _dotWasPressed;

		private List<int> _lastPresses;

		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();
		}

		private bool UpdateDotInput() {
			_dotWasPressed = _dotPressed;
			short dotState = Win32API.GetAsyncKeyState(Key);

			if (!_dotWasPressed && dotState != 0) {
				_dotPressed = true;
				return true;
			}
			if (_dotPressed && dotState == 0) {
				_dotPressed = false;
			}
			return false;
		}

		private bool UpdateLInput() {
			_lWasPressed = _lPressed;
			short lState = Win32API.GetAsyncKeyState(LKey);

			if (!_lWasPressed && lState != 0) {
				_lPressed = true;
				return true;
			}
			if (_lPressed && lState == 0) {
				_lPressed = false;
			}
			return false;
		}
	}
}
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: 057b144441c443ccb0a24de75397b551
timeCreated: 1713114678
 No newline at end of file
+6 −8
Original line number Diff line number Diff line
namespace Domain.Input {
	public abstract class InputReader {
		private bool _wasPressed;
		private bool _isPressed;
		protected bool WasPressed;
		protected bool IsPressed;

		protected abstract int Key { get; }

		public void UpdateInput() {
			_wasPressed = _isPressed;
			_isPressed = Win32API.GetAsyncKeyState(Key) != 0;
		public virtual void UpdateInput() {
			WasPressed = IsPressed;
			IsPressed = Win32API.GetAsyncKeyState(Key) != 0;
		}

		public bool KeyDown() => _isPressed && !_wasPressed;
		public bool KeyPressed() => _isPressed;
		public bool KeyUp() => !_isPressed && _wasPressed;
		public bool KeyDown() => IsPressed && !WasPressed;
	}
}
 No newline at end of file
+34 −0
Original line number Diff line number Diff line
using System.Collections.Generic;

namespace Domain {
	public class KeyHistory {
		private readonly List<int> _lastPresses = new();
		private readonly List<int> _desiredSequence;

		public KeyHistory(List<int> desiredSequence) {
			_desiredSequence = desiredSequence;
		}

		public void KeyPressed(int key) => _lastPresses.Add(key);
		public void ClearPressed() => _lastPresses.Clear();

		public bool ContainsSequence() {
			if (_lastPresses.Count < _desiredSequence.Count)
				return false;

			for (int i = 0; i < _lastPresses.Count; i++) {
				if (i >= _desiredSequence.Count)
					break;

				int keyPressed = _lastPresses[_lastPresses.Count - 1 - i];
				int sequenceKey = _desiredSequence[_desiredSequence.Count - 1 - i];

				if (keyPressed != sequenceKey) {
					return false;
				}
			}

			return true;
		}
	}
}
 No newline at end of file
Loading