This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathinput.js
756 lines (699 loc) · 29.7 KB
/
input.js
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/**
* @fileoverview Maps keyboard, mouse, and touch inputs to device inputs
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2019 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/devices/machine.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
/**
* @typedef {Config} InputConfig
* @property {string} class
* @property {Object} [bindings]
* @property {number} [version]
* @property {Array.<string>} [overrides]
* @property {Array.<number>} location
* @property {Array.<Array.<number>>} [map]
* @property {boolean} [drag]
* @property {boolean} [scroll]
* @property {boolean} [hexagonal]
* @property {number} [buttonDelay]
*/
/**
* @class {Input}
* @unrestricted
* @property {InputConfig} config
* @property {Array.<number>} location
* @property {Array.<Array.<number>>} map
* @property {boolean} fDrag
* @property {boolean} fScroll
* @property {boolean} fHexagonal
* @property {number} buttonDelay
* @property {{
* surface: HTMLImageElement|undefined
* }} bindings
*/
class Input extends Device {
/**
* Input(idMachine, idDevice, config)
*
* Sample config:
*
* "input": {
* "class": "Input",
* "location": [139, 325, 368, 478, 0.34, 0.5, 640, 853],
* "map": [
* ["2nd", "inv", "lnx", "\\b", "clr"],
* ["lrn", "xchg", "sq", "sqrt", "rcp"],
* ["sst", "sto", "rcl", "sum", "exp"],
* ["bst", "ee", "(", ")", "/"],
* ["gto", "7", "8", "9", "*"],
* ["sbr", "4", "5", "6", "-"],
* ["rst", "1", "2", "3", "+"],
* ["r/s", "0", ".", "+/-", "=|\\r"]
* ],
* "drag": false,
* "bindings": {
* "surface": "imageTI57",
* "power": "powerTI57",
* "reset": "resetTI57"
* }
* }
*
* A word about the "power" button: the page will likely use absolute positioning to overlay the HTML button
* onto the image of the physical button, and the temptation might be to use the style "display:none" to hide
* it, but "opacity:0" should be used instead, because otherwise our efforts to use it as focusable element
* may fail.
*
* @this {Input}
* @param {string} idMachine
* @param {string} idDevice
* @param {InputConfig} [config]
*/
constructor(idMachine, idDevice, config)
{
super(idMachine, idDevice, Input.VERSION, config);
this.time = /** @type {Time} */ (this.findDeviceByClass(Machine.CLASS.TIME));
this.onInput = null;
this.onPower = null;
this.onReset = null;
this.onHover = null;
/*
* If 'drag' is true, then the onInput() handler will be called whenever the current col and/or row
* changes, even if the mouse hasn't been released since the previous onInput() call.
*
* The default is false, because in general, allowing drag is a bad idea for calculator buttons. But
* I've made this an option for other input surfaces, like LED arrays, where you might want to turn a
* series of LEDs on or off.
*/
this.fDrag = this.getDefaultBoolean('drag', false);
/*
* If 'scroll' is true, then we do NOT call preventDefault() on touch events; this permits the input
* surface to be scrolled like any other part of the page. The default is false, because this has other
* side-effects (eg, inadvertent zooms).
*/
this.fScroll = this.getDefaultBoolean('scroll', false);
/*
* This is set on receipt of the first 'touch' event of any kind, and is used by the 'mouse' event
* handlers to disregard mouse events if set.
*/
this.fTouch = false;
let element = this.bindings[Input.BINDING.SURFACE];
if (element) {
/*
* The location array, eg:
*
* "location": [139, 325, 368, 478, 0.34, 0.5, 640, 853, 180, 418, 75, 36],
*
* contains the top left corner (xInput, yInput) and dimensions (cxInput, cyInput)
* of the input rectangle where the buttons described in the map are located, relative
* to the surface image. It also describes the average amount of horizontal and vertical
* space between buttons, as fractions of the average button width and height (hGap, vGap).
*
* With all that, we can now calculate the center lines for each column and row. This
* obviously assumes that all the buttons are evenly laid out in a perfect grid. For
* devices that don't have such a nice layout, a different location array format will
* have to be defined.
*
* NOTE: While element.naturalWidth and element.naturalHeight should, for all modern
* browsers, contain the surface image's dimensions as well, those values still might not
* be available if our constructor is called before the page's onload event has fired,
* so we allow them to be stored in the next two elements of the location array, too.
*
* Finally, the position and size of the device's power button may be stored in the array
* as well, in case some browsers refuse to generate onClickPower() events (eg, if they
* think the button is inaccessible/not visible).
*/
let location = this.config['location'];
this.xInput = location[0];
this.yInput = location[1];
this.cxInput = location[2];
this.cyInput = location[3];
this.hGap = location[4] || 1.0;
this.vGap = location[5] || 1.0;
this.cxSurface = location[6] || element.naturalWidth || this.cxInput;
this.cySurface = location[7] || element.naturalHeight || this.cyInput;
this.xPower = location[8] || 0;
this.yPower = location[9] || 0;
this.cxPower = location[10] || 0;
this.cyPower = location[11] || 0;
this.map = this.config['map'];
if (this.map) {
this.nRows = this.map.length;
this.nCols = this.map[0].length;
} else {
this.nCols = this.hGap;
this.nRows = this.vGap;
this.hGap = this.vGap = 0;
}
/*
* If 'hexagonal' is true, then we treat the input grid as hexagonal, where even rows of the associated
* display are offset.
*/
this.fHexagonal = this.getDefaultBoolean('hexagonal', false);
/*
* The 'buttonDelay' setting is only necessary for devices (ie, old calculator chips) that are either slow
* to respond and/or have debouncing logic that would otherwise be defeated.
*/
this.buttonDelay = this.getDefaultNumber('buttonDelay', 0);
/*
* To calculate the average button width (cxButton), we know that the overall width
* must equal the sum of all the button widths + the sum of all the button gaps:
*
* cxInput = nCols * cxButton + nCols * (cxButton * hGap)
*
* The number of gaps would normally be (nCols - 1), but we require that cxInput include
* only 1/2 the gap at the edges, too. Solving for cxButton:
*
* cxButton = cxInput / (nCols + nCols * hGap)
*/
this.cxButton = (this.cxInput / (this.nCols + this.nCols * this.hGap))|0;
this.cyButton = (this.cyInput / (this.nRows + this.nRows * this.vGap))|0;
this.cxGap = (this.cxButton * this.hGap)|0;
this.cyGap = (this.cyButton * this.vGap)|0;
/*
* xStart and yStart record the last 'touchstart' or 'mousedown' position on the surface
* image; they will be reset to -1 when movement has ended (eg, 'touchend' or 'mouseup').
*/
this.xStart = this.yStart = -1;
this.captureMouse(element);
this.captureTouch(element);
if (this.time) {
/*
* We use a timer for the touch/mouse release events, to ensure that the machine had
* enough time to notice the input before releasing it.
*/
let input = this;
if (this.buttonDelay) {
this.timerInputRelease = this.time.addTimer("timerInputRelease", function onInputRelease() {
if (input.xStart < 0 && input.yStart < 0) { // auto-release ONLY if it's REALLY released
input.setPosition(-1, -1);
}
});
}
if (this.map) {
/*
* This auto-releases the last key reported after an appropriate delay, to ensure that
* the machine had enough time to notice the corresponding button was pressed.
*/
if (this.buttonDelay) {
this.timerKeyRelease = this.time.addTimer("timerKeyRelease", function onKeyRelease() {
input.onKeyTimer();
});
}
/*
* I used to maintain a single-key buffer (this.keyPressed) and would immediately release
* that key as soon as another key was pressed, but it appears that the ROM wants a minimum
* delay between release and the next press -- probably for de-bouncing purposes. So we
* maintain a key state: 0 means no key has gone down or up recently, 1 means a key just went
* down, and 2 means a key just went up. keysPressed maintains a queue of keys (up to 16)
* received while key state is non-zero.
*/
this.keyState = 0;
this.keysPressed = [];
/*
* I'm attaching my 'keypress' handlers to the document object, since image elements are
* not focusable. I'm disinclined to do what I've done with other machines (ie, create an
* invisible <textarea> overlay), because in this case, I don't really want a soft keyboard
* popping up and obscuring part of the display.
*
* A side-effect, however, is that if the user attempts to explicitly give the image
* focus, we don't have anything for focus to attach to. We address that in onMouseDown(),
* by redirecting focus to the "power" button, if any, not because we want that or any other
* button to have focus, but simply to remove focus from any other input element on the page.
*/
this.captureKeys(document);
}
}
/*
* Finally, the active input state. If there is no active input, col and row are -1. After
* this point, these variables will be updated by setPosition().
*/
this.col = this.row = -1;
}
}
/**
* addBinding(binding, element)
*
* @this {Input}
* @param {string} binding
* @param {Element} element
*/
addBinding(binding, element)
{
let input = this;
switch(binding) {
case Input.BINDING.POWER:
element.onclick = function onClickPower() {
if (input.onPower) input.onPower();
};
break;
case Input.BINDING.RESET:
element.onclick = function onClickReset() {
if (input.onReset) input.onReset();
};
break;
}
super.addBinding(binding, element);
}
/**
* addClick(onPower, onReset)
*
* Called by the Chip device to set up power and reset notifications.
*
* @this {Input}
* @param {function()} [onPower] (called when the "power" button, if any, is clicked)
* @param {function()} [onReset] (called when the "reset" button, if any, is clicked)
*/
addClick(onPower, onReset)
{
this.onPower = onPower;
this.onReset = onReset;
}
/**
* addHover(onHover)
*
* @this {Input}
* @param {function(number, number)} onHover
*/
addHover(onHover)
{
this.onHover = onHover;
}
/**
* addInput(onInput)
*
* Called by the Chip device to set up input notifications.
*
* @this {Input}
* @param {function(number,number)} onInput
*/
addInput(onInput)
{
this.onInput = onInput;
}
/**
* advanceKeyState()
*
* @this {Input}
*/
advanceKeyState()
{
if (!this.buttonDelay) {
this.onKeyTimer();
} else {
this.time.setTimer(this.timerKeyRelease, this.buttonDelay);
}
}
/**
* captureKeys(element)
*
* @this {Input}
* @param {Document|Element} element
*/
captureKeys(element)
{
let input = this;
element.addEventListener(
'keydown',
function onKeyDown(event) {
event = event || window.event;
let activeElement = document.activeElement;
if (activeElement == input.bindings[Input.BINDING.POWER]) {
let keyCode = event.which || event.keyCode;
let ch = Input.KEYCODE[keyCode];
if (ch && input.onKeyPress(ch)) event.preventDefault();
}
}
);
element.addEventListener(
'keypress',
function onKeyPress(event) {
event = event || window.event;
let charCode = event.which || event.charCode;
let ch = String.fromCharCode(charCode);
if (ch && input.onKeyPress(ch)) event.preventDefault();
}
);
}
/**
* captureMouse(element)
*
* @this {Input}
* @param {HTMLImageElement} element
*/
captureMouse(element)
{
let input = this;
element.addEventListener(
'mousedown',
function onMouseDown(event) {
if (input.fTouch) return;
/*
* If there are any text input elements on the page that might currently have focus,
* this is a good time to divert focus to a focusable element of our own (eg, a "power"
* button). Otherwise, key presses could be confusingly processed in two places.
*
* Unfortunately, setting focus on an element can cause the browser to scroll the element
* into view, so to avoid that, we use the following scrollTo() work-around.
*/
let button = input.bindings[Input.BINDING.POWER];
if (button) {
let x = window.scrollX, y = window.scrollY;
button.focus();
window.scrollTo(x, y);
}
if (!event.button) {
input.processEvent(element, Input.ACTION.PRESS, event);
}
}
);
element.addEventListener(
'mousemove',
function onMouseMove(event) {
if (input.fTouch) return;
input.processEvent(element, Input.ACTION.MOVE, event);
}
);
element.addEventListener(
'mouseup',
function onMouseUp(event) {
if (input.fTouch) return;
if (!event.button) {
input.processEvent(element, Input.ACTION.RELEASE, event);
}
}
);
element.addEventListener(
'mouseout',
function onMouseOut(event) {
if (input.fTouch) return;
if (input.xStart < 0) {
input.processEvent(element, Input.ACTION.MOVE, event);
} else {
input.processEvent(element, Input.ACTION.RELEASE, event);
}
}
);
}
/**
* captureTouch(element)
*
* @this {Input}
* @param {HTMLImageElement} element
*/
captureTouch(element)
{
let input = this;
/*
* NOTE: The mouse event handlers below deal only with events where the left button is involved
* (ie, left button is pressed, down, or released).
*/
element.addEventListener(
'touchstart',
function onTouchStart(event) {
/*
* Under normal circumstances (ie, when fScroll is false), when any touch events arrive,
* processEvent() calls preventDefault(), which prevents a variety of potentially annoying
* behaviors (ie, zooming, scrolling, fake mouse events, etc). Under non-normal circumstances,
* (ie, when fScroll is true), we set fTouch on receipt of a 'touchstart' event, which will
* help our mouse event handlers avoid any redundant actions due to fake mouse events.
*/
if (input.fScroll) input.fTouch = true;
input.processEvent(element, Input.ACTION.PRESS, event);
}
);
element.addEventListener(
'touchmove',
function onTouchMove(event) {
input.processEvent(element, Input.ACTION.MOVE, event);
}
);
element.addEventListener(
'touchend',
function onTouchEnd(event) {
input.processEvent(element, Input.ACTION.RELEASE, event);
}
);
}
/**
* onKeyPress(ch)
*
* @this {Input}
* @param {string} ch
* @returns {boolean} (true if processed, false if not)
*/
onKeyPress(ch)
{
for (let row = 0; row < this.map.length; row++) {
let rowMap = this.map[row];
for (let col = 0; col < rowMap.length; col++) {
let aParts = rowMap[col].split('|');
if (aParts.indexOf(ch) >= 0) {
if (this.keyState) {
if (this.keysPressed.length < 16) {
this.keysPressed.push(ch);
}
} else {
this.keyState = 1;
this.setPosition(col, row);
this.advanceKeyState();
}
return true;
}
}
}
this.printf("unrecognized key '%s' (0x%02x)\n", ch, ch.charCodeAt(0));
return false;
}
/**
* onKeyTimer()
*
* @this {Input}
*/
onKeyTimer()
{
this.assert(this.keyState);
if (this.keyState == 1) {
this.keyState++;
this.setPosition(-1, -1);
this.advanceKeyState();
} else {
this.keyState = 0;
if (this.keysPressed.length) {
this.onKeyPress(this.keysPressed.shift());
}
}
}
/**
* processEvent(element, action, event)
*
* @this {Input}
* @param {HTMLImageElement} element
* @param {number} action
* @param {Event|MouseEvent|TouchEvent} [event] (eg, the object from a 'touch' or 'mouse' event)
*/
processEvent(element, action, event)
{
let col = -1, row = -1;
let fMultiTouch = false;
let x, y, xInput, yInput, fButton, fInput, fPower;
if (action < Input.ACTION.RELEASE) {
/**
* @name Event
* @property {Array} targetTouches
*/
event = event || window.event;
if (!event.targetTouches || !event.targetTouches.length) {
x = event.pageX;
y = event.pageY;
} else {
x = event.targetTouches[0].pageX;
y = event.targetTouches[0].pageY;
fMultiTouch = (event.targetTouches.length > 1);
}
/*
* Touch coordinates (that is, the pageX and pageY properties) are relative to the page, so to make
* them relative to the element, we must subtract the element's left and top positions. This Apple web page:
*
* https://developer.apple.com/library/safari/documentation/AudioVideo/Conceptual/HTML-canvas-guide/AddingMouseandTouchControlstoCanvas/AddingMouseandTouchControlstoCanvas.html
*
* makes it sound simple, but it turns out we have to walk the element's entire "parentage" of DOM elements
* to get the exact offsets.
*/
let xOffset = 0;
let yOffset = 0;
let elementNext = element;
do {
if (!isNaN(elementNext.offsetLeft)) {
xOffset += elementNext.offsetLeft;
yOffset += elementNext.offsetTop;
}
} while ((elementNext = elementNext.offsetParent));
/*
* Due to the responsive nature of our pages, the displayed size of the surface image may be smaller than
* the original size, and the coordinates we receive from events are based on the currently displayed size.
*/
x = ((x - xOffset) * (this.cxSurface / element.offsetWidth))|0;
y = ((y - yOffset) * (this.cySurface / element.offsetHeight))|0;
xInput = x - this.xInput;
yInput = y - this.yInput;
/*
* fInput is set if the event occurred somewhere within the input region (ie, the calculator keypad),
* either on a button or between buttons, whereas fButton is set if the event occurred squarely (rectangularly?)
* on a button. fPower deals separately with the power button; it is set if the event occurred on the
* power button.
*/
fInput = fButton = false;
fPower = (x >= this.xPower && x < this.xPower + this.cxPower && y >= this.yPower && y < this.yPower + this.cyPower);
/*
* I use the top of the input region, less some gap, to calculate a dividing line, above which
* default actions should be allowed, and below which they should not. Ditto for any event inside
* the power button.
*/
if (xInput >= 0 && xInput < this.cxInput && yInput + this.cyGap >= 0 || fPower) {
/*
* If we allow touch events to be processed, they will generate mouse events as well, causing
* confusion and delays. We can sidestep that problem by preventing default actions on any event
* that occurs within the input region. One downside is that you can no longer scroll or zoom the
* image using touch, but that may be just as well, because you probably don't want sloppy touches
* moving your display around (or worse, a rapid double-tap zooming the display). I do try to
* make one small concession for two-finger zoom operations (see fMultiTouch), but that's a bit
* fiddly, because it depends on both fingers hitting the surface at the same instant.
*/
if (!fMultiTouch && !this.fScroll) event.preventDefault();
if (xInput >= 0 && xInput < this.cxInput && yInput >= 0 && yInput < this.cyInput) {
fInput = true;
/*
* The width and height of each column and row could be determined by computing cxGap + cxButton
* and cyGap + cyButton, respectively, but those gap and button sizes are merely estimates, and should
* only be used to help with the final button coordinate checks farther down.
*/
let cxCol = (this.cxInput / this.nCols) | 0;
let cyCol = (this.cyInput / this.nRows) | 0;
let colInput = (xInput / cxCol) | 0;
let rowInput = (yInput / cyCol) | 0;
/*
* If the grid is hexagonal (aka "Lite-Brite" mode), then the cells of even-numbered rows are
* offset horizontally by 1/2 cell. In addition, the last cell in those rows is unused, so if
* after compensating by 1/2 cell, the target column is the last cell, we set xInput to -1,
* effectively ignoring input on that cell.
*/
if (this.fHexagonal && !(rowInput & 0x1)) {
xInput -= (cxCol >> 1);
colInput = (xInput / cxCol) | 0;
if (colInput == this.nCols - 1) xInput = -1;
}
/*
* (xCol,yCol) will be the top left corner of the button closest to the point of input. However, that's
* based on our gap estimate. If things seem "too tight", shrink the gap estimates, which will automatically
* increase the button size estimates.
*/
let xCol = colInput * cxCol + (this.cxGap >> 1);
let yCol = rowInput * cyCol + (this.cyGap >> 1);
xInput -= xCol;
yInput -= yCol;
if (xInput >= 0 && xInput < this.cxButton && yInput >= 0 && yInput < this.cyButton) {
col = colInput;
row = rowInput;
fButton = true;
}
}
}
}
if (fMultiTouch) return;
if (action == Input.ACTION.PRESS) {
/*
* Record the position of the event, transitioning xStart and yStart to non-negative values.
*/
this.xStart = x;
this.yStart = y;
if (fInput) {
/*
* The event occurred in the input region, so we call setPosition() regardless of whether
* it hit or missed a button.
*/
this.setPosition(col, row);
/*
* On the other hand, if it DID hit a button, then we arm the auto-release timer, to ensure
* a minimum amount of time (ie, BUTTON_DELAY).
*/
if (fButton && this.buttonDelay) {
this.time.setTimer(this.timerInputRelease, this.buttonDelay, true);
}
} else if (fPower && this.onPower) {
this.onPower();
}
}
else if (action == Input.ACTION.MOVE) {
if (this.xStart >= 0 && this.yStart >= 0 && this.fDrag) {
this.setPosition(col, row);
}
else if (this.onHover) {
this.onHover(col, row);
}
}
else if (action == Input.ACTION.RELEASE) {
/*
* Don't immediately signal the release if the release timer is active (let the timer take care of it).
*/
if (!this.buttonDelay || !this.time.isTimerSet(this.timerInputRelease)) {
this.setPosition(-1, -1);
}
this.xStart = this.yStart = -1;
}
else {
this.println("unrecognized action: " + action);
}
}
/**
* setPosition(col, row)
*
* @this {Input}
* @param {number} col
* @param {number} row
*/
setPosition(col, row)
{
if (col != this.col || row != this.row) {
this.col = col;
this.row = row;
if (this.onInput) this.onInput(col, row);
}
}
}
Input.ACTION = {
PRESS: 1, // eg, an action triggered by a 'mousedown' or 'touchstart' event
MOVE: 2, // eg, an action triggered by a 'mousemove' or 'touchmove' event
RELEASE: 3 // eg, an action triggered by a 'mouseup' (or 'mouseout') or 'touchend' event
};
Input.BINDING = {
POWER: "power",
RESET: "reset",
SURFACE: "surface"
};
Input.KEYCODE = { // keyCode from keydown/keyup events
0x08: "\b" // backspace
};
Input.BUTTON_DELAY = 50; // minimum number of milliseconds to ensure between button presses and releases
Input.VERSION = +VERSION || 1.00;