-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputHandler.cs
124 lines (112 loc) · 5.9 KB
/
InputHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
namespace Game1 {
public static class InputHandler {
public static Subject<KeyboardState> KeyboardStatesStream { get; set; }
public static IObservable<KeyboardState> DistinctKeyboardStatesStream { get; }
private static readonly Subject<Keys[]> pressedKeys;
private static readonly IObservable<Keys[]> distinctPressedKeys;
private static readonly IObservable<IList<Keys[]>> distinctPressedKeyArrayBuffer;
private static readonly Random randomKeySelector;
private static Keys latestHorizontalArrowKey;
private static Keys latestVerticalArrowKey;
private static List<Keys[]> horizontalKeyArrayBuffer;
private static List<Keys[]> verticalKeyArrayBuffer;
private static (Keys key1, Keys key2) horizontalCounterKeys = (key1: Keys.Left, key2: Keys.Right);
private static (Keys key1, Keys key2) verticalCounterKeys = (key1: Keys.Up, key2: Keys.Down);
private static readonly IObserver<IList<Keys[]>> horizontalInputFilterObserver = Observer
.Create(SetHorizontalKeyArrayBuffer(horizontalCounterKeys));
private static readonly IObserver<IList<Keys[]>> verticalInputFilterObserver = Observer
.Create(SetVerticalKeyArrayBuffer(verticalCounterKeys));
public static BehaviorSubject<Keys[]> InputOutKeys { get; }
public static BehaviorSubject<Keys> LatestHorizontalArrowKey { get; }
public static BehaviorSubject<Keys> LatestVerticalArrowKey { get; }
static InputHandler() {
randomKeySelector = new Random();
KeyboardStatesStream = new Subject<KeyboardState>();
pressedKeys = new Subject<Keys[]>();
DistinctKeyboardStatesStream = KeyboardStatesStream.DistinctUntilChanged();
distinctPressedKeys = pressedKeys.DistinctUntilChanged();
InputOutKeys = new BehaviorSubject<Keys[]>(new Keys[1]);
horizontalKeyArrayBuffer = new List<Keys[]>();
verticalKeyArrayBuffer = new List<Keys[]>();
LatestHorizontalArrowKey = new BehaviorSubject<Keys>(Keys.None);
LatestVerticalArrowKey = new BehaviorSubject<Keys>(Keys.None);
KeyboardStatesStream.Subscribe(onNext: state => pressedKeys.OnNext(state.GetPressedKeys()));
pressedKeys.Subscribe(onNext: FilterKeys);
distinctPressedKeyArrayBuffer = distinctPressedKeys.Buffer(2, 1);
distinctPressedKeyArrayBuffer.Subscribe(horizontalInputFilterObserver);
distinctPressedKeyArrayBuffer.Subscribe(verticalInputFilterObserver);
distinctPressedKeyArrayBuffer.Subscribe(HandleConflictingInput);
}
private static Action<IList<Keys[]>> HandleConflictingInput => _ => {
SetLatestKey(horizontalKeyArrayBuffer, ref latestHorizontalArrowKey);
SetLatestKey(verticalKeyArrayBuffer, ref latestVerticalArrowKey);
};
private static Action<IList<Keys[]>> SetHorizontalKeyArrayBuffer(
(Keys key1, Keys key2) counterKeys) {
return array => {
List<Keys[]> newKeyArray = BuildCounterKeyArrayBuffer(counterKeys, array);
horizontalKeyArrayBuffer = newKeyArray;
};
}
private static Action<IList<Keys[]>> SetVerticalKeyArrayBuffer(
(Keys key1, Keys key2) counterKeys) {
return array => {
List<Keys[]> newKeyArray = BuildCounterKeyArrayBuffer(counterKeys, array);
verticalKeyArrayBuffer = newKeyArray;
};
}
private static List<Keys[]> BuildCounterKeyArrayBuffer((Keys key1, Keys key2) counterKeys, IList<Keys[]> array) {
var newKeyArray = new List<Keys[]>();
foreach (Keys[] keyArray in array) {
var outKeys = (from Keys key in keyArray
where key == counterKeys.key1
|| key == counterKeys.key2
select key).ToArray();
newKeyArray.Add(outKeys);
}
return newKeyArray;
}
private static void SetLatestKey(List<Keys[]> keyArrayBuffer, ref Keys key) {
if (keyArrayBuffer?.Count == 2) {
if (keyArrayBuffer[1].Length == 0) {
key = Keys.None;
}
else if (keyArrayBuffer[0].SequenceEqual(keyArrayBuffer[1]) && keyArrayBuffer[0].Length == 1) {
key = keyArrayBuffer[1].Single();
}
else if (keyArrayBuffer[1].Length > keyArrayBuffer[0].Length) {
try {
key = keyArrayBuffer[1].Except(keyArrayBuffer[0].DefaultIfEmpty()).Single();
}
catch (InvalidOperationException) {
key = keyArrayBuffer[1][randomKeySelector.Next(0, keyArrayBuffer[1].Length - 1)];
}
}
else if (keyArrayBuffer[0].Length > keyArrayBuffer[1].Length) {
key = keyArrayBuffer[1].Single();
}
}
}
public static void Update() {
KeyboardStatesStream.OnNext(Keyboard.GetState());
LatestHorizontalArrowKey.OnNext(latestHorizontalArrowKey);
LatestVerticalArrowKey.OnNext(latestVerticalArrowKey);
}
private static void FilterKeys(Keys[] keys) {
var filteredKeys = (from key in keys
where key != Keys.Up
&& key != Keys.Down
&& key != Keys.Left
&& key != Keys.Right
select key).ToArray();
InputOutKeys.OnNext(filteredKeys);
}
}
}