forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
136 lines (126 loc) · 2.17 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "cursesdef.h"
#include "input.h"
#include <fstream>
/* 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;
case 'f':
case 'F':
return Filter;
case 'r':
case 'R':
return Reset;
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;
}
}
//helper function for those have problem inputing certain characters.
std::string get_input_string_from_file(std::string fname)
{
std::string ret = "";
std::ifstream fin(fname.c_str());
if (fin){
getline(fin, ret);
//remove utf8 bmm
if(ret.size()>0 && (unsigned char)ret[0]==0xef) {
ret.erase(0,3);
}
while(ret.size()>0 && (ret[ret.size()-1]=='\r' || ret[ret.size()-1]=='\n')){
ret.erase(ret.size()-1,1);
}
}
return ret;
}