1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 14:38:34 +01:00

added very basic touch support to the input handler

This commit is contained in:
Ellpeck 2019-08-29 18:12:02 +02:00
parent 0668d044b6
commit e04f2fbdd2

View file

@ -1,8 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using MLEM.Misc; using MLEM.Misc;
namespace MLEM.Input { namespace MLEM.Input {
@ -12,7 +14,7 @@ namespace MLEM.Input {
public KeyboardState LastKeyboardState { get; private set; } public KeyboardState LastKeyboardState { get; private set; }
public KeyboardState KeyboardState { get; private set; } public KeyboardState KeyboardState { get; private set; }
private readonly bool handleKeyboard; public bool HandleKeyboard;
public MouseState LastMouseState { get; private set; } public MouseState LastMouseState { get; private set; }
public MouseState MouseState { get; private set; } public MouseState MouseState { get; private set; }
@ -20,29 +22,38 @@ namespace MLEM.Input {
public Point LastMousePosition => this.LastMouseState.Position; public Point LastMousePosition => this.LastMouseState.Position;
public int ScrollWheel => this.MouseState.ScrollWheelValue; public int ScrollWheel => this.MouseState.ScrollWheelValue;
public int LastScrollWheel => this.LastMouseState.ScrollWheelValue; public int LastScrollWheel => this.LastMouseState.ScrollWheelValue;
private readonly bool handleMouse; public bool HandleMouse;
private readonly GamePadState[] lastGamepads = new GamePadState[GamePad.MaximumGamePadCount]; private readonly GamePadState[] lastGamepads = new GamePadState[GamePad.MaximumGamePadCount];
private readonly GamePadState[] gamepads = new GamePadState[GamePad.MaximumGamePadCount]; private readonly GamePadState[] gamepads = new GamePadState[GamePad.MaximumGamePadCount];
private readonly bool handleGamepads;
public int ConnectedGamepads { get; private set; } public int ConnectedGamepads { get; private set; }
public bool HandleGamepads;
public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true) { public TouchCollection LastTouchState { get; private set; }
this.handleKeyboard = handleKeyboard; public TouchCollection TouchState { get; private set; }
this.handleMouse = handleMouse; public readonly ReadOnlyCollection<GestureSample> Gestures;
this.handleGamepads = handleGamepads; private readonly List<GestureSample> gestures = new List<GestureSample>();
public bool HandleTouch;
public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true, bool handleTouch = true) {
this.HandleKeyboard = handleKeyboard;
this.HandleMouse = handleMouse;
this.HandleGamepads = handleGamepads;
this.HandleTouch = handleTouch;
this.Gestures = this.gestures.AsReadOnly();
} }
public void Update() { public void Update() {
if (this.handleKeyboard) { if (this.HandleKeyboard) {
this.LastKeyboardState = this.KeyboardState; this.LastKeyboardState = this.KeyboardState;
this.KeyboardState = Keyboard.GetState(); this.KeyboardState = Keyboard.GetState();
} }
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) {
for (var i = 0; i < GamePad.MaximumGamePadCount; i++) { for (var i = 0; i < GamePad.MaximumGamePadCount; i++) {
this.lastGamepads[i] = this.gamepads[i]; this.lastGamepads[i] = this.gamepads[i];
this.gamepads[i] = GamePad.GetState(i); this.gamepads[i] = GamePad.GetState(i);
@ -50,6 +61,14 @@ namespace MLEM.Input {
this.ConnectedGamepads = i; this.ConnectedGamepads = i;
} }
} }
if (this.HandleTouch) {
this.LastTouchState = this.TouchState;
this.TouchState = TouchPanel.GetState();
this.gestures.Clear();
while (TouchPanel.IsGestureAvailable)
this.gestures.Add(TouchPanel.ReadGesture());
}
} }
public GamePadState GetLastGamepadState(int index) { public GamePadState GetLastGamepadState(int index) {
@ -157,6 +176,14 @@ namespace MLEM.Input {
return this.WasGamepadButtonUp(button, index) && this.IsGamepadButtonDown(button, index); return this.WasGamepadButtonUp(button, index) && this.IsGamepadButtonDown(button, index);
} }
public GestureSample GetGesture(GestureType type) {
foreach (var gesture in this.Gestures) {
if (gesture.GestureType == type)
return gesture;
}
return default;
}
public bool IsDown(object control, int index = -1) { public bool IsDown(object control, int index = -1) {
if (control is Keys key) if (control is Keys key)
return this.IsKeyDown(key); return this.IsKeyDown(key);