feat: special key reading
This commit is contained in:
		
							parent
							
								
									c475204437
								
							
						
					
					
						commit
						2a6f41713e
					
				
					 5 changed files with 144 additions and 8 deletions
				
			
		|  | @ -3,6 +3,6 @@ using System.Linq; | |||
| 
 | ||||
| namespace Domain.Input { | ||||
| 	public class AObertaReader : SequentialInputReader { | ||||
| 		public AObertaReader(KeyHistory history) : base(history, new List<int>{ 0xBA, 0x41 }) { } | ||||
| 		public AObertaReader(KeyHistory history) : base(history, new List<int>{ 0xBA, 0x41 }, 0x10, false) { } | ||||
| 	} | ||||
| } | ||||
|  | @ -1,4 +1,5 @@ | |||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| 
 | ||||
| namespace Domain.Input { | ||||
| 	public abstract class SequentialInputReader : InputReader { | ||||
|  | @ -7,14 +8,31 @@ namespace Domain.Input { | |||
| 		private readonly KeyHistory _history; | ||||
| 		private readonly List<int> _desiredSequence; | ||||
| 
 | ||||
| 		private readonly bool _useSpecial; | ||||
| 		private readonly int _specialKey; | ||||
| 		private readonly bool _mustContain; | ||||
| 
 | ||||
| 		protected SequentialInputReader(KeyHistory history, List<int> desiredSequence) { | ||||
| 			_useSpecial = false; | ||||
| 			_history = history; | ||||
| 			_desiredSequence = desiredSequence; | ||||
| 		} | ||||
| 
 | ||||
| 		protected SequentialInputReader(KeyHistory history, List<int> desiredSequence, int specialKey, bool mustContain) { | ||||
| 			_useSpecial = true; | ||||
| 			_history = history; | ||||
| 			_desiredSequence = desiredSequence; | ||||
| 
 | ||||
| 			_specialKey = specialKey; | ||||
| 			_mustContain = mustContain; | ||||
| 		} | ||||
| 
 | ||||
| 		public override void UpdateInput() { | ||||
| 			WasPressed = IsPressed; | ||||
| 			IsPressed = _history.ContainsSequence(_desiredSequence); | ||||
| 			if (_useSpecial) | ||||
| 				IsPressed = _history.ContainsSequence(_desiredSequence, _specialKey, _mustContain); | ||||
| 			else | ||||
| 				IsPressed = _history.ContainsSequence(_desiredSequence); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | @ -1,15 +1,28 @@ | |||
| using System.Collections.Generic; | ||||
| using Extensions; | ||||
| using UnityEngine; | ||||
| 
 | ||||
| namespace Domain { | ||||
| 	public class KeyHistory { | ||||
| 		private readonly LimitedSizeList<int> _lastPresses = new(10); | ||||
| 		private readonly LimitedSizeList<int> _lastPressesWithSpecial = new(20); | ||||
| 
 | ||||
| 		private readonly int[] _customKeys = { 191, 51, 222, 186, 0xBA }; | ||||
| 		private readonly bool[] _isPressed = new bool[26 + 6]; | ||||
| 		private readonly bool[] _wasPressed = new bool[26 + 6]; | ||||
| 		private const int AlphabetSize = 26; | ||||
| 		private const int CustomKeysSize = 5; | ||||
| 		private const int SpecialKeysSize = 1; | ||||
| 
 | ||||
| 		public void KeyPressed(int key) => _lastPresses.Add(key); | ||||
| 		private readonly int[] _customKeys = { 191, 51, 222, 186 }; | ||||
| 		private readonly int[] _specialKeys = { 0x10 }; | ||||
| 		private readonly bool[] _isPressed = new bool[AlphabetSize + CustomKeysSize + SpecialKeysSize]; | ||||
| 		private readonly bool[] _wasPressed = new bool[AlphabetSize + CustomKeysSize + SpecialKeysSize]; | ||||
| 
 | ||||
| 		public void KeyPressed(int key, bool isSpecial = false) { | ||||
| 			if (!isSpecial) | ||||
| 				_lastPresses.Add(key); | ||||
| 			_lastPressesWithSpecial.Add(key); | ||||
| 
 | ||||
| 			Debug.Log(_lastPressesWithSpecial); | ||||
| 		} | ||||
| 
 | ||||
| 		public void CheckPresses() { | ||||
| 			const int aIndex = 0x41; | ||||
|  | @ -28,7 +41,7 @@ namespace Domain { | |||
| 			} | ||||
| 
 | ||||
| 			for (int i = 0; i < _customKeys.Length; i++) { | ||||
| 				int pressIndex = 26 + i; | ||||
| 				int pressIndex = AlphabetSize + i; | ||||
| 				_wasPressed[pressIndex] = _isPressed[pressIndex]; | ||||
| 				short state = Win32API.GetAsyncKeyState(_customKeys[i]); | ||||
| 
 | ||||
|  | @ -39,6 +52,19 @@ namespace Domain { | |||
| 					_isPressed[pressIndex] = false; | ||||
| 				} | ||||
| 			} | ||||
| 
 | ||||
| 			for (int i = 0; i < _specialKeys.Length; i++) { | ||||
| 				int pressIndex = AlphabetSize + CustomKeysSize + i; | ||||
| 				_wasPressed[pressIndex] = _isPressed[pressIndex]; | ||||
| 				short state = Win32API.GetAsyncKeyState(_specialKeys[i]); | ||||
| 
 | ||||
| 				if (!_wasPressed[pressIndex] && state != 0) { | ||||
| 					_isPressed[pressIndex] = true; | ||||
| 					KeyPressed(_specialKeys[i], true); | ||||
| 				}else if (_isPressed[pressIndex] && state == 0) { | ||||
| 					_isPressed[pressIndex] = false; | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		public bool ContainsSequence(List<int> sequence) { | ||||
|  | @ -57,6 +83,35 @@ namespace Domain { | |||
| 				} | ||||
| 			} | ||||
| 
 | ||||
| 			_lastPresses.List.Clear(); | ||||
| 			_lastPressesWithSpecial.List.Clear(); | ||||
| 			return true; | ||||
| 		} | ||||
| 
 | ||||
| 		public bool ContainsSequence(List<int> sequence, int beginning, bool mustAppear) { | ||||
| 			if (_lastPressesWithSpecial.List.Count < sequence.Count + 1) { | ||||
| 				if (mustAppear) | ||||
| 					return false; | ||||
| 			} else { | ||||
| 				if (_lastPressesWithSpecial.List[^(sequence.Count + 1)] != beginning && mustAppear) | ||||
| 					return false; | ||||
| 				if (_lastPressesWithSpecial.List[^(sequence.Count + 1)] == beginning && !mustAppear) | ||||
| 					return false; | ||||
| 			} | ||||
| 
 | ||||
| 			for (int i = 0; i < _lastPressesWithSpecial.List.Count; i++) { | ||||
| 				if (i >= sequence.Count) | ||||
| 					break; | ||||
| 
 | ||||
| 				int keyPressed = _lastPressesWithSpecial.List[_lastPressesWithSpecial.List.Count - 1 - i]; | ||||
| 				int sequenceKey = sequence[sequence.Count - 1 - i]; | ||||
| 
 | ||||
| 				if (keyPressed != sequenceKey) { | ||||
| 					return false; | ||||
| 				} | ||||
| 			} | ||||
| 
 | ||||
| 			_lastPressesWithSpecial.List.Clear(); | ||||
| 			_lastPresses.List.Clear(); | ||||
| 			return true; | ||||
| 		} | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Gerard Gascón
						Gerard Gascón