|
2 | 2 |
|
3 | 3 |
|
4 | 4 | namespace SDL {
|
5 |
| - namespace events { |
6 |
| - KeyCombination::KeyCombination(const SDL_Keysym keysym) { |
7 |
| - this->sdl_keysym = keysym; |
8 |
| - } |
9 |
| - |
10 |
| - KeyCombination::KeyCombination(const Uint16 modifiers, const ScanKey key) { |
11 |
| - this->scan_key(key); |
12 |
| - this->sdl_keysym.mod = modifiers; |
13 |
| - } |
14 |
| - |
15 |
| - KeyCombination::KeyCombination(const Uint16 modifiers, const Key key) { |
16 |
| - this->key(key); |
17 |
| - this->sdl_keysym.mod = modifiers; |
18 |
| - } |
19 |
| - |
20 |
| - KeyCombination::KeyCombination(const ScanKey key) : KeyCombination(0, key) { |
21 |
| - } |
22 |
| - |
23 |
| - KeyCombination::KeyCombination(const Key key) : KeyCombination(0, key) { |
24 |
| - } |
25 |
| - |
26 |
| - bool KeyCombination::modifier(const Modifier m) const { |
27 |
| - return this->sdl_keysym.mod & Uint16(m); |
28 |
| - } |
29 |
| - |
30 |
| - void KeyCombination::modifier(const Modifier m, const bool value) { |
31 |
| - if (this->modifier(m) != value) |
32 |
| - this->sdl_keysym.mod = this->sdl_keysym.mod ^ Uint16(m); |
33 |
| - } |
34 |
| - |
35 |
| - ScanKey KeyCombination::scan_key() const { |
36 |
| - return ScanKey(this->sdl_keysym.scancode); |
37 |
| - } |
38 |
| - |
39 |
| - void KeyCombination::scan_key(const ScanKey key) { |
40 |
| - this->sdl_keysym.scancode = SDL_Scancode(key); |
41 |
| - this->sdl_keysym.sym = SDL_GetKeyFromScancode(SDL_Scancode(key)); |
42 |
| - } |
43 |
| - |
44 |
| - Key KeyCombination::key() const { |
45 |
| - return Key(this->sdl_keysym.sym); |
46 |
| - } |
47 |
| - |
48 |
| - void KeyCombination::key(const Key key) { |
49 |
| - this->sdl_keysym.sym = SDL_Keycode(key); |
50 |
| - this->sdl_keysym.scancode = SDL_GetScancodeFromKey(SDL_Keycode(key)); |
51 |
| - } |
| 5 | + KeyCombination::KeyCombination(const SDL_Keysym keysym) { |
| 6 | + this->sdl_keysym = keysym; |
| 7 | + } |
| 8 | + |
| 9 | + KeyCombination::KeyCombination(const Uint16 modifiers, const ScanKey key) { |
| 10 | + this->scan_key(key); |
| 11 | + this->sdl_keysym.mod = modifiers; |
| 12 | + } |
| 13 | + |
| 14 | + KeyCombination::KeyCombination(const Uint16 modifiers, const Key key) { |
| 15 | + this->key(key); |
| 16 | + this->sdl_keysym.mod = modifiers; |
| 17 | + } |
| 18 | + |
| 19 | + KeyCombination::KeyCombination(const ScanKey key) : KeyCombination(0, key) { |
| 20 | + } |
| 21 | + |
| 22 | + KeyCombination::KeyCombination(const Key key) : KeyCombination(0, key) { |
| 23 | + } |
| 24 | + |
| 25 | + bool KeyCombination::modifier(const Modifier m) const { |
| 26 | + return this->sdl_keysym.mod & Uint16(m); |
| 27 | + } |
| 28 | + |
| 29 | + void KeyCombination::modifier(const Modifier m, const bool value) { |
| 30 | + if (this->modifier(m) != value) |
| 31 | + this->sdl_keysym.mod = this->sdl_keysym.mod ^ Uint16(m); |
| 32 | + } |
| 33 | + |
| 34 | + ScanKey KeyCombination::scan_key() const { |
| 35 | + return ScanKey(this->sdl_keysym.scancode); |
| 36 | + } |
| 37 | + |
| 38 | + void KeyCombination::scan_key(const ScanKey key) { |
| 39 | + this->sdl_keysym.scancode = SDL_Scancode(key); |
| 40 | + this->sdl_keysym.sym = SDL_GetKeyFromScancode(SDL_Scancode(key)); |
| 41 | + } |
| 42 | + |
| 43 | + Key KeyCombination::key() const { |
| 44 | + return Key(this->sdl_keysym.sym); |
| 45 | + } |
| 46 | + |
| 47 | + void KeyCombination::key(const Key key) { |
| 48 | + this->sdl_keysym.sym = SDL_Keycode(key); |
| 49 | + this->sdl_keysym.scancode = SDL_GetScancodeFromKey(SDL_Keycode(key)); |
| 50 | + } |
| 51 | + |
| 52 | + Key key_from_name(const std::string& s) { |
| 53 | + return Key(SDL_GetKeyFromName(s.c_str())); |
| 54 | + } |
| 55 | + |
| 56 | + Key key_from_name(std::string&& s) { |
| 57 | + return Key(SDL_GetKeyFromName(s.c_str())); |
| 58 | + } |
| 59 | + |
| 60 | + Key key_from_scankey(const ScanKey key) { |
| 61 | + return Key(SDL_GetKeyFromScancode(SDL_Scancode(key))); |
| 62 | + } |
| 63 | + |
| 64 | + std::string get_key_name(const Key key) { |
| 65 | + return SDL_GetKeyName(SDL_Keycode(key)); |
| 66 | + } |
| 67 | + |
| 68 | + // TODO: Window& get_keyboard_focus() |
| 69 | + |
| 70 | + SDL_Keymod get_modifiers() { |
| 71 | + return SDL_GetModState(); |
| 72 | + } |
| 73 | + |
| 74 | + void set_modifiers(SDL_Keymod keymod) { |
| 75 | + SDL_SetModState(keymod); |
| 76 | + } |
| 77 | + |
| 78 | + ScanKey get_scankey_from_key(const Key key) { |
| 79 | + return ScanKey(SDL_GetScancodeFromKey(SDL_Keycode(key))); |
| 80 | + } |
| 81 | + |
| 82 | + ScanKey get_scankey_from_name(const std::string& s) { |
| 83 | + return ScanKey(SDL_GetScancodeFromName(s.c_str())); |
| 84 | + } |
| 85 | + |
| 86 | + ScanKey get_scankey_from_name(std::string&& s) { |
| 87 | + return ScanKey(SDL_GetScancodeFromName(s.c_str())); |
| 88 | + } |
| 89 | + |
| 90 | + std::string get_scankey_name(const ScanKey key) { |
| 91 | + return SDL_GetScancodeName(SDL_Scancode(key)); |
| 92 | + } |
| 93 | + |
| 94 | + bool has_screen_keyboard_support() { |
| 95 | + return SDL_HasScreenKeyboardSupport() == SDL_TRUE; |
| 96 | + } |
| 97 | + |
| 98 | + // TODO: bool screen_keyboard_shown(Window& window) |
| 99 | + |
| 100 | + bool text_input_active() { |
| 101 | + return SDL_IsTextInputActive() == SDL_TRUE; |
| 102 | + } |
| 103 | + |
| 104 | + void start_text_input() { |
| 105 | + SDL_StartTextInput(); |
| 106 | + } |
| 107 | + |
| 108 | + void stop_text_input() { |
| 109 | + SDL_StopTextInput(); |
| 110 | + } |
| 111 | + |
| 112 | + void set_text_input_rectangle(const RectangleI rect) { |
| 113 | + SDL_Rect sdl_rect; |
| 114 | + sdl_rect.x = x(rect.position1); |
| 115 | + sdl_rect.y = y(rect.position1); |
| 116 | + auto size = rect.position2 - rect.position1; |
| 117 | + sdl_rect.w = x(size); |
| 118 | + sdl_rect.h = y(size); |
| 119 | + SDL_SetTextInputRect(&sdl_rect); |
| 120 | + } |
| 121 | + |
| 122 | + boost::dynamic_bitset<> get_keyboard_state() { |
| 123 | + const Uint8* state = SDL_GetKeyboardState(NULL); |
| 124 | + boost::dynamic_bitset<> out{sizeof(state) / sizeof(state[0])}; |
| 125 | + for (size_t i = 0; i < out.size(); ++i) |
| 126 | + out[i] = state[i]; |
| 127 | + return out; |
52 | 128 | }
|
53 | 129 | }
|
0 commit comments