-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
323 lines (273 loc) · 9.21 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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Elli Server</title>
<script>
const serverIP = "192.168.0.102"
const instructionSendIntervalMilis = 500
function getRequestEndpoint(type) {
return "http://" + serverIP + ":5000/" + type
}
function queryStatus() {
let http = new XMLHttpRequest();
http.onreadystatechange = () => {
if (http.readyState === XMLHttpRequest.DONE) {
const response = JSON.parse(http.responseText)
if (response.status === "OK") {
connected(response.addr[0])
} else {
setTimeout(queryStatus, 1000)
}
}
};
const endpoint = getRequestEndpoint("status")
http.open("GET", endpoint, true);
http.send();
}
function connected(addr) {
let statusTextEl = document.getElementsByClassName("status-text")[0];
statusTextEl.innerHTML = "Connected! - " + addr;
statusTextEl.style.fontWeight = "bold";
let ledEl = document.getElementsByClassName("led")[0];
ledEl.classList.remove('led-yellow');
ledEl.classList.add('led-green');
let arrowsEl = document.getElementsByClassName("arrows-container")[0];
arrowsEl.style.display = "block";
}
var elementIdToDirection = {
"up-arrow": "straight",
"down-arrow": "back",
"left-arrow": "left",
"right-arrow": "right"
}
function sendInstruction() {
if (window.currentArrow) {
let direction = elementIdToDirection[window.currentArrow];
let data = JSON.stringify({
"direction": direction
})
var xhr = new XMLHttpRequest();
xhr.open("POST", getRequestEndpoint("instruction"), true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(data);
setTimeout(sendInstruction, instructionSendIntervalMilis)
}
}
function getArrowId(event) {
let arrowElement = event.target.id ? event.target : event.target.parentElement;
return arrowElement.id;
}
function onArrowDown(event) {
let id = getArrowId(event);
onNamedArrowDown(id);
}
function onNamedArrowDown(id) {
window.currentArrow = id;
sendInstruction();
let arrowEl = document.getElementById(id);
arrowEl.classList.remove("arrow-hover")
arrowEl.classList.add("arrow-pressed")
}
function onArrowUp(event) {
if (window.currentArrow) {
let arrowEl = document.getElementById(window.currentArrow);
arrowEl.classList.remove("arrow-pressed")
let releasedId = getArrowId(event)
if (releasedId) {
let releasedArrowEl = document.getElementById(releasedId);
releasedArrowEl.classList.add("arrow-hover");
}
window.currentArrow = undefined
}
}
function onArrowOver(event) {
if (!window.currentArrow) {
let id = getArrowId(event);
let arrowEl = document.getElementById(id);
arrowEl.classList.add("arrow-hover")
}
}
function onArrowOut(event) {
let id = getArrowId(event);
let arrowEl = document.getElementById(id);
arrowEl.classList.remove("arrow-hover")
}
function setArrowHooks() {
let arrowEls = document.getElementsByClassName("arrow-btn");
for (index = 0; index < arrowEls.length; index++) {
arrowEls[index].addEventListener("mousedown", onArrowDown);
arrowEls[index].addEventListener("touchstart", onArrowDown);
arrowEls[index].addEventListener("mouseover", onArrowOver);
arrowEls[index].addEventListener("mouseout", onArrowOut);
}
document.addEventListener("mouseup", onArrowUp)
document.addEventListener("touchend", onArrowUp)
}
function setKeyboardHooks() {
document.onkeydown = (e) => {
switch(e.key) {
case "ArrowUp":
onNamedArrowDown("up-arrow");
break;
case "ArrowDown":
onNamedArrowDown("down-arrow");
break;
case "ArrowLeft":
onNamedArrowDown("left-arrow");
break;
case "ArrowRight":
onNamedArrowDown("right-arrow");
break;
}
}
document.onkeyup = (e) => {
onArrowUp(e);
}
}
window.onload = () => {
queryStatus()
setArrowHooks()
setKeyboardHooks()
}
</script>
<style>
body,
html {
min-height: 100%;
margin: 0;
}
body {
color: azure;
background: linear-gradient(180deg, rgba(2, 0, 36, 1) 0%, rgba(2, 0, 36, 1) 67%, rgba(200, 113, 10, 1) 100%);
}
p {
color: #b4afaf;
}
.container {
padding: 7rem;
display: flex;
align-items: flex-start;
justify-content: space-between
}
.status {
border: 1px solid #b4afaf;
border-radius: 20px;
padding: 1rem;
width: 20rem;
}
.status-container {
display: flex;
justify-content: space-between;
}
.status-text {
margin: 0px;
}
.led {
margin: 1rem;
width: 24px;
height: 24px;
border-radius: 50%;
}
.led-yellow {
background-color: #FF0;
box-shadow: rgba(0, 0, 0, 0.2) 0 -1px 7px 1px, inset #808002 0 -1px 9px, #FF0 0 2px 12px;
animation: blinkYellow 1s infinite;
}
@keyframes blinkYellow {
from {
background-color: #FF0;
}
50% {
background-color: #AA0;
box-shadow: rgba(0, 0, 0, 0.2) 0 -1px 7px 1px, inset #808002 0 -1px 9px, #FF0 0 2px 0;
}
to {
background-color: #FF0;
}
}
.led-green {
background-color: #ABFF00;
box-shadow: rgba(0, 0, 0, 0.2) 0 -1px 7px 1px, inset #304701 0 -1px 9px, #89FF00 0 2px 12px;
}
.arrows-container {
text-align: center;
display: none;
margin-bottom: 2rem;
}
.arrow-btn {
border: 2px solid white;
border-radius: 5px;
background: inherit;
padding: 10px;
}
button:hover {
cursor: pointer;
}
.arrow-hover {
background: rgba(255, 255, 255, 0.15);
}
.arrow-pressed {
background: rgba(255, 255, 255, 0.70);
}
div.arrow {
border: solid white;
border-width: 0 8px 8px 0;
display: inline-block;
padding: 8px;
}
.right {
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
}
.left {
transform: rotate(135deg);
-webkit-transform: rotate(135deg);
}
.up {
transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
}
.down {
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
</style>
</head>
<body>
<div class="container">
<div class="text">
<h1>Elli Server</h1>
<p>This is a simple server that listens on UDP connections, then allowes key inputs to be send.</p>
</div>
<div class="status">
<div class="status-container">
<h2>Status</h2>
<div class="led led-yellow"></div>
</div>
<p class="status-text">
Connecting ...
</p>
</div>
</div>
<div class="arrows-container">
<div class="row">
<button id="up-arrow" class="arrow-btn">
<div class="arrow up"></div>
</button>
</div>
<div class="row">
<button id="left-arrow" class="arrow-btn">
<div class="arrow left"></div>
</button>
<button id="down-arrow" class="arrow-btn">
<div class="arrow down"></div>
</button>
<button id="right-arrow" class="arrow-btn">
<div class="arrow right"></div>
</button>
</div>
</div>
</body>
</html>