1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using MLEM.Misc;
namespace MLEM.Input {
@ -12,7 +14,7 @@ namespace MLEM.Input {
public KeyboardState LastKeyboardState { get; private set; }
public KeyboardState KeyboardState { get; private set; }
private readonly bool handleKeyboard;
public bool HandleKeyboard;
public MouseState LastMouseState { get; private set; }
public MouseState MouseState { get; private set; }
@ -20,29 +22,38 @@ namespace MLEM.Input {
public Point LastMousePosition => this.LastMouseState.Position;
public int ScrollWheel => this.MouseState.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[] gamepads = new GamePadState[GamePad.MaximumGamePadCount];
private readonly bool handleGamepads;
public int ConnectedGamepads { get; private set; }
public bool HandleGamepads;
public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true) {
this.handleKeyboard = handleKeyboard;
this.handleMouse = handleMouse;
this.handleGamepads = handleGamepads;
public TouchCollection LastTouchState { get; private set; }
public TouchCollection TouchState { get; private set; }
public readonly ReadOnlyCollection<GestureSample> Gestures;
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() {
if (this.handleKeyboard) {
if (this.HandleKeyboard) {
this.LastKeyboardState = this.KeyboardState;
this.KeyboardState = Keyboard.GetState();
}
if (this.handleMouse) {
if (this.HandleMouse) {
this.LastMouseState = this.MouseState;
this.MouseState = Mouse.GetState();
}
if (this.handleGamepads) {
if (this.HandleGamepads) {
for (var i = 0; i < GamePad.MaximumGamePadCount; i++) {
this.lastGamepads[i] = this.gamepads[i];
this.gamepads[i] = GamePad.GetState(i);
@ -50,6 +61,14 @@ namespace MLEM.Input {
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) {
@ -157,6 +176,14 @@ namespace MLEM.Input {
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) {
if (control is Keys key)
return this.IsKeyDown(key);