forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
618 lines (538 loc) · 18 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
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
607
608
609
610
611
612
613
614
615
616
617
618
#include "cursesdef.h"
#include "input.h"
#include "json.h"
#include "output.h"
#include "keypress.h"
#include "game.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 '\t':
return Tab;
case '?':
return Help;
case ',':
case 'g':
return Pickup;
case 'f':
case 'F':
return Filter;
case 'r':
case 'R':
return Reset;
default:
return Undefined;
}
}
bool is_mouse_enabled()
{
#if ((defined _WIN32 || defined WINDOWS) && !(defined SDLTILES || defined TILES))
return false;
#else
return true;
#endif
}
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;
}
input_manager inp_mngr;
void input_manager::init() {
init_keycode_mapping();
std::ifstream data_file;
std::string file_name = "data/raw/keybindings.json";
data_file.open(file_name.c_str(), std::ifstream::in | std::ifstream::binary);
if(!data_file.good()) {
throw "Could not read " + file_name;
}
JsonIn jsin(&data_file);
//Crawl through once and create an entry for every definition
jsin.start_array();
while (!jsin.end_array()) {
// JSON object representing the action
JsonObject action = jsin.get_object();
const std::string action_id = action.get_string("id");
actionID_to_name[action_id] = action.get_string("name", action_id);
const std::string context = action.get_string("category", "default");
// Iterate over the bindings JSON array
JsonArray bindings = action.get_array("bindings");
const bool defaultcontext = (context == "default");
while (bindings.has_more()) {
JsonObject keybinding = bindings.next_object();
std::string input_method = keybinding.get_string("input_method");
input_event new_event;
if(input_method == "keyboard") {
new_event.type = CATA_INPUT_KEYBOARD;
} else if(input_method == "gamepad") {
new_event.type = CATA_INPUT_GAMEPAD;
} else if(input_method == "mouse") {
new_event.type = CATA_INPUT_MOUSE;
}
if (keybinding.has_array("key")) {
JsonArray keys = keybinding.get_array("key");
while (keys.has_more()) {
new_event.sequence.push_back(
get_keycode(keys.next_string())
);
}
} else { // assume string if not array, and throw if not string
new_event.sequence.push_back(
get_keycode(keybinding.get_string("key"))
);
}
if (defaultcontext) {
action_to_input[action_id].push_back(new_event);
} else {
action_contexts[context][action_id].push_back(new_event);
}
}
}
data_file.close();
}
void input_manager::add_keycode_pair(long ch, const std::string& name) {
keycode_to_keyname[ch] = name;
keyname_to_keycode[name] = ch;
}
void input_manager::add_gamepad_keycode_pair(long ch, const std::string& name) {
gamepad_keycode_to_keyname[ch] = name;
keyname_to_keycode[name] = ch;
}
void input_manager::init_keycode_mapping() {
// Between space and tilde, all keys more or less map
// to themselves(see ASCII table)
for(char c=' '; c<='~'; c++) {
std::string name(1, c);
add_keycode_pair(c, name);
}
add_keycode_pair(KEY_UP, "UP");
add_keycode_pair(KEY_DOWN, "DOWN");
add_keycode_pair(KEY_LEFT, "LEFT");
add_keycode_pair(KEY_RIGHT, "RIGHT");
add_keycode_pair(KEY_NPAGE, "NPAGE");
add_keycode_pair(KEY_PPAGE, "PPAGE");
add_keycode_pair(KEY_ESCAPE, "ESC");
add_keycode_pair('\n', "RETURN");
add_gamepad_keycode_pair(JOY_LEFT, "JOY_LEFT");
add_gamepad_keycode_pair(JOY_RIGHT, "JOY_RIGHT");
add_gamepad_keycode_pair(JOY_UP, "JOY_UP");
add_gamepad_keycode_pair(JOY_DOWN, "JOY_DOWN");
add_gamepad_keycode_pair(JOY_LEFTUP, "JOY_LEFTUP");
add_gamepad_keycode_pair(JOY_LEFTDOWN, "JOY_LEFTDOWN");
add_gamepad_keycode_pair(JOY_RIGHTUP, "JOY_RIGHTUP");
add_gamepad_keycode_pair(JOY_RIGHTDOWN, "JOY_RIGHTDOWN");
add_gamepad_keycode_pair(JOY_0, "JOY_0");
add_gamepad_keycode_pair(JOY_1, "JOY_1");
add_gamepad_keycode_pair(JOY_2, "JOY_2");
add_gamepad_keycode_pair(JOY_3, "JOY_3");
add_gamepad_keycode_pair(JOY_4, "JOY_4");
add_gamepad_keycode_pair(JOY_5, "JOY_5");
add_gamepad_keycode_pair(JOY_6, "JOY_6");
add_gamepad_keycode_pair(JOY_7, "JOY_7");
keyname_to_keycode["MOUSE_LEFT"] = MOUSE_BUTTON_LEFT;
keyname_to_keycode["MOUSE_RIGHT"] = MOUSE_BUTTON_RIGHT;
keyname_to_keycode["SCROLL_UP"] = SCROLLWHEEL_UP;
keyname_to_keycode["SCROLL_DOWN"] = SCROLLWHEEL_DOWN;
keyname_to_keycode["MOUSE_MOVE"] = MOUSE_MOVE;
}
long input_manager::get_keycode(std::string name) {
return keyname_to_keycode[name];
}
std::string input_manager::get_keyname(long ch, input_event_t inp_type) {
if(inp_type == CATA_INPUT_KEYBOARD) {
return keycode_to_keyname[ch];
} else if(inp_type == CATA_INPUT_MOUSE) {
if(ch == MOUSE_BUTTON_LEFT) {
return "MOUSE_LEFT";
} else if(ch == MOUSE_BUTTON_RIGHT) {
return "MOUSE_RIGHT";
} else if(ch == SCROLLWHEEL_UP) {
return "SCROLL_UP";
} else if(ch == SCROLLWHEEL_DOWN) {
return "SCROLL_DOWN";
} else if(ch == MOUSE_MOVE) {
return "MOUSE_MOVE";
} else {
return "MOUSE_UNKNOWN";
}
} else if (inp_type == CATA_INPUT_GAMEPAD) {
return gamepad_keycode_to_keyname[ch];
} else {
return "UNKNOWN";
}
}
const std::vector<input_event>& input_manager::get_input_for_action(const std::string& action_descriptor, const std::string context, bool *overwrites_default) {
// First we check if we have a special override in this particular context.
if(context != "default" && action_contexts[context].count(action_descriptor)) {
if(overwrites_default) *overwrites_default = true;
return action_contexts[context][action_descriptor];
}
// If not, we use the default binding.
if(overwrites_default) *overwrites_default = false;
return action_to_input[action_descriptor];
}
const std::string& input_manager::get_action_name(const std::string& action) {
return actionID_to_name[action];
}
const std::string CATA_ERROR = "ERROR";
const std::string UNDEFINED = "UNDEFINED";
const std::string ANY_INPUT = "ANY_INPUT";
const std::string COORDINATE = "COORDINATE";
const std::string TIMEOUT = "TIMEOUT";
const std::string& input_context::input_to_action(input_event& inp) {
for(int i=0; i<registered_actions.size(); i++) {
const std::string& action = registered_actions[i];
const std::vector<input_event>& check_inp = inp_mngr.get_input_for_action(action, category);
// Does this action have our queried input event in its keybindings?
for(int i=0; i<check_inp.size(); i++) {
if(check_inp[i] == inp) {
return action;
}
}
}
return CATA_ERROR;
}
void input_manager::set_timeout(int delay)
{
timeout(delay);
// Use this to determine when curses should return a CATA_INPUT_TIMEOUT event.
input_timeout = delay;
}
void input_context::register_action(const std::string& action_descriptor) {
if(action_descriptor == "ANY_INPUT") {
registered_any_input = true;
} else if(action_descriptor == "COORDINATE") {
handling_coordinate_input = true;
}
registered_actions.push_back(action_descriptor);
}
const std::string input_context::get_desc(const std::string& action_descriptor) {
if(action_descriptor == "ANY_INPUT") {
return "(*)"; // * for wildcard
}
const std::vector<input_event>& events = inp_mngr.get_input_for_action(action_descriptor, category);
if(events.size() == 0) {
return UNDEFINED;
}
std::vector<input_event> inputs_to_show;
for(int i=0; i<events.size(); i++) {
const input_event& event = events[i];
// Only display gamepad buttons if a gamepad is available.
if(gamepad_available() || event.type != CATA_INPUT_GAMEPAD) {
inputs_to_show.push_back(event);
}
}
std::stringstream rval;
for(int i=0; i < inputs_to_show.size(); i++) {
for(int j=0; j<inputs_to_show[i].sequence.size(); j++) {
rval << inp_mngr.get_keyname(inputs_to_show[i].sequence[j], inputs_to_show[i].type);
}
// We're generating a list separated by "," and "or"
if(i + 2 == inputs_to_show.size()) {
rval << " or ";
} else if(i + 1 < inputs_to_show.size()) {
rval << ", ";
}
}
return rval.str();
}
const std::string& input_context::handle_input() {
next_action.type = CATA_INPUT_ERROR;
while(1) {
next_action = inp_mngr.get_input_event(NULL);
if (next_action.type == CATA_INPUT_TIMEOUT) {
return TIMEOUT;
}
const std::string& action = input_to_action(next_action);
// Special help action
if(action == "HELP_KEYBINDINGS") {
display_help();
continue;
}
if(next_action.type == CATA_INPUT_MOUSE) {
if(!handling_coordinate_input) {
continue; // Ignore this mouse input.
}
coordinate_input_received = true;
coordinate_x = next_action.mouse_x;
coordinate_y = next_action.mouse_y;
} else {
coordinate_input_received = false;
}
if(action != CATA_ERROR) {
return action;
}
// If we registered to receive any input, return ANY_INPUT
// to signify that an unregistered key was pressed.
if(registered_any_input) {
return ANY_INPUT;
}
// If it's an invalid key, just keep looping until the user
// enters something proper.
}
}
void input_context::register_directions() {
register_cardinal();
register_action("LEFTUP");
register_action("LEFTDOWN");
register_action("RIGHTUP");
register_action("RIGHTDOWN");
}
void input_context::register_updown() {
register_action("UP");
register_action("DOWN");
}
void input_context::register_leftright() {
register_action("LEFT");
register_action("RIGHT");
}
void input_context::register_cardinal() {
register_updown();
register_leftright();
}
void input_context::get_direction(int& dx, int& dy, const std::string& action) {
if(action == "UP") {
dx = 0;
dy = -1;
} else if(action == "DOWN") {
dx = 0;
dy = 1;
} else if(action == "LEFT") {
dx = -1;
dy = 0;
} else if(action == "RIGHT") {
dx = 1;
dy = 0;
} else if(action == "LEFTUP") {
dx = -1;
dy = -1;
} else if(action == "RIGHTUP") {
dx = 1;
dy = -1;
} else if(action == "LEFTDOWN") {
dx = -1;
dy = 1;
} else if(action == "RIGHTDOWN") {
dx = 1;
dy = 1;
} else {
dx = -2;
dy = -2;
}
}
void input_context::display_help() {
// Shamelessly stolen from help.cpp
WINDOW* w_help = newwin(FULL_SCREEN_HEIGHT-2, FULL_SCREEN_WIDTH-2,
1 + (int)((TERMY > FULL_SCREEN_HEIGHT) ? (TERMY-FULL_SCREEN_HEIGHT)/2 : 0),
1 + (int)((TERMX > FULL_SCREEN_WIDTH) ? (TERMX-FULL_SCREEN_WIDTH)/2 : 0));
werase(w_help);
mvwprintz(w_help, 1, 51, c_ltred, _("Unbound keys"));
mvwprintz(w_help, 2, 51, c_ltgreen, _("Keybinding active only"));
mvwprintz(w_help, 3, 51, c_ltgreen, _("on this screen"));
mvwprintz(w_help, 4, 51, c_ltgray, _("Keybinding active globally"));
// Clear the lines
for (int i = 0; i < FULL_SCREEN_HEIGHT-2; i++)
mvwprintz(w_help, i, 0, c_black, " ");
for (int i=0; i<registered_actions.size(); i++) {
const std::string& action_id = registered_actions[i];
if(action_id == "ANY_INPUT") continue;
bool overwrite_default;
const std::vector<input_event>& input_events = inp_mngr.get_input_for_action(action_id, category, &overwrite_default);
nc_color col = input_events.size() ? c_white : c_ltred;
mvwprintz(w_help, i, 3, col, "%s: ", inp_mngr.get_action_name(action_id).c_str());
if (!input_events.size()) {
mvwprintz(w_help, i, 30, c_ltred, _("Unbound!"));
} else {
// The color depends on whether this input draws from context-local or from
// default settings. Default will be ltgray, overwrite will be ltgreen.
col = overwrite_default ? c_ltgreen : c_ltgray;
mvwprintz(w_help, i, 30, col, "%s", get_desc(action_id).c_str());
}
}
wrefresh(w_help);
refresh();
long ch = getch();
while (ch != 'q' && ch != 'Q' && ch != KEY_ESCAPE) { ch = getch(); };
werase(w_help);
}
input_event input_context::get_raw_input()
{
return next_action;
}
#ifndef TILES
// If we're using curses, we need to provide get_input_event() here.
input_event input_manager::get_input_event(WINDOW* win)
{
int key = get_keypress();
input_event rval;
if (key == ERR) {
if (input_timeout > 0) {
rval.type = CATA_INPUT_TIMEOUT;
} else {
rval.type = CATA_INPUT_ERROR;
}
#if !(defined TILES || defined SDLTILES || defined _WIN32 || defined WINDOWS || defined __CYGWIN__)
// ncurses mouse handling
} else if (key == KEY_MOUSE) {
MEVENT event;
if (getmouse(&event) == OK) {
rval.type = CATA_INPUT_MOUSE;
rval.mouse_x = event.x - VIEW_OFFSET_X;
rval.mouse_y = event.y - VIEW_OFFSET_Y;
if (event.bstate & BUTTON1_CLICKED) {
rval.add_input(MOUSE_BUTTON_LEFT);
} else if (event.bstate & BUTTON3_CLICKED) {
rval.add_input(MOUSE_BUTTON_RIGHT);
} else if (event.bstate & REPORT_MOUSE_POSITION) {
rval.add_input(MOUSE_MOVE);
if (input_timeout > 0) {
// Mouse movement seems to clear ncurses timeout
set_timeout(input_timeout);
}
} else {
rval.type = CATA_INPUT_ERROR;
}
} else {
rval.type = CATA_INPUT_ERROR;
}
#endif
} else {
rval.type = CATA_INPUT_KEYBOARD;
rval.add_input(key);
}
return rval;
}
// Also specify that we don't have a gamepad plugged in.
bool gamepad_available()
{
return false;
}
bool input_context::get_coordinates(WINDOW* capture_win, int& x, int& y)
{
if (!coordinate_input_received) {
return false;
}
int view_columns = getmaxx(capture_win);
int view_rows = getmaxy(capture_win);
int win_left = getbegx(capture_win) - VIEW_OFFSET_X;
int win_right = win_left + view_columns - 1;
int win_top = getbegy(capture_win) - VIEW_OFFSET_Y;
int win_bottom = win_top + view_rows - 1;
if (coordinate_x < win_left || coordinate_x > win_right || coordinate_y < win_top || coordinate_y > win_bottom) {
return false;
}
x = g->ter_view_x - ((view_columns/2) - coordinate_x);
y = g->ter_view_y - ((view_rows/2) - coordinate_y);
return true;
}
#endif
#ifndef SDLTILES
void init_interface()
{
#if !(defined TILES || defined _WIN32 || defined WINDOWS || defined __CYGWIN__)
// ncurses mouse registration
mousemask(BUTTON1_CLICKED | BUTTON3_CLICKED | REPORT_MOUSE_POSITION, NULL);
#endif
}
#endif