Rose
Keyboard.h
1 
8 #pragma once
9 
10 #include "Button.h"
11 #include "Manager.h"
12 #include <array>
13 #include <utility>
14 
15 namespace rose {
16 
17  struct KeySpec {
18  bool imageKey;
19  std::array<uint, 4> command;
20 
21  constexpr KeySpec(bool imgKey, std::array<uint, 4> cmd) : command(cmd), imageKey(imgKey) {
22  }
23  };
24 
25  static constexpr std::array<KeySpec, 1> K0{{
26  {true, {SDLK_CAPSLOCK}}
27  }};
28 
33  class Keyboard : public Grid {
34  protected:
35  bool mCapsLock{false};
36  bool mShiftActive{false};
37  uint mKeyState{0};
38  uint mAltState{0};
39 
41  static constexpr std::array<KeySpec, 11> keyDataRow0
42  {{
43  {false, {SDLK_q, SDLK_q, SDLK_1, SDLK_1}},
44  {false, {SDLK_w, SDLK_w, SDLK_2, SDLK_2}},
45  {false, {SDLK_e, SDLK_e, SDLK_3, SDLK_3}},
46  {false, {SDLK_r, SDLK_r, SDLK_4, SDLK_4}},
47  {false, {SDLK_t, SDLK_t, SDLK_5, SDLK_5}},
48  {false, {SDLK_y, SDLK_y, SDLK_6, SDLK_6}},
49  {false, {SDLK_u, SDLK_u, SDLK_7, SDLK_7}},
50  {false, {SDLK_i, SDLK_i, SDLK_8, SDLK_8}},
51  {false, {SDLK_o, SDLK_o, SDLK_9, SDLK_9}},
52  {false, {SDLK_p, SDLK_p, SDLK_0, SDLK_0}},
53  {true, {SDLK_BACKSPACE, SDLK_BACKSPACE, SDLK_BACKSPACE, SDLK_BACKSPACE}}
54  }};
55 
57  static constexpr std::array<KeySpec, 10> keyDataRow1
58  {{
59  {false, {SDLK_a, SDLK_a, SDLK_AT, SDLK_AT}},
60  {false, {SDLK_s, SDLK_s, SDLK_HASH, SDLK_HASH}},
61  {false, {SDLK_d, SDLK_d, SDLK_DOLLAR, SDLK_DOLLAR}},
62  {false, {SDLK_f, SDLK_f, SDLK_PERCENT, SDLK_PERCENT}},
63  {false, {SDLK_g, SDLK_g, SDLK_CARET, SDLK_CARET}},
64  {false, {SDLK_h, SDLK_h, SDLK_AMPERSAND, SDLK_AMPERSAND}},
65  {false, {SDLK_j, SDLK_j, SDLK_ASTERISK, SDLK_ASTERISK}},
66  {false, {SDLK_k, SDLK_k, SDLK_LEFTPAREN, SDLK_LEFTPAREN}},
67  {false, {SDLK_l, SDLK_l, SDLK_RIGHTPAREN, SDLK_RIGHTPAREN}},
68  {true, {SDLK_RETURN, SDLK_RETURN, SDLK_RETURN, SDLK_RETURN}}
69  }};
70 
72  static constexpr std::array<KeySpec, 11> keyDataRow2
73  {{
74  {true, {SDLK_CAPSLOCK, SDLK_CAPSLOCK, SDLK_CAPSLOCK, SDLK_CAPSLOCK}},
75  {false, {SDLK_z, SDLK_z, '~', '~'}},
76  {false, {SDLK_x, SDLK_x, SDLK_BACKSLASH, SDLK_BACKSLASH}},
77  {false, {SDLK_c, SDLK_c, SDLK_UNDERSCORE, SDLK_UNDERSCORE}},
78  {false, {SDLK_v, SDLK_v, SDLK_LEFTBRACKET, SDLK_BACKQUOTE}},
79  {false, {SDLK_b, SDLK_b, SDLK_RIGHTBRACKET, SDLK_COMMA}},
80  {false, {SDLK_n, SDLK_n, '{', SDLK_PERIOD}},
81  {false, {SDLK_m, SDLK_m, '}', SDLK_QUESTION}},
82  {false, {SDLK_COMMA, SDLK_QUESTION, SDLK_COLON, SDLK_COLON}},
83  {false, {SDLK_PERIOD, SDLK_SLASH, SDLK_SEMICOLON, SDLK_SEMICOLON}},
84  {true, {SDLK_RSHIFT, SDLK_RSHIFT, SDLK_RSHIFT, SDLK_RSHIFT}}
85  }};
86 
88  static constexpr std::array<KeySpec, 6> keyDataRow3
89  {{
90  {true, {SDLK_LSHIFT, SDLK_LSHIFT, SDLK_LSHIFT, SDLK_LSHIFT}},
91  {true, {SDLK_LALT, SDLK_LALT, SDLK_LALT, SDLK_LALT}},
92  {false, {SDLK_SPACE, SDLK_SPACE, SDLK_SPACE, SDLK_SPACE}},
93  {true, {SDLK_LEFT, SDLK_LEFT, SDLK_LEFT, SDLK_LEFT}},
94  {true, {SDLK_RIGHT, SDLK_RIGHT, SDLK_RIGHT, SDLK_RIGHT}},
95  {true, {SDLK_RALT, SDLK_RALT, SDLK_RALT, SDLK_RALT}}
96  }};
97 
98  public:
99  Keyboard() : Grid() {}
100 
101  ~Keyboard() override = default;
102 
103  void addedToContainer() override;
104 
106  void keyCommandCallback(ButtonStateChange stateChange, uint command);
107  };
108 
113  class LetterKey : public TextButton {
114  protected:
115  std::array<uint, 4> mCommand;
116  uint mKeyState{0};
117 
118  void setTextFromCommand();
119 
120  public:
121  LetterKey() = delete;
122 
123  ~LetterKey() override = default;
124 
129  explicit LetterKey(std::array<uint, 4> cmd) : TextButton(), mCommand(cmd) {
130  mCentreVertical = true;
131  mCentreHorizontal = true;
132  }
133 
134  void addedToContainer() override;
135 
137  void setKeyState(uint keyState) {
138  mKeyState = keyState;
139  setTextFromCommand();
140  }
141  };
142 
147  class ImageKey : public ImageButton {
148  protected:
149  std::array<uint, 4> mCommand;
150  uint mKeyState{0};
151 
152  void setImageFromCommand();
153 
154  public:
155  ImageKey() = delete;
156 
157  ~ImageKey() override = default;
158 
163  explicit ImageKey(std::array<uint, 4> cmd) : ImageButton(), mCommand(cmd) {
164  mCentreVertical = true;
165  mCentreHorizontal = true;
166  }
167 
168  void addedToContainer() override;
169 
171  void setKeyState(uint keyState) {
172  mKeyState = keyState;
173  setImageFromCommand();
174  }
175  };
176 
181  class CapsLockKey : public ImageKey {
182  protected:
183  bool mLockState{false};
184 
185  void setImageFromLockState();
186 
187  public:
188  CapsLockKey() = delete;
189 
194  explicit CapsLockKey(std::array<uint, 4> cmd) : ImageKey(cmd) {
195  }
196 
197  void addedToContainer() override;
198  };
199 }
200 
Definition: Manager.h:88
ButtonStateChange
Definition: Callbacks.h:28
ImageKey(std::array< uint, 4 > cmd)
Construct an Image key.
Definition: Keyboard.h:163
std::array< uint, 4 > mCommand
The glyphs and controlls supported.
Definition: Keyboard.h:149
void setKeyState(uint keyState)
Set the key state.
Definition: Keyboard.h:137
LetterKey(std::array< uint, 4 > cmd)
Construct a letter key.
Definition: Keyboard.h:129
Definition: Button.h:65
A touch keyboard.
Definition: Keyboard.h:33
A key which produces a character of input.
Definition: Keyboard.h:113
CapsLockKey(std::array< uint, 4 > cmd)
Construct a caps lock key.
Definition: Keyboard.h:194
ImageKey
Definition: Keyboard.h:147
A specialized ImageKey for Caps Lock.
Definition: Keyboard.h:181
Definition: Button.h:182
void setKeyState(uint keyState)
Set the key state.
Definition: Keyboard.h:171
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
Definition: Keyboard.h:17