|
| 1 | +using Kirei.Application.System.InputProcessing; |
| 2 | +using Kirei.Infrastructure.Configuration; |
| 3 | + |
| 4 | +using NeatInput; |
| 5 | +using NeatInput.Domain.Processing; |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Threading; |
| 9 | + |
| 10 | +namespace Kirei.Infrastructure.System.InputProcessing |
| 11 | +{ |
| 12 | + public class InputListener : |
| 13 | + IInputListener |
| 14 | + { |
| 15 | + private InputProvider _inputProvider; |
| 16 | + private IInputActionMapper _inputActionMapper; |
| 17 | + |
| 18 | + private DateTime lastInputReceivedAt; |
| 19 | + private bool hasIconsBeenHidden = false; |
| 20 | + |
| 21 | + public void Listen(IInputActionMapper inputActionMapper) |
| 22 | + { |
| 23 | + _inputProvider = new InputProvider(); |
| 24 | + _inputActionMapper = inputActionMapper; |
| 25 | + |
| 26 | + lastInputReceivedAt = DateTime.Now; |
| 27 | + |
| 28 | + _inputProvider.InputReceived += OnInputReceived; |
| 29 | + |
| 30 | + var inactiveAfterMs = ConfigurationProvider.Configuration.Application.InactiveAfterMs; |
| 31 | + var inputPollingRateMs = ConfigurationProvider.Configuration.Application.InputPollingRate; |
| 32 | + |
| 33 | + while (true) |
| 34 | + { |
| 35 | + var lastInputInMilliseconds = DateTime.Now.Subtract(lastInputReceivedAt).TotalMilliseconds; |
| 36 | + |
| 37 | + if (lastInputInMilliseconds >= inactiveAfterMs && !hasIconsBeenHidden) |
| 38 | + { |
| 39 | + _inputActionMapper.HandleInput(); |
| 40 | + hasIconsBeenHidden = true; |
| 41 | + } |
| 42 | + else if (lastInputInMilliseconds < inactiveAfterMs && hasIconsBeenHidden) |
| 43 | + { |
| 44 | + _inputActionMapper.HandleInput(); |
| 45 | + hasIconsBeenHidden = false; |
| 46 | + } |
| 47 | + |
| 48 | + Thread.Sleep(inputPollingRateMs); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void OnInputReceived(Input input) |
| 53 | + { |
| 54 | + lastInputReceivedAt = DateTime.Now; |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments