refactor: made the input reader expandable
This commit is contained in:
parent
12304c6bb4
commit
59a3d96b5b
10 changed files with 72 additions and 24 deletions
23
Assets/Scripts/Domain/CustomInput.cs
Normal file
23
Assets/Scripts/Domain/CustomInput.cs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Domain.Input;
|
||||||
|
|
||||||
|
namespace Domain {
|
||||||
|
public class CustomInput {
|
||||||
|
private readonly Dictionary<Type, InputReader> _readers = new();
|
||||||
|
|
||||||
|
public CustomInput() {
|
||||||
|
_readers.Add(typeof(CedillaReader), new CedillaReader());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateInput() {
|
||||||
|
foreach (KeyValuePair<Type, InputReader> 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();
|
||||||
|
}
|
||||||
|
}
|
3
Assets/Scripts/Domain/CustomInput.cs.meta
Normal file
3
Assets/Scripts/Domain/CustomInput.cs.meta
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: aab774881fcc4971913f24037e449469
|
||||||
|
timeCreated: 1713112890
|
3
Assets/Scripts/Domain/Input.meta
Normal file
3
Assets/Scripts/Domain/Input.meta
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ad3db3db2d5e4647bad7a257606eea0c
|
||||||
|
timeCreated: 1713112359
|
5
Assets/Scripts/Domain/Input/CedillaReader.cs
Normal file
5
Assets/Scripts/Domain/Input/CedillaReader.cs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
namespace Domain.Input {
|
||||||
|
public class CedillaReader : InputReader {
|
||||||
|
protected override int Key { get; } = 0xBF; //VK_OEM_2 | cedilla
|
||||||
|
}
|
||||||
|
}
|
3
Assets/Scripts/Domain/Input/CedillaReader.cs.meta
Normal file
3
Assets/Scripts/Domain/Input/CedillaReader.cs.meta
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d07951ea53d84d559bda9060a769ce93
|
||||||
|
timeCreated: 1713112770
|
23
Assets/Scripts/Domain/Input/InputReader.cs
Normal file
23
Assets/Scripts/Domain/Input/InputReader.cs
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
3
Assets/Scripts/Domain/Input/InputReader.cs.meta
Normal file
3
Assets/Scripts/Domain/Input/InputReader.cs.meta
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3b3b656d6cb1415eab0de81628a5b54e
|
||||||
|
timeCreated: 1713112368
|
|
@ -2,28 +2,7 @@
|
||||||
|
|
||||||
namespace Domain {
|
namespace Domain {
|
||||||
public static class Win32API {
|
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")]
|
[DllImport("User32.dll")]
|
||||||
private static extern short GetAsyncKeyState(int vKey);
|
public static extern short GetAsyncKeyState(int vKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
using Domain;
|
using Domain;
|
||||||
|
using Domain.Input;
|
||||||
using Presenter;
|
using Presenter;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
@ -7,9 +8,11 @@ namespace View {
|
||||||
public class CTrencadaInput : MonoBehaviour, ICTrencadaInput {
|
public class CTrencadaInput : MonoBehaviour, ICTrencadaInput {
|
||||||
[SerializeField] private TMP_Text text;
|
[SerializeField] private TMP_Text text;
|
||||||
private CTrencadaClick _click;
|
private CTrencadaClick _click;
|
||||||
|
private CustomInput _customInput;
|
||||||
|
|
||||||
private void Start() {
|
private void Start() {
|
||||||
_click = FindObjectOfType<Dependencies>().CTrencadaClick;
|
_click = FindObjectOfType<Dependencies>().CTrencadaClick;
|
||||||
|
_customInput = FindObjectOfType<Dependencies>().CustomInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update() {
|
private void Update() {
|
||||||
|
@ -17,8 +20,8 @@ namespace View {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CheckInput() {
|
private void CheckInput() {
|
||||||
Win32API.UpdateInput();
|
_customInput.UpdateInput();
|
||||||
if (Win32API.CTrencadaDown())
|
if (_customInput.KeyDown(typeof(CedillaReader)))
|
||||||
_click.Execute();
|
_click.Execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,12 +7,15 @@ namespace View {
|
||||||
public class Dependencies : MonoBehaviour {
|
public class Dependencies : MonoBehaviour {
|
||||||
public CTrencadaClick CTrencadaClick { private set; get; }
|
public CTrencadaClick CTrencadaClick { private set; get; }
|
||||||
public Model Model { private set; get; }
|
public Model Model { private set; get; }
|
||||||
|
public CustomInput CustomInput { private set; get; }
|
||||||
|
|
||||||
private void Awake() {
|
private void Awake() {
|
||||||
Model = new Model(0);
|
Model = new Model(0);
|
||||||
|
|
||||||
ICTrencadaInput input = FindObjectOfType<CTrencadaInput>();
|
ICTrencadaInput input = FindObjectOfType<CTrencadaInput>();
|
||||||
CTrencadaClick = new CTrencadaClick(Model, input);
|
CTrencadaClick = new CTrencadaClick(Model, input);
|
||||||
|
|
||||||
|
CustomInput = new CustomInput();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue