diff --git a/Assets/Scripts/Domain/CustomInput.cs b/Assets/Scripts/Domain/CustomInput.cs new file mode 100644 index 0000000..b3754a3 --- /dev/null +++ b/Assets/Scripts/Domain/CustomInput.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using Domain.Input; + +namespace Domain { + public class CustomInput { + private readonly Dictionary _readers = new(); + + public CustomInput() { + _readers.Add(typeof(CedillaReader), new CedillaReader()); + } + + public void UpdateInput() { + foreach (KeyValuePair reader in _readers) { + reader.Value.UpdateInput(); + } + } + + 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 diff --git a/Assets/Scripts/Domain/CustomInput.cs.meta b/Assets/Scripts/Domain/CustomInput.cs.meta new file mode 100644 index 0000000..1370c62 --- /dev/null +++ b/Assets/Scripts/Domain/CustomInput.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: aab774881fcc4971913f24037e449469 +timeCreated: 1713112890 \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input.meta b/Assets/Scripts/Domain/Input.meta new file mode 100644 index 0000000..eef5162 --- /dev/null +++ b/Assets/Scripts/Domain/Input.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ad3db3db2d5e4647bad7a257606eea0c +timeCreated: 1713112359 \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/CedillaReader.cs b/Assets/Scripts/Domain/Input/CedillaReader.cs new file mode 100644 index 0000000..957159b --- /dev/null +++ b/Assets/Scripts/Domain/Input/CedillaReader.cs @@ -0,0 +1,5 @@ +namespace Domain.Input { + public class CedillaReader : InputReader { + protected override int Key { get; } = 0xBF; //VK_OEM_2 | cedilla + } +} \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/CedillaReader.cs.meta b/Assets/Scripts/Domain/Input/CedillaReader.cs.meta new file mode 100644 index 0000000..9bcfae2 --- /dev/null +++ b/Assets/Scripts/Domain/Input/CedillaReader.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d07951ea53d84d559bda9060a769ce93 +timeCreated: 1713112770 \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/InputReader.cs b/Assets/Scripts/Domain/Input/InputReader.cs new file mode 100644 index 0000000..2f38bf8 --- /dev/null +++ b/Assets/Scripts/Domain/Input/InputReader.cs @@ -0,0 +1,23 @@ +namespace Domain.Input { + public abstract class InputReader { + private bool _wasPressed; + private bool _isPressed; + + protected abstract int Key { get; } + + public void UpdateInput() { + _wasPressed = _isPressed; + short keyState = Win32API.GetAsyncKeyState(Key); + + if (_wasPressed && keyState == 0) { + _isPressed = false; + }else if (!_isPressed && keyState != 0) { + _isPressed = true; + } + } + + public bool KeyDown() => _isPressed && !_wasPressed; + public bool KeyPressed() => _isPressed; + public bool KeyUp() => !_isPressed && _wasPressed; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Domain/Input/InputReader.cs.meta b/Assets/Scripts/Domain/Input/InputReader.cs.meta new file mode 100644 index 0000000..07e9c82 --- /dev/null +++ b/Assets/Scripts/Domain/Input/InputReader.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3b3b656d6cb1415eab0de81628a5b54e +timeCreated: 1713112368 \ No newline at end of file diff --git a/Assets/Scripts/Domain/Win32API.cs b/Assets/Scripts/Domain/Win32API.cs index 67b3ad3..9312368 100644 --- a/Assets/Scripts/Domain/Win32API.cs +++ b/Assets/Scripts/Domain/Win32API.cs @@ -2,28 +2,7 @@ namespace Domain { public static class Win32API { - private static bool _cTrencadaWasPressed; - private static bool _cTrencadaPressed; - //VK_OEM_2 = 0xBF | cedilla - //https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes - private const int Key = 0xBF; - - public static void UpdateInput() { - _cTrencadaWasPressed = _cTrencadaPressed; - if (_cTrencadaPressed && GetAsyncKeyState(Key) == 0) { - _cTrencadaPressed = false; - return; - } - if (!_cTrencadaPressed && GetAsyncKeyState(Key) != 0) { - _cTrencadaPressed = true; - } - } - - public static bool CTrencadaDown() { - return _cTrencadaPressed && !_cTrencadaWasPressed; - } - [DllImport("User32.dll")] - private static extern short GetAsyncKeyState(int vKey); + public static extern short GetAsyncKeyState(int vKey); } } \ No newline at end of file diff --git a/Assets/Scripts/View/CTrencadaInput.cs b/Assets/Scripts/View/CTrencadaInput.cs index 16395d4..59ff2fb 100644 --- a/Assets/Scripts/View/CTrencadaInput.cs +++ b/Assets/Scripts/View/CTrencadaInput.cs @@ -1,4 +1,5 @@ using Domain; +using Domain.Input; using Presenter; using TMPro; using UnityEngine; @@ -7,9 +8,11 @@ namespace View { public class CTrencadaInput : MonoBehaviour, ICTrencadaInput { [SerializeField] private TMP_Text text; private CTrencadaClick _click; + private CustomInput _customInput; private void Start() { _click = FindObjectOfType().CTrencadaClick; + _customInput = FindObjectOfType().CustomInput; } private void Update() { @@ -17,8 +20,8 @@ namespace View { } private void CheckInput() { - Win32API.UpdateInput(); - if (Win32API.CTrencadaDown()) + _customInput.UpdateInput(); + if (_customInput.KeyDown(typeof(CedillaReader))) _click.Execute(); } diff --git a/Assets/Scripts/View/Dependencies.cs b/Assets/Scripts/View/Dependencies.cs index d5acb0d..054868f 100644 --- a/Assets/Scripts/View/Dependencies.cs +++ b/Assets/Scripts/View/Dependencies.cs @@ -7,12 +7,15 @@ namespace View { public class Dependencies : MonoBehaviour { public CTrencadaClick CTrencadaClick { private set; get; } public Model Model { private set; get; } + public CustomInput CustomInput { private set; get; } private void Awake() { Model = new Model(0); ICTrencadaInput input = FindObjectOfType(); CTrencadaClick = new CTrencadaClick(Model, input); + + CustomInput = new CustomInput(); } } } \ No newline at end of file