nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
InputManager.cpp
1#include <Flow/InputManager.hpp>
2#include <Interface/Ncurses.hpp>
3
4#include <ncurses.h>
5
6int InputManager::pressedKey = ERR; // Starting with blank value
7std::map<std::string, int> InputManager::binds;
8
9void InputManager::bind(std::string name, int key)
10{
11 if (name.empty() || key == ERR)
12 return;
13
14 InputManager::binds[name] = key;
15}
16
17void InputManager::unbind(std::string name)
18{
19 InputManager::binds.erase(name);
20}
21
22int InputManager::getBind(std::string name)
23{
24 // If #key is not binded to anything...
25 if (InputManager::binds.find(name) == InputManager::binds.end())
26 return ERR;
27
28 return (InputManager::binds[name]);
29}
30void InputManager::update(int delay_ms)
31{
32 InputManager::pressedKey = Ncurses::getInput(delay_ms);
33}
34
35bool InputManager::noKeyPressed()
36{
37 return (InputManager::pressedKey == ERR);
38}
39
40bool InputManager::isPressed(int key)
41{
42 return (InputManager::pressedKey == key);
43}
44
45bool InputManager::isPressed(std::string key)
46{
47 // If #key is not binded to anything, will return false
48 if (InputManager::binds.find(key) == InputManager::binds.end())
49 return false;
50
51 return (InputManager::isPressed(InputManager::binds[key]));
52}
53
54std::string InputManager::keyToString(int value)
55{
56 // Is character inside the ASCII table?
57 if (value >= 0 && value <= 127)
58 {
59 if (value == ' ')
60 return "space";
61
62 // The "printable" part of the ASCII table - easy
63 if (value > ' ' && value <= '~')
64 {
65 // Converting (int -> char -> char* -> std::string)
66 char c[2] = { (char)value, '\0' };
67
68 return std::string(c);
69 }
70
71 // Non-printable, then...
72 // Let's get some names
73 switch (value)
74 {
75 case 0: return "null";
76 case 27: return "escape";
77 case 127: return "delete";
78 }
79 }
80
81 // If not, then this character is a special Ncurses value.
82 // Those things were directy taken from <ncurses.h>
83 //
84 // NOTE: Wont use KEY_BREAK, KEY_SRESET, KEY_RESET, KEY_F0
85 // and KEY_EIC beucase they're strange..
86 // NOTE: Also not using KEY_MOUSE, KEY_RESIZE and KEY_EVENT
87 // because they're Ncurses' flags for other things
88 // than the keyboard.
89 //
90 switch (value)
91 {
92 // Special case - value for "no key pressed"
93 case ERR: return "undefined";
94
95 case KEY_DOWN: return "down";
96 case KEY_UP: return "up";
97 case KEY_LEFT: return "left";
98 case KEY_RIGHT: return "right";
99 case KEY_HOME: return "home";
100 case KEY_BACKSPACE: return "backspace";
101 case KEY_F(1): return "f1";
102 case KEY_F(2): return "f2";
103 case KEY_F(3): return "f3";
104 case KEY_F(4): return "f4";
105 case KEY_F(5): return "f5";
106 case KEY_F(6): return "f6";
107 case KEY_F(7): return "f7";
108 case KEY_F(8): return "f8";
109 case KEY_F(9): return "f9";
110 case KEY_F(10): return "f10";
111 case KEY_F(11): return "f11";
112 case KEY_F(12): return "f12";
113 case KEY_DL: return "delete-line";
114 case KEY_IL: return "insert-line";
115 case KEY_DC: return "delete-char";
116 case KEY_IC: return "insert";
117 case KEY_CLEAR: return "clear";
118 case KEY_EOS: return "clear-to-end-of-screen";
119 case KEY_EOL: return "clear-to-end-of-line";
120 case KEY_SF: return "scroll-forward";
121 case KEY_SR: return "scroll-backward";
122 case KEY_NPAGE: return "page-down";
123 case KEY_PPAGE: return "page-up";
124 case KEY_STAB: return "set-tab";
125 case KEY_CTAB: return "clear-tab";
126 case KEY_CATAB: return "clear-all-tabs";
127 case KEY_ENTER: return "enter";
128 case KEY_PRINT: return "print";
129 case KEY_LL: return "home-down"; // wtf?
130 case KEY_A1: return "keypad-upper-left";
131 case KEY_A3: return "keypad-upper-right";
132 case KEY_B2: return "keypad-center";
133 case KEY_C1: return "keypad-lower-left";
134 case KEY_C3: return "keypad-lower-right";
135 case KEY_BTAB: return "back-tab";
136 case KEY_BEG: return "begin";
137 case KEY_CANCEL: return "cancel";
138 case KEY_CLOSE: return "close";
139 case KEY_COMMAND: return "command"; // not mac/osx's
140 case KEY_COPY: return "copy";
141 case KEY_CREATE: return "create";
142 case KEY_END: return "end";
143 case KEY_EXIT: return "exit";
144 case KEY_FIND: return "find";
145 case KEY_HELP: return "help";
146 case KEY_MARK: return "mark";
147 case KEY_MESSAGE: return "message";
148 case KEY_MOVE: return "move";
149 case KEY_NEXT: return "next";
150 case KEY_OPEN: return "open";
151 case KEY_OPTIONS: return "options";
152 case KEY_PREVIOUS: return "previous";
153 case KEY_REDO: return "redo";
154 case KEY_REFERENCE: return "reference";
155 case KEY_REFRESH: return "refresh";
156 case KEY_REPLACE: return "replace";
157 case KEY_RESTART: return "restart";
158 case KEY_RESUME: return "resume";
159 case KEY_SAVE: return "save";
160 case KEY_SBEG: return "shift-begin";
161 case KEY_SCANCEL: return "shift-cancel";
162 case KEY_SCOMMAND: return "shift-command";
163 case KEY_SCOPY: return "shift-copy";
164 case KEY_SCREATE: return "shift-create";
165 case KEY_SDC: return "shift-delete-char";
166 case KEY_SDL: return "shift-delete-line";
167 case KEY_SELECT: return "select";
168 case KEY_SEND: return "shift-end";
169 case KEY_SEOL: return "shift-clear-to-end-of-line";
170 case KEY_SEXIT: return "shift-exit";
171 case KEY_SFIND: return "shift-find";
172 case KEY_SHELP: return "shift-help";
173 case KEY_SHOME: return "shift-home";
174 case KEY_SIC: return "shift-insert";
175 case KEY_SLEFT: return "shift-left";
176 case KEY_SMESSAGE: return "shift-message";
177 case KEY_SMOVE: return "shift-move";
178 case KEY_SNEXT: return "shift-next";
179 case KEY_SOPTIONS: return "shift-options";
180 case KEY_SPREVIOUS: return "shift-previous";
181 case KEY_SPRINT: return "shift-print";
182 case KEY_SREDO: return "shift-redo";
183 case KEY_SREPLACE: return "shift-replace";
184 case KEY_SRIGHT: return "shift-right";
185 case KEY_SRSUME: return "shift-resume";
186 case KEY_SSAVE: return "shift-save";
187 case KEY_SSUSPEND: return "shift-suspend";
188 case KEY_SUNDO: return "shift-undo";
189 case KEY_SUSPEND: return "suspend";
190 case KEY_UNDO: return "undo";
191
192 default: break;
193 }
194 return "undefined";
195}
196
197int InputManager::stringToKey(std::string string)
198{
199 if (string == "space")
200 return ' ';
201
202 // Let's hope it's a single char from the ASCII table
203 if (string.size() == 1)
204 {
205 char c = string.c_str()[0];
206
207 if (c > ' ' && c <= '~')
208 return c;
209
210 // undefined, sorry :(
211 return ERR;
212 }
213
214 // Special case, unknown key
215 if (string == "undefined") return ERR;
216
217 if (string == "down") return KEY_DOWN;
218 if (string == "up") return KEY_UP;
219 if (string == "left") return KEY_LEFT;
220 if (string == "right") return KEY_RIGHT;
221 if (string == "home") return KEY_HOME;
222 if (string == "backspace") return KEY_BACKSPACE;
223 if (string == "f1") return KEY_F(1);
224 if (string == "f2") return KEY_F(2);
225 if (string == "f3") return KEY_F(3);
226 if (string == "f4") return KEY_F(4);
227 if (string == "f5") return KEY_F(5);
228 if (string == "f6") return KEY_F(6);
229 if (string == "f7") return KEY_F(7);
230 if (string == "f8") return KEY_F(8);
231 if (string == "f9") return KEY_F(9);
232 if (string == "f10") return KEY_F(10);
233 if (string == "f11") return KEY_F(11);
234 if (string == "f12") return KEY_F(12);
235 if (string == "delete-line") return KEY_DL;
236 if (string == "insert-line") return KEY_IL;
237 if (string == "delete-char") return KEY_DC;
238 if (string == "insert") return KEY_IC;
239 if (string == "clear") return KEY_CLEAR;
240 if (string == "clear-to-end-of-screen") return KEY_EOS;
241 if (string == "clear-to-end-of-line") return KEY_EOL;
242 if (string == "scroll-forward") return KEY_SF;
243 if (string == "scroll-backward") return KEY_SR;
244 if (string == "page-down") return KEY_NPAGE;
245 if (string == "page-up") return KEY_PPAGE;
246 if (string == "set-tab") return KEY_STAB;
247 if (string == "clear-tab") return KEY_CTAB;
248 if (string == "clear-all-tabs") return KEY_CATAB;
249 if (string == "enter") return KEY_ENTER;
250 if (string == "print") return KEY_PRINT;
251 if (string == "home-down") return KEY_LL;
252 if (string == "keypad-upper-left") return KEY_A1;
253 if (string == "keypad-upper-right") return KEY_A3;
254 if (string == "keypad-center") return KEY_B2;
255 if (string == "keypad-lower-left") return KEY_C1;
256 if (string == "keypad-lower-right") return KEY_C3;
257 if (string == "back-tab") return KEY_BTAB;
258 if (string == "begin") return KEY_BEG;
259 if (string == "cancel") return KEY_CANCEL;
260 if (string == "close") return KEY_CLOSE;
261 if (string == "command") return KEY_COMMAND;
262 if (string == "copy") return KEY_COPY;
263 if (string == "create") return KEY_CREATE;
264 if (string == "end") return KEY_END;
265 if (string == "exit") return KEY_EXIT;
266 if (string == "find") return KEY_FIND;
267 if (string == "help") return KEY_HELP;
268 if (string == "mark") return KEY_MARK;
269 if (string == "message") return KEY_MESSAGE;
270 if (string == "move") return KEY_MOVE;
271 if (string == "next") return KEY_NEXT;
272 if (string == "open") return KEY_OPEN;
273 if (string == "options") return KEY_OPTIONS;
274 if (string == "previous") return KEY_PREVIOUS;
275 if (string == "redo") return KEY_REDO;
276 if (string == "reference") return KEY_REFERENCE;
277 if (string == "refresh") return KEY_REFRESH;
278 if (string == "replace") return KEY_REPLACE;
279 if (string == "restart") return KEY_RESTART;
280 if (string == "resume") return KEY_RESUME;
281 if (string == "save") return KEY_SAVE;
282 if (string == "shift-begin") return KEY_SBEG;
283 if (string == "shift-cancel") return KEY_SCANCEL;
284 if (string == "shift-command") return KEY_SCOMMAND;
285 if (string == "shift-copy") return KEY_SCOPY;
286 if (string == "shift-create") return KEY_SCREATE;
287 if (string == "shift-delete-char") return KEY_SDC;
288 if (string == "shift-delete-line") return KEY_SDL;
289 if (string == "select") return KEY_SELECT;
290 if (string == "shift-end") return KEY_SEND;
291 if (string == "shift-clear-to-end-of-line") return KEY_SEOL;
292 if (string == "shift-exit") return KEY_SEXIT;
293 if (string == "shift-find") return KEY_SFIND;
294 if (string == "shift-help") return KEY_SHELP;
295 if (string == "shift-home") return KEY_SHOME;
296 if (string == "shift-insert") return KEY_SIC;
297 if (string == "shift-left") return KEY_SLEFT;
298 if (string == "shift-message") return KEY_SMESSAGE;
299 if (string == "shift-move") return KEY_SMOVE;
300 if (string == "shift-next") return KEY_SNEXT;
301 if (string == "shift-options") return KEY_SOPTIONS;
302 if (string == "shift-previous") return KEY_SPREVIOUS;
303 if (string == "shift-print") return KEY_SPRINT;
304 if (string == "shift-redo") return KEY_SREDO;
305 if (string == "shift-replace") return KEY_SREPLACE;
306 if (string == "shift-right") return KEY_SRIGHT;
307 if (string == "shift-resume") return KEY_SRSUME;
308 if (string == "shift-save") return KEY_SSAVE;
309 if (string == "shift-suspend") return KEY_SSUSPEND;
310 if (string == "shift-undo") return KEY_SUNDO;
311 if (string == "suspend") return KEY_SUSPEND;
312 if (string == "undo") return KEY_UNDO;
313
314 // Undefined key :(
315 return ERR;
316}
317
int getInput(int delay_ms=-1)
Returns a pressed character within a timespan of delay_ms (milliseconds).
Definition Ncurses.cpp:37
off_t size(std::string path)
Returns the file size of #path in bytes.
Definition Utils.cpp:88