-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
111 lines (102 loc) · 1.52 KB
/
input.cpp
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
#include "cursesdef.h"
#include "input.h"
/* TODO Replace the hardcoded values with an abstraction layer.
* Lower redundancy across the methods. */
InputEvent get_input(int ch)
{
if (ch == '\0')
ch = getch();
switch(ch)
{
case 'k':
case '8':
case KEY_UP:
return DirectionN;
case 'j':
case '2':
case KEY_DOWN:
return DirectionS;
case 'l':
case '6':
case KEY_RIGHT:
return DirectionE;
case 'h':
case '4':
case KEY_LEFT:
return DirectionW;
case 'y':
case '7':
return DirectionNW;
case 'u':
case '9':
return DirectionNE;
case 'b':
case '1':
return DirectionSW;
case 'n':
case '3':
return DirectionSE;
case '.':
case '5':
return DirectionNone;
case '>':
return DirectionDown;
case '<':
return DirectionUp;
case '\n':
return Confirm;
case ' ':
return Close;
case 27: /* TODO Fix delay */
case 'q':
return Cancel;
case '?':
return Help;
case ',':
case 'g':
return Pickup;
default:
return Undefined;
}
}
void get_direction(int &x, int &y, InputEvent &input)
{
x = 0;
y = 0;
switch(input) {
case DirectionN:
--y;
break;
case DirectionS:
++y;
break;
case DirectionE:
++x;
break;
case DirectionW:
--x;
break;
case DirectionNW:
--x;
--y;
break;
case DirectionNE:
++x;
--y;
break;
case DirectionSW:
--x;
++y;
break;
case DirectionSE:
++x;
++y;
break;
case DirectionNone:
case Pickup:
break;
default:
x = -2;
y = -2;
}
}