From 44c1454eed3a2710bbf087c721cc4bd7c56fe1b4 Mon Sep 17 00:00:00 2001 From: GeorgeAbbott <57576261+GeorgeAbbott@users.noreply.github.com> Date: Sun, 6 Sep 2020 18:09:11 +0100 Subject: Add files via upload --- InputHandler.cs | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 InputHandler.cs (limited to 'InputHandler.cs') diff --git a/InputHandler.cs b/InputHandler.cs new file mode 100644 index 0000000..1c74993 --- /dev/null +++ b/InputHandler.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Xna.Framework.Input; + +namespace Class_War +{ + class InputHandler + { + private KeyboardState _prevState; + public KeyboardState PreviousState { get => _prevState; set => _prevState = value; } + + private KeyboardState _currentState; + public KeyboardState CurrentState { get => _currentState; set => _currentState = value; } + + public void ReadInput () + { + PreviousState = CurrentState; + CurrentState = Keyboard.GetState(); + } + + public bool IsKeyDown (Keys key) + { + if (CurrentState[key] == KeyState.Down) return true; + else return false; + } + + + private DateTime _isKeyDownLastCheck = DateTime.Now; + public DateTime KeyDownLastCheck + { + get { return _isKeyDownLastCheck; } + private set { _isKeyDownLastCheck = DateTime.Now; } + } + + + + /// + /// Checks if a key is down, and will return false if the last keypress was earlier than + /// the timegap parameter. Keep changeKeyDownLastCheckIfFalse as false, + /// otherwise calling this function will interrupt the checking process. + /// + /// + /// + /// + /// + public bool IsKeyDown (Keys key, double timegap, bool changeKeyDownLastCheckIfFalse = false) + { + // Takes a DateTime, will return false if the DateTime is not after the last check. + // If enough time has passed, it will perform the check and return either true or false. + // Will not change KeyDownLastCheck unless true returned, except if changeKeyDownLastCheckIfFalse is true. + + if (!((DateTime.Now - KeyDownLastCheck).TotalSeconds > timegap)) + { // if not enough time has passed since last check + if (changeKeyDownLastCheckIfFalse) KeyDownLastCheck = DateTime.Now; + return false; + } + else // enough time has passed + { + if (CurrentState[key] == KeyState.Down) + { // Key has been pressed + KeyDownLastCheck = DateTime.Now; + return true; + } + else + { // Key has not been pressed + if (changeKeyDownLastCheckIfFalse) + { + KeyDownLastCheck = DateTime.Now; + return false; + } + else return false; + } + } + + + + + + } + + public bool OnKeyPress (Keys key) + { + if (CurrentState[key] == KeyState.Down && PreviousState[key] == KeyState.Up) return true; + else return false; + } + + public bool OnKeyRelease (Keys key) + { + if (CurrentState[key] == KeyState.Up && PreviousState[key] == KeyState.Down) return true; + else return false; + } + + } +} -- cgit v1.2.1