/
HTML5Application.hx
668 lines (591 loc) · 15 KB
/
HTML5Application.hx
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
package lime._internal.backend.html5;
import js.html.DeviceMotionEvent;
import js.html.KeyboardEvent;
import js.Browser;
import lime.app.Application;
import lime.media.AudioManager;
import lime.system.Sensor;
import lime.system.SensorType;
import lime.ui.GamepadAxis;
import lime.ui.KeyCode;
import lime.ui.KeyModifier;
import lime.ui.Gamepad;
import lime.ui.GamepadButton;
import lime.ui.Joystick;
import lime.ui.Window;
@:access(lime._internal.backend.html5.HTML5Window)
@:access(lime.app.Application)
@:access(lime.system.Sensor)
@:access(lime.ui.Gamepad)
@:access(lime.ui.Joystick)
@:access(lime.ui.Window)
class HTML5Application
{
private var accelerometer:Sensor;
private var currentUpdate:Float;
private var deltaTime:Float;
private var framePeriod:Float;
private var gameDeviceCache = new Map<Int, GameDeviceData>();
private var hidden:Bool;
private var lastUpdate:Float;
private var nextUpdate:Float;
private var parent:Application;
#if stats
private var stats:Dynamic;
#end
public inline function new(parent:Application)
{
this.parent = parent;
currentUpdate = 0;
lastUpdate = 0;
nextUpdate = 0;
framePeriod = -1;
AudioManager.init();
accelerometer = Sensor.registerSensor(SensorType.ACCELEROMETER, 0);
}
private function convertKeyCode(keyCode:Int):KeyCode
{
if (keyCode >= 65 && keyCode <= 90)
{
return keyCode + 32;
}
switch (keyCode)
{
case 12:
return KeyCode.CLEAR;
case 16:
return KeyCode.LEFT_SHIFT;
case 17:
return KeyCode.LEFT_CTRL;
case 18:
return KeyCode.LEFT_ALT;
case 19:
return KeyCode.PAUSE;
case 20:
return KeyCode.CAPS_LOCK;
case 33:
return KeyCode.PAGE_UP;
case 34:
return KeyCode.PAGE_DOWN;
case 35:
return KeyCode.END;
case 36:
return KeyCode.HOME;
case 37:
return KeyCode.LEFT;
case 38:
return KeyCode.UP;
case 39:
return KeyCode.RIGHT;
case 40:
return KeyCode.DOWN;
case 41:
return KeyCode.SELECT;
case 43:
return KeyCode.EXECUTE;
case 44:
return KeyCode.PRINT_SCREEN;
case 45:
return KeyCode.INSERT;
case 46:
return KeyCode.DELETE;
case 91:
return KeyCode.LEFT_META;
case 92:
return KeyCode.RIGHT_META;
case 93:
return KeyCode.RIGHT_META; // this maybe should be APPLICATION if on Windows
case 95:
return KeyCode.SLEEP;
case 96:
return KeyCode.NUMPAD_0;
case 97:
return KeyCode.NUMPAD_1;
case 98:
return KeyCode.NUMPAD_2;
case 99:
return KeyCode.NUMPAD_3;
case 100:
return KeyCode.NUMPAD_4;
case 101:
return KeyCode.NUMPAD_5;
case 102:
return KeyCode.NUMPAD_6;
case 103:
return KeyCode.NUMPAD_7;
case 104:
return KeyCode.NUMPAD_8;
case 105:
return KeyCode.NUMPAD_9;
case 106:
return KeyCode.NUMPAD_MULTIPLY;
case 107:
return KeyCode.NUMPAD_PLUS;
case 108:
return KeyCode.NUMPAD_PERIOD;
case 109:
return KeyCode.NUMPAD_MINUS;
case 110:
return KeyCode.NUMPAD_PERIOD;
case 111:
return KeyCode.NUMPAD_DIVIDE;
case 112:
return KeyCode.F1;
case 113:
return KeyCode.F2;
case 114:
return KeyCode.F3;
case 115:
return KeyCode.F4;
case 116:
return KeyCode.F5;
case 117:
return KeyCode.F6;
case 118:
return KeyCode.F7;
case 119:
return KeyCode.F8;
case 120:
return KeyCode.F9;
case 121:
return KeyCode.F10;
case 122:
return KeyCode.F11;
case 123:
return KeyCode.F12;
case 124:
return KeyCode.F13;
case 125:
return KeyCode.F14;
case 126:
return KeyCode.F15;
case 127:
return KeyCode.F16;
case 128:
return KeyCode.F17;
case 129:
return KeyCode.F18;
case 130:
return KeyCode.F19;
case 131:
return KeyCode.F20;
case 132:
return KeyCode.F21;
case 133:
return KeyCode.F22;
case 134:
return KeyCode.F23;
case 135:
return KeyCode.F24;
case 144:
return KeyCode.NUM_LOCK;
case 145:
return KeyCode.SCROLL_LOCK;
case 160:
return KeyCode.CARET;
case 161:
return KeyCode.EXCLAMATION;
case 163:
return KeyCode.HASH;
case 164:
return KeyCode.DOLLAR;
case 166:
return KeyCode.APP_CONTROL_BACK;
case 167:
return KeyCode.APP_CONTROL_FORWARD;
case 168:
return KeyCode.APP_CONTROL_REFRESH;
case 169:
return KeyCode.RIGHT_PARENTHESIS; // is this correct?
case 170:
return KeyCode.ASTERISK;
case 171:
return KeyCode.GRAVE;
case 172:
return KeyCode.HOME;
case 173:
return KeyCode.MINUS; // or mute/unmute?
case 174:
return KeyCode.VOLUME_DOWN;
case 175:
return KeyCode.VOLUME_UP;
case 176:
return KeyCode.AUDIO_NEXT;
case 177:
return KeyCode.AUDIO_PREVIOUS;
case 178:
return KeyCode.AUDIO_STOP;
case 179:
return KeyCode.AUDIO_PLAY;
case 180:
return KeyCode.MAIL;
case 181:
return KeyCode.AUDIO_MUTE;
case 182:
return KeyCode.VOLUME_DOWN;
case 183:
return KeyCode.VOLUME_UP;
case 186:
return KeyCode.SEMICOLON; // or ñ?
case 187:
return KeyCode.EQUALS;
case 188:
return KeyCode.COMMA;
case 189:
return KeyCode.MINUS;
case 190:
return KeyCode.PERIOD;
case 191:
return KeyCode.SLASH;
case 192:
return KeyCode.GRAVE;
case 193:
return KeyCode.QUESTION;
case 194:
return KeyCode.NUMPAD_PERIOD;
case 219:
return KeyCode.LEFT_BRACKET;
case 220:
return KeyCode.BACKSLASH;
case 221:
return KeyCode.RIGHT_BRACKET;
case 222:
return KeyCode.SINGLE_QUOTE;
case 223:
return KeyCode.GRAVE;
case 224:
return KeyCode.LEFT_META;
case 226:
return KeyCode.BACKSLASH;
}
return keyCode;
}
public function exec():Int
{
Browser.window.addEventListener("keydown", handleKeyEvent, false);
Browser.window.addEventListener("keyup", handleKeyEvent, false);
Browser.window.addEventListener("focus", handleWindowEvent, false);
Browser.window.addEventListener("blur", handleWindowEvent, false);
Browser.window.addEventListener("resize", handleWindowEvent, false);
Browser.window.addEventListener("beforeunload", handleWindowEvent, false);
if (Reflect.hasField(Browser.window, "Accelerometer"))
{
Browser.window.addEventListener("devicemotion", handleSensorEvent, false);
}
#if stats
stats = untyped #if haxe4 js.Syntax.code #else __js__ #end ("new Stats ()");
stats.domElement.style.position = "absolute";
stats.domElement.style.top = "0px";
Browser.document.body.appendChild(stats.domElement);
#end
untyped #if haxe4 js.Syntax.code #else __js__ #end ("
if (!CanvasRenderingContext2D.prototype.isPointInStroke) {
CanvasRenderingContext2D.prototype.isPointInStroke = function (path, x, y) {
return false;
};
}
if (!CanvasRenderingContext2D.prototype.isPointInPath) {
CanvasRenderingContext2D.prototype.isPointInPath = function (path, x, y) {
return false;
};
}
if ('performance' in window == false) {
window.performance = {};
}
if ('now' in window.performance == false) {
var offset = Date.now();
if (performance.timing && performance.timing.navigationStart) {
offset = performance.timing.navigationStart
}
window.performance.now = function now() {
return Date.now() - offset;
}
}
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = window.performance.now();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
window.requestAnimFrame = window.requestAnimationFrame;
");
lastUpdate = Browser.window.performance.now();
handleApplicationEvent();
return 0;
}
public function exit():Void {}
private function handleApplicationEvent(?__):Void
{
// TODO: Support independent window frame rates
for (window in parent.__windows)
{
window.__backend.updateSize();
}
updateGameDevices();
currentUpdate = Browser.window.performance.now();
if (currentUpdate >= nextUpdate)
{
#if stats
stats.begin();
#end
deltaTime = currentUpdate - lastUpdate;
for (window in parent.__windows)
{
parent.onUpdate.dispatch(Std.int(deltaTime));
if (window.context != null) window.onRender.dispatch(window.context);
}
#if stats
stats.end();
#end
if (framePeriod < 0)
{
nextUpdate = currentUpdate;
}
else
{
nextUpdate = currentUpdate - (currentUpdate % framePeriod) + framePeriod;
}
lastUpdate = currentUpdate;
}
Browser.window.requestAnimationFrame(cast handleApplicationEvent);
}
private function handleKeyEvent(event:KeyboardEvent):Void
{
if (parent.window != null)
{
// space and arrow keys
// switch (event.keyCode) {
// case 32, 37, 38, 39, 40: event.preventDefault ();
// }
// TODO: Use event.key instead where supported
var keyCode = cast convertKeyCode(event.keyCode != null ? event.keyCode : event.which);
var modifier = (event.shiftKey ? (KeyModifier.SHIFT) : 0) | (event.ctrlKey ? (KeyModifier.CTRL) : 0) | (event.altKey ? (KeyModifier.ALT) : 0) | (event.metaKey ? (KeyModifier.META) : 0);
if (event.type == "keydown")
{
parent.window.onKeyDown.dispatch(keyCode, modifier);
if (parent.window.onKeyDown.canceled && event.cancelable)
{
event.preventDefault();
}
}
else
{
parent.window.onKeyUp.dispatch(keyCode, modifier);
if (parent.window.onKeyUp.canceled && event.cancelable)
{
event.preventDefault();
}
}
}
}
private function handleSensorEvent(event:DeviceMotionEvent):Void
{
accelerometer.onUpdate.dispatch(event.accelerationIncludingGravity.x, event.accelerationIncludingGravity.y, event.accelerationIncludingGravity.z);
}
private function handleWindowEvent(event:js.html.Event):Void
{
if (parent.window != null)
{
switch (event.type)
{
case "focus":
if (hidden)
{
parent.window.onFocusIn.dispatch();
parent.window.onActivate.dispatch();
hidden = false;
}
case "blur":
if (!hidden)
{
parent.window.onFocusOut.dispatch();
parent.window.onDeactivate.dispatch();
hidden = true;
}
case "visibilitychange":
if (Browser.document.hidden)
{
if (!hidden)
{
parent.window.onFocusOut.dispatch();
parent.window.onDeactivate.dispatch();
hidden = true;
}
}
else
{
if (hidden)
{
parent.window.onFocusIn.dispatch();
parent.window.onActivate.dispatch();
hidden = false;
}
}
case "resize":
parent.window.__backend.handleResizeEvent(event);
case "beforeunload":
// Mobile Chrome dispatches 'beforeunload' after device sleep,
// but returns later without reloading the page. This triggers
// a window.onClose(), without us creating the window again.
//
// For now, let focus in/out and activate/deactivate trigger
// on blur and focus, and do not dispatch a closed window event
// since it may actually never close.
// if (!event.defaultPrevented) {
// parent.window.onClose.dispatch ();
// if (parent.window != null && parent.window.onClose.canceled && event.cancelable) {
// event.preventDefault ();
// }
// }
}
}
}
private function updateGameDevices():Void
{
var devices = Joystick.__getDeviceData();
if (devices == null) return;
var id, gamepad, joystick, data:Dynamic, cache;
for (i in 0...devices.length)
{
id = i;
data = devices[id];
if (data == null) continue;
if (!gameDeviceCache.exists(id))
{
cache = new GameDeviceData();
cache.id = id;
cache.connected = data.connected;
for (i in 0...data.buttons.length)
{
cache.buttons.push(data.buttons[i].value);
}
for (i in 0...data.axes.length)
{
cache.axes.push(data.axes[i]);
}
if (data.mapping == "standard")
{
cache.isGamepad = true;
}
gameDeviceCache.set(id, cache);
if (data.connected)
{
Joystick.__connect(id);
if (cache.isGamepad)
{
Gamepad.__connect(id);
}
}
}
cache = gameDeviceCache.get(id);
joystick = Joystick.devices.get(id);
gamepad = Gamepad.devices.get(id);
if (data.connected)
{
var button:GamepadButton;
var value:Float;
for (i in 0...data.buttons.length)
{
value = data.buttons[i].value;
if (value != cache.buttons[i])
{
if (i == 6)
{
joystick.onAxisMove.dispatch(data.axes.length, value);
if (gamepad != null) gamepad.onAxisMove.dispatch(GamepadAxis.TRIGGER_LEFT, value);
}
else if (i == 7)
{
joystick.onAxisMove.dispatch(data.axes.length + 1, value);
if (gamepad != null) gamepad.onAxisMove.dispatch(GamepadAxis.TRIGGER_RIGHT, value);
}
else
{
if (value > 0)
{
joystick.onButtonDown.dispatch(i);
}
else
{
joystick.onButtonUp.dispatch(i);
}
if (gamepad != null)
{
button = switch (i)
{
case 0: GamepadButton.A;
case 1: GamepadButton.B;
case 2: GamepadButton.X;
case 3: GamepadButton.Y;
case 4: GamepadButton.LEFT_SHOULDER;
case 5: GamepadButton.RIGHT_SHOULDER;
case 8: GamepadButton.BACK;
case 9: GamepadButton.START;
case 10: GamepadButton.LEFT_STICK;
case 11: GamepadButton.RIGHT_STICK;
case 12: GamepadButton.DPAD_UP;
case 13: GamepadButton.DPAD_DOWN;
case 14: GamepadButton.DPAD_LEFT;
case 15: GamepadButton.DPAD_RIGHT;
case 16: GamepadButton.GUIDE;
default: continue;
}
if (value > 0)
{
gamepad.onButtonDown.dispatch(button);
}
else
{
gamepad.onButtonUp.dispatch(button);
}
}
}
cache.buttons[i] = value;
}
}
for (i in 0...data.axes.length)
{
if (data.axes[i] != cache.axes[i])
{
joystick.onAxisMove.dispatch(i, data.axes[i]);
if (gamepad != null) gamepad.onAxisMove.dispatch(i, data.axes[i]);
cache.axes[i] = data.axes[i];
}
}
}
else if (cache.connected)
{
cache.connected = false;
Joystick.__disconnect(id);
Gamepad.__disconnect(id);
}
}
}
}
class GameDeviceData
{
public var connected:Bool;
public var id:Int;
public var isGamepad:Bool;
public var buttons:Array<Float>;
public var axes:Array<Float>;
public function new()
{
connected = true;
buttons = [];
axes = [];
}
}