10
10
#include " common/sbapp.h"
11
11
#include " common/keymap.h"
12
12
#include " lib/maapi.h"
13
+ #include " ui/image_codec.h"
14
+ #include " ui/keypad-icons.h"
13
15
#include " keypad.h"
14
16
15
17
constexpr char cutIcon[] = {' \346 ' , ' \0 ' };
@@ -95,6 +97,41 @@ constexpr int rowCharLengths[][5] = {
95
97
{6 , 10 , 8 , 14 , 0 }, // symbols
96
98
};
97
99
100
+ //
101
+ // KeypadDrawContext
102
+ //
103
+ KeypadDrawContext::KeypadDrawContext (int charWidth, int charHeight) :
104
+ _charWidth(charWidth),
105
+ _charHeight(charHeight),
106
+ _shiftActive(false ),
107
+ _capsLockActive(false ) {
108
+ if (img_decode (img_cut, img_cut_len, &_cutImage) != 0 ||
109
+ img_decode (img_copy, img_copy_len, &_copyImage) != 0 ||
110
+ img_decode (img_clipboard_paste, img_clipboard_paste_len, &_pasteImage) != 0 ||
111
+ img_decode (img_save, img_save_len, &_saveImage) != 0 ||
112
+ img_decode (img_bug_play, img_bug_play_len, &_runImage) != 0 ||
113
+ img_decode (img_book_info_2, img_book_info_2_len, &_helpImage) != 0 ||
114
+ img_decode (img_backspace, img_backspace_len, &_backImage) != 0 ||
115
+ img_decode (img_arrow_enter, img_arrow_enter_len, &_enterImage) != 0 ) {
116
+ deviceLog (" Failed to load image [%s]\n " , img_get_last_error ());
117
+ }
118
+ }
119
+
120
+ void KeypadDrawContext::toggleShift () {
121
+ _shiftActive = !_shiftActive;
122
+ }
123
+
124
+ bool KeypadDrawContext::useShift (bool specialKey) {
125
+ bool useShift = _shiftActive ^ _capsLockActive;
126
+ if (_shiftActive && !specialKey) {
127
+ _shiftActive = false ;
128
+ }
129
+ return useShift;
130
+ }
131
+
132
+ //
133
+ // Key
134
+ //
98
135
Key::Key (const RawKey &k) :
99
136
_label(k._normal),
100
137
_altLabel(k._shifted) {
@@ -118,7 +155,19 @@ int Key::color(const KeypadTheme *theme, bool shiftActive) const {
118
155
return result;
119
156
}
120
157
121
- void Key::drawButton (const KeypadTheme *theme) const {
158
+ void Key::drawImage (const ImageData *image) const {
159
+ MAPoint2d dstPoint;
160
+ MARect srcRect;
161
+ dstPoint.x = _x;
162
+ dstPoint.y = _y;
163
+ srcRect.left = 0 ;
164
+ srcRect.top = 0 ;
165
+ srcRect.width = image->_width ;
166
+ srcRect.height = image->_height ;
167
+ maDrawRGB (&dstPoint, image->_pixels , &srcRect, 0 , image->_width );
168
+ }
169
+
170
+ void Key::draw (const KeypadTheme *theme, const KeypadDrawContext *context) const {
122
171
int rc = 5 ;
123
172
int pad = 2 ;
124
173
int rx = _x + _w - pad; // right x
@@ -151,6 +200,26 @@ void Key::drawButton(const KeypadTheme *theme) const {
151
200
maArc (xcR, ycT, rc, PI * 3 / 2 , 0 , aspect); // Top-right corner
152
201
maArc (xcR, ycB, rc, 0 , PI / 2 , aspect); // Bottom-right corner
153
202
maArc (xcL, ycB, rc, PI / 2 , PI, aspect); // Bottom-left corner
203
+
204
+ String label;
205
+ if (_special) {
206
+ label = _label;
207
+ } else {
208
+ bool useShift = context->_shiftActive ^ context->_capsLockActive ;
209
+ label = useShift ? _altLabel : _label;
210
+ }
211
+
212
+ int xOffset = (_w - (context->_charWidth * _labelLength)) / 2 ;
213
+ int yOffset = (_h - context->_charHeight ) / 2 ;
214
+ int textX = _x + xOffset;
215
+ int textY = _y + yOffset;
216
+ maSetColor (color (theme, context->_shiftActive ));
217
+
218
+ if (_special && _labelLength == 1 ) {
219
+ drawImage (&context->_enterImage );
220
+ } else {
221
+ maDrawText (textX, textY, label.c_str (), _labelLength);
222
+ }
154
223
}
155
224
156
225
bool Key::inside (int x, int y) const {
@@ -210,10 +279,7 @@ Keypad::Keypad(int charWidth, int charHeight)
210
279
_posY(0 ),
211
280
_width(0 ),
212
281
_height(0 ),
213
- _charWidth(charWidth),
214
- _charHeight(charHeight),
215
- _shiftActive(false ),
216
- _capsLockActive(false ),
282
+ _context(charWidth, charHeight),
217
283
_theme(&modernDarkTheme),
218
284
_currentLayout(LayoutLetters) {
219
285
generateKeys ();
@@ -256,17 +322,20 @@ void Keypad::layout(int x, int y, int w, int h) {
256
322
_width = w;
257
323
_height = h;
258
324
325
+ const int charWidth = _context._charWidth ;
326
+ const int charHeight = _context._charHeight ;
327
+
259
328
// start with optimum padding, then reduce to fit width
260
329
int padding = defaultPadding;
261
- int width = maxCols * (_charWidth + (padding * 2 ));
330
+ int width = maxCols * (charWidth + (padding * 2 ));
262
331
263
332
while (width > w && padding > 0 ) {
264
333
padding--;
265
- width = maxCols * (_charWidth + (padding * 2 ));
334
+ width = maxCols * (charWidth + (padding * 2 ));
266
335
}
267
336
268
- int keyW = _charWidth + (padding * 2 );
269
- int keyH = _charHeight + (padding * 2 );
337
+ int keyW = charWidth + (padding * 2 );
338
+ int keyH = charHeight + (padding * 2 );
270
339
int xStart = _posX + ((w - width) / 2 );
271
340
int yPos = _posY;
272
341
int index = 0 ;
@@ -277,7 +346,7 @@ void Keypad::layout(int x, int y, int w, int h) {
277
346
int xPos = xStart;
278
347
if (cols < maxCols) {
279
348
// center narrow row
280
- int rowWidth = (chars * _charWidth ) + (cols * padding * 2 );
349
+ int rowWidth = (chars * charWidth ) + (cols * padding * 2 );
281
350
if (rowWidth > width) {
282
351
xPos -= (rowWidth - width) / 2 ;
283
352
} else {
@@ -292,7 +361,7 @@ void Keypad::layout(int x, int y, int w, int h) {
292
361
int length = key->_labelLength ;
293
362
int keyWidth = keyW;
294
363
if (length > 1 ) {
295
- keyWidth = (length * _charWidth ) + (padding * 2 );
364
+ keyWidth = (length * charWidth ) + (padding * 2 );
296
365
}
297
366
key->_x = xPos;
298
367
key->_y = yPos;
@@ -311,49 +380,25 @@ void Keypad::clicked(int x, int y, bool pressed) {
311
380
312
381
if (pressed && inside) {
313
382
if (key->_label .equals (shiftKey)) {
314
- toggleShift ();
383
+ _context. toggleShift ();
315
384
} else if (key->_label .equals (toggleKey)) {
316
385
_currentLayout = static_cast <KeypadLayout>((_currentLayout + 1 ) % 3 );
317
386
generateKeys ();
318
387
layout (_posX, _posY, _width, _height);
319
388
break ;
320
389
} else {
321
- bool useShift = _shiftActive ^ _capsLockActive;
322
- if (_shiftActive && !key->_special ) {
323
- _shiftActive = false ;
324
- }
325
- key->onClick (useShift);
390
+ key->onClick (_context.useShift (key->_special ));
326
391
}
327
392
break ;
328
393
}
329
394
}
330
395
}
331
396
332
- void Keypad::toggleShift () {
333
- _shiftActive = !_shiftActive;
334
- }
335
-
336
397
void Keypad::draw () const {
337
398
maSetColor (_theme->_bg );
338
399
maFillRect (_posX, _posY, _width, _height);
339
400
for (const auto &key : _keys) {
340
- key->drawButton (_theme);
341
-
342
- String label;
343
- if (key->_special ) {
344
- label = key->_label ;
345
- } else {
346
- bool useShift = _shiftActive ^ _capsLockActive;
347
- label = useShift ? key->_altLabel : key->_label ;
348
- }
349
-
350
- int labelLength = key->_labelLength ;
351
- int xOffset = (key->_w - (_charWidth * labelLength)) / 2 ;
352
- int yOffset = (key->_h - _charHeight) / 2 ;
353
- int textX = key->_x + xOffset;
354
- int textY = key->_y + yOffset;
355
- maSetColor (key->color (_theme, _shiftActive));
356
- maDrawText (textX, textY, label.c_str (), labelLength);
401
+ key->draw (_theme, &_context);
357
402
}
358
403
}
359
404
0 commit comments