Rose
Keyboard.h
1 
8 #pragma once
9 
10 #include <utility>
11 
12 #include "Frame.h"
13 #include "Button.h"
14 #include "Container.h"
15 
16 namespace rose {
17 
22  enum class KeyboardMode {
23  LowerCase,
24  UpperCase,
25  Numbers,
26  Symbols,
27  };
28 
30  template<size_t N>
31  using KeySpec = std::array<uint32_t,N>;
32 
33  template<size_t N>
34  constexpr auto makeKeySpec(const char *values) noexcept {
35  auto a = KeySpec<N>{};
36  const char *cp = values;
37  for (auto &v : a) {
38  if (*cp != '\0') {
39  v = static_cast<uint32_t>(*cp++);
40  } else {
41  v = 0;
42  }
43  }
44  return a;
45  }
46 
47  template<class I, typename Arg, typename ... Args>
48  constexpr I makeKeySpecInit(I first, I last, Arg arg, Args ... args) noexcept {
49  if (first != last) {
50  *first = static_cast<uint32_t>(arg);
51  ++first;
52  if constexpr (sizeof...(args) > 0) {
53  first = makeKeySpecInit(first, last, args...);
54  }
55  }
56  return first;
57  }
58 
59  template<size_t N, typename ... Args>
60  constexpr auto makeKeySpec(Args ... args) noexcept {
61  auto a = KeySpec<N>{};
62  auto first = a.begin();
63  auto last = a.end();
64  last = makeKeySpecInit(first, last, args...);
65  return a;
66  }
67 
69  template<size_t M, size_t N>
70  using KeySpecRow = std::array<KeySpec<N>,M>;
71 
73  using KSS = std::array<char,5>;
74 
76  template<size_t R, size_t M, size_t N>
77  using KeyboardSpec = std::array<std::array<KeySpec<N>,M>,R>;
78 
79  class Keyboard;
80 
86  public:
87  KeyboardPlugin() = default;
88  virtual ~KeyboardPlugin() = default;
89 
90  virtual void
91  build(shared_ptr <Keyboard> keyboard, Size keySize, int fontSize, const std::string &fontName) = 0;
92  };
93 
98  class Keyboard : public Frame {
99  protected:
100  std::shared_ptr<KeyboardPlugin> mKeyboardPlugin{};
101 
103 
104  char mEm{'M'};
105 // std::shared_ptr<Column> mKeysGrid; ///< The Column of key rows.
106  Size mKeySize{};
107 
108  std::string mFontName{};
109  int mFontSize{};
110 
111  public:
112 
113  Keyboard() = delete;
114 
115  ~Keyboard() override = default;
116 
117  Keyboard(Keyboard &&) = delete;
118 
119  Keyboard(const Keyboard &) = delete;
120 
121  Keyboard &operator=(Keyboard &&) = delete;
122 
123  Keyboard &operator=(const Keyboard &) = delete;
124 
129  explicit Keyboard(std::shared_ptr<KeyboardPlugin> keyboardPlugin) : mKeyboardPlugin(std::move(keyboardPlugin)) {}
130 
132  Rectangle widgetLayout(sdl::Renderer &renderer, Rectangle available, uint layoutStage) override;
133 
135  void draw(sdl::Renderer &renderer, Rectangle parentRect) override;
136 
138  void initializeComposite() override;
139  };
140 
145  class QUERTY : public KeyboardPlugin {
146  protected:
147  static constexpr size_t Rows = 4;
148  static constexpr size_t KeysPerRow = 11;
149  static constexpr size_t Depth = 4;
151 
152  std::weak_ptr<Keyboard> mKeyboard{};
153 
154  using KeySpecType = KeySpec<Depth>;
155 
160  enum class KeyboardMode : size_t {
161  LowerCase, UpperCase, Numeric, Symbols,
162  };
163 
168  enum class SymbolState : size_t {
169  None, Symbols1, Symbols2,
170  };
171 
172  uint32_t mTempShiftKey{0};
173  bool mCapsLock{false};
174 
177 
178  std::shared_ptr<Slot<Button::SignalType>> rxKey{};
179  std::shared_ptr<Slot<Button::SignalType>> rxCtl{};
180  std::shared_ptr<Slot<Button::SignalType>> rxToggle{};
181 
188  auto controlKeyData(const KeySpec<Depth> &keyData);
189 
194  void setKeyboardMode();
195 
199  void changeKeyboardFace();
200 
201  public:
202  QUERTY() = default;
203  ~QUERTY() override = default;
204 
210  void build(shared_ptr <Keyboard> keyboard, Size keySize, int fontSize, const std::string &fontName) override;
211  };
212 
217  class NumberPad : public KeyboardPlugin {
218  protected:
219  static constexpr size_t Rows = 5;
220  static constexpr size_t KeysPerRow = 3;
222 
223  static constexpr size_t SideCols = 1;
224  static constexpr size_t SideKeysPerCol = 5;
226 
227  std::shared_ptr<Slot<Button::SignalType>> rxKey{};
228 
229  public:
230  NumberPad() = default;
231  ~NumberPad() override = default;
232 
238  void build(std::shared_ptr<Keyboard> keyboard, Size keySize, int fontSize, const std::string &fontName) override;
239  };
240 }
241 
std::array< std::array< KeySpec< N >, M >, R > KeyboardSpec
Type for a full Keyboard specification.
Definition: Keyboard.h:77
SymbolState
The state of symbol selection.
Definition: Keyboard.h:168
std::array< KeySpec< N >, M > KeySpecRow
A row of KeySpec specifications.
Definition: Keyboard.h:70
Lower case letters.
KeyboardMode
Keyboard modes (cases)
Definition: Keyboard.h:22
Definition: Frame.h:214
A touch keyboard.
Definition: Keyboard.h:33
Numbers and symbols.
static const KeyboardSpec< Rows, KeysPerRow, 1 > NumberData
The main number pad KeyboardSpec.
Definition: Keyboard.h:221
KeyboardMode
Set the state of the keyboard.
Definition: Keyboard.h:160
Encapsulate the KeyboardSpec specification and build function.
Definition: Keyboard.h:85
Numbers and more symbols.
static const KeyboardSpec< SideCols, SideKeysPerCol, 1 > SideNumberData
The side key data.
Definition: Keyboard.h:225
std::array< char, 5 > KSS
Type to help construct a KeySpec from a string.
Definition: Keyboard.h:73
A composite of a Position and a Size.
Definition: Types.h:307
A KeyboardPlugin for a QWERTY keyboard.
Definition: Keyboard.h:145
Written as a workaround for an issue in the SDL2 Library.
Definition: Renderer.h:64
A size in integer dimensions.
Definition: Types.h:230
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
static const KeyboardSpec< Rows, KeysPerRow, Depth > QWERTYData
The KeyboardSpec.
Definition: Keyboard.h:150
Upper case letters.
Definition: Keyboard.h:17
A numeric keypad KeyboardPlugin.
Definition: Keyboard.h:217
Keyboard(std::shared_ptr< KeyboardPlugin > keyboardPlugin)
Constructor.
Definition: Keyboard.h:129