-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.js
More file actions
278 lines (224 loc) · 6.46 KB
/
input.js
File metadata and controls
278 lines (224 loc) · 6.46 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { flush } from "./buffer";
import error from "./error";
import { get as getOption } from "./options";
import { get as getPrompt, scrollOrFocus, setDoScroll } from "./prompt";
// current mode of input the game expects: buffer, getkey, getline or endgame.
// null is no input accepted (during startup)
let inputMode = null;
// stores keypresses pressed when the engine isn't specifically expecting them ("buffer" inputMode)
const keypressBuffer = [];
let isTextPrinted = false;
// custom keypress hooks
let submitHook = null;
// is input blocked?
let blocked = false;
let keyResponse = () => {};
/**
* Prevents user input.
*/
export function block() {
blocked = true;
}
/**
* Returns the current input mode.
*
* @returns {string}
*/
export function getMode() {
return inputMode;
}
/**
* Returns the isTextPrinted flag.
*/
export function getIsTextPrinted() {
return isTextPrinted;
}
/**
* Registers listeners and hooks
*
* @param {object} opt
*/
export function init( opt ) {
// register hooks
if( typeof opt.expectHook === "function" ) {
keypress.addListener( opt.expectHook );
}
submitHook = opt.submitHook;
// listen to keypresses and mouse clicks
const keypressFunction = ( ...args ) => {
if( !blocked ) {
keypress.send( ...args );
}
};
document.addEventListener( "keydown", keypressFunction, false );
document.addEventListener( "click", keypressFunction, false );
}
/**
* Returns the input block status
*
* @returns {boolean} True if blocked
*/
export function isBlocked() {
return blocked;
}
const keypressListeners = [];
export const keypress = {
/**
* Add a listener that's called when the engine starts waiting for a keypress.
*
* @param {function} listener The function that's called
* @returns {function} A function that will remove the listener when called.
*/
addListener: function( listener ) {
keypressListeners.push( listener );
return () => keypress.removeListener( listener );
},
/**
* Called when the game starts.
*/
init: function() {
// start expecting keypresses
if( !inputMode ) {
inputMode = "buffer";
}
},
/**
* Check if there's a keypress waiting in the buffer.
*
* Called by the engine.
*
* @returns {boolean}
*/
isWaiting: function() {
flush();
if( isTextPrinted ) {
scrollOrFocus();
}
return keypressBuffer.length > 0;
},
/**
* Remove a keypress listener.
*
* @param {function} listener The function to remove
* @returns {boolean} True if the listener was found and removed, false if it wasn't found as a listener
*/
removeListener: function( listener ) {
const index = keypressListeners.indexOf( listener );
if( index === -1 ) {
return false;
}
keypressListeners.splice( index, 1 );
return true;
},
/**
* Send the keypress to the engine.
*
* @param {object} e The keypress event.
*/
send: function( e ) {
const keyCode = e.keyCode;
const isClick = e instanceof MouseEvent;
const doc = document.documentElement;
const scrolltop = ( window.pageYOffset || doc.scrollTop ) - ( doc.clientTop || 0 );
const promptHidden = !getPrompt().parentNode;
let shouldSendChar = false;
// don't react to modifier keys
if( e.altKey || e.ctrlKey || e.metaKey || e.shiftKey ) {
return;
}
switch( inputMode ) {
case "buffer":
keypressBuffer.push( keyCode );
break;
case "getline":
case null:
// do nothing except scroll
scrollOrFocus( e );
return;
case "getkey":
if( e.preventDefault ) {
e.preventDefault();
}
// continue with script
shouldSendChar = true;
break;
case "endgame":
window.location = getOption( "exit_url" );
return;
default:
error( "Interpreter error: unknown input mode " + inputMode );
}
// let the scroll handler take this if we're not at the end of the page
if( scrolltop + window.innerHeight < document.body.clientHeight - ( promptHidden ? 0 : 40 ) ) {
scrollOrFocus( e );
// If the force flag is set, continue with the action.
// Otherwise this was a user action and we can interpret it as
// "just scroll to bottom".
if( !e.force ) {
return;
}
}
inputMode = "buffer";
if( shouldSendChar ) {
if( typeof submitHook === "function" ) {
const hook = submitHook({
event: e.force ? null : e,
input: isClick ? null : keyCode,
original: isClick ? null : keyCode,
userAction: !e.force,
mouseClick: isClick
});
if( typeof hook === "object" && hook.then ) {
hook.then( () => window.Glk.sendChar( keyCode ) );
return;
}
}
if( window.Glk ) {
window.Glk.sendChar( keyCode );
}
else {
keyResponse( keyCode );
}
}
},
wait: function() {
inputMode = "getkey";
flush();
scrollOrFocus();
setDoScroll();
setTimeout( () => {
keypressListeners.forEach( listener => listener() );
// if there's something in the keypress buffer, "push" that key
if( keypressBuffer.length > 0 ) {
keypress.send({ keyCode: keypressBuffer.shift() });
}
}, 1 );
},
waitPromise: function() {
return new Promise( resolve => {
keyResponse = ( cmd ) => resolve( cmd );
});
}
};
/**
* Set a new input mode.
*
* @param mode
*/
export function setMode( mode ) {
inputMode = mode;
}
/**
* Makes a note that text has been printed on the screen since last check
*
* @param newState
*/
export function textWasPrinted( newState = true ) {
isTextPrinted = newState;
}
/**
* Unblock the UI.
*/
export function unblock() {
blocked = false;
}