-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateInput.ts
More file actions
248 lines (220 loc) · 7.02 KB
/
Copy pathcreateInput.ts
File metadata and controls
248 lines (220 loc) · 7.02 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import {
Observable,
OperatorFunction,
Subject,
bufferCount,
distinctUntilChanged,
filter,
fromEvent,
map,
merge,
pairwise,
pipe,
scan,
throttleTime,
} from "rxjs";
import { observable, reaction, runInAction } from "mobx";
// DIRECTLY COPIED FROM LATTICEXYZ/PHASERX
// https://github.com/latticexyz/mud/blob/main/packages/phaserx/src/createInput.ts
import Phaser from "phaser";
function filterNullish<T>(): OperatorFunction<T, NonNullable<T>> {
return pipe<Observable<T>, Observable<NonNullable<T>>>(
filter<T>((x: T) => x != null) as OperatorFunction<T, NonNullable<T>>
);
}
type Area = {
x: number;
y: number;
width: number;
height: number;
};
export type Key =
| keyof typeof Phaser.Input.Keyboard.KeyCodes
| "POINTER_LEFT"
| "POINTER_RIGHT";
export function createInput(inputPlugin: Phaser.Input.InputPlugin) {
const disposers = new Set<() => void>();
const enabled = { current: true };
inputPlugin.mouse?.disableContextMenu();
function disableInput() {
enabled.current = false;
}
function enableInput() {
enabled.current = true;
}
function setCursor(cursor: string) {
inputPlugin.setDefaultCursor(cursor);
}
const keyboard$ = new Subject<Phaser.Input.Keyboard.Key>();
const pointermove$ = fromEvent(document, "mousemove").pipe(
filter(() => enabled.current),
map(() => {
return { pointer: inputPlugin.manager?.activePointer };
}),
filterNullish()
);
const pointerdown$: Observable<{
pointer: Phaser.Input.Pointer;
event: MouseEvent;
}> = fromEvent(document, "mousedown").pipe(
filter(() => enabled.current),
map((event) => ({
pointer: inputPlugin.manager?.activePointer,
event: event as MouseEvent,
})),
filterNullish()
);
const pointerup$: Observable<{
pointer: Phaser.Input.Pointer;
event: MouseEvent;
}> = fromEvent(document, "mouseup").pipe(
filter(() => enabled.current),
map((event) => ({
pointer: inputPlugin.manager?.activePointer,
event: event as MouseEvent,
})),
filterNullish()
);
// Click stream
const click$ = merge(pointerdown$, pointerup$).pipe(
filter(() => enabled.current),
map<
{ pointer: Phaser.Input.Pointer; event: MouseEvent },
[boolean, number]
>(({ event }) => [
event.type === "mousedown" && event.button === 0,
Date.now(),
]), // Map events to whether the left button is down and the current timestamp
bufferCount(2, 1), // Store the last two timestamps
filter(([prev, now]) => prev[0] && !now[0] && now[1] - prev[1] < 250), // Only care if button was pressed before and is not anymore and it happened within 500ms
map(() => inputPlugin.manager?.activePointer), // Return the current pointer
filterNullish()
);
// Double click stream
const doubleClick$ = pointerdown$.pipe(
filter(() => enabled.current),
map(() => Date.now()), // Get current timestamp
bufferCount(2, 1), // Store the last two timestamps
filter(([prev, now]) => now - prev < 500), // Filter clicks with more than 500ms distance
throttleTime(500), // A third click within 500ms is not counted as another double click
map(() => inputPlugin.manager?.activePointer), // Return the current pointer
filterNullish()
);
// Right click stream
const rightClick$ = merge(pointerdown$, pointerup$).pipe(
filter(({ pointer }) => enabled.current && pointer.rightButtonDown()),
map(() => inputPlugin.manager?.activePointer), // Return the current pointer
filterNullish()
);
// Drag stream
const drag$ = merge(
pointerdown$.pipe(map(() => undefined)), // Reset the drag on left click
merge(pointerup$, pointermove$).pipe(
pairwise(), // Take the last two move or pointerup events
scan<
[{ pointer: Phaser.Input.Pointer }, { pointer: Phaser.Input.Pointer }],
Area | undefined
>(
(acc, [{ pointer: prev }, { pointer: curr }]) =>
curr.leftButtonDown() // If the left butten is pressed...
? prev.leftButtonDown() && acc // If the previous event wasn't mouseup and if the drag already started...
? {
...acc,
width: curr.worldX - acc.x,
height: curr.worldY - acc.y,
} // Update the width/height
: { x: curr.worldX, y: curr.worldY, width: 0, height: 0 } // Else start the drag
: undefined,
undefined
),
filterNullish(),
filter((area) => Math.abs(area.width) > 10 && Math.abs(area.height) > 10) // Prevent clicking to be mistaken as a drag
)
).pipe(
filter(() => enabled.current),
distinctUntilChanged() // Prevent same value to be emitted in a row
);
const pressedKeys = observable(new Set<Key>());
const phaserKeyboard = inputPlugin.keyboard;
const codeToKey = new Map<number, Key>();
// Listen to all keys
for (const key of Object.keys(Phaser.Input.Keyboard.KeyCodes)) addKey(key);
// Subscriptions
const keySub = keyboard$
.pipe(filter(() => enabled.current))
.subscribe((key) => {
const keyName = codeToKey.get(key.keyCode);
if (!keyName) return;
runInAction(() => {
if (key.isDown) pressedKeys.add(keyName);
if (key.isUp) pressedKeys.delete(keyName);
});
});
disposers.add(() => keySub?.unsubscribe());
const pointerSub = merge(pointerdown$, pointerup$).subscribe(
({ pointer }) => {
runInAction(() => {
if (pointer.leftButtonDown()) pressedKeys.add("POINTER_LEFT");
else pressedKeys.delete("POINTER_LEFT");
if (pointer.rightButtonDown()) pressedKeys.add("POINTER_RIGHT");
else pressedKeys.delete("POINTER_RIGHT");
});
//
}
);
disposers.add(() => pointerSub?.unsubscribe());
// Adds a key to include in the state
function addKey(key: string) {
if (!phaserKeyboard) {
console.warn(`Adding key ${key} failed. No phaser keyboard detected.`);
return;
}
// Add the key to the phaser keyboard input plugin
const keyObj = phaserKeyboard.addKey(key, false);
// Store the cleartext key map
codeToKey.set(keyObj.keyCode, key as Key);
keyObj.removeAllListeners();
keyObj.emitOnRepeat = true;
keyObj.on("down", (keyEvent: Phaser.Input.Keyboard.Key) =>
keyboard$.next(keyEvent)
);
keyObj.on("up", (keyEvent: Phaser.Input.Keyboard.Key) =>
keyboard$.next(keyEvent)
);
}
function onKeyPress(
keySelector: (pressedKeys: Set<Key>) => boolean,
callback: () => void
) {
const disposer = reaction(
() => keySelector(pressedKeys),
(passes) => {
if (passes) callback();
},
{ fireImmediately: true }
);
disposers.add(disposer);
}
function dispose() {
for (const disposer of disposers) {
disposer();
}
}
return {
keyboard$: keyboard$.asObservable(),
pointermove$,
pointerdown$,
pointerup$,
click$,
doubleClick$,
rightClick$,
drag$,
pressedKeys,
dispose,
disableInput,
enableInput,
setCursor,
enabled,
onKeyPress,
};
}