-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
606 lines (560 loc) · 19.9 KB
/
input.c
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
#include "input.h"
#include "tiles.h"
#include "coordinates.h"
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdbool.h>
// Current state of input
typedef struct InputState
{
char *promptText;
char *enteredText;
int maxLength;
bool upperCase;
int result;
SDL_Renderer *renderer;
SDL_Texture *tiles;
TTF_Font *font;
} InputState;
// Render input to renderer based on menu state
// Menu state must include proper renderer, tiles and font
static void renderInput(InputState *state)
{
SDL_Renderer *renderer = state->renderer;
SDL_Texture *tiles = state->tiles;
TTF_Font *font = state->font;
SDL_RenderClear(renderer);
// brown-ish color for texts
SDL_Color color = {205, 140, 74, 255};
SDL_Color white = {255, 255, 255, 255};
// background floor
renderTiles(renderer, tiles, greenFloor, 0, 0, 19, 11);
// key layout for on-screen keyboard
char *keysLC = "0123456789öüqwertzuiopőúasdfghjkléáűíyxcvbnm,.-ó"; // lowercase
char *keysUC = "§'\"+!%/=()ÖÜQWERTZUIOPŐÚASDFGHJKLÉÁŰÍYXCVBNM?:_Ó"; // uppercase
int keyIndex = 0; // current index of key string
// render keyboard
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 12; j++)
{
char key[3]; // current key text, characters with accent require 2 chars + \0 at the end
key[0] = state->upperCase ? keysUC[keyIndex] : keysLC[keyIndex]; // get first char of key based on keyboard case
keyIndex++; // next char in key string
if ((key[0] < 32 || key[0] > 127) && key[0] != 0) // if current key has an invalid code <==> accent character ==> next character is needed
{
key[1] = state->upperCase ? keysUC[keyIndex] : keysLC[keyIndex]; // get next character of key string (to complete accent character)
keyIndex++; // next char in key string
key[2] = '\0'; // terminate string
}
else
key[1] = '\0'; // terminate string
// renter font and key to correct coordinates
Coordinates pos = {j + 4, i + 6};
renderTileC(renderer, tiles, blank, pos);
renderFontC(renderer, font, color, key, pos, true, true);
}
}
// space bar
renderTile(renderer, tiles, blankR, 6, 10);
renderTiles(renderer, tiles, blankLR, 7, 10, 12, 10);
renderTile(renderer, tiles, blankL, 13, 10);
// shift keys
renderTile(renderer, tiles, blank, 3, 9);
renderFont(renderer, font, color, "^", 3, 9, true, true);
renderTile(renderer, tiles, blank, 16, 9);
renderFont(renderer, font, color, "^", 16, 9, true, true);
// backspace
renderTile(renderer, tiles, blankR, 16, 6);
renderTile(renderer, tiles, blankL, 17, 6);
renderFont(renderer, font, color, "<-", 16, 6, false, true);
// OK button
renderTile(renderer, tiles, blankR, 17, 1);
renderTile(renderer, tiles, blankL, 18, 1);
renderFont(renderer, font, color, "OK", 17, 1, false, true);
// back button
renderTile(renderer, tiles, blankR, 17, 3);
renderTile(renderer, tiles, blankL, 18, 3);
renderFont(renderer, font, color, "<-", 17, 3, false, true);
// render current text with border and prompt
renderFont(renderer, font, white, state->promptText, 1, 1, false, true);
renderTiles(renderer, tiles, greyFloor, 1, 2, 15, 4);
renderTiles(renderer, tiles, wall, 1, 2, 15, 2);
renderTiles(renderer, tiles, wall, 1, 4, 15, 4);
renderTile(renderer, tiles, wall, 1, 3);
renderTile(renderer, tiles, wall, 15, 3);
renderFont(renderer, font, white, state->enteredText, 2, 3, false, true);
SDL_RenderPresent(renderer);
}
// Delete last character of current text
static void deleteChar(InputState *state)
{
char *text = state->enteredText; // for simpler code
int length = strlen(text); // get current length
if (length >= 1) // if there are characters in string
{
if ((text[length - 2] < 32 || text[length - 2] > 127) && text[length - 2] != 0 && // last 2 characters are invalid <==> make an accent character together
(text[length - 1] < 32 || text[length - 1] > 127) && text[length - 1] != 0 &&
length >= 2) // and length is correct for accent character
{
text[length - 2] = 0; // set string end
}
else
{
text[length - 1] = 0; // set string end
}
}
}
// Handle SDL key down event
// Returns true if erernder is needed
static bool handleKeydown(InputState *state, SDL_Keycode key)
{
bool shift = !state->upperCase;
switch (key)
{
case 0xe5: // right shift
case 0xe1: // left shift
state->upperCase = true;
return true;
case 0x28: // enter
state->result = 1;
return false;
case 0x29: // esc
state->result = 2;
return false;
case 0x2a: // backspace
deleteChar(state);
break;
case 0x2c: // space
strcat(state->enteredText, " ");
break;
case 0x35: // 0 / §
strcat(state->enteredText, shift ? "0" : "§");
break;
case 0x1e: // 1 / '
strcat(state->enteredText, shift ? "1" : "'");
break;
case 0x1f: // 2 / "
strcat(state->enteredText, shift ? "2" : "\"");
break;
case 0x20: // 3 / +
strcat(state->enteredText, shift ? "3" : "+");
break;
case 0x21: // 4 / !
strcat(state->enteredText, shift ? "4" : "!");
break;
case 0x22: // 5 / %
strcat(state->enteredText, shift ? "5" : "%");
break;
case 0x23: // 6 / /
strcat(state->enteredText, shift ? "6" : "/");
break;
case 0x24: // 7 / =
strcat(state->enteredText, shift ? "7" : "=");
break;
case 0x25: // 8 / (
strcat(state->enteredText, shift ? "8" : "(");
break;
case 0x26: // 9 / )
strcat(state->enteredText, shift ? "9" : ")");
break;
case 0x27: // ö / Ö
strcat(state->enteredText, shift ? "ö" : "Ö");
break;
case 0x2d: // ü / Ü
strcat(state->enteredText, shift ? "ü" : "Ü");
break;
case 0x14: // q / Q
strcat(state->enteredText, shift ? "q" : "Q");
break;
case 0x1a: // w / W
strcat(state->enteredText, shift ? "w" : "W");
break;
case 0x08: // e / E
strcat(state->enteredText, shift ? "e" : "E");
break;
case 0x15: // r / R
strcat(state->enteredText, shift ? "r" : "R");
break;
case 0x17: // t / T
strcat(state->enteredText, shift ? "t" : "T");
break;
case 0x1c: // z / Z
strcat(state->enteredText, shift ? "z" : "Z");
break;
case 0x18: // u / U
strcat(state->enteredText, shift ? "u" : "U");
break;
case 0x0c: // i / I
strcat(state->enteredText, shift ? "i" : "I");
break;
case 0x12: // o / O
strcat(state->enteredText, shift ? "o" : "O");
break;
case 0x13: // p / P
strcat(state->enteredText, shift ? "p" : "P");
break;
case 0x2f: // ő / Ő
strcat(state->enteredText, shift ? "ő" : "Ő");
break;
case 0x30: // ú / Ú
strcat(state->enteredText, shift ? "ú" : "Ú");
break;
case 0x04: // a / A
strcat(state->enteredText, shift ? "a" : "A");
break;
case 0x16: // s / S
strcat(state->enteredText, shift ? "s" : "S");
break;
case 0x07: // d / D
strcat(state->enteredText, shift ? "d" : "D");
break;
case 0x09: // f / F
strcat(state->enteredText, shift ? "f" : "F");
break;
case 0x0a: // g / G
strcat(state->enteredText, shift ? "g" : "G");
break;
case 0x0b: // h / H
strcat(state->enteredText, shift ? "h" : "H");
break;
case 0x0d: // j / J
strcat(state->enteredText, shift ? "j" : "J");
break;
case 0x0e: // k / K
strcat(state->enteredText, shift ? "k" : "K");
break;
case 0x0f: // l / L
strcat(state->enteredText, shift ? "l" : "L");
break;
case 0x33: // é / É
strcat(state->enteredText, shift ? "é" : "É");
break;
case 0x34: // á / Á
strcat(state->enteredText, shift ? "á" : "Á");
break;
case 0x31: // ű / Ű
strcat(state->enteredText, shift ? "ű" : "Ű");
break;
case 0x64: // í / Í
strcat(state->enteredText, shift ? "í" : "Í");
break;
case 0x1d: // y / Y
strcat(state->enteredText, shift ? "y" : "Y");
break;
case 0x1b: // x / X
strcat(state->enteredText, shift ? "x" : "X");
break;
case 0x06: // c / C
strcat(state->enteredText, shift ? "c" : "C");
break;
case 0x19: // v / V
strcat(state->enteredText, shift ? "v" : "V");
break;
case 0x05: // b / B
strcat(state->enteredText, shift ? "b" : "B");
break;
case 0x11: // n / N
strcat(state->enteredText, shift ? "n" : "N");
break;
case 0x10: // m / M
strcat(state->enteredText, shift ? "m" : "M");
break;
case 0x36: // , / ?
strcat(state->enteredText, shift ? "," : "?");
break;
case 0x37: // . / :
strcat(state->enteredText, shift ? "." : ":");
break;
case 0x38: // - / _
strcat(state->enteredText, shift ? "-" : "_");
break;
case 0x2e: // ó / Ó
strcat(state->enteredText, shift ? "ó" : "Ó");
break;
default:
return false;
break;
}
return true;
}
// Handle SDL key up event
// Returns true if erernder is needed
static bool handleKeyup(InputState *state, SDL_Keycode key)
{
switch (key)
{
case 0xe5: // right shift
case 0xe1: // left shift
state->upperCase = false;
break;
default:
return false;
break;
}
return true;
}
// Handle SDL mouse click
// Returns true if rerender is needed
static bool handleClick(InputState *state, int x, int y)
{
bool shift = !state->upperCase;
if (clickTile(3, 9, x, y)) // left shift
state->upperCase = !state->upperCase;
if (clickTile(16, 9, x, y)) // right shift
state->upperCase = !state->upperCase;
if (clickTiles(6, 10, 13, 10, x, y)) // space
strcat(state->enteredText, " ");
if (clickTiles(16, 6, 17, 6, x, y)) // backspace
deleteChar(state);
if (clickTiles(17, 1, 18, 1, x, y)) // ok button
state->result = 1;
if (clickTiles(17, 3, 18, 3, x, y)) // back button
state->result = 2;
if (clickTile(4, 6, x, y))
strcat(state->enteredText, shift ? "0" : "§");
if (clickTile(5, 6, x, y))
strcat(state->enteredText, shift ? "1" : "'");
if (clickTile(6, 6, x, y))
strcat(state->enteredText, shift ? "2" : "\"");
if (clickTile(7, 6, x, y))
strcat(state->enteredText, shift ? "3" : "+");
if (clickTile(8, 6, x, y))
strcat(state->enteredText, shift ? "4" : "!");
if (clickTile(9, 6, x, y))
strcat(state->enteredText, shift ? "5" : "%");
if (clickTile(10, 6, x, y))
strcat(state->enteredText, shift ? "6" : "/");
if (clickTile(11, 6, x, y))
strcat(state->enteredText, shift ? "7" : "=");
if (clickTile(12, 6, x, y))
strcat(state->enteredText, shift ? "8" : "(");
if (clickTile(13, 6, x, y))
strcat(state->enteredText, shift ? "9" : ")");
if (clickTile(14, 6, x, y))
strcat(state->enteredText, shift ? "ö" : "Ö");
if (clickTile(15, 6, x, y))
strcat(state->enteredText, shift ? "ü" : "Ü");
if (clickTile(4, 7, x, y))
strcat(state->enteredText, shift ? "q" : "Q");
if (clickTile(5, 7, x, y))
strcat(state->enteredText, shift ? "w" : "W");
if (clickTile(6, 7, x, y))
strcat(state->enteredText, shift ? "e" : "E");
if (clickTile(7, 7, x, y))
strcat(state->enteredText, shift ? "r" : "R");
if (clickTile(8, 7, x, y))
strcat(state->enteredText, shift ? "t" : "T");
if (clickTile(9, 7, x, y))
strcat(state->enteredText, shift ? "z" : "Z");
if (clickTile(10, 7, x, y))
strcat(state->enteredText, shift ? "u" : "U");
if (clickTile(11, 7, x, y))
strcat(state->enteredText, shift ? "i" : "I");
if (clickTile(12, 7, x, y))
strcat(state->enteredText, shift ? "o" : "O");
if (clickTile(13, 7, x, y))
strcat(state->enteredText, shift ? "p" : "P");
if (clickTile(14, 7, x, y))
strcat(state->enteredText, shift ? "ő" : "Ő");
if (clickTile(15, 7, x, y))
strcat(state->enteredText, shift ? "ú" : "Ú");
if (clickTile(4, 8, x, y))
strcat(state->enteredText, shift ? "a" : "A");
if (clickTile(5, 8, x, y))
strcat(state->enteredText, shift ? "s" : "S");
if (clickTile(6, 8, x, y))
strcat(state->enteredText, shift ? "d" : "D");
if (clickTile(7, 8, x, y))
strcat(state->enteredText, shift ? "f" : "F");
if (clickTile(8, 8, x, y))
strcat(state->enteredText, shift ? "g" : "G");
if (clickTile(9, 8, x, y))
strcat(state->enteredText, shift ? "h" : "H");
if (clickTile(10, 8, x, y))
strcat(state->enteredText, shift ? "j" : "J");
if (clickTile(11, 8, x, y))
strcat(state->enteredText, shift ? "k" : "K");
if (clickTile(12, 8, x, y))
strcat(state->enteredText, shift ? "l" : "L");
if (clickTile(13, 8, x, y))
strcat(state->enteredText, shift ? "é" : "É");
if (clickTile(14, 8, x, y))
strcat(state->enteredText, shift ? "á" : "Á");
if (clickTile(15, 8, x, y))
strcat(state->enteredText, shift ? "ű" : "Ű");
if (clickTile(4, 9, x, y))
strcat(state->enteredText, shift ? "í" : "Í");
if (clickTile(5, 9, x, y))
strcat(state->enteredText, shift ? "y" : "Y");
if (clickTile(6, 9, x, y))
strcat(state->enteredText, shift ? "x" : "X");
if (clickTile(7, 9, x, y))
strcat(state->enteredText, shift ? "c" : "C");
if (clickTile(8, 9, x, y))
strcat(state->enteredText, shift ? "v" : "V");
if (clickTile(9, 9, x, y))
strcat(state->enteredText, shift ? "b" : "B");
if (clickTile(10, 9, x, y))
strcat(state->enteredText, shift ? "n" : "N");
if (clickTile(11, 9, x, y))
strcat(state->enteredText, shift ? "m" : "M");
if (clickTile(12, 9, x, y))
strcat(state->enteredText, shift ? "," : "?");
if (clickTile(13, 9, x, y))
strcat(state->enteredText, shift ? "." : ":");
if (clickTile(14, 9, x, y))
strcat(state->enteredText, shift ? "-" : "_");
if (clickTile(15, 9, x, y))
strcat(state->enteredText, shift ? "ó" : "Ó");
return true;
}
// Handle input event
// Returns true if rerender is needed
static bool handleEvent(SDL_Event event, InputState *state)
{
switch (event.type)
{
case SDL_KEYDOWN:
return handleKeydown(state, event.key.keysym.scancode);
case SDL_KEYUP:
return handleKeyup(state, event.key.keysym.scancode);
case SDL_MOUSEBUTTONDOWN:
return handleClick(state, event.button.x, event.button.y);
case SDL_QUIT: // return on quit signal
state->result = 0;
return false;
default:
return false;
break;
}
}
// Input text from user
// Prompt text is displayed to user, input is written to enteredText
// Returns 0 on exit, 1 on success and 2 on user cancel
int textInput(SDL_Renderer *renderer, SDL_Texture *tiles, TTF_Font *font, char *promptText, char *enteredText, int maxLength)
{
InputState state;
// set renderer, tiles and font to state for easier function calls
state.renderer = renderer;
state.tiles = tiles;
state.font = font;
// initialize state variables
state.promptText = promptText;
state.enteredText = enteredText;
state.maxLength = maxLength;
state.upperCase = false;
state.result = -1;
// render initialized state
renderInput(&state);
SDL_Event ev;
while (SDL_WaitEvent(&ev))
{
bool rerender = handleEvent(ev, &state);
if (state.result != -1) // if result was set
{
return state.result; // return with result
}
if (rerender)
renderInput(&state);
}
return 0;
}
// Display alert to user
// Returns 0 on exit and 1 on user dismiss
int alertBox(SDL_Renderer *renderer, SDL_Texture *tiles, TTF_Font *font, char *promptText)
{
SDL_RenderClear(renderer);
// brown-ish color for texts
SDL_Color color = {205, 140, 74, 255};
SDL_Color white = {255, 255, 255, 255};
// background floor
renderTiles(renderer, tiles, greenFloor, 0, 0, 19, 11);
// render prompt text with border
renderTiles(renderer, tiles, greyFloor, 1, 1, 18, 3);
renderTiles(renderer, tiles, wall, 1, 1, 18, 1);
renderTiles(renderer, tiles, wall, 1, 3, 18, 3);
renderTile(renderer, tiles, wall, 1, 2);
renderTile(renderer, tiles, wall, 18, 2);
renderFont(renderer, font, white, promptText, 2, 2, false, true);
// OK button
renderTile(renderer, tiles, blankR, 8, 5);
renderTiles(renderer, tiles, blankLR, 9, 5, 10, 5);
renderTile(renderer, tiles, blankL, 11, 5);
renderFont(renderer, font, color, "OK", 9, 5, false, true);
SDL_RenderPresent(renderer);
SDL_Event ev;
while (SDL_WaitEvent(&ev))
{
switch (ev.type)
{
case SDL_KEYDOWN:
if (ev.key.keysym.scancode == 0x28) // enter
return 1;
break;
case SDL_MOUSEBUTTONDOWN:
if (clickTiles(8, 5, 11, 5, ev.button.x, ev.button.y)) // ok button
return 1;
break;
case SDL_QUIT: // return on quit signal
return 0;
default:
break;
}
}
return 0;
}
// Display yes/no dialog to user
// Returns 0 on exit and 1 on yes and 2 on no
int dialogBox(SDL_Renderer *renderer, SDL_Texture *tiles, TTF_Font *font, char *promptText)
{
SDL_RenderClear(renderer);
// brown-ish color for texts
SDL_Color color = {205, 140, 74, 255};
SDL_Color white = {255, 255, 255, 255};
// background floor
renderTiles(renderer, tiles, greenFloor, 0, 0, 19, 11);
// render prompt text with border
renderTiles(renderer, tiles, greyFloor, 1, 1, 18, 3);
renderTiles(renderer, tiles, wall, 1, 1, 18, 1);
renderTiles(renderer, tiles, wall, 1, 3, 18, 3);
renderTile(renderer, tiles, wall, 1, 2);
renderTile(renderer, tiles, wall, 18, 2);
renderFont(renderer, font, white, promptText, 2, 2, false, true);
// OK button
renderTile(renderer, tiles, blankR, 5, 5);
renderTiles(renderer, tiles, blankLR, 6, 5, 7, 5);
renderTile(renderer, tiles, blankL, 8, 5);
renderFont(renderer, font, color, "OK", 6, 5, false, true);
// OK button
renderTile(renderer, tiles, blankR, 11, 5);
renderTiles(renderer, tiles, blankLR, 12, 5, 13, 5);
renderTile(renderer, tiles, blankL, 14, 5);
renderFont(renderer, font, color, "Mégse", 11, 5, false, true);
SDL_RenderPresent(renderer);
SDL_Event ev;
while (SDL_WaitEvent(&ev))
{
switch (ev.type)
{
case SDL_KEYDOWN:
if (ev.key.keysym.scancode == 0x28) // enter
return 1;
if (ev.key.keysym.scancode == 0x29) // esc
return 2;
break;
case SDL_MOUSEBUTTONDOWN:
if (clickTiles(5, 5, 8, 5, ev.button.x, ev.button.y)) // ok button
return 1;
if (clickTiles(11, 5, 14, 5, ev.button.x, ev.button.y)) // cancel button
return 2;
break;
case SDL_QUIT: // return on quit signal
return 0;
default:
break;
}
}
return 0;
}