forked from msokalski/asciicker
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_web.html
1464 lines (1247 loc) · 40.9 KB
/
game_web.html
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
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<link rel="manifest" href="asciicker.json">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>ASCIICKER - Let's kick some ASCII</title>
<style>
.asciicker { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
textarea.asciicker { font-family: monospace; width: 80%; }
div.asciicker { text-align: center; }
div.asciicker_border { border: 1px solid black; }
canvas
{
position: absolute;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-crisp-edges;
image-rendering: pixelated;
image-rendering: crisp-edges;
}
html, body, canvas
{
width:100%;
height:100%;
border:0;
padding:0;
margin:0;
color:#FFF;
background-color:#000;
overflow:hidden;
}
.spinner {
height: 50px;
width: 50px;
margin: 0px auto;
-webkit-animation: rotation .8s linear infinite;
-moz-animation: rotation .8s linear infinite;
-o-animation: rotation .8s linear infinite;
animation: rotation 0.8s linear infinite;
border-left: 10px solid rgb(0,150,240);
border-right: 10px solid rgb(0,150,240);
border-bottom: 10px solid rgb(0,150,240);
border-top: 10px solid rgb(100,0,200);
border-radius: 100%;
background-color: rgb(200,100,250);
}
@-webkit-keyframes rotation {
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(360deg);}
}
@-moz-keyframes rotation {
from {-moz-transform: rotate(0deg);}
to {-moz-transform: rotate(360deg);}
}
@-o-keyframes rotation {
from {-o-transform: rotate(0deg);}
to {-o-transform: rotate(360deg);}
}
@keyframes rotation {
from {transform: rotate(0deg);}
to {transform: rotate(360deg);}
}
</style>
<script type='text/javascript'>
"use strict";
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('asciicker.js').then(function (registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function (err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
if (history.state === null)
history.replaceState({inline:history.length},document.title,location.href);
}
</script>
<script id="vs" type="x-shader/x-vertex">
//<![CDATA[
attribute vec2 xy;
uniform float width;
uniform float height;
uniform float tex_width;
uniform float tex_height;
varying vec2 tex_coord;
varying vec2 cell_coord;
void main()
{
gl_Position = vec4(2.0*xy - vec2(1.0,1.0), 0.0,1.0);
cell_coord = xy * vec2(width,height);
tex_coord = cell_coord / vec2(tex_width,tex_height);
}
//]]>
</script>
<script id="fs" type="x-shader/x-fragment">
//<![CDATA[
precision highp float;
uniform sampler2D fnt;
uniform sampler2D tex;
varying vec2 tex_coord;
varying vec2 cell_coord;
vec3 Pal(float p)
{
p = clamp(floor(p-16.0+0.5), 0.0, 215.0);
float blue = floor(p / 36.0);
p-= 36.0*blue;
float green = floor(p / 6.0);
float red = p - 6.0*green;
return vec3(blue, green, red) * 0.2;
}
void main()
{
// sample cell buffer
vec4 cell = texture2D(tex, tex_coord);
// chracter code / 16
float code = floor(cell.z * 255.00 + 0.5)/16.0; // bb fix !
vec2 glyph;
glyph.y = floor(code); // character y position (0..15)
glyph.x = (code-glyph.y)*16.0; // character x position (0..15)
vec2 raster = fract(cell_coord);
raster.y = 1.0-raster.y;
// normalized glyph texel position
glyph = (glyph + raster) / vec2(16.0,16.0);
float mask = texture2D(fnt, glyph).g;
vec3 fg = Pal(cell.x*255.00);
vec3 bg = Pal(cell.y*255.00);
gl_FragColor = vec4(mix(bg,fg,mask),1.0);
}
//]]>
</script>
<script type='text/javascript'>
"use strict";
var audio_ctx = null;
var audio_cb = null;
var audio_node = null;
var audio_port = null;
var audio_call_cache = null;
var audio_vol_cache = null;
function audioResume()
{
if (audio_ctx)
{
if (audio_ctx.state === 'suspended' || audio_ctx.state === 'interrupted')
audio_ctx.resume();
}
}
var ak_connection = null;
var Join = null;
var Load = null;
var Packet = null;
var ak_packet = 0;
var ak_joined = false;
function Send()
{
if (ak_connection)
{
var chunk = new Uint8Array(ak_packet.buffer,ak_packet.byteOffset+1,ak_packet[0]);
ak_connection.send(chunk);
// console.log("sending " + chunk.length + " bytes ..." + chunk[0]);
}
}
function ConsoleLog()
{
var chunk = new Uint8Array(ak_packet.buffer,ak_packet.byteOffset+1,ak_packet[0]);
var str = new TextDecoder("utf-8").decode(chunk);
console.log(str);
}
function Connect()
{
console.log('ws connecting...');
var connection = null;
var urlParams = new URLSearchParams(location.search);
var player_name = urlParams.get("player");
if (!player_name)
player_name = "player";
var player_name_len = player_name.length;
if (player_name_len>30)
player_name_len=30;
var port_override = "";
//var port_override = ":8080";
// Y8 is NOT READY yet
Load(player_name);
Resize(null);
window.requestAnimationFrame(AsciickerLoop);
return;
if (location.protocol !== 'https:')
{
connection = new WebSocket('ws://' + location.hostname + port_override + '/ws/y8/');
}
else
{
connection = new WebSocket('wss://' + location.hostname + port_override + '/ws/y8/');
}
connection.binaryType = "arraybuffer";
connection.onclose = function(err)
{
// handled in onerror
/*
if (ak_connection)
{
ak_connection.onclose = null;
ak_connection.onmessage = null;
ak_connection.onerror = null;
ak_connection=null;
}
if (!ak_joined)
{
console.log("ws error (onclose), starting solo ...");
Load();
Resize(null);
window.requestAnimationFrame(AsciickerLoop);
}
else
{
Join("",-1,-1);
}
*/
};
connection.onerror = function (err)
{
if (ak_connection)
{
ak_connection.onclose = null;
ak_connection.onmessage = null;
ak_connection.onerror = null;
ak_connection.close();
ak_connection=null;
}
if (!ak_joined)
{
console.log("ws error (onerror), starting solo ...");
Load(player_name);
Resize(null);
window.requestAnimationFrame(AsciickerLoop);
}
else
{
Join("",-1,-1);
}
};
connection.onopen = function ()
{
// connection is opened and ready to use
ak_connection = connection;
console.log('ws ready!');
// send join request
var req_join = new Uint8Array(32);
req_join[0] = 0x4A;
for (var i=0; i<player_name_len; i++)
req_join[i+1] = player_name.charCodeAt(i) & 0xFF;
ak_connection.send(req_join);
ak_connection.onmessage = function (msg)
{
msg = new Uint8Array(msg.data);
if (!ak_joined)
{
ak_joined = true;
console.log("response " + msg[0]);
if (msg[0]==0x6A)
{
var max_cli = msg[1];
var id = msg[2] | (msg[3] << 8);
console.log("calling Join(" + player_name + "," + id + "," + max_cli + ");");
if (Join)
{
var mem = Join(player_name,id,max_cli);
ak_packet = new Uint8Array(Module.HEAP8.buffer,mem,257);
}
Load(player_name);
Resize(null);
window.requestAnimationFrame(AsciickerLoop);
}
else
{
Load(player_name);
Resize(null);
window.requestAnimationFrame(AsciickerLoop);
}
}
else
{
if (Packet)
{
Packet(msg,msg.length);
}
}
};
};
}
</script>
<script type='text/javascript'>
"use strict";
var Render = null;
var Size = null;
var Keyb = null;
var Mouse = null;
var Touch = null;
var Focus = null;
var PollGamePad = null; // js handler
var GamePad = null; // wasm handler
var gamepad = -1; // gamepad in use, index into navigator.getGamepads();
var ak_canvas;
var ak_ctx;
var ak_vtx;
var ak_prg;
var ak_tex;
var user_zoom = 0;
var ak_fonts;
var ak_font;
var ak_buf = null;
var ak_prev_arr = null;
var ak_prev_mem = null;
var ak_uni_width;
var ak_uni_height;
var ak_uni_tex_width;
var ak_uni_tex_height;
var ak_width=0;
var ak_height=0;
var ak_max_width = 160;
var ak_max_height = 90;
var ak_ratio = 1;
function FindFont()
{
ak_ratio = 1;
if (window.screen.systemXDPI !== undefined &&
window.screen.logicalXDPI !== undefined &&
window.screen.systemXDPI > window.screen.logicalXDPI)
{
// Only allow for values > 1
ak_ratio = window.screen.systemXDPI / window.screen.logicalXDPI;
}
else
if (window.devicePixelRatio !== undefined)
{
ak_ratio = window.devicePixelRatio;
}
//ak_ratio = 1;
var w = window.innerWidth;
var h = window.innerHeight;
console.log("WxH = " + w + "x" + h );
ak_ctx.canvas.width = Math.floor(w * ak_ratio);
ak_ctx.canvas.height = Math.floor(h * ak_ratio);
var rw = ak_ctx.canvas.width*0.001;
var rh = ak_ctx.canvas.height*0.001;
var rs = 7.5*Math.sqrt(rw*rw+rh*rh) * ak_ratio;
// 1. find best font regarding window aspect
let f = 0;
for (var i=1; i<ak_fonts.length; i++)
{
if (Math.abs(ak_fonts[f].size - rs) > Math.abs(ak_fonts[i].size - rs))
{
f = i;
}
}
return f;
}
function ZoomIn()
{
if (ak_font == ak_fonts.length-1)
return 0;
let f = ak_font;
let u = user_zoom;
user_zoom = ak_font - FindFont() + 1;
Resize(null);
if (ak_font == f) // clamped!
{
user_zoom = u; // revert!
return 0;
}
return 1;
}
function ZoomOut()
{
if (ak_font == 0)
return 0;
let f = ak_font;
let u = user_zoom;
user_zoom = ak_font - FindFont() - 1;
Resize(null);
if (ak_font == f) // clamped!
{
user_zoom = u; // revert!
return 0;
}
return 1;
}
function Resize(e)
{
ak_font = FindFont();
// 2. apply user glasses
ak_font += user_zoom;
if (ak_font < 0)
ak_font = 0;
if (ak_font >= ak_fonts.length)
ak_font = ak_fonts.length-1;
var w = window.innerWidth;
var h = window.innerHeight;
// 3. fix resolutions smaller than 45x36
while (ak_font>=1)
{
let cw,ch;
var fw = ak_fonts[ak_font].width >> 4;
var fh = ak_fonts[ak_font].height >> 4;
cw = Math.floor(w/fw*ak_ratio);
ch = Math.floor(h/fh*ak_ratio);
if (cw<45 || ch<36)
ak_font--;
else
break;
}
console.log("FONT="+ak_font);
var fw = ak_fonts[ak_font].width >> 4;
var fh = ak_fonts[ak_font].height >> 4;
if (ak_ratio<1.0)
Size(w,h, fw/ak_ratio,fh/ak_ratio);
else
Size(w*ak_ratio,h*ak_ratio, fw,fh);
}
function AsciickerLoop(tm)
{
var display_w = ak_ctx.drawingBufferWidth;// & ~1;
var display_h = ak_ctx.drawingBufferHeight;// & ~1;
var render_w = Math.floor( display_w / (ak_fonts[ak_font].width >> 4) );
var render_h = Math.floor( display_h / (ak_fonts[ak_font].height >> 4) );
if (render_w > ak_max_width)
render_w = ak_max_width;
if (render_h > ak_max_height)
render_h = ak_max_height;
display_w = render_w * (ak_fonts[ak_font].width >> 4);
display_h = render_h * (ak_fonts[ak_font].height >> 4);
var display_x = (ak_ctx.drawingBufferWidth - display_w) >> 1;
var display_y = (ak_ctx.drawingBufferHeight - display_h) >> 1;
if (gamepad>=0 && GamePad)
PollGamePad();
var mem = Render(render_w,render_h);
if (mem != ak_prev_mem || Module.HEAP8 != ak_prev_arr || render_w != ak_width || render_h != ak_height)
{
// realloc handler
ak_buf = new Uint8Array(Module.HEAP8.buffer,mem,render_w*render_h*4);
ak_prev_arr = Module.HEAP8;
ak_prev_mem = mem;
ak_width = render_w;
ak_height = render_h;
}
ak_ctx.activeTexture(ak_ctx.TEXTURE0);
ak_ctx.bindTexture(ak_ctx.TEXTURE_2D, ak_tex);
ak_ctx.texSubImage2D(ak_ctx.TEXTURE_2D, 0, 0, 0, render_w,render_h, ak_ctx.RGBA, ak_ctx.UNSIGNED_BYTE, ak_buf);
ak_ctx.viewport(display_x,display_y,display_w,display_h);
ak_ctx.clearColor(0,0,0,0);
ak_ctx.clear(ak_ctx.COLOR_BUFFER_BIT);
var fnt = ak_fonts[ak_font];
ak_ctx.activeTexture(ak_ctx.TEXTURE1);
ak_ctx.bindTexture(ak_ctx.TEXTURE_2D, fnt.tex);
// render quad
ak_ctx.useProgram(ak_prg);
ak_ctx.uniform1f(ak_uni_width, render_w);
ak_ctx.uniform1f(ak_uni_height, render_h);
ak_ctx.uniform1f(ak_uni_tex_width, ak_max_width);
ak_ctx.uniform1f(ak_uni_tex_height, ak_max_height);
ak_ctx.bindBuffer(ak_ctx.ARRAY_BUFFER, ak_vtx);
ak_ctx.drawArrays(ak_ctx.TRIANGLE_FAN,0,4);
window.requestAnimationFrame(AsciickerLoop);
}
function setTouchHandlers()
{
var touch_mask = 0xFFFC; // upto 3 touches!
var touches = {};
window.addEventListener("touchstart",
function(e)
{
e.preventDefault();
audioResume();
var data = event.changedTouches;
for (var i=0; i<data.length; i++)
{
if (touch_mask === 0xFFFF)
break;
var id = 0;
for (var bit=0; bit<16; bit++)
{
if ( (touch_mask & (1<<bit)) === 0 )
{
id = bit+1;
touch_mask |= 1<<bit;
break;
}
}
var x = data[i].pageX;
var y = data[i].pageY;
var key = data[i].identifier;
touches[key] = id;
if (ak_ratio>1.0)
{
x *= ak_ratio;
y *= ak_ratio;
}
Touch(1/*BEGIN*/,id,x,y);
}
}, { passive: false });
window.addEventListener("touchmove",
function(e)
{
e.preventDefault();
var data = event.changedTouches;
for (var i=0; i<data.length; i++)
{
var key = data[i].identifier;
var id = touches[key];
if (!id)
continue;
var x = data[i].pageX;
var y = data[i].pageY;
if (ak_ratio>1.0)
{
x *= ak_ratio;
y *= ak_ratio;
}
Touch(0/*MOVE*/,id,x,y);
}
}, { passive: false });
window.addEventListener("touchend",
function(e)
{
e.preventDefault();
var data = event.changedTouches;
for (var i=0; i<data.length; i++)
{
var key = data[i].identifier;
var id = touches[key];
if (!id)
continue;
touches[key] = 0;
touch_mask &= ~(1<<(id-1));
var x = data[i].pageX;
var y = data[i].pageY;
if (ak_ratio>1.0)
{
x *= ak_ratio;
y *= ak_ratio;
}
Touch(2/*END*/,id,x,y);
}
}, { passive: false });
window.addEventListener("touchcancel",
function(e)
{
e.preventDefault();
var data = event.changedTouches;
for (var i=0; i<data.length; i++)
{
var key = data[i].identifier;
var id = touches[key];
if (!id)
continue;
touches[key] = 0;
touch_mask &= ~(1<<(id-1));
var x = data[i].pageX;
var y = data[i].pageY;
if (ak_ratio>1.0)
{
x *= ak_ratio;
y *= ak_ratio;
}
Touch(3/*CANCEL*/,id,x,y);
}
}, { passive: false });
}
function setKeybHandlers()
{
var CodeMap =
{
"Backspace" : 1,
"Tab" : 2,
"Enter" : 3,
"Pause" : 4,
"Escape" : 5,
"Space" : 6,
"PageUp" : 7,
"PageDown" : 8,
"End" : 9,
"Home" : 10,
"ArrowLeft" : 11,
"ArrowUp" : 12,
"ArrowRight" : 13,
"ArrowDown" : 14,
"PrintScreen" : 15,
"Insert" : 16,
"Delete" : 17,
"Digit0" : 18,
"Digit1" : 19,
"Digit2" : 20,
"Digit3" : 21,
"Digit4" : 22,
"Digit5" : 23,
"Digit6" : 24,
"Digit7" : 25,
"Digit8" : 26,
"Digit9" : 27,
"KeyA" : 28,
"KeyB" : 29,
"KeyC" : 30,
"KeyD" : 31,
"KeyE" : 32,
"KeyF" : 33,
"KeyG" : 34,
"KeyH" : 35,
"KeyI" : 36,
"KeyJ" : 37,
"KeyK" : 38,
"KeyL" : 39,
"KeyM" : 40,
"KeyN" : 41,
"KeyO" : 42,
"KeyP" : 43,
"KeyQ" : 44,
"KeyR" : 45,
"KeyS" : 46,
"KeyT" : 47,
"KeyU" : 48,
"KeyV" : 49,
"KeyW" : 50,
"KeyX" : 51,
"KeyY" : 52,
"KeyZ" : 53,
"OSLeft" : 54,
"OSRight" : 55,
"ContextMenu" : 56,
"Numpad0" : 57,
"Numpad1" : 58,
"Numpad2" : 59,
"Numpad3" : 60,
"Numpad4" : 61,
"Numpad5" : 62,
"Numpad6" : 63,
"Numpad7" : 64,
"Numpad8" : 65,
"Numpad9" : 66,
"NumpadMultiply" : 67,
"NumpadDivide" : 68,
"NumpadAdd" : 69,
"NumpadSubtract" : 70,
"NumpadDecimal" : 71,
"NumpadEnter" : 72,
"F1" : 73,
"F2" : 74,
"F3" : 75,
"F4" : 76,
"F5" : 77,
"F6" : 78,
"F7" : 79,
"F8" : 80,
"F9" : 81,
"F10" : 82,
"F11" : 83,
"F12" : 84,
"F13" : 85,
"F14" : 86,
"F15" : 87,
"F16" : 88,
"F17" : 89,
"F18" : 90,
"F19" : 91,
"F20" : 92,
"F21" : 93,
"F22" : 94,
"F23" : 95,
"F24" : 96,
"CapsLock" : 97,
"NumLock" : 98,
"ScrollLock" : 99,
"ShiftLeft" : 100,
"ShiftRight" : 101,
"ControlLeft" : 102,
"ControlRight" : 103,
"AltLeft" : 104,
"AltRight" : 105,
"Semicolon" : 106,
"Equal" : 107,
"Comma" : 108,
"Minus" : 109,
"Period" : 110,
"Slash" : 111,
"Backquote" : 112,
"BracketLeft" : 113,
"BracketRight" : 114,
"Backslash" : 115,
"Quote" : 116
};
window.addEventListener("keydown",
function(e)
{
//var str = "repeat=" + e.repeat + " | " + "code=" + e.code + " | " + "key=" + e.key;
//console.log( str );
audioResume();
if (e.key == "F11" || (e.key == "-" || e.key == "+" || e.key == "=" || e.key == "0") && e.ctrlKey)
return; // do not process / prevent
if ( e.code )
{
var key = CodeMap[e.code];
if (key)
{
if (e.repeat)
key += 256;
Keyb(0/*DOWN*/,key);
}
else
return; // do not process / prevent
}
else
return; // do not process / prevent
switch (e.key)
{
case "Tab": Keyb(2/*CHAR*/, 5); break;
case "Enter": Keyb(2/*CHAR*/, 10); break;
case "Backspace": Keyb(2/*CHAR*/, 8); break;
case "Delete": Keyb(2/*CHAR*/, 127); break;
default:
if (e.key.length == 1)
Keyb(2/*CHAR*/, e.key.charCodeAt(0));
}
e.preventDefault();
},{ passive: false });
window.addEventListener("keyup",
function(e)
{
if (e.key == "F11" || (e.key == "-" || e.key == "+" || e.key == "=" || e.key == "0") && e.ctrlKey)
return; // do not process / prevent
if ( e.code )
{
var key = CodeMap[e.code];
if (key)
{
Keyb(1/*UP*/,key);
}
else
return; // do not process / prevent
}
else
return; // do not process / prevent
e.preventDefault();
},{ passive: false });
}
function setGamePadHandlers()
{
var axes = null;
var buts = null;
function GamePadMap(gp)
{
GamePad(3,gp.buttons.length | (gp.axes.length<<16),0);
if (gp.mapping=='standard')
{
for (var a=0; a<gp.axes.length; a++)
{
var m = 0xFFFF;
if (a<4)
{
m = (a | (1<<6)) | (a<<8);
}
GamePad(5,(a<<16) | m,0);
}
for (var b=0; b<gp.buttons.length; b++)
{
var m = 0xFF;
if (b<17)
{
switch (b)
{
case 0: m=0 | (1<<7); break;
case 1: m=1 | (1<<7); break;
case 2: m=2 | (1<<7); break;
case 3: m=3 | (1<<7); break;
// shoulders
case 4: m=9 | (1<<7); break;
case 5: m=10 | (1<<7); break;
// triggers
case 6: m=4; break;
case 7: m=5; break;
case 8: m=4 | (1<<7); break; // back
case 9: m=6 | (1<<7); break; // start
// thumbs
case 10: m=7 | (1<<7); break;
case 11: m=8 | (1<<7); break;
// dpad
case 12: m=11 | (1<<7); break;
case 13: m=12 | (1<<7); break;
case 14: m=13 | (1<<7); break;
case 15: m=14 | (1<<7); break;
// guide
case 16: m=5 | (1<<7); break;
}
}
GamePad(4,(b<<8) | m,0);
}
}
else
{
// generic x-box pad
for (var b=0; b<gp.buttons.length; b++)
{
var m = 0xFF;
switch (b)
{
case 0x00: m = (1<<7) | 0x00; break;
case 0x01: m = (1<<7) | 0x01; break;
case 0x02: m = (1<<7) | 0x02; break;
case 0x03: m = (1<<7) | 0x03; break;
case 0x06: m = (1<<7) | 0x04; break;
case 0x08: m = (1<<7) | 0x05; break;
case 0x07: m = (1<<7) | 0x06; break;
case 0x09: m = (1<<7) | 0x07; break;
case 0x0A: m = (1<<7) | 0x08; break;
case 0x04: m = (1<<7) | 0x09; break;
case 0x05: m = (1<<7) | 0x0A; break;
}
GamePad(4,(b<<8) | m,0);
}
for (var a=0; a<gp.axes.length; a++)
{
var m = 0xFFFF;
switch (a)
{
case 0: m = ((0<<7) | (1<<6) | 0x00) | ((0<<7) | (0<<6) | 0x00)<<8; break;
case 1: m = ((0<<7) | (1<<6) | 0x01) | ((0<<7) | (0<<6) | 0x01)<<8; break;
case 2: m = ((0<<7) | (0<<6) | 0x04) | ((0<<7) | (0<<6) | 0x04)<<8; break;
case 3: m = ((0<<7) | (1<<6) | 0x02) | ((0<<7) | (0<<6) | 0x02)<<8; break;
case 4: m = ((0<<7) | (1<<6) | 0x03) | ((0<<7) | (0<<6) | 0x03)<<8; break;
case 5: m = ((0<<7) | (0<<6) | 0x05) | ((0<<7) | (0<<6) | 0x05)<<8; break;
case 6: m = ((1<<7) | (0<<6) | 0x0D) | ((1<<7) | (0<<6) | 0x0E)<<8; break;
case 7: m = ((1<<7) | (0<<6) | 0x0B) | ((1<<7) | (0<<6) | 0x0C)<<8; break;
}
GamePad(5,(a<<16) | m,0);
}
}
}
function poll_handler()
{
var gm = navigator.getGamepads()[gamepad];
if (GamePad && gm)
{
var nb = gm.buttons.length;
for (var i=0; i<nb; i++)
{
if (buts[i] != gm.buttons[i].value)
{
GamePad(1,i,gm.buttons[i].value);