5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/TextInputTarget.h"
9#include "TinyMaterialDesign/Core/Widget.h"
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
48 this->bounds = bounds;
49 this->visible =
false;
55 template <
typename Field>
57 field.onFocus = [
this, &field]() {
58 if (target_ !=
nullptr && target_ != &field) target_
->blur();
62 mode_ = Mode::kNumeric;
63 }
else if (mode_ == Mode::kNumeric) {
64 mode_ = Mode::kLetters;
70 void hide() {
this->visible =
false; }
72 void draw(tinygpu::ISurface<RGB_T>& target)
override {
73 target.fillRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
74 toPx(
this->bounds.h),
this->theme().colors.surfaceVariant);
76 forEachKey([&](
const Key& key,
const Bounds& rect) {
77 const bool highlighted = key.action == Action::kShift && shift_;
78 const RGB_T keyColor = highlighted ?
this->theme().colors.secondaryContainer :
this->theme().colors.surface;
79 target.fillRoundRect(toPx(rect.x + 2), toPx(rect.y + 2), toPx(rect.w - 4), toPx(rect.h - 4),
80 toPx(
this->theme().shape.small), keyColor);
82 char labelBuffer[2] = {0, 0};
83 const char* labelText = key.label;
84 if (key.action == Action::kChar) {
85 labelBuffer[0] = shift_ ?
static_cast<
char>(std::toupper(key.ch)) : key.ch;
86 labelText = labelBuffer;
87 }
else if (key.action == Action::kToggleSymbols) {
88 labelText = (mode_ == Mode::kLetters) ?
"123" :
"ABC";
91 const size_t textWidth =
this->theme().typography.body->measureTextWidth(labelText);
92 const int32_t textX = rect.centerX() -
static_cast<int32_t>(textWidth) / 2;
93 const int32_t textY = rect
.centerY() -
static_cast<int32_t>(
this->theme().typography.body->getHeight(1)) / 2;
94 this->theme().typography.body->drawText(target,
static_cast<int16_t>(textX),
95 static_cast<int16_t>(textY), labelText,
96 this->theme().colors.onSurface, keyColor,
false);
101 bool onGesture(
const tinygpu::GestureEvent& event)
override {
102 if (!isTapGesture(event.type))
return false;
103 const Key* hit =
nullptr;
104 forEachKey([&](
const Key& key,
const Bounds& rect) {
105 if (rect.contains(event.point.x, event.point.y)) {
111 if (hit ==
nullptr)
return true;
117 enum class Action { kChar, kShift, kBackspace, kSpace, kNewline, kEnter, kToggleSymbols };
118 enum class Mode { kLetters, kSymbols, kNumeric };
130 static constexpr Key kRow0[] = {
131 {Action::kToggleSymbols, 0,
"123", 1.5f},
132 {Action::kChar,
'1',
"1", 1}, {Action::kChar,
'2',
"2", 1}, {Action::kChar,
'3',
"3", 1},
133 {Action::kChar,
'4',
"4", 1}, {Action::kChar,
'5',
"5", 1}, {Action::kChar,
'6',
"6", 1},
134 {Action::kChar,
'7',
"7", 1}, {Action::kChar,
'8',
"8", 1}, {Action::kChar,
'9',
"9", 1},
135 {Action::kChar,
'0',
"0", 1},
137 static constexpr Key kRow1[] = {
138 {Action::kChar,
'q',
"q", 1}, {Action::kChar,
'w',
"w", 1}, {Action::kChar,
'e',
"e", 1},
139 {Action::kChar,
'r',
"r", 1}, {Action::kChar,
't',
"t", 1}, {Action::kChar,
'y',
"y", 1},
140 {Action::kChar,
'u',
"u", 1}, {Action::kChar,
'i',
"i", 1}, {Action::kChar,
'o',
"o", 1},
141 {Action::kChar,
'p',
"p", 1},
143 static constexpr Key kRow2[] = {
144 {Action::kChar,
'a',
"a", 1}, {Action::kChar,
's',
"s", 1}, {Action::kChar,
'd',
"d", 1},
145 {Action::kChar,
'f',
"f", 1}, {Action::kChar,
'g',
"g", 1}, {Action::kChar,
'h',
"h", 1},
146 {Action::kChar,
'j',
"j", 1}, {Action::kChar,
'k',
"k", 1}, {Action::kChar,
'l',
"l", 1},
148 static constexpr Key kRow3[] = {
149 {Action::kChar,
'z',
"z", 1}, {Action::kChar,
'x',
"x", 1}, {Action::kChar,
'c',
"c", 1},
150 {Action::kChar,
'v',
"v", 1}, {Action::kChar,
'b',
"b", 1}, {Action::kChar,
'n',
"n", 1},
151 {Action::kChar,
'm',
"m", 1},
154 static constexpr Key kRow1Symbols[] = {
155 {Action::kChar,
'!',
"!", 1}, {Action::kChar,
'@',
"@", 1}, {Action::kChar,
'#',
"#", 1},
156 {Action::kChar,
'$',
"$", 1}, {Action::kChar,
'%',
"%", 1}, {Action::kChar,
'^',
"^", 1},
157 {Action::kChar,
'&',
"&", 1}, {Action::kChar,
'*',
"*", 1}, {Action::kChar,
'(',
"(", 1},
158 {Action::kChar,
')',
")", 1},
160 static constexpr Key kRow2Symbols[] = {
161 {Action::kChar,
'-',
"-", 1}, {Action::kChar,
'_',
"_", 1}, {Action::kChar,
'=',
"=", 1},
162 {Action::kChar,
'+',
"+", 1}, {Action::kChar,
'[',
"[", 1}, {Action::kChar,
']',
"]", 1},
163 {Action::kChar,
'{',
"{", 1}, {Action::kChar,
'}',
"}", 1}, {Action::kChar,
'\\',
"\\", 1},
165 static constexpr Key kRow3Symbols[] = {
166 {Action::kChar,
';',
";", 1}, {Action::kChar,
':',
":", 1}, {Action::kChar,
'\'',
"'", 1},
167 {Action::kChar,
'"',
"\"", 1}, {Action::kChar,
',',
",", 1}, {Action::kChar,
'.',
".", 1},
168 {Action::kChar,
'/',
"/", 1},
172 static constexpr Key kNum0[] = {
173 {Action::kChar,
'7',
"7", 1}, {Action::kChar,
'8',
"8", 1}, {Action::kChar,
'9',
"9", 1}};
174 static constexpr Key kNum1[] = {
175 {Action::kChar,
'4',
"4", 1}, {Action::kChar,
'5',
"5", 1}, {Action::kChar,
'6',
"6", 1}};
176 static constexpr Key kNum2[] = {
177 {Action::kChar,
'1',
"1", 1}, {Action::kChar,
'2',
"2", 1}, {Action::kChar,
'3',
"3", 1}};
178 static constexpr Key kNum3[] = {
179 {Action::kChar,
'.',
".", 1}, {Action::kChar,
'0',
"0", 1}, {Action::kChar,
'-',
"-", 1}};
180 static constexpr Key kNum4[] = {
181 {Action::kToggleSymbols, 0,
"ABC", 1}, {Action::kBackspace, 0,
"<-", 1}, {Action::kEnter, 0,
"Done", 1}};
182 static constexpr Key kRow4[] = {
183 {Action::kShift, 0,
"Shift", 1.5f},
184 {Action::kSpace, 0,
"", 3.5f},
185 {Action::kNewline, 0,
"Enter", 1.5f},
186 {Action::kBackspace, 0,
"<-", 1.5f},
187 {Action::kEnter, 0,
"Done", 1.5f},
192 Mode mode_ = Mode::kLetters;
200 template <
typename Visitor>
201 void forEachKey(Visitor visitor)
const {
206 const bool symbols = mode_ == Mode::kSymbols;
207 const Row numeric[] = {
208 {kNum0, 3}, {kNum1, 3}, {kNum2, 3}, {kNum3, 3}, {kNum4, 3},
210 const Row letters[] = {
211 {kRow0,
sizeof(kRow0) /
sizeof(kRow0[0])},
212 symbols ? Row{kRow1Symbols,
sizeof(kRow1Symbols) /
sizeof(kRow1Symbols[0])}
213 : Row{kRow1,
sizeof(kRow1) /
sizeof(kRow1[0])},
214 symbols ? Row{kRow2Symbols,
sizeof(kRow2Symbols) /
sizeof(kRow2Symbols[0])}
215 : Row{kRow2,
sizeof(kRow2) /
sizeof(kRow2[0])},
216 symbols ? Row{kRow3Symbols,
sizeof(kRow3Symbols) /
sizeof(kRow3Symbols[0])}
217 : Row{kRow3,
sizeof(kRow3) /
sizeof(kRow3[0])},
218 {kRow4,
sizeof(kRow4) /
sizeof(kRow4[0])},
220 const Row* rows = mode_ == Mode::kNumeric ? numeric : letters;
221 constexpr size_t kRowCount =
sizeof(letters) /
sizeof(letters[0]);
222 const int32_t rowHeight =
this->bounds.h /
static_cast<int32_t>(kRowCount);
224 for (size_t r = 0; r < kRowCount; ++r) {
225 float totalWeight = 0.0f;
226 for (size_t i = 0; i < rows[r].count; ++i) totalWeight += rows[r].keys[i].weight;
228 const int32_t rowY =
this->bounds.y +
static_cast<int32_t>(r) * rowHeight;
230 for (size_t i = 0; i < rows[r].count; ++i) {
231 const Key& key = rows[r].keys[i];
232 const int32_t x0 =
this->bounds.x +
static_cast<int32_t>(cursor / totalWeight *
this->bounds.w);
233 cursor += key.weight;
234 const int32_t x1 =
this->bounds.x +
static_cast<int32_t>(cursor / totalWeight *
this->bounds.w);
235 const Bounds rect(x0, rowY, x1 - x0, rowHeight);
236 if (visitor(key, rect))
return;
241 void handleKey(
const Key& key) {
242 switch (key.action) {
244 if (target_ !=
nullptr) {
245 target_
->insertChar(shift_ ?
static_cast<
char>(std::toupper(key.ch)) : key.ch
);
251 case Action::kBackspace:
257 case Action::kNewline:
265 if (target_ ==
nullptr || target_
->handleEnter())
this->visible =
false;
267 case Action::kToggleSymbols:
269 mode_ = (mode_ == Mode::kLetters) ? Mode::kSymbols : Mode::kLetters;
On-screen QWERTY keyboard that drives one text input at a time.
Definition: Keyboard.h:44
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Keyboard.h:101
void manage(Field &field)
Definition: Keyboard.h:56
Keyboard(Bounds bounds)
Definition: Keyboard.h:47
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Keyboard.h:72
void hide()
Definition: Keyboard.h:70
Definition: TextInputTarget.h:22
virtual bool handleEnter()=0
virtual void insertChar(char c)=0
virtual void backspace()=0
Definition: AnchorLayout.h:4
InputType
Minimal interface a Keyboard drives.
Definition: TextInputTarget.h:20
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t centerY() const
Definition: Bounds.h:33