-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
741 lines (662 loc) · 21.5 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
.body {
background-color: grey;
text-align: left;
vertical-align: middle;
overflow: hidden;
width: 100%;
}
.gamebutton {
text-align: left;
width: 100%;
padding: 20% 0% 20% 85%;
text-align: center;
background-color: paleturquoise;
color: palevioletred;
display: inline-flex;
font-size: 30px;
cursor: pointer;
}
.gamebuttontest {
width: 50%;
height: 20%;
padding: 20% 0% 0% 20%;
text-align: center;
background-color: paleturquoise;
display: inline-flex;
font-size: 30px;
cursor: pointer;
}
.gamebuttonjoy {
touch-action: none;
width: 65%;
text-align: center;
align-content: center;
display: inline-flex;
border: 1px solid black;
}
.setup{
text-align: center;
}
#output {
overflow: auto;
}
#output > p {
overflow-wrap: break-word;
}
#output span {
color: blue;
}
#output span.error {
color: red;
}
#joystick{
border: 1px solid black;
background-color: paleturquoise;
touch-action: none;
text-align: left;
border: 1px solid black;
}
</style>
</head>
<body class="body" onresize="OnResize()">
<h4 style="size: 10%; color: white; text-align: center;">Flow Controller</h4>
<!-- The field that people insert their IP -->
<div id="setup" class="setup">
IP: <br/>
<input
id="wsip"
type="text"
value="10.151.2.138"
style="background-color: lightpink; color: blue; border: none;"
/>
<!-- The token field. called "Code" because it's what a normal user would refer to it as -->
<br/> Code: <br/>
<input
id="token"
type="text"
value=""
style="background-color: lightpink; color: blue; border: none;"
/>
<!-- The field in which a user enters their username -->
<br/> Username: <br/>
<input
id="username"
type="text"
value="Anonymous"
style="background-color: lightpink; color: blue; border: none;"
/>
<br/>
<!-- The "Connect" button -->
<button id="setupB" style="background-color: cadetblue; border: none;">
Connect
</button>
</div>
<div id="quizButtons" hidden > <!-- debug by removing hidden -->
<button id="Xbutton" class="gamebuttontest" style="background-color:paleturquoise; ">
<button id="Ybutton" class="gamebuttontest" style="background-color:rgb(229, 229, 155);" > </button>
</button>
<button id="Abutton" class="gamebuttontest" style="background-color:rgb(121, 230, 121); ">
<button id="Bbutton" class="gamebuttontest" style="background-color:palevioletred;" > </button>
</button>
</div>
<div id="gameButtons" hidden> <!-- debug by removing hidden -->
<div id="left" class="gamebuttonjoy"> <button id="right" class="gamebutton" style="background-color:palevioletred; color: paleturquoise;" > </button></div>
</div>
<div id="output"></div>
<script>
/**
* Code for the joystick
*/
//code modified from https://github.com/bobboteck/JoyStick MIT
var JoyStick = (function(container, parameters) {
parameters = parameters || {};
var title = (undefined === parameters.title ? 'joystick' : parameters.title),
width = (undefined === parameters.width ? 0 : parameters.width),
height = (undefined === parameters.height ? 0 : parameters.height),
internalFillColor = (undefined === parameters.internalFillColor ? '#00AA00' : parameters.internalFillColor),
internalLineWidth = (undefined === parameters.internalLineWidth ? 2 : parameters.internalLineWidth),
internalStrokeColor = (undefined === parameters.internalStrokeColor ? '#003300' : parameters.internalStrokeColor),
externalLineWidth = (undefined === parameters.externalLineWidth ? 2 : parameters.externalLineWidth),
externalStrokeColor = (undefined === parameters.externalStrokeColor ? '#008000' : parameters.externalStrokeColor),
autoReturnToCenter = (undefined === parameters.autoReturnToCenter ? true : parameters.autoReturnToCenter);
// Create Canvas element and add it in the Container object
var objContainer = document.getElementById(container);
var canvas = document.createElement('canvas');
canvas.id = title;
console.log(objContainer.offsetWidth)
if(width == 0) width = 250;
if(height == 0) height = 250;
canvas.width = width;
canvas.height = height;
objContainer.prepend(canvas);
var context=canvas.getContext('2d');
var pressed = 0; // Bool - 1=Yes - 0=No
var circumference = 2 * Math.PI;
//if you want to change the width or height change the 25
var internalRadius = (canvas.width - (width - 200)/2 -((50*2)+10)) /2;
var maxMoveStick = internalRadius + 5;
var externalRadius = internalRadius + 30;
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var directionHorizontalLimitPos = canvas.width / 10;
var directionHorizontalLimitNeg = directionHorizontalLimitPos * -1;
var directionVerticalLimitPos = canvas.height / 10;
var directionVerticalLimitNeg = directionVerticalLimitPos * -1;
// Used to save current position of stick
var movedX=centerX;
var movedY=centerY;
canvas.width = canvas.width;
canvas.height = canvas.height;
// Check if the device support the touch or not
canvas.addEventListener('touchstart', onTouchStart, false);
canvas.addEventListener('touchmove', onTouchMove, false);
canvas.addEventListener('touchend', onTouchEnd, false);
canvas.addEventListener('mousedown', onMouseDown, false);
canvas.addEventListener('mousemove', onMouseMove, false);
canvas.addEventListener('mouseup', onMouseUp, false);
// Draw the object
drawExternal();
drawInternal(centerX, centerY);
/******************************************************
* Private methods
*****************************************************/
/**
* @desc Draw the external circle used as reference position
*/
function drawExternal()
{
context.beginPath();
context.arc(centerX, centerY, externalRadius, 0, circumference, false);
context.lineWidth = externalLineWidth;
context.strokeStyle = externalStrokeColor;
context.stroke();
}
/**
* @desc Draw the internal stick in the current position the user have moved it
*/
function drawInternal()
{
context.beginPath();
if(movedX<internalRadius) movedX=maxMoveStick;
if((movedX+internalRadius)>canvas.width) movedX=canvas.width-(maxMoveStick);
if(movedY<internalRadius) movedY=maxMoveStick;
if((movedY+internalRadius)>canvas.height) movedY=canvas.height-(maxMoveStick);
context.arc(movedX, movedY, internalRadius, 0, circumference, false);
// create radial gradient
var grd = context.createRadialGradient(centerX, centerY, 5, centerX, centerY, 200);
// Light color
grd.addColorStop(0, internalFillColor);
// Dark color
grd.addColorStop(1, internalStrokeColor);
context.fillStyle = grd;
context.fill();
context.lineWidth = internalLineWidth;
context.strokeStyle = internalStrokeColor;
context.stroke();
}
/**
* @desc Events for manage touch
*/
function onTouchStart(event)
{
pressed=1;
}
function onTouchMove(event)
{
// Prevent the browser from doing its default thing (scroll, zoom)
event.preventDefault();
if(pressed==1)
{
movedX=event.touches[0].pageX;
movedY=event.touches[0].pageY;
// Manage offset
movedX-=canvas.offsetLeft;
movedY-=canvas.offsetTop;
// Delete canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// Redraw object
drawExternal();
drawInternal();
}
}
function onTouchEnd(event)
{
pressed=0;
// If required reset position store variable
// was if auto return to center. I want this true
if(true)
{
movedX=centerX;
movedY=centerY;
}
// Delete canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// Redraw object
drawExternal();
drawInternal();
//canvas.unbind('touchmove');
}
/**
* @desc Events for manage mouse
*/
function onMouseDown(event)
{
pressed=1;
}
function onMouseMove(event)
{
if(pressed==1)
{
movedX=event.pageX;
movedY=event.pageY;
// Manage offset
movedX-=canvas.offsetLeft;
movedY-=canvas.offsetTop;
// Delete canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// Redraw object
drawExternal();
drawInternal();
}
}
function onMouseUp(event)
{
pressed=0;
// If required reset position store variable
// was if auto return to center. I want this true
if(true)
{
movedX=centerX;
movedY=centerY;
}
// Delete canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// Redraw object
drawExternal();
drawInternal();
//canvas.unbind('mousemove');
}
/******************************************************
* Public methods
*****************************************************/
/**
* @desc The width of canvas
* @return Number of pixel width
*/
this.GetWidth = function ()
{
return canvas.width;
};
/**
* @desc The height of canvas
* @return Number of pixel height
*/
this.GetPressed= function ()
{
return pressed;
};
/**
* @desc The height of canvas
* @return Number of pixel height
*/
this.GetHeight = function ()
{
return canvas.height;
};
/**
* @desc The X position of the cursor relative to the canvas that contains it and to its dimensions
* @return Number that indicate relative position
*/
this.GetPosX = function ()
{
return movedX;
};
/**
* @desc The Y position of the cursor relative to the canvas that contains it and to its dimensions
* @return Number that indicate relative position
*/
this.GetPosY = function ()
{
return movedY;
};
/**
* @desc Normalizzed value of X move of stick
* @return Integer from -100 to +100
*/
this.GetX = function ()
{
return (((movedX - centerX)/maxMoveStick)).toFixed(2);
};
/**
* @desc Normalizzed value of Y move of stick
* @return Integer from -100 to +100
*/
this.GetY = function ()
{
return ((((movedY - centerY)/maxMoveStick))*-1).toFixed(2);
};
/**
* @desc Get the direction of the cursor as a string that indicates the cardinal points where this is oriented
* @return String of cardinal point N, NE, E, SE, S, SW, W, NW and C when it is placed in the center
*/
this.GetDir = function()
{
var result = "";
var orizontal = movedX - centerX;
var vertical = movedY - centerY;
if(vertical>=directionVerticalLimitNeg && vertical<=directionVerticalLimitPos)
{
result = "C";
}
if(vertical<directionVerticalLimitNeg)
{
result = "N";
}
if(vertical>directionVerticalLimitPos)
{
result = "S";
}
if(orizontal<directionHorizontalLimitNeg)
{
if(result=="C")
{
result = "W";
}
else
{
result += "W";
}
}
if(orizontal>directionHorizontalLimitPos)
{
if(result=="C")
{
result = "E";
}
else
{
result += "E";
}
}
return result;
};
});
</script>
<script>
// http://www.websocket.org/echo.html
/**
* The JSON object for our controller
*/
var betterController = {
joystick: {
x: 0,
y: 0
},
a: false,
b: false,
x: false,
y: false,
start: false
}
/**
* The JSON object that has all information of a user and they current controlelr state
*/
var user = {
name: "",
ip: "",
controller: {
joystick: {
x: 0,
y: 0
},
a: false,
b: false,
x: false,
y: false,
start: false
}
}
/**
* JSON object for the header
*/
var header = {
header: "",
type: "",
jsonBlock: ""
}
/**
* JSON object we send the server to verify tokens
*/
var token = {
token: ""
}
// Inintialize field variables
let output = document.getElementById("output"),
left = document.getElementById("left"),
right = document.getElementById("right"),
setupbtn = document.getElementById("setupB");
xButton = document.getElementById("Xbutton");
yButton = document.getElementById("Ybutton");
aButton = document.getElementById("Abutton");
bButton = document.getElementById("Bbutton");
let wsip = document.getElementById("wsip");
let tokenInput = document.getElementById("token");
let gameButtons = document.getElementById("gameButtons");
let setupButtons = document.getElementById("setup");
let quizButtons = document.getElementById("quizButtons");
wsip.value = window.location.hostname;
let leftJoy = null;
setInterval(onLeftJoystick,50);
// Add events for the case of a button being hit or the joystick being moved
setupbtn.addEventListener("click", setupConnection);
right.addEventListener("click", onXButton);
right.addEventListener("touchstart", onXButton);
xButton.addEventListener("click", onXButton);
xButton.addEventListener("touchstart", onXButton);
yButton.addEventListener("click", onYButton);
yButton.addEventListener("touchstart", onYButton);
aButton.addEventListener("click", onAButton);
aButton.addEventListener("touchstart", onAButton);
bButton.addEventListener("click", onBButton);
bButton.addEventListener("touchstart", onBButton);
//add parameters to the joystick to make it resize
let browersWidth = left.clientWidth / 1.5;
let joyParm = '{"width" : '+ browersWidth +', "height": '+ browersWidth+ '}';
//debug
//leftJoy = new JoyStick('left',JSON.parse(joyParm));
let ISCONNECTED = false;
let username = "null";
/**
* Sets up the connection with the server
*/
function setupConnection() {
username = document.getElementById("username").value;
let ip = setupbtn.value;
if(username == null || token == null){
return;
}
user.name = username;
user.ip = "0.0.0.0";
user.header = "user";
console.log(ip);
wsUri = "ws://" + wsip.value + ":8000" + "/";
websocket = new WebSocket(wsUri);
websocket.onopen = function(e) {
openFullscreen();
writeToScreen("CONNECTED");
ISCONNECTED = true;
gameButtons.hidden = false;
setupButtons.hidden = true;
token.token = tokenInput.value;
//force upercase
token.token = token.token.toUpperCase();
header.header = "user";
header.type = "Token";
header.jsonBlock = JSON.stringify(token);
doSend(JSON.stringify(header));
if(leftJoy == null){
leftJoy = new JoyStick('left',JSON.parse(joyParm));
}
};
websocket.onclose = function(e) {
closeFullscreen();
ISCONNECTED = false;
writeToScreen("DISCONNECTED");
gameButtons.hidden = true;
quizButtons.hidden = true;
setupButtons.hidden = false;
};
websocket.onmessage = function(e) {
console.log("revived " + e.data);
if(e.data == "QUIZ"){
quizButtons.hidden = false;
gameButtons.hidden = true;
}else{
quizButtons.hidden = true;
gameButtons.hidden = false;
}
};
websocket.onerror = function(e) {
writeToScreen("<span class=error>ERROR:</span> " + e.data);
};
}
/**
* Sends a string to the server
*/
function doSend(message) {
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen(message) {
output.innerHTML = message;
}
/**
* Causes the appropriate reaction in the case of the joystick being moved
*/
function onLeftJoystick() {
if(leftJoy){
if(leftJoy.GetPressed()){
betterController.joystick.x = leftJoy.GetX();
betterController.joystick.y = leftJoy.GetY();
}else{
betterController.joystick.x = 0;
betterController.joystick.y = 0;
}
sendController();
}
}
/**
* Sends the current state of the user (and by extension, the controller)
*/
function sendController() {
user.controller.a = betterController.a;
user.controller.b = betterController.b;
user.controller.x = betterController.x;
user.controller.y = betterController.y;
user.controller.start = betterController.start;
user.controller.joystick.x = betterController.joystick.x;
user.controller.joystick.y = betterController.joystick.y;
header.header = "user";
header.type = "user";
header.jsonBlock = JSON.stringify(user);
doSend(JSON.stringify(header));
betterController.a = false;
betterController.b = false;
betterController.x = false;
betterController.y = false;
betterController.start = false;
}
/**
* Causes the appropriate reaction in the case of the A button being pressed
*/
function onAButton() {
betterController.a = true;
vibrate();
sendController();
}
/**
* Causes the appropriate reaction in the case of the B button being pressed
*/
function onBButton() {
betterController.b = true;
vibrate();
sendController();
}
/**
* Causes the appropriate reaction in the case of the X button being pressed
*/
function onXButton() {
betterController.x = true;
vibrate();
sendController();
}
/**
* Causes the appropriate reaction in the case of the Y button being pressed
*/
function onYButton() {
betterController.y = true;
vibrate();
sendController();
}
/**
* Vibrates. Not sure what you expected. it'
*/
function vibrate() {
if ("vibrate" in navigator) {
navigator.vibrate(50);
}
}
/**
* Sets a device to full screen
*/
var elem = document.documentElement;
function openFullscreen() {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) {
/* Firefox */
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
/* Chrome, Safari & Opera */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) {
/* IE/Edge */
elem.msRequestFullscreen();
}
}
/**
* Allows you to exit full screen mode
*/
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
/**
* determines the sixe of a window when the size is changed
*/
function OnResize(){
if(ISCONNECTED){
document.getElementById('joystick').remove();
browersWidth = left.clientWidth / 1.5;
joyParm = '{"width" : '+ browersWidth +', "height": '+ browersWidth+ '}'
leftJoy = new JoyStick('left',JSON.parse(joyParm));
}
}
</script>
</body>
</html>