forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
1460 lines (1291 loc) · 54.8 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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "input.h"
#include <algorithm>
#include <cctype>
#include <fstream>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <array>
#include <exception>
#include <locale>
#include <memory>
#include <set>
#include <utility>
#include "action.h"
#include "catacharset.h"
#include "cursesdef.h"
#include "debug.h"
#include "filesystem.h"
#include "fstream_utils.h"
#include "game.h"
#include "help.h"
#include "ime.h"
#include "json.h"
#include "options.h"
#include "output.h"
#include "path_info.h"
#include "popup.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "string_utils.h"
#include "translations.h"
#include "ui_manager.h"
#include "color.h"
#include "point.h"
using std::min; // from <algorithm>
using std::max;
static const std::string default_context_id( "default" );
template <class T1, class T2>
struct ContainsPredicate {
const T1 &container;
ContainsPredicate( const T1 &container ) : container( container ) { }
// Operator overload required to leverage std functional interface.
bool operator()( T2 c ) {
return std::find( container.begin(), container.end(), c ) != container.end();
}
};
static int str_to_int( const std::string &number )
{
// ensure user's locale doesn't interfere with number format
std::istringstream buffer( number );
buffer.imbue( std::locale::classic() );
int result;
buffer >> result;
return result;
}
static std::string int_to_str( int number )
{
// ensure user's locale doesn't interfere with number format
std::ostringstream buffer;
buffer.imbue( std::locale::classic() );
buffer << number;
return buffer.str();
}
bool is_mouse_enabled()
{
#if defined(_WIN32) && !defined(TILES)
return false;
#else
return true;
#endif
}
//helper function for those have problem inputting certain characters.
std::string get_input_string_from_file( const std::string &fname )
{
std::string ret;
read_from_file_optional( fname, [&ret]( std::istream & fin ) {
getline( fin, ret );
//remove utf8 bmm
if( !ret.empty() && static_cast<unsigned char>( ret[0] ) == 0xef ) {
ret.erase( 0, 3 );
}
while( !ret.empty() && ( ret.back() == '\r' || ret.back() == '\n' ) ) {
ret.erase( ret.size() - 1, 1 );
}
} );
return ret;
}
int input_event::get_first_input() const
{
if( sequence.empty() ) {
return UNKNOWN_UNICODE;
}
return sequence[0];
}
input_manager inp_mngr;
void input_manager::init()
{
std::map<char, action_id> keymap;
std::string keymap_file_loaded_from;
std::set<action_id> unbound_keymap;
init_keycode_mapping();
reset_timeout();
// recursively load all keybindings from the data/raw directory
for( const auto &file : get_files_from_path( ".json", PATH_INFO::keybindingsdir(), true, true ) ) {
try {
load( file, false );
} catch( const JsonError &err ) {
throw std::runtime_error( err.what() );
}
}
// user keybindings are searched from separate directory
try {
load( PATH_INFO::user_keybindings(), true );
} catch( const JsonError &err ) {
throw std::runtime_error( err.what() );
}
if( keymap_file_loaded_from.empty() || ( keymap.empty() && unbound_keymap.empty() ) ) {
// No keymap file was loaded, or the file has no mappings and no unmappings,
// we can skip the remaining part of the function, especially the save function
return;
}
t_actions &actions = action_contexts["DEFAULTMODE"];
std::set<action_id> touched;
for( std::map<char, action_id>::const_iterator a = keymap.begin(); a != keymap.end(); ++a ) {
const std::string action_id = action_ident( a->second );
// Put the binding from keymap either into the global context
// (if an action with that ident already exists there - think movement keys)
// or otherwise to the DEFAULTMODE context.
std::string context = "DEFAULTMODE";
if( action_contexts[default_context_id].count( action_id ) > 0 ) {
context = default_context_id;
} else if( touched.count( a->second ) == 0 ) {
// Note: movement keys are somehow special as the default in keymap
// does not contain the arrow keys, so we don't clear existing keybindings
// for them.
// If the keymap contains a binding for this action, erase all the
// previously (default!) existing bindings, to only keep the bindings,
// the user is used to
action_contexts[action_id].clear();
touched.insert( a->second );
}
add_input_for_action( action_id, context, input_event( a->first, CATA_INPUT_KEYBOARD ) );
}
// Unmap actions that are explicitly not mapped
for( const auto &elem : unbound_keymap ) {
const std::string action_id = action_ident( elem );
actions[action_id].input_events.clear();
}
// Imported old bindings from old keymap file, save those to the new
// keybindings.json file.
try {
save();
} catch( std::exception &err ) {
debugmsg( "Could not write imported keybindings: %s", err.what() );
return;
}
// Finally if we did import a file, and saved it to the new keybindings
// file, delete the old keymap file to prevent re-importing it.
if( !keymap_file_loaded_from.empty() ) {
remove_file( keymap_file_loaded_from );
}
}
void input_manager::load( const std::string &file_name, bool is_user_preferences )
{
std::ifstream data_file( file_name.c_str(), std::ifstream::in | std::ifstream::binary );
if( !data_file.good() ) {
// Only throw if this is the first file to load, that file _must_ exist,
// otherwise the keybindings can not be read at all.
if( action_contexts.empty() ) {
throw std::runtime_error( std::string( "Could not read " ) + file_name );
}
return;
}
JsonIn jsin( data_file, file_name );
//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 type = action.get_string( "type", "keybinding" );
if( type != "keybinding" ) {
debugmsg( "Only objects of type 'keybinding' (not %s) should appear in the "
"keybindings file '%s'", type, file_name );
continue;
}
const std::string action_id = action.get_string( "id" );
const std::string context = action.get_string( "category", default_context_id );
t_actions &actions = action_contexts[context];
if( !is_user_preferences && action.has_member( "name" ) ) {
// Action names are not user preferences. Some experimental builds
// post-0.A had written action names into the user preferences
// config file. Any names that exist in user preferences will be
// ignored.
action.read( "name", actions[action_id].name );
}
t_input_event_list events;
for( const JsonObject keybinding : action.get_array( "bindings" ) ) {
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" ) ) {
for( const std::string line : keybinding.get_array( "key" ) ) {
new_event.sequence.push_back( get_keycode( line ) );
}
} else { // assume string if not array, and throw if not string
new_event.sequence.push_back(
get_keycode( keybinding.get_string( "key" ) )
);
}
events.push_back( new_event );
}
// An invariant of this class is that user-created, local keybindings
// with an empty set of input_events do not exist in the
// action_contexts map. In prior versions of this class, this was not
// true, so users of experimental builds post-0.A will have empty
// local keybindings saved in their keybindings.json config.
//
// To be backwards compatible with keybindings.json from prior
// experimental builds, we will detect user-created, local keybindings
// with empty input_events and disregard them. When keybindings are
// later saved, these remnants won't be saved.
if( !is_user_preferences ||
!events.empty() ||
context == default_context_id ||
actions.count( action_id ) > 0 ) {
// In case this is the second file containing user preferences,
// this replaces the default bindings with the user's preferences.
action_attributes &attributes = actions[action_id];
attributes.input_events = events;
if( action.has_member( "is_user_created" ) ) {
attributes.is_user_created = action.get_bool( "is_user_created" );
}
}
}
}
void input_manager::save()
{
write_to_file( PATH_INFO::user_keybindings(), [&]( std::ostream & data_file ) {
JsonOut jsout( data_file, true );
jsout.start_array();
for( t_action_contexts::const_iterator a = action_contexts.begin(); a != action_contexts.end();
++a ) {
const t_actions &actions = a->second;
for( const auto &action : actions ) {
const t_input_event_list &events = action.second.input_events;
jsout.start_object();
jsout.member( "id", action.first );
jsout.member( "category", a->first );
bool is_user_created = action.second.is_user_created;
if( is_user_created ) {
jsout.member( "is_user_created", is_user_created );
}
jsout.member( "bindings" );
jsout.start_array();
for( const auto &event : events ) {
jsout.start_object();
switch( event.type ) {
case CATA_INPUT_KEYBOARD:
jsout.member( "input_method", "keyboard" );
break;
case CATA_INPUT_GAMEPAD:
jsout.member( "input_method", "gamepad" );
break;
case CATA_INPUT_MOUSE:
jsout.member( "input_method", "mouse" );
break;
default:
throw std::runtime_error( "unknown input_event_t" );
}
jsout.member( "key" );
jsout.start_array();
for( size_t i = 0; i < event.sequence.size(); i++ ) {
jsout.write( get_keyname( event.sequence[i], event.type, true ) );
}
jsout.end_array();
jsout.end_object();
}
jsout.end_array();
jsout.end_object();
}
}
jsout.end_array();
}, _( "key bindings configuration" ) );
}
void input_manager::add_keycode_pair( int ch, const std::string &name )
{
keycode_to_keyname[ch] = name;
keyname_to_keycode[name] = ch;
}
void input_manager::add_gamepad_keycode_pair( int ch, const std::string &name )
{
gamepad_keycode_to_keyname[ch] = name;
keyname_to_keycode[name] = ch;
}
constexpr int char_key_beg = ' ';
constexpr int char_key_end = '~';
void input_manager::init_keycode_mapping()
{
// Between space and tilde, all keys more or less map
// to themselves(see ASCII table)
for( char c = char_key_beg; c <= char_key_end; c++ ) {
std::string name( 1, c );
add_keycode_pair( c, name );
}
add_keycode_pair( '\t', translate_marker_context( "key name", "TAB" ) );
add_keycode_pair( KEY_BTAB, translate_marker_context( "key name", "BACKTAB" ) );
add_keycode_pair( ' ', translate_marker_context( "key name", "SPACE" ) );
add_keycode_pair( KEY_UP, translate_marker_context( "key name", "UP" ) );
add_keycode_pair( KEY_DOWN, translate_marker_context( "key name", "DOWN" ) );
add_keycode_pair( KEY_LEFT, translate_marker_context( "key name", "LEFT" ) );
add_keycode_pair( KEY_RIGHT, translate_marker_context( "key name", "RIGHT" ) );
add_keycode_pair( KEY_NPAGE, translate_marker_context( "key name", "NPAGE" ) );
add_keycode_pair( KEY_PPAGE, translate_marker_context( "key name", "PPAGE" ) );
add_keycode_pair( KEY_ESCAPE, translate_marker_context( "key name", "ESC" ) );
add_keycode_pair( KEY_BACKSPACE, translate_marker_context( "key name", "BACKSPACE" ) );
add_keycode_pair( KEY_HOME, translate_marker_context( "key name", "HOME" ) );
add_keycode_pair( KEY_BREAK, translate_marker_context( "key name", "BREAK" ) );
add_keycode_pair( KEY_END, translate_marker_context( "key name", "END" ) );
add_keycode_pair( '\n', translate_marker_context( "key name", "RETURN" ) );
// function keys, as defined by ncurses
for( int i = F_KEY_NUM_BEG; i <= F_KEY_NUM_END; i++ ) {
// not marked for translation here, but specially handled in get_keyname so
// it gets properly translated.
add_keycode_pair( KEY_F( i ), string_format( "F%d", i ) );
}
add_gamepad_keycode_pair( JOY_LEFT, translate_marker_context( "key name", "JOY_LEFT" ) );
add_gamepad_keycode_pair( JOY_RIGHT, translate_marker_context( "key name", "JOY_RIGHT" ) );
add_gamepad_keycode_pair( JOY_UP, translate_marker_context( "key name", "JOY_UP" ) );
add_gamepad_keycode_pair( JOY_DOWN, translate_marker_context( "key name", "JOY_DOWN" ) );
add_gamepad_keycode_pair( JOY_LEFTUP, translate_marker_context( "key name", "JOY_LEFTUP" ) );
add_gamepad_keycode_pair( JOY_LEFTDOWN, translate_marker_context( "key name", "JOY_LEFTDOWN" ) );
add_gamepad_keycode_pair( JOY_RIGHTUP, translate_marker_context( "key name", "JOY_RIGHTUP" ) );
add_gamepad_keycode_pair( JOY_RIGHTDOWN, translate_marker_context( "key name", "JOY_RIGHTDOWN" ) );
add_gamepad_keycode_pair( JOY_0, translate_marker_context( "key name", "JOY_0" ) );
add_gamepad_keycode_pair( JOY_1, translate_marker_context( "key name", "JOY_1" ) );
add_gamepad_keycode_pair( JOY_2, translate_marker_context( "key name", "JOY_2" ) );
add_gamepad_keycode_pair( JOY_3, translate_marker_context( "key name", "JOY_3" ) );
add_gamepad_keycode_pair( JOY_4, translate_marker_context( "key name", "JOY_4" ) );
add_gamepad_keycode_pair( JOY_5, translate_marker_context( "key name", "JOY_5" ) );
add_gamepad_keycode_pair( JOY_6, translate_marker_context( "key name", "JOY_6" ) );
add_gamepad_keycode_pair( JOY_7, translate_marker_context( "key name", "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;
}
int input_manager::get_keycode( const std::string &name ) const
{
const t_name_to_key_map::const_iterator a = keyname_to_keycode.find( name );
if( a != keyname_to_keycode.end() ) {
return a->second;
}
// Not found in map, try to parse as int
if( name.compare( 0, 8, "UNKNOWN_" ) == 0 ) {
return str_to_int( name.substr( 8 ) );
}
return 0;
}
std::string input_manager::get_keyname( int ch, input_event_t inp_type, bool portable ) const
{
std::optional<std::string> raw;
if( inp_type == CATA_INPUT_KEYBOARD ) {
const t_key_to_name_map::const_iterator a = keycode_to_keyname.find( ch );
if( a != keycode_to_keyname.end() ) {
if( IS_F_KEY( ch ) ) {
// special case it since F<num> key names are generated using loop
// and not marked individually for translation
if( portable ) {
return a->second;
} else {
return string_format( pgettext( "function key name", "F%d" ), F_KEY_NUM( ch ) );
}
} else if( ch >= char_key_beg && ch <= char_key_end && ch != ' ' ) {
// character keys except space need no translation
return a->second;
}
raw = a->second;
}
} else if( inp_type == CATA_INPUT_MOUSE ) {
if( ch == MOUSE_BUTTON_LEFT ) {
raw = translate_marker_context( "key name", "MOUSE_LEFT" );
} else if( ch == MOUSE_BUTTON_RIGHT ) {
raw = translate_marker_context( "key name", "MOUSE_RIGHT" );
} else if( ch == SCROLLWHEEL_UP ) {
raw = translate_marker_context( "key name", "SCROLL_UP" );
} else if( ch == SCROLLWHEEL_DOWN ) {
raw = translate_marker_context( "key name", "SCROLL_DOWN" );
} else if( ch == MOUSE_MOVE ) {
raw = translate_marker_context( "key name", "MOUSE_MOVE" );
}
} else if( inp_type == CATA_INPUT_GAMEPAD ) {
const t_key_to_name_map::const_iterator a = gamepad_keycode_to_keyname.find( ch );
if( a != gamepad_keycode_to_keyname.end() ) {
raw = a->second;
}
} else {
raw = translate_marker_context( "key name", "UNKNOWN" );
}
if( !raw ) {
if( portable ) {
return std::string( "UNKNOWN_" ) + int_to_str( ch );
}
return string_format( _( "unknown key %ld" ), ch );
}
return portable ? *raw : pgettext( "key name", raw->c_str() );
}
const std::vector<input_event> &input_manager::get_input_for_action( const std::string
&action_descriptor, const std::string &context, bool *overwrites_default )
{
const action_attributes &attributes =
get_action_attributes( action_descriptor, context, overwrites_default );
return attributes.input_events;
}
int input_manager::get_first_char_for_action( const std::string &action_descriptor,
const std::string &context )
{
std::vector<input_event> input_events = get_input_for_action( action_descriptor, context );
return input_events.empty() ? 0 : input_events[0].get_first_input();
}
const action_attributes &input_manager::get_action_attributes(
const std::string &action_id,
const std::string &context,
bool *overwrites_default )
{
if( context != default_context_id ) {
// Check if the action exists in the provided context
const t_action_contexts::const_iterator action_context = action_contexts.find( context );
if( action_context != action_contexts.end() ) {
const t_actions::const_iterator action = action_context->second.find( action_id );
if( action != action_context->second.end() ) {
if( overwrites_default ) {
*overwrites_default = true;
}
return action->second;
}
}
}
// If not, we use the default binding.
if( overwrites_default ) {
*overwrites_default = false;
}
t_actions &default_action_context = action_contexts[default_context_id];
const t_actions::const_iterator default_action = default_action_context.find( action_id );
if( default_action == default_action_context.end() ) {
// A new action is created in the event that the requested action is
// not in the keybindings configuration e.g. the entry is missing.
default_action_context[action_id].name = get_default_action_name( action_id );
}
return default_action_context[action_id];
}
translation input_manager::get_default_action_name( const std::string &action_id ) const
{
const t_action_contexts::const_iterator default_action_context = action_contexts.find(
default_context_id );
if( default_action_context == action_contexts.end() ) {
return no_translation( action_id );
}
const t_actions::const_iterator default_action = default_action_context->second.find( action_id );
if( default_action != default_action_context->second.end() ) {
return default_action->second.name;
} else {
return no_translation( action_id );
}
}
input_manager::t_input_event_list &input_manager::get_or_create_event_list(
const std::string &action_descriptor, const std::string &context )
{
// A new context is created in the event that the user creates a local
// keymapping in a context that doesn't yet exist e.g. a context without
// any pre-existing keybindings.
t_actions &actions = action_contexts[context];
// A new action is created in the event that the user creates a local
// keymapping that masks a global one.
if( actions.find( action_descriptor ) == actions.end() ) {
action_attributes &attributes = actions[action_descriptor];
attributes.name = get_default_action_name( action_descriptor );
attributes.is_user_created = true;
}
return actions[action_descriptor].input_events;
}
void input_manager::remove_input_for_action(
const std::string &action_descriptor, const std::string &context )
{
const t_action_contexts::iterator action_context = action_contexts.find( context );
if( action_context != action_contexts.end() ) {
t_actions &actions = action_context->second;
t_actions::iterator action = actions.find( action_descriptor );
if( action != actions.end() ) {
if( action->second.is_user_created ) {
// Since this is a user created hotkey, remove it so that the
// user will fallback to the hotkey in the default context.
actions.erase( action );
} else {
// If a context no longer has any keybindings remaining for an action but
// there's an attempt to remove bindings anyway, presumably the user wants
// to fully remove the binding from that context.
if( action->second.input_events.empty() ) {
actions.erase( action );
} else {
action->second.input_events.clear();
}
}
}
}
}
void input_manager::add_input_for_action(
const std::string &action_descriptor, const std::string &context, const input_event &event )
{
t_input_event_list &events = get_or_create_event_list( action_descriptor, context );
for( auto &events_a : events ) {
if( events_a == event ) {
return;
}
}
events.push_back( event );
}
bool input_context::action_uses_input( const std::string &action_id,
const input_event &event ) const
{
const auto &events = inp_mngr.get_action_attributes( action_id, category ).input_events;
return std::find( events.begin(), events.end(), event ) != events.end();
}
std::string input_context::get_conflicts( const input_event &event ) const
{
return enumerate_as_string( registered_actions.begin(), registered_actions.end(),
[ this, &event ]( const std::string & action ) {
return action_uses_input( action, event ) ? get_action_name( action ) : std::string();
} );
}
void input_context::clear_conflicting_keybindings( const input_event &event )
{
// The default context is always included to cover cases where the same
// keybinding exists for the same action in both the global and local
// contexts.
input_manager::t_actions &default_actions = inp_mngr.action_contexts[default_context_id];
input_manager::t_actions &category_actions = inp_mngr.action_contexts[category];
for( std::vector<std::string>::const_iterator registered_action = registered_actions.begin();
registered_action != registered_actions.end();
++registered_action ) {
input_manager::t_actions::iterator default_action = default_actions.find( *registered_action );
input_manager::t_actions::iterator category_action = category_actions.find( *registered_action );
if( default_action != default_actions.end() ) {
std::vector<input_event> &events = default_action->second.input_events;
events.erase( std::remove( events.begin(), events.end(), event ), events.end() );
}
if( category_action != category_actions.end() ) {
std::vector<input_event> &events = category_action->second.input_events;
events.erase( std::remove( events.begin(), events.end(), event ), events.end() );
}
}
}
const std::string CATA_ERROR = "ERROR";
const std::string ANY_INPUT = "ANY_INPUT";
const std::string HELP_KEYBINDINGS = "HELP_KEYBINDINGS";
const std::string COORDINATE = "COORDINATE";
const std::string TIMEOUT = "TIMEOUT";
const std::string &input_context::input_to_action( const input_event &inp ) const
{
for( auto &elem : registered_actions ) {
const std::string &action = elem;
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( auto &check_inp_i : check_inp ) {
if( check_inp_i == inp ) {
return action;
}
}
}
return CATA_ERROR;
}
#if defined(__ANDROID__)
std::list<input_context *> input_context::input_context_stack;
void input_context::register_manual_key( manual_key mk )
{
// Prevent duplicates
for( const manual_key &manual_key : registered_manual_keys )
if( manual_key.key == mk.key ) {
return;
}
registered_manual_keys.push_back( mk );
}
void input_context::register_manual_key( int key, const std::string text )
{
// Prevent duplicates
for( const manual_key &manual_key : registered_manual_keys )
if( manual_key.key == key ) {
return;
}
registered_manual_keys.push_back( manual_key( key, text ) );
}
#endif
void input_context::register_action( const std::string &action_descriptor )
{
register_action( action_descriptor, translation() );
}
void input_context::register_action( const std::string &action_descriptor, const translation &name )
{
if( action_descriptor == "ANY_INPUT" ) {
registered_any_input = true;
} else if( action_descriptor == "COORDINATE" ) {
handling_coordinate_input = true;
}
registered_actions.push_back( action_descriptor );
if( !name.empty() ) {
action_name_overrides[action_descriptor] = name;
}
}
std::vector<char> input_context::keys_bound_to( const std::string &action_descriptor,
const bool restrict_to_printable ) const
{
std::vector<char> result;
const std::vector<input_event> &events = inp_mngr.get_input_for_action( action_descriptor,
category );
for( const auto &events_event : events ) {
// Ignore multi-key input and non-keyboard input
// TODO: fix for Unicode.
if( events_event.type == CATA_INPUT_KEYBOARD && events_event.sequence.size() == 1 ) {
if( !restrict_to_printable || ( events_event.sequence.front() < 0xFF &&
isprint( events_event.sequence.front() ) ) ) {
result.push_back( static_cast<char>( events_event.sequence.front() ) );
}
}
}
return result;
}
std::string input_context::key_bound_to( const std::string &action_descriptor, const size_t index,
const bool restrict_to_printable ) const
{
const auto bound_keys = keys_bound_to( action_descriptor, restrict_to_printable );
return bound_keys.size() > index ? std::string( 1, bound_keys[index] ) : "";
}
std::string input_context::get_available_single_char_hotkeys( std::string requested_keys )
{
for( const auto ®istered_action : registered_actions ) {
const std::vector<input_event> &events =
inp_mngr.get_input_for_action( registered_action, category );
for( const auto &events_event : events ) {
// Only consider keyboard events without modifiers
if( events_event.type == CATA_INPUT_KEYBOARD && events_event.modifiers.empty() ) {
requested_keys.erase( std::remove_if( requested_keys.begin(), requested_keys.end(),
ContainsPredicate<std::vector<int>, char>(
events_event.sequence ) ),
requested_keys.end() );
}
}
}
return requested_keys;
}
const input_context::input_event_filter input_context::disallow_lower_case =
[]( const input_event &evt ) -> bool {
return evt.type != CATA_INPUT_KEYBOARD ||
// std::lower from <cctype> is undefined outside unsigned char range
// and std::lower from <locale> may throw bad_cast for some locales
evt.get_first_input() < 'a' || evt.get_first_input() > 'z';
};
const input_context::input_event_filter input_context::allow_all_keys =
[]( const input_event & ) -> bool {
return true;
};
std::string input_context::get_desc( const std::string &action_descriptor,
const unsigned int max_limit,
const input_context::input_event_filter &evt_filter ) const
{
if( action_descriptor == "ANY_INPUT" ) {
return "(*)"; // * for wildcard
}
bool is_local = false;
const std::vector<input_event> &events = inp_mngr.get_input_for_action( action_descriptor,
category, &is_local );
if( events.empty() ) {
return is_local ? _( "Unbound locally!" ) : _( "Unbound globally!" );
}
std::vector<input_event> inputs_to_show;
for( auto &events_i : events ) {
const input_event &event = events_i;
if( evt_filter( event ) &&
// Only display gamepad buttons if a gamepad is available.
( gamepad_available() || event.type != CATA_INPUT_GAMEPAD ) ) {
inputs_to_show.push_back( event );
}
if( max_limit > 0 && inputs_to_show.size() == max_limit ) {
break;
}
}
if( inputs_to_show.empty() ) {
return pgettext( "keybinding", "Disabled" );
}
std::string rval;
for( size_t i = 0; i < inputs_to_show.size(); ++i ) {
for( size_t 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;
}
std::string input_context::get_desc( const std::string &action_descriptor,
const std::string &text,
const input_context::input_event_filter &evt_filter ) const
{
if( action_descriptor == "ANY_INPUT" ) {
// \u00A0 is the non-breaking space
//~ keybinding description for anykey
return string_format( pgettext( "keybinding", "[any]\u00A0%s" ), text );
}
const auto &events = inp_mngr.get_input_for_action( action_descriptor, category );
bool na = true;
for( const auto &evt : events ) {
if( evt_filter( evt ) &&
// Only display gamepad buttons if a gamepad is available.
( gamepad_available() || evt.type != CATA_INPUT_GAMEPAD ) ) {
na = false;
if( evt.type == CATA_INPUT_KEYBOARD && evt.sequence.size() == 1 ) {
const int ch = evt.get_first_input();
const std::string key = utf32_to_utf8( ch );
const auto pos = ci_find_substr( text, key );
if( ch > ' ' && ch <= '~' && pos >= 0 ) {
return text.substr( 0, pos ) + "(" + key + ")" + text.substr( pos + key.size() );
}
}
}
}
if( na ) {
//~ keybinding description for unbound or disabled keys
return string_format( pgettext( "keybinding", "[n/a]\u00A0%s" ), text );
} else {
//~ keybinding description for bound keys
return string_format( pgettext( "keybinding", "[%s]\u00A0%s" ),
get_desc( action_descriptor, 1, evt_filter ), text );
}
}
std::string input_context::describe_key_and_name( const std::string &action_descriptor,
const input_context::input_event_filter &evt_filter ) const
{
return get_desc( action_descriptor, get_action_name( action_descriptor ), evt_filter );
}
const std::string &input_context::handle_input()
{
return handle_input( timeout );
}
const std::string &input_context::handle_input( const int timeout )
{
const auto old_timeout = inp_mngr.get_timeout();
if( timeout >= 0 ) {
inp_mngr.set_timeout( timeout );
}
next_action.type = CATA_INPUT_ERROR;
const std::string *result = &CATA_ERROR;
while( true ) {
next_action = inp_mngr.get_input_event();
if( next_action.type == CATA_INPUT_TIMEOUT ) {
result = &TIMEOUT;
break;
}
const std::string &action = input_to_action( next_action );
// Special help action
if( action == "HELP_KEYBINDINGS" ) {
inp_mngr.reset_timeout();
display_menu();
inp_mngr.set_timeout( timeout );
result = &HELP_KEYBINDINGS;
break;
}
if( next_action.type == CATA_INPUT_MOUSE ) {
if( !handling_coordinate_input && action == CATA_ERROR ) {
continue; // Ignore this mouse input.
}
coordinate_input_received = true;
coordinate = next_action.mouse_pos;
} else {
coordinate_input_received = false;
}
if( action != CATA_ERROR ) {
result = &action;
break;
}
// If we registered to receive any input, return ANY_INPUT
// to signify that an unregistered key was pressed.
if( registered_any_input ) {
result = &ANY_INPUT;
break;
}
// If it's an invalid key, just keep looping until the user
// enters something proper.
}
inp_mngr.set_timeout( old_timeout );
return *result;
}
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();
}
// dx and dy are -1, 0, or +1. Rotate the indicated direction 1/8 turn clockwise.
void rotate_direction_cw( int &dx, int &dy )
{
// convert to
// 0 1 2
// 3 4 5
// 6 7 8
int dir_num = ( dy + 1 ) * 3 + dx + 1;
// rotate to
// 1 2 5
// 0 4 8
// 3 6 7
static const std::array<int, 9> rotate_direction_vec = {{ 1, 2, 5, 0, 4, 8, 3, 6, 7 }};
dir_num = rotate_direction_vec[dir_num];
// convert back to -1,0,+1
dx = dir_num % 3 - 1;
dy = dir_num / 3 - 1;
}
std::optional<tripoint> input_context::get_direction( const std::string &action ) const
{
static const auto noop = static_cast<tripoint( * )( tripoint )>( []( tripoint p ) {
return p;
} );
static const auto rotate = static_cast<tripoint( * )( tripoint )>( []( tripoint p ) {
rotate_direction_cw( p.x, p.y );
return p;
} );
const auto transform = iso_mode && tile_iso && use_tiles ? rotate : noop;
if( action == "UP" ) {
return transform( tripoint_north );
} else if( action == "DOWN" ) {
return transform( tripoint_south );
} else if( action == "LEFT" ) {
return transform( tripoint_west );
} else if( action == "RIGHT" ) {
return transform( tripoint_east );
} else if( action == "LEFTUP" ) {
return transform( tripoint_north_west );
} else if( action == "RIGHTUP" ) {
return transform( tripoint_north_east );
} else if( action == "LEFTDOWN" ) {
return transform( tripoint_south_west );
} else if( action == "RIGHTDOWN" ) {
return transform( tripoint_south_east );
} else {
return std::nullopt;
}
}
// Custom set of hotkeys that explicitly don't include the hardcoded
// alternative hotkeys, which mustn't be included so that the hardcoded
// hotkeys do not show up beside entries within the window.
const std::string display_help_hotkeys =
"abcdefghijkpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:;'\",/<>?!@#$%^&*()_[]\\{}|`~";
action_id input_context::display_menu( const bool permit_execute_action )
{
action_id action_to_execute = ACTION_NULL;
// Shamelessly stolen from help.cpp
input_context ctxt( "HELP_KEYBINDINGS" );
ctxt.register_action( "UP", to_translation( "Scroll up" ) );
ctxt.register_action( "DOWN", to_translation( "Scroll down" ) );