mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
added gamepad repeat events
This commit is contained in:
parent
70f926eb49
commit
c1726e4f1b
2 changed files with 58 additions and 5 deletions
|
@ -35,14 +35,21 @@ namespace MLEM.Input {
|
||||||
private readonly List<GestureSample> gestures = new List<GestureSample>();
|
private readonly List<GestureSample> gestures = new List<GestureSample>();
|
||||||
public bool HandleTouch;
|
public bool HandleTouch;
|
||||||
|
|
||||||
public bool HandleKeyboardRepeats = true;
|
|
||||||
public TimeSpan KeyRepeatDelay = TimeSpan.FromSeconds(0.65);
|
public TimeSpan KeyRepeatDelay = TimeSpan.FromSeconds(0.65);
|
||||||
public TimeSpan KeyRepeatRate = TimeSpan.FromSeconds(0.05);
|
public TimeSpan KeyRepeatRate = TimeSpan.FromSeconds(0.05);
|
||||||
|
|
||||||
|
public bool HandleKeyboardRepeats = true;
|
||||||
private DateTime heldKeyStart;
|
private DateTime heldKeyStart;
|
||||||
private DateTime lastKeyRepeat;
|
private DateTime lastKeyRepeat;
|
||||||
private bool triggerRepeat;
|
private bool triggerKeyRepeat;
|
||||||
private Keys heldKey;
|
private Keys heldKey;
|
||||||
|
|
||||||
|
public bool HandleGamepadRepeats = true;
|
||||||
|
private DateTime[] heldGamepadButtonStarts = new DateTime[GamePad.MaximumGamePadCount];
|
||||||
|
private DateTime[] lastGamepadButtonRepeats = new DateTime[GamePad.MaximumGamePadCount];
|
||||||
|
private bool[] triggerGamepadButtonRepeat = new bool[GamePad.MaximumGamePadCount];
|
||||||
|
private Buttons?[] heldGamepadButtons = new Buttons?[GamePad.MaximumGamePadCount];
|
||||||
|
|
||||||
public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true, bool handleTouch = true) {
|
public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true, bool handleTouch = true) {
|
||||||
this.HandleKeyboard = handleKeyboard;
|
this.HandleKeyboard = handleKeyboard;
|
||||||
this.HandleMouse = handleMouse;
|
this.HandleMouse = handleMouse;
|
||||||
|
@ -52,13 +59,13 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update() {
|
public void Update() {
|
||||||
this.triggerRepeat = false;
|
|
||||||
if (this.HandleKeyboard) {
|
if (this.HandleKeyboard) {
|
||||||
this.LastKeyboardState = this.KeyboardState;
|
this.LastKeyboardState = this.KeyboardState;
|
||||||
this.KeyboardState = Keyboard.GetState();
|
this.KeyboardState = Keyboard.GetState();
|
||||||
this.PressedKeys = this.KeyboardState.GetPressedKeys();
|
this.PressedKeys = this.KeyboardState.GetPressedKeys();
|
||||||
|
|
||||||
if (this.HandleKeyboardRepeats) {
|
if (this.HandleKeyboardRepeats) {
|
||||||
|
this.triggerKeyRepeat = false;
|
||||||
if (this.heldKey == Keys.None) {
|
if (this.heldKey == Keys.None) {
|
||||||
// if we're not repeating a key, set the first key being held to the repeat key
|
// if we're not repeating a key, set the first key being held to the repeat key
|
||||||
// note that modifier keys don't count as that wouldn't really make sense
|
// note that modifier keys don't count as that wouldn't really make sense
|
||||||
|
@ -81,17 +88,19 @@ namespace MLEM.Input {
|
||||||
if (diff >= this.KeyRepeatRate) {
|
if (diff >= this.KeyRepeatRate) {
|
||||||
this.lastKeyRepeat = now;
|
this.lastKeyRepeat = now;
|
||||||
// then trigger a repeat, causing IsKeyPressed to be true once
|
// then trigger a repeat, causing IsKeyPressed to be true once
|
||||||
this.triggerRepeat = true;
|
this.triggerKeyRepeat = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.HandleMouse) {
|
if (this.HandleMouse) {
|
||||||
this.LastMouseState = this.MouseState;
|
this.LastMouseState = this.MouseState;
|
||||||
this.MouseState = Mouse.GetState();
|
this.MouseState = Mouse.GetState();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.HandleGamepads) {
|
if (this.HandleGamepads) {
|
||||||
this.ConnectedGamepads = GamePad.MaximumGamePadCount;
|
this.ConnectedGamepads = GamePad.MaximumGamePadCount;
|
||||||
for (var i = 0; i < GamePad.MaximumGamePadCount; i++) {
|
for (var i = 0; i < GamePad.MaximumGamePadCount; i++) {
|
||||||
|
@ -100,7 +109,38 @@ namespace MLEM.Input {
|
||||||
if (this.ConnectedGamepads > i && !this.gamepads[i].IsConnected)
|
if (this.ConnectedGamepads > i && !this.gamepads[i].IsConnected)
|
||||||
this.ConnectedGamepads = i;
|
this.ConnectedGamepads = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.HandleGamepadRepeats) {
|
||||||
|
for (var i = 0; i < this.ConnectedGamepads; i++) {
|
||||||
|
this.triggerGamepadButtonRepeat[i] = false;
|
||||||
|
|
||||||
|
if (!this.heldGamepadButtons[i].HasValue) {
|
||||||
|
foreach (var b in EnumHelper.Buttons) {
|
||||||
|
if (this.IsGamepadButtonDown(b, i)) {
|
||||||
|
this.heldGamepadButtons[i] = b;
|
||||||
|
this.heldGamepadButtonStarts[i] = DateTime.UtcNow;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!this.IsGamepadButtonDown(this.heldGamepadButtons[i].Value, i)) {
|
||||||
|
this.heldGamepadButtons[i] = null;
|
||||||
|
} else {
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
var holdTime = now - this.heldGamepadButtonStarts[i];
|
||||||
|
if (holdTime >= this.KeyRepeatDelay) {
|
||||||
|
var diff = now - this.lastGamepadButtonRepeats[i];
|
||||||
|
if (diff >= this.KeyRepeatRate) {
|
||||||
|
this.lastGamepadButtonRepeats[i] = now;
|
||||||
|
this.triggerGamepadButtonRepeat[i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this.HandleTouch) {
|
if (this.HandleTouch) {
|
||||||
this.LastTouchState = this.TouchState;
|
this.LastTouchState = this.TouchState;
|
||||||
this.TouchState = TouchPanel.GetState();
|
this.TouchState = TouchPanel.GetState();
|
||||||
|
@ -137,7 +177,7 @@ namespace MLEM.Input {
|
||||||
|
|
||||||
public bool IsKeyPressed(Keys key) {
|
public bool IsKeyPressed(Keys key) {
|
||||||
// if the queried key is the held key and a repeat should be triggered, return true
|
// if the queried key is the held key and a repeat should be triggered, return true
|
||||||
if (key == this.heldKey && this.triggerRepeat)
|
if (this.HandleKeyboardRepeats && key == this.heldKey && this.triggerKeyRepeat)
|
||||||
return true;
|
return true;
|
||||||
return this.WasKeyUp(key) && this.IsKeyDown(key);
|
return this.WasKeyUp(key) && this.IsKeyDown(key);
|
||||||
}
|
}
|
||||||
|
@ -207,6 +247,15 @@ namespace MLEM.Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsGamepadButtonPressed(Buttons button, int index = -1) {
|
public bool IsGamepadButtonPressed(Buttons button, int index = -1) {
|
||||||
|
if (this.HandleGamepadRepeats) {
|
||||||
|
if (index < 0) {
|
||||||
|
for (var i = 0; i < this.ConnectedGamepads; i++)
|
||||||
|
if (this.heldGamepadButtons[i] == button && this.triggerGamepadButtonRepeat[i])
|
||||||
|
return true;
|
||||||
|
} else if (this.heldGamepadButtons[index] == button && this.triggerGamepadButtonRepeat[index]) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
return this.WasGamepadButtonUp(button, index) && this.IsGamepadButtonDown(button, index);
|
return this.WasGamepadButtonUp(button, index) && this.IsGamepadButtonDown(button, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
|
||||||
namespace MLEM.Misc {
|
namespace MLEM.Misc {
|
||||||
public static class EnumHelper {
|
public static class EnumHelper {
|
||||||
|
|
||||||
|
public static readonly Buttons[] Buttons = GetValues<Buttons>().ToArray();
|
||||||
|
public static readonly Keys[] Keys = GetValues<Keys>().ToArray();
|
||||||
|
|
||||||
public static IEnumerable<T> GetValues<T>() {
|
public static IEnumerable<T> GetValues<T>() {
|
||||||
return Enum.GetValues(typeof(T)).Cast<T>();
|
return Enum.GetValues(typeof(T)).Cast<T>();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue