-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.js
384 lines (307 loc) · 13.3 KB
/
ui.js
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
//stores where exactly is being in the ui if not inside the gamesession
let in_main_menu = {
home: true,
single_player: false,
two_player: false,
multiplayer: false,
tournament: false,
settings: false,
about: false
};
let game_duration_selected;
let two_player_menu_btns;
if (window.innerWidth > window.innerHeight) {
//LAPTOPS
two_player_menu_btns = {
"30_seconds": {
x: 15,
y: 60,
w: 20,
h: 20,
},
"60_seconds": {
x: 15,
y: 90,
w: 20,
h: 20,
},
"90_seconds": {
x: 15,
y: 120,
w: 20,
h: 20,
},
"120_seconds": {
x: 15,
y: 150,
w: 20,
h: 20,
},
"3_goals": {
x: 15,
y: 190,
w: 20,
h: 20,
},
"5_goals": {
x: 15,
y: 220,
w: 20,
h: 20,
},
"10_goals": {
x: 15,
y: 250,
w: 20,
h: 20,
}
}
} else {
//SMARTPHONES
two_player_menu_btns = {
"30_seconds": {
x: 25,
y: -window.innerWidth + 70,
w: 10,
h: 10,
},
"60_seconds": {
x: 25,
y: -window.innerWidth + 90,
w: 10,
h: 10,
},
"90_seconds": {
x: 25,
y: -window.innerWidth + 110,
w: 10,
h: 10,
},
"120_seconds": {
x: 25,
y: -window.innerWidth + 130,
w: 10,
h: 10,
},
"3_goals": {
x: 25,
y: -window.innerWidth + 160,
w: 10,
h: 10,
},
"5_goals": {
x: 25,
y: -window.innerWidth + 180,
w: 10,
h: 10,
},
"10_goals": {
x: 25,
y: -window.innerWidth + 200,
w: 10,
h: 10,
}
}
}
two_player_menu_btns['30_seconds'].name = '30_seconds';
two_player_menu_btns['60_seconds'].name = '60_seconds';
two_player_menu_btns['90_seconds'].name = '90_seconds';
two_player_menu_btns['120_seconds'].name = '120_seconds';
two_player_menu_btns['3_goals'].name = '3_goals';
two_player_menu_btns['5_goals'].name = '5_goals';
two_player_menu_btns['10_goals'].name = '10_goals';
two_player_menu_btns['30_seconds'].elm = document.getElementById('radio_unchecked');
two_player_menu_btns['60_seconds'].elm = document.getElementById('radio_checked');
two_player_menu_btns['90_seconds'].elm = document.getElementById('radio_unchecked');
two_player_menu_btns['120_seconds'].elm = document.getElementById('radio_unchecked');
two_player_menu_btns['3_goals'].elm = document.getElementById('radio_unchecked');
two_player_menu_btns['5_goals'].elm = document.getElementById('radio_unchecked');
two_player_menu_btns['10_goals'].elm = document.getElementById('radio_unchecked');
//creating the canvas for the user interface
ui_canvas = document.createElement("canvas");
ui_canvas.id = "ui_canvas";
ui_canvas.style.border = "1px solid #000000";
ui_canvas.width = window.innerWidth;
ui_canvas.height = window.innerHeight;
ui_ctx = ui_canvas.getContext("2d");
document.getElementById("main_menu").appendChild(ui_canvas);
let positions_before_swipe = [];
function draw_ui() {
//home menu
if (in_main_menu.home) {
//draw the grass
ui_ctx.drawImage(document.getElementById("ui_grass"), 0, 0, window.innerWidth, window.innerHeight);
//write the ui buttons
for (i in ui_buttons) {
//buttons that only appear in one page
if (ui_buttons[i].page != "all") {
ui_ctx.beginPath();
ui_ctx.arc(ui_buttons[i].cx, ui_buttons[i].cy, ui_buttons[i].radius, 0, 2 * Math.PI);
ui_ctx.stroke();
ui_ctx.fillStyle = ui_buttons[i].color;
ui_ctx.fill();
ui_ctx.font = "20px Comic Sans MS";
ui_ctx.fillStyle = "red";
if (window.innerHeight > window.innerWidth) {
//on SMARTPHONES, text should be rotated
ui_ctx.save();
ui_ctx.rotate(90 * Math.PI / 180);
if (ui_buttons[i].text_1 != undefined) {
ui_ctx.fillText(ui_buttons[i].text_1, ui_buttons[i].cy - (ui_ctx.measureText(ui_buttons[i].text_1).width / 2), (-ui_buttons[i].cx));
ui_ctx.fillText(ui_buttons[i].text_2, ui_buttons[i].cy - (ui_ctx.measureText(ui_buttons[i].text_1).width / 2), (-ui_buttons[i].cx) + 20);
} else {
ui_ctx.fillText(ui_buttons[i].text, ui_buttons[i].cy - (ui_ctx.measureText(ui_buttons[i].text_1).width / 2), (-ui_buttons[i].cx));
}
ui_ctx.restore();
//move the buttons on swipe
if (ui_swiped == 'right' && ui_buttons[i].cy < ui_buttons[i].defaults.page_1.cy) {
ui_buttons[i].cy += ui_swipe_speed;
} else if (ui_swiped == 'left' && ui_buttons[i].cy > ui_buttons[i].defaults.page_2.cy) {
ui_buttons[i].cy -= ui_swipe_speed;
}
} else {
//on LAPTOPS
if (ui_buttons[i].text_1 != undefined) {
ui_ctx.fillText(ui_buttons[i].text_1, ui_buttons[i].cx - (ui_ctx.measureText(ui_buttons[i].text_1).width / 2), ui_buttons[i].cy);
ui_ctx.fillText(ui_buttons[i].text_2, ui_buttons[i].cx - (ui_ctx.measureText(ui_buttons[i].text_1).width / 2), ui_buttons[i].cy + 20);
} else {
ui_ctx.fillText(ui_buttons[i].text, ui_buttons[i].cx - (ui_ctx.measureText(ui_buttons[i].text_1).width / 2), ui_buttons[i].cy);
}
//move the buttons on swipe
if (ui_swiped == 'right' && ui_buttons[i].cx < ui_buttons[i].defaults.page_1.cx) {
ui_buttons[i].cx += ui_swipe_speed;
} else if (ui_swiped == 'left' && ui_buttons[i].cx > ui_buttons[i].defaults.page_2.cx) {
ui_buttons[i].cx -= ui_swipe_speed;
}
}
} else {
//buttons that appear in all the pages of the UI
if (ui_buttons[i].name != 'back_button') {
ui_ctx.drawImage(ui_buttons[i].img, ui_buttons[i].x, ui_buttons[i].y, ui_buttons[i].width, ui_buttons[i].height);
}
}
}
} else if (in_main_menu.single_player) {
//settings menu
//draw the grass
ui_ctx.drawImage(document.getElementById("ui_grass"), 0, 0, window.innerWidth, window.innerHeight);
//the back and fullscreen button
ui_ctx.drawImage(ui_buttons.fullscreen.img, ui_buttons.fullscreen.x, ui_buttons.fullscreen.y, ui_buttons.fullscreen.width, ui_buttons.fullscreen.height);
ui_ctx.drawImage(ui_buttons.back_button.img, ui_buttons.back_button.x, ui_buttons.back_button.y, ui_buttons.back_button.width, ui_buttons.back_button.height);
} else if (in_main_menu.two_player) {
//draw the grass
ui_ctx.drawImage(document.getElementById("ui_grass"), 0, 0, window.innerWidth, window.innerHeight);
//the back and fullscreen button
ui_ctx.drawImage(ui_buttons.fullscreen.img, ui_buttons.fullscreen.x, ui_buttons.fullscreen.y, ui_buttons.fullscreen.width, ui_buttons.fullscreen.height);
ui_ctx.drawImage(ui_buttons.back_button.img, ui_buttons.back_button.x, ui_buttons.back_button.y, ui_buttons.back_button.width, ui_buttons.back_button.height);
two_player_menu();
} else if (in_main_menu.multiplayer) {
//settings menu
//draw the grass
ui_ctx.drawImage(document.getElementById("ui_grass"), 0, 0, window.innerWidth, window.innerHeight);
//the back and fullscreen button
ui_ctx.drawImage(ui_buttons.fullscreen.img, ui_buttons.fullscreen.x, ui_buttons.fullscreen.y, ui_buttons.fullscreen.width, ui_buttons.fullscreen.height);
ui_ctx.drawImage(ui_buttons.back_button.img, ui_buttons.back_button.x, ui_buttons.back_button.y, ui_buttons.back_button.width, ui_buttons.back_button.height);
} else if (in_main_menu.tournament) {
//settings menu
//draw the grass
ui_ctx.drawImage(document.getElementById("ui_grass"), 0, 0, window.innerWidth, window.innerHeight);
//the back and fullscreen button
ui_ctx.drawImage(ui_buttons.fullscreen.img, ui_buttons.fullscreen.x, ui_buttons.fullscreen.y, ui_buttons.fullscreen.width, ui_buttons.fullscreen.height);
ui_ctx.drawImage(ui_buttons.back_button.img, ui_buttons.back_button.x, ui_buttons.back_button.y, ui_buttons.back_button.width, ui_buttons.back_button.height);
} else if (in_main_menu.settings) {
//settings menu
//draw the grass
ui_ctx.drawImage(document.getElementById("ui_grass"), 0, 0, window.innerWidth, window.innerHeight);
//the back and fullscreen button
ui_ctx.drawImage(ui_buttons.fullscreen.img, ui_buttons.fullscreen.x, ui_buttons.fullscreen.y, ui_buttons.fullscreen.width, ui_buttons.fullscreen.height);
ui_ctx.drawImage(ui_buttons.back_button.img, ui_buttons.back_button.x, ui_buttons.back_button.y, ui_buttons.back_button.width, ui_buttons.back_button.height);
settings_ui();
} else if (in_main_menu.about) {
//settings menu
//draw the grass
ui_ctx.drawImage(document.getElementById("ui_grass"), 0, 0, window.innerWidth, window.innerHeight);
//the back and fullscreen button
ui_ctx.drawImage(ui_buttons.fullscreen.img, ui_buttons.fullscreen.x, ui_buttons.fullscreen.y, ui_buttons.fullscreen.width, ui_buttons.fullscreen.height);
ui_ctx.drawImage(ui_buttons.back_button.img, ui_buttons.back_button.x, ui_buttons.back_button.y, ui_buttons.back_button.width, ui_buttons.back_button.height);
}
//call the function again in the next frame
requestAnimationFrame(draw_ui);
}
draw_ui();
function settings_ui() {
//setting interface
}
let two_player_start_game_btn;
if (window.innerWidth > window.innerHeight) {
two_player_start_game_btn = {
x: window.innerWidth - 310,
y: window.innerHeight - 160,
w: 220,
h: 50
};
} else {
two_player_start_game_btn = {
x: window.innerHeight - 215,
y: -110,
w: 150,
h: 50
}
}
function two_player_menu() {
ui_ctx.fillStyle = "black";
ui_ctx.font = "50px Arial";
if (window.innerWidth > window.innerHeight) {
//LAPTOPS
ui_ctx.font = "40px Arial";
//Game Length Menu
ui_ctx.fillText("Game Length", 10, 50);
for (const btn in two_player_menu_btns) {
ui_ctx.drawImage(two_player_menu_btns[btn].elm, two_player_menu_btns[btn].x, two_player_menu_btns[btn].y, two_player_menu_btns[btn].w, two_player_menu_btns[btn].h);
}
//Start Game button
ui_ctx.fillStyle = "blue";
ui_ctx.fillRect(two_player_start_game_btn.x, two_player_start_game_btn.y, two_player_start_game_btn.w, two_player_start_game_btn.h);
ui_ctx.font = "40px Arial";
ui_ctx.fillStyle = "black";
ui_ctx.fillText("Start Game", two_player_start_game_btn.x + 10, two_player_start_game_btn.y + 40);
//Options
ui_ctx.font = "25px Comic Sans MS";
//Select time
ui_ctx.fillText("30 seconds", 45, 80);
ui_ctx.fillText("60 seconds", 45, 110);
ui_ctx.fillText("90 seconds", 45, 140);
ui_ctx.fillText("120 seconds", 45, 170);
ui_ctx.fillText("3 goals", 45, 210);
ui_ctx.fillText("5 goals", 45, 240);
ui_ctx.fillText("10 goals", 45, 270);
} else {
//SMARTPHONES
ui_ctx.save();
ui_ctx.rotate(90 * Math.PI / 180);
ui_ctx.font = "30px Arial";
//Settings Menu
ui_ctx.fillText("Game Length", 10, -window.innerWidth + 50);
for (const btn in two_player_menu_btns) {
ui_ctx.drawImage(two_player_menu_btns[btn].elm, two_player_menu_btns[btn].x, two_player_menu_btns[btn].y, two_player_menu_btns[btn].w, two_player_menu_btns[btn].h);
}
//Start Game button
ui_ctx.fillStyle = "blue";
ui_ctx.fillRect(two_player_start_game_btn.x, two_player_start_game_btn.y, two_player_start_game_btn.w, two_player_start_game_btn.h);
ui_ctx.font = "25px Arial";
ui_ctx.fillStyle = "black";
ui_ctx.fillText("Start Game", two_player_start_game_btn.x + 15, two_player_start_game_btn.y + 30);
//Time options
ui_ctx.font = "15px Comic Sans MS";
ui_ctx.fillText("30 seconds", 40, -window.innerWidth + 80);
ui_ctx.fillText("60 seconds", 40, -window.innerWidth + 100);
ui_ctx.fillText("90 seconds", 40, -window.innerWidth + 120);
ui_ctx.fillText("120 seconds", 40, -window.innerWidth + 140);
ui_ctx.fillText("3 goals", 40, -window.innerWidth + 170);
ui_ctx.fillText("5 goals", 40, -window.innerWidth + 190);
ui_ctx.fillText("10 goals", 40, -window.innerWidth + 210);
ui_ctx.restore();
}
//in_main_menu.two_player = false;
//start_game();
}