/
SystemImpl.hx
1363 lines (1177 loc) · 37.6 KB
/
SystemImpl.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
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package kha;
import js.Syntax;
import js.html.webgl.GL;
import js.html.WheelEvent;
import js.Browser;
import js.html.CanvasElement;
import js.html.KeyboardEvent;
import js.html.MouseEvent;
import js.html.Touch;
import js.html.TouchEvent;
import js.html.ClipboardEvent;
import js.html.DeviceMotionEvent;
import js.html.DeviceOrientationEvent;
import kha.graphics4.TextureFormat;
import kha.input.Gamepad;
import kha.input.Keyboard;
import kha.input.KeyCode;
import kha.input.Mouse;
import kha.input.Sensor;
import kha.input.Surface;
import kha.js.AudioElementAudio;
import kha.js.CanvasGraphics;
import kha.js.MobileWebAudio;
import kha.js.vr.VrInterface;
import kha.System;
class GamepadStates {
public var axes: Array<Float>;
public var buttons: Array<Float>;
public function new() {
axes = new Array<Float>();
buttons = new Array<Float>();
}
}
class SystemImpl {
public static var gl: GL;
public static var gl2: Bool;
public static var halfFloat: Dynamic;
public static var anisotropicFilter: Dynamic;
public static var depthTexture: Dynamic;
public static var drawBuffers: Dynamic;
public static var elementIndexUint: Dynamic;
@:noCompletion public static var _hasWebAudio: Bool;
// public static var graphics(default, null): Graphics;
public static var khanvas: CanvasElement;
static var options: SystemOptions;
public static var mobile: Bool = false;
public static var ios: Bool = false;
public static var mobileAudioPlaying: Bool = false;
static var chrome: Bool = false;
static var firefox: Bool = false;
static var ie: Bool = false;
public static var insideInputEvent: Bool = false;
static var window: Window;
public static var estimatedRefreshRate: Int = 60;
static function errorHandler(message: String, source: String, lineno: Int, colno: Int, error: Dynamic) {
Browser.console.error("Error: " + message);
Browser.console.error("Stack:\n" + error.stack);
return true;
}
public static function init(options: SystemOptions, callback: Window->Void): Void {
SystemImpl.options = options;
#if kha_debug_html5
Browser.window.onerror = cast errorHandler;
var electron = Syntax.code("require('electron')");
if (electron.webFrame.setZoomLevelLimits != null) { // TODO: Figure out why this check is sometimes required
electron.webFrame.setZoomLevelLimits(1, 1);
}
var wndOpts = {
type: 'showWindow',
title: options.title,
x: options.window.x,
y: options.window.y,
width: options.width,
height: options.height,
}
electron.ipcRenderer.send('asynchronous-message', wndOpts); // Wait a second so the debugger can attach
Browser.window.setTimeout(function() {
initSecondStep(callback);
}, 1000);
chrome = true;
mobileAudioPlaying = true;
#else
mobile = isMobile();
ios = isIOS();
chrome = isChrome();
firefox = isFirefox();
ie = isIE();
mobileAudioPlaying = !mobile && !chrome && !firefox;
initSecondStep(callback);
#end
}
static function initSecondStep(callback: Window->Void): Void {
init2(options.window.width, options.window.height);
initAnimate(callback);
}
public static function initSensor(): Void {
if (ios) { // In Safari for iOS the directions are reversed on axes x, y and z
Browser.window.ondevicemotion = function(event: DeviceMotionEvent) {
Sensor._changed(0, -event.accelerationIncludingGravity.x, -event.accelerationIncludingGravity.y, -event.accelerationIncludingGravity.z);
};
}
else {
Browser.window.ondevicemotion = function(event: DeviceMotionEvent) {
Sensor._changed(0, event.accelerationIncludingGravity.x, event.accelerationIncludingGravity.y, event.accelerationIncludingGravity.z);
};
}
Browser.window.ondeviceorientation = function(event: DeviceOrientationEvent) {
Sensor._changed(1, event.beta, event.gamma, event.alpha);
};
}
static function isMobile(): Bool {
var agent = js.Browser.navigator.userAgent;
if (agent.indexOf("Android") >= 0 || agent.indexOf("webOS") >= 0 || agent.indexOf("BlackBerry") >= 0 || agent.indexOf("Windows Phone") >= 0) {
return true;
}
if (isIOS())
return true;
return false;
}
static function isIOS(): Bool {
var agent = js.Browser.navigator.userAgent;
if (agent.indexOf("iPhone") >= 0 || agent.indexOf("iPad") >= 0 || agent.indexOf("iPod") >= 0) {
return true;
}
return false;
}
static function isChrome(): Bool {
var agent = js.Browser.navigator.userAgent;
if (agent.indexOf("Chrome") >= 0) {
return true;
}
return false;
}
static function isFirefox(): Bool {
var agent = js.Browser.navigator.userAgent;
if (agent.indexOf("Firefox") >= 0) {
return true;
}
return false;
}
static function isIE(): Bool {
var agent = js.Browser.navigator.userAgent;
if (agent.indexOf("MSIE ") >= 0 || agent.indexOf("Trident/") >= 0) {
return true;
}
return false;
}
public static function setCanvas(canvas: CanvasElement): Void {
khanvas = canvas;
}
public static function getScreenRotation(): ScreenRotation {
return ScreenRotation.RotationNone;
}
public static function getTime(): Float {
final now = js.Browser.window.performance != null ? js.Browser.window.performance.now() : js.lib.Date.now();
return now / 1000;
}
public static function getSystemId(): String {
return "HTML5";
}
public static function vibrate(ms: Int): Void {
Browser.navigator.vibrate(ms);
}
public static function getLanguage(): String {
final lang = Browser.navigator.language;
return lang.substr(0, 2).toLowerCase();
}
public static function requestShutdown(): Bool {
Browser.window.close();
return true;
}
static inline var maxGamepads: Int = 4;
static var frame: Framebuffer;
static var pressedKeys: Array<Bool>;
static var keyboard: Keyboard = null;
static var mouse: kha.input.Mouse;
static var surface: Surface;
static var gamepads: Array<Gamepad>;
static var gamepadStates: Array<GamepadStates>;
static var minimumScroll: Int = 999;
static var mouseX: Int;
static var mouseY: Int;
static var touchX: Int;
static var touchY: Int;
static var lastFirstTouchX: Int = 0;
static var lastFirstTouchY: Int = 0;
static function init2(defaultWidth: Int, defaultHeight: Int, ?backbufferFormat: TextureFormat) {
#if !kha_no_keyboard
keyboard = new Keyboard();
#end
mouse = new kha.input.MouseImpl();
surface = new Surface();
gamepads = new Array<Gamepad>();
gamepadStates = new Array<GamepadStates>();
for (i in 0...maxGamepads) {
gamepads[i] = new Gamepad(i);
gamepadStates[i] = new GamepadStates();
}
js.Browser.window.addEventListener("gamepadconnected", (e) -> {
Gamepad.sendConnectEvent(e.gamepad.index);
});
js.Browser.window.addEventListener("gamepaddisconnected", (e) -> {
Gamepad.sendDisconnectEvent(e.gamepad.index);
});
var sysGamepads = getGamepads();
if (sysGamepads != null) {
for (i in 0...sysGamepads.length) {
var pad = sysGamepads[i];
if (pad != null) {
gamepads[pad.index].connected = true;
}
}
}
if (ie) {
pressedKeys = new Array<Bool>();
for (i in 0...256)
pressedKeys.push(false);
for (i in 0...256)
pressedKeys.push(null);
}
function onCopy(e: ClipboardEvent): Void {
if (System.copyListener != null) {
var data = System.copyListener();
if (data != null)
e.clipboardData.setData("text/plain", data);
e.preventDefault();
}
}
function onCut(e: ClipboardEvent): Void {
if (System.cutListener != null) {
var data = System.cutListener();
if (data != null)
e.clipboardData.setData("text/plain", data);
e.preventDefault();
}
}
function onPaste(e: ClipboardEvent): Void {
if (System.pasteListener != null) {
System.pasteListener(e.clipboardData.getData("text/plain"));
e.preventDefault();
}
}
var document = Browser.document;
document.addEventListener("copy", onCopy);
document.addEventListener("cut", onCut);
document.addEventListener("paste", onPaste);
CanvasImage.init();
Scheduler.init();
loadFinished(defaultWidth, defaultHeight);
}
public static function copyToClipboard(text: String) {
var textArea = Browser.document.createElement("textarea");
untyped textArea.value = text;
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
Browser.document.body.appendChild(textArea);
textArea.focus();
untyped textArea.select();
try {
Browser.document.execCommand('copy');
}
catch (err) {}
Browser.document.body.removeChild(textArea);
}
public static function getMouse(num: Int): Mouse {
if (num != 0)
return null;
return mouse;
}
public static function getKeyboard(num: Int): Keyboard {
if (num != 0)
return null;
return keyboard;
}
static function checkGamepad(pad: js.html.Gamepad) {
for (i in 0...pad.axes.length) {
if (pad.axes[i] != null) {
var axis = pad.axes[i];
if (gamepadStates[pad.index].axes[i] != axis) {
gamepadStates[pad.index].axes[i] = axis;
gamepads[pad.index].sendAxisEvent(i, axis);
}
}
}
for (i in 0...pad.buttons.length) {
if (pad.buttons[i] != null) {
if (gamepadStates[pad.index].buttons[i] != pad.buttons[i].value) {
gamepadStates[pad.index].buttons[i] = pad.buttons[i].value;
gamepads[pad.index].sendButtonEvent(i, pad.buttons[i].value);
}
}
}
if (pad.axes.length <= 4 && pad.buttons.length > 7) {
// Fix for the triggers not being axis in html5
gamepadStates[pad.index].axes[4] = pad.buttons[6].value;
gamepads[pad.index].sendAxisEvent(4, pad.buttons[6].value);
gamepadStates[pad.index].axes[5] = pad.buttons[7].value;
gamepads[pad.index].sendAxisEvent(5, pad.buttons[7].value);
}
}
static function getCanvasElement(): CanvasElement {
if (khanvas != null)
return khanvas;
// Only consider custom canvas ID for release builds
#if (kha_debug_html5 || !canvas_id)
return cast Browser.document.getElementById("khanvas");
#else
return cast Browser.document.getElementById(Macros.canvasId());
#end
}
static function loadFinished(defaultWidth: Int, defaultHeight: Int) {
var canvas: CanvasElement = getCanvasElement();
canvas.style.cursor = "default";
var gl: Bool = false;
#if kha_webgl
try {
SystemImpl.gl = canvas.getContext("webgl2",
{
alpha: false,
antialias: options.framebuffer.samplesPerPixel > 1,
stencil: true
}); // preserveDrawingBuffer: true } ); Warning: preserveDrawingBuffer can cause huge performance issues on mobile browsers
SystemImpl.gl.pixelStorei(GL.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
halfFloat = {HALF_FLOAT_OES: 0x140B}; // GL_HALF_FLOAT
depthTexture = {UNSIGNED_INT_24_8_WEBGL: 0x84FA}; // GL_UNSIGNED_INT_24_8
drawBuffers = {COLOR_ATTACHMENT0_WEBGL: GL.COLOR_ATTACHMENT0};
elementIndexUint = true;
SystemImpl.gl.getExtension("EXT_color_buffer_float");
SystemImpl.gl.getExtension("OES_texture_float_linear");
SystemImpl.gl.getExtension("OES_texture_half_float_linear");
anisotropicFilter = SystemImpl.gl.getExtension("EXT_texture_filter_anisotropic");
if (anisotropicFilter == null)
anisotropicFilter = SystemImpl.gl.getExtension("WEBKIT_EXT_texture_filter_anisotropic");
gl = true;
gl2 = true;
Shaders.init();
}
catch (e:Dynamic) {
trace("Could not initialize WebGL 2, falling back to WebGL.");
}
if (!gl2) {
try {
SystemImpl.gl = canvas.getContext("experimental-webgl",
{
alpha: false,
antialias: options.framebuffer.samplesPerPixel > 1,
stencil: true
}); // preserveDrawingBuffer: true } ); WARNING: preserveDrawingBuffer causes huge performance issues (on mobile browser)!
SystemImpl.gl.pixelStorei(GL.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
SystemImpl.gl.getExtension("OES_texture_float");
SystemImpl.gl.getExtension("OES_texture_float_linear");
halfFloat = SystemImpl.gl.getExtension("OES_texture_half_float");
SystemImpl.gl.getExtension("OES_texture_half_float_linear");
depthTexture = SystemImpl.gl.getExtension("WEBGL_depth_texture");
SystemImpl.gl.getExtension("EXT_shader_texture_lod");
SystemImpl.gl.getExtension("OES_standard_derivatives");
anisotropicFilter = SystemImpl.gl.getExtension("EXT_texture_filter_anisotropic");
if (anisotropicFilter == null)
anisotropicFilter = SystemImpl.gl.getExtension("WEBKIT_EXT_texture_filter_anisotropic");
drawBuffers = SystemImpl.gl.getExtension('WEBGL_draw_buffers');
elementIndexUint = SystemImpl.gl.getExtension("OES_element_index_uint");
gl = true;
Shaders.init();
}
catch (e:Dynamic) {
trace("Could not initialize WebGL, falling back to <canvas>.");
}
}
#end
setCanvas(canvas);
window = new Window(0, defaultWidth, defaultHeight, canvas);
// var widthTransform: Float = canvas.width / Loader.the.width;
// var heightTransform: Float = canvas.height / Loader.the.height;
// var transform: Float = Math.min(widthTransform, heightTransform);
if (gl) {
var g4 = new kha.js.graphics4.Graphics();
frame = new Framebuffer(0, null, null, g4);
frame.init(new kha.graphics2.Graphics1(frame), new kha.js.graphics4.Graphics2(frame), g4); // new kha.graphics1.Graphics4(frame));
}
else {
Syntax.code("kha_js_Font.Kravur = kha_Kravur; kha_Kravur = kha_js_Font");
var g2 = new CanvasGraphics(canvas.getContext("2d"));
frame = new Framebuffer(0, null, g2, null);
frame.init(new kha.graphics2.Graphics1(frame), g2, null);
}
// canvas.getContext("2d").scale(transform, transform);
if (!mobile && kha.audio2.Audio._init()) {
SystemImpl._hasWebAudio = true;
kha.audio2.Audio1._init();
}
else if (mobile) {
SystemImpl._hasWebAudio = false;
MobileWebAudio._init();
Syntax.code("kha_audio2_Audio1 = kha_js_MobileWebAudio");
}
else {
SystemImpl._hasWebAudio = false;
Syntax.code("kha_audio2_Audio1 = kha_js_AudioElementAudio");
}
kha.vr.VrInterface.instance = new VrInterface();
// Autofocus
canvas.focus();
#if kha_disable_context_menu
canvas.oncontextmenu = function(event: Dynamic) {
event.stopPropagation();
event.preventDefault();
}
#end
canvas.onmousedown = mouseDown;
canvas.onmousemove = mouseMove;
if (keyboard != null) {
canvas.onkeydown = keyDown;
canvas.onkeyup = keyUp;
canvas.onkeypress = keyPress;
}
canvas.onblur = onBlur;
canvas.onfocus = onFocus;
untyped (canvas.onmousewheel = canvas.onwheel = mouseWheel);
canvas.onmouseleave = mouseLeave;
canvas.addEventListener("wheel mousewheel", mouseWheel, false);
canvas.addEventListener("touchstart", touchDown, false);
canvas.addEventListener("touchend", touchUp, false);
canvas.addEventListener("touchmove", touchMove, false);
canvas.addEventListener("touchcancel", touchCancel, false);
#if kha_debug_html5
Browser.document.addEventListener('dragover', function(event) {
event.preventDefault();
});
Browser.document.addEventListener('drop', function(event: js.html.DragEvent) {
event.preventDefault();
if (event.dataTransfer != null && event.dataTransfer.files != null) {
for (file in event.dataTransfer.files) {
// https://developer.mozilla.org/en-US/docs/Web/API/File
// - use mozFullPath or webkitRelativePath?
System.dropFiles(Syntax.code('file.path'));
}
}
});
#end
Browser.window.addEventListener("unload", function() {
System.shutdown();
});
}
static function initAnimate(callback: Window->Void) {
var canvas: CanvasElement = getCanvasElement();
var window: Dynamic = Browser.window;
var requestAnimationFrame = window.requestAnimationFrame;
if (requestAnimationFrame == null)
requestAnimationFrame = window.mozRequestAnimationFrame;
if (requestAnimationFrame == null)
requestAnimationFrame = window.webkitRequestAnimationFrame;
if (requestAnimationFrame == null)
requestAnimationFrame = window.msRequestAnimationFrame;
function animate(timestamp) {
var window: Dynamic = Browser.window;
if (requestAnimationFrame == null)
window.setTimeout(animate, 1000.0 / 60.0);
else
requestAnimationFrame(animate);
var sysGamepads = getGamepads();
if (sysGamepads != null) {
for (i in 0...sysGamepads.length) {
var pad = sysGamepads[i];
if (pad != null) {
checkGamepad(pad);
}
}
}
Scheduler.executeFrame();
if (canvas.getContext != null) {
// Lookup the size the browser is displaying the canvas.
// TODO deal with window.devicePixelRatio ?
var displayWidth = canvas.clientWidth;
var displayHeight = canvas.clientHeight;
// Check if the canvas is not the same size.
if (canvas.width != displayWidth || canvas.height != displayHeight) {
// Make the canvas the same size
canvas.width = displayWidth;
canvas.height = displayHeight;
}
System.render([frame]);
if (SystemImpl.gl != null) {
// Clear alpha for IE11
SystemImpl.gl.clearColor(1, 1, 1, 1);
SystemImpl.gl.colorMask(false, false, false, true);
SystemImpl.gl.clear(GL.COLOR_BUFFER_BIT);
SystemImpl.gl.colorMask(true, true, true, true);
}
}
}
var initialTimestamp: Int = 0;
var prevTimestamp: Int = 0;
var currentSamples: Int = 0;
var timeDiffs: Array<Int> = [];
var SAMPLE_COUNT: Int = 90;
var MEAN_TRUNCATION_CUTOFF: Float = 1 / 3;
function roundToKnownRefreshRate(hz: Int): Int {
var hz30 = {low: 27, high: 33, target: 30};
var hz60 = {low: 57, high: 63, target: 60};
var hz75 = {low: 72, high: 78, target: 75};
var hz90 = {low: 87, high: 93, target: 90};
var hz120 = {low: 117, high: 123, target: 120};
var hz144 = {low: 141, high: 147, target: 144};
var hz240 = {low: 237, high: 243, target: 240};
var hz340 = {low: 337, high: 343, target: 340};
var hz360 = {low: 357, high: 363, target: 360};
var rates = [hz30, hz60, hz75, hz90, hz120, hz144, hz240, hz340, hz360];
var nearestHz = hz;
for (rate in rates) {
if (hz >= rate.low && hz <= rate.high) {
nearestHz = rate.target;
}
}
return nearestHz;
}
// HTML5 has no real way to query the actual monitor refresh rate
// The only thing that can be done is attempt to measure the interval between requestAnimationFrame calls
// Without requestAnimationFrame we're out of luck
// We try and make a best guess while nothing intensive is happening
function detectRefreshRate(timestamp) {
var window: Dynamic = Browser.window;
if (initialTimestamp == 0) {
initialTimestamp = timestamp;
}
var timeDifferential = (timestamp - prevTimestamp) - initialTimestamp;
prevTimestamp = timestamp - initialTimestamp;
if (timeDifferential != 0) {
timeDiffs.push(timeDifferential);
}
if (currentSamples < SAMPLE_COUNT) {
currentSamples++;
if (requestAnimationFrame == null)
window.setTimeout(detectRefreshRate, 1000.0 / 60.0);
else
requestAnimationFrame(detectRefreshRate);
}
else {
// Remove extreme frametime values before averaging
{
haxe.ds.ArraySort.sort(timeDiffs, (a, b) -> {
a - b;
});
var truncatedTimeDiffs: Array<Int> = [];
var cutoff = Math.round(timeDiffs.length * MEAN_TRUNCATION_CUTOFF);
for (i in cutoff...timeDiffs.length - cutoff) {
truncatedTimeDiffs.push(timeDiffs[i]);
}
var total = 0;
for (time in truncatedTimeDiffs) {
total += time;
}
var avg = total / truncatedTimeDiffs.length;
// We may have an accurate frequency, but it might be possible to be off the actual refresh rate by a few hz
// Manually round to common refresh rates as well, just for security's sake
estimatedRefreshRate = roundToKnownRefreshRate(Math.round(1000 / avg));
}
Scheduler.start();
if (requestAnimationFrame == null)
window.setTimeout(animate, 1000.0 / 60.0);
else
requestAnimationFrame(animate);
callback(SystemImpl.window);
}
}
// Run through refresh rate detection first and then start animating
if (requestAnimationFrame == null)
window.setTimeout(detectRefreshRate, 1000.0 / 60.0);
else
requestAnimationFrame(detectRefreshRate);
}
public static function lockMouse(): Void {
untyped if (SystemImpl.khanvas.requestPointerLock) {
SystemImpl.khanvas.requestPointerLock();
}
else if (SystemImpl.khanvas.mozRequestPointerLock) {
SystemImpl.khanvas.mozRequestPointerLock();
}
else if (SystemImpl.khanvas.webkitRequestPointerLock) {
SystemImpl.khanvas.webkitRequestPointerLock();
}
}
public static function unlockMouse(): Void {
untyped if (document.exitPointerLock) {
document.exitPointerLock();
}
else if (document.mozExitPointerLock) {
document.mozExitPointerLock();
}
else if (document.webkitExitPointerLock) {
document.webkitExitPointerLock();
}
}
public static function canLockMouse(): Bool {
return Syntax.code("'pointerLockElement' in document ||
'mozPointerLockElement' in document ||
'webkitPointerLockElement' in document");
}
public static function isMouseLocked(): Bool {
return Syntax.code("document.pointerLockElement === kha_SystemImpl.khanvas ||
document.mozPointerLockElement === kha_SystemImpl.khanvas ||
document.webkitPointerLockElement === kha_SystemImpl.khanvas");
}
public static function notifyOfMouseLockChange(func: Void->Void, error: Void->Void): Void {
js.Browser.document.addEventListener('pointerlockchange', func, false);
js.Browser.document.addEventListener('mozpointerlockchange', func, false);
js.Browser.document.addEventListener('webkitpointerlockchange', func, false);
js.Browser.document.addEventListener('pointerlockerror', error, false);
js.Browser.document.addEventListener('mozpointerlockerror', error, false);
js.Browser.document.addEventListener('webkitpointerlockerror', error, false);
}
public static function removeFromMouseLockChange(func: Void->Void, error: Void->Void): Void {
js.Browser.document.removeEventListener('pointerlockchange', func, false);
js.Browser.document.removeEventListener('mozpointerlockchange', func, false);
js.Browser.document.removeEventListener('webkitpointerlockchange', func, false);
js.Browser.document.removeEventListener('pointerlockerror', error, false);
js.Browser.document.removeEventListener('mozpointerlockerror', error, false);
js.Browser.document.removeEventListener('webkitpointerlockerror', error, false);
}
static function setMouseXY(event: MouseEvent): Void {
var rect = SystemImpl.khanvas.getBoundingClientRect();
var borderWidth = SystemImpl.khanvas.clientLeft;
var borderHeight = SystemImpl.khanvas.clientTop;
mouseX = Std.int((event.clientX - rect.left - borderWidth) * SystemImpl.khanvas.width / (rect.width - 2 * borderWidth));
mouseY = Std.int((event.clientY - rect.top - borderHeight) * SystemImpl.khanvas.height / (rect.height - 2 * borderHeight));
}
static var iosSoundEnabled: Bool = false;
static function unlockiOSSound(): Void {
if (!ios || iosSoundEnabled)
return;
var buffer = MobileWebAudio._context.createBuffer(1, 1, 22050);
var source = MobileWebAudio._context.createBufferSource();
source.buffer = buffer;
source.connect(MobileWebAudio._context.destination);
// untyped(if (source.noteOn) source.noteOn(0));
source.start();
source.stop();
iosSoundEnabled = true;
}
static var soundEnabled = false;
static function unlockSound(): Void {
if (!soundEnabled) {
var context = kha.audio2.Audio._context;
if (context == null) {
context = Syntax.code('kha_audio2_Audio1._context');
}
if (context != null) {
context.resume().then(function(c) {
soundEnabled = true;
}).catchError(function(err) {
trace(err);
});
}
kha.audio2.Audio.wakeChannels();
}
unlockiOSSound();
}
static function mouseLeave(): Void {
mouse.sendLeaveEvent(0);
}
static function mouseWheel(event: WheelEvent): Void {
unlockSound();
insideInputEvent = true;
switch (Mouse.wheelEventBlockBehavior) {
case Full:
event.preventDefault();
case Custom(func):
if (func(event))
event.preventDefault();
case None:
}
// Deltamode == 0, deltaY is in pixels.
if (event.deltaMode == 0) {
if (event.deltaY < 0) {
mouse.sendWheelEvent(0, -1);
}
else if (event.deltaY > 0) {
mouse.sendWheelEvent(0, 1);
}
insideInputEvent = false;
return;
}
// Lines
if (event.deltaMode == 1) {
minimumScroll = Std.int(Math.min(minimumScroll, Math.abs(event.deltaY)));
mouse.sendWheelEvent(0, Std.int(event.deltaY / minimumScroll));
insideInputEvent = false;
return;
}
insideInputEvent = false;
return;
}
static function mouseDown(event: MouseEvent): Void {
insideInputEvent = true;
unlockSound();
setMouseXY(event);
if (event.which == 1) { // left button
mouse.sendDownEvent(0, 0, mouseX, mouseY);
if (khanvas.setCapture != null) {
khanvas.setCapture();
}
else {
khanvas.ownerDocument.addEventListener('mousemove', documentMouseMove, true);
}
khanvas.ownerDocument.addEventListener('mouseup', mouseLeftUp);
}
else if (event.which == 2) { // middle button
mouse.sendDownEvent(0, 2, mouseX, mouseY);
khanvas.ownerDocument.addEventListener('mouseup', mouseMiddleUp);
}
else if (event.which == 3) { // right button
mouse.sendDownEvent(0, 1, mouseX, mouseY);
khanvas.ownerDocument.addEventListener('mouseup', mouseRightUp);
}
else if (event.which == 4) { // backwards sidebutton
mouse.sendDownEvent(0, 3, mouseX, mouseY);
khanvas.ownerDocument.addEventListener('mouseup', mouseBackUp);
}
else if (event.which == 5) { // forwards sidebutton
mouse.sendDownEvent(0, 4, mouseX, mouseY);
khanvas.ownerDocument.addEventListener('mouseup', mouseForwardUp);
}
insideInputEvent = false;
}
static function mouseLeftUp(event: MouseEvent): Void {
unlockSound();
if (event.which != 1)
return;
insideInputEvent = true;
khanvas.ownerDocument.removeEventListener('mouseup', mouseLeftUp);
if (khanvas.releaseCapture != null) {
khanvas.ownerDocument.releaseCapture();
}
else {
khanvas.ownerDocument.removeEventListener("mousemove", documentMouseMove, true);
}
mouse.sendUpEvent(0, 0, mouseX, mouseY);
insideInputEvent = false;
}
static function mouseMiddleUp(event: MouseEvent): Void {
unlockSound();
if (event.which != 2)
return;
insideInputEvent = true;
khanvas.ownerDocument.removeEventListener('mouseup', mouseMiddleUp);
mouse.sendUpEvent(0, 2, mouseX, mouseY);
insideInputEvent = false;
}
static function mouseRightUp(event: MouseEvent): Void {
unlockSound();
if (event.which != 3)
return;
insideInputEvent = true;
khanvas.ownerDocument.removeEventListener('mouseup', mouseRightUp);
mouse.sendUpEvent(0, 1, mouseX, mouseY);
insideInputEvent = false;
}
static function mouseBackUp(event: MouseEvent): Void {
unlockSound();
if (event.which != 4)
return;
insideInputEvent = true;
khanvas.ownerDocument.removeEventListener('mouseup', mouseBackUp);
mouse.sendUpEvent(0, 3, mouseX, mouseY);
insideInputEvent = false;
}
static function mouseForwardUp(event: MouseEvent): Void {
unlockSound();
if (event.which != 5)
return;
insideInputEvent = true;
khanvas.ownerDocument.removeEventListener('mouseup', mouseForwardUp);
mouse.sendUpEvent(0, 4, mouseX, mouseY);
insideInputEvent = false;
}
static function documentMouseMove(event: MouseEvent): Void {
event.stopPropagation();
mouseMove(event);
}
static function mouseMove(event: MouseEvent): Void {
insideInputEvent = true;
var lastMouseX = mouseX;
var lastMouseY = mouseY;
setMouseXY(event);
var movementX = event.movementX;
var movementY = event.movementY;
if (event.movementX == null) {
movementX = (untyped event.mozMovementX != null) ? untyped event.mozMovementX : ((untyped event.webkitMovementX != null) ? untyped event.webkitMovementX : (mouseX
- lastMouseX));
movementY = (untyped event.mozMovementY != null) ? untyped event.mozMovementY : ((untyped event.webkitMovementY != null) ? untyped event.webkitMovementY : (mouseY
- lastMouseY));
}
// this ensures same behaviour across browser until they fix it
if (firefox) {
movementX = Std.int(movementX * Browser.window.devicePixelRatio);
movementY = Std.int(movementY * Browser.window.devicePixelRatio);
}
mouse.sendMoveEvent(0, mouseX, mouseY, movementX, movementY);
insideInputEvent = false;
}
static function setTouchXY(touch: Touch): Void {
var rect = SystemImpl.khanvas.getBoundingClientRect();
var borderWidth = SystemImpl.khanvas.clientLeft;
var borderHeight = SystemImpl.khanvas.clientTop;
touchX = Std.int((touch.clientX - rect.left - borderWidth) * SystemImpl.khanvas.width / (rect.width - 2 * borderWidth));
touchY = Std.int((touch.clientY - rect.top - borderHeight) * SystemImpl.khanvas.height / (rect.height - 2 * borderHeight));
}
static var iosTouchs: Array<Int> = [];
static function touchDown(event: TouchEvent): Void {
insideInputEvent = true;
unlockSound();
event.stopPropagation();
switch (Surface.touchDownEventBlockBehavior) {
case Full:
event.preventDefault();
case Custom(func):
if (func(event))
event.preventDefault();
case None:
}
var index = 0;
for (touch in event.changedTouches) {
var id = touch.identifier;
if (ios) {
id = iosTouchs.indexOf(-1);
if (id == -1)
id = iosTouchs.length;
iosTouchs[id] = touch.identifier;
}
setTouchXY(touch);
mouse.sendDownEvent(0, 0, touchX, touchY);
surface.sendTouchStartEvent(id, touchX, touchY);
if (index == 0) {
lastFirstTouchX = touchX;
lastFirstTouchY = touchY;
}
index++;
}
insideInputEvent = false;
}
static function touchUp(event: TouchEvent): Void {
insideInputEvent = true;
unlockSound();
for (touch in event.changedTouches) {
var id = touch.identifier;
if (ios) {
id = iosTouchs.indexOf(id);
iosTouchs[id] = -1;
}
setTouchXY(touch);
mouse.sendUpEvent(0, 0, touchX, touchY);
surface.sendTouchEndEvent(id, touchX, touchY);
}
insideInputEvent = false;
}
static function touchMove(event: TouchEvent): Void {
insideInputEvent = true;