@@ -21,9 +21,16 @@ constexpr int QWERTY_ROW = 1;
21
21
constexpr int ASDF_ROW = 2 ;
22
22
constexpr int SPACE_COLS = 3 ;
23
23
constexpr int IMAGE_SIZE = 30 ;
24
- constexpr int MIN_DENSITY = 280 ;
25
24
constexpr double PI = 3.14159 ;
26
- constexpr double PADDING_FACTOR = 0.95 ;
25
+
26
+ // padding size based on character height
27
+ constexpr double PADDING_FACTOR = 1.1 ;
28
+
29
+ // maximum keyboard height as based on screen height
30
+ constexpr double MAX_HEIGHT_FACTOR = 0.48 ;
31
+
32
+ // image height based on available rectangle
33
+ constexpr double IMAGE_SIZE_FACTOR = 0.65 ;
27
34
28
35
// https://materialui.co/colors
29
36
KeypadTheme MODERN_DARK_THEME = {
@@ -113,24 +120,31 @@ KeypadImage::KeypadImage() : ImageCodec() {
113
120
void KeypadImage::draw (int x, int y, int w, int h) const {
114
121
MAPoint2d dstPoint;
115
122
MARect srcRect;
116
- dstPoint.x = x + (w - _width) / 2 ;
123
+ // buttons become narrow with larger font sizes
124
+ int width = MIN (w, _width);
125
+ dstPoint.x = x + (w - width) / 2 ;
117
126
dstPoint.y = y + (h - _height) / 2 ;
127
+ if (dstPoint.x < 0 ) {
128
+ dstPoint.x = x;
129
+ }
130
+ if (dstPoint.y < 0 ) {
131
+ dstPoint.y = y;
132
+ }
118
133
srcRect.left = 0 ;
119
134
srcRect.top = 0 ;
120
- srcRect.width = _width ;
135
+ srcRect.width = width ;
121
136
srcRect.height = _height;
122
137
maDrawRGB (&dstPoint, _pixels, &srcRect, 0 , _width);
123
138
}
124
139
125
140
//
126
141
// KeypadDrawContext
127
142
//
128
- KeypadDrawContext::KeypadDrawContext (int charWidth, int charHeight, int padding ) :
143
+ KeypadDrawContext::KeypadDrawContext (int charWidth, int charHeight) :
129
144
_charWidth(charWidth),
130
145
_charHeight(charHeight),
146
+ _imageSize(IMAGE_SIZE),
131
147
_keySet(kLower ) {
132
- const int imageSize = static_cast <int >(MAX (padding * 1.4 , charHeight) * 1.2 );
133
-
134
148
if (!_cutImage.decode (img_cut, img_cut_len) ||
135
149
!_copyImage.decode (img_copy, img_copy_len) ||
136
150
!_pasteImage.decode (img_clipboard_paste, img_clipboard_paste_len) ||
@@ -147,22 +161,6 @@ KeypadDrawContext::KeypadDrawContext(int charWidth, int charHeight, int padding)
147
161
!_tagImage.decode (img_tag, img_tag_len) ||
148
162
!_toggleImage.decode (img_layers, img_layers_len)) {
149
163
deviceLog (" %s" , _cutImage.getLastError ());
150
- } else if (imageSize < IMAGE_SIZE - 2 || imageSize > IMAGE_SIZE + 2 ) {
151
- _cutImage.resize (imageSize, imageSize);
152
- _copyImage.resize (imageSize, imageSize);
153
- _pasteImage.resize (imageSize, imageSize);
154
- _saveImage.resize (imageSize, imageSize);
155
- _runImage.resize (imageSize, imageSize);
156
- _helpImage.resize (imageSize, imageSize);
157
- _backImage.resize (imageSize, imageSize);
158
- _enterImage.resize (imageSize, imageSize);
159
- _searchImage.resize (imageSize, imageSize);
160
- _lineUpImage.resize (imageSize, imageSize);
161
- _pageUpImage.resize (imageSize, imageSize);
162
- _lineDownImage.resize (imageSize, imageSize);
163
- _pageDownImage.resize (imageSize, imageSize);
164
- _toggleImage.resize (imageSize, imageSize);
165
- _tagImage.resize (imageSize, imageSize);
166
164
}
167
165
}
168
166
@@ -189,10 +187,6 @@ const KeypadImage *KeypadDrawContext::getImage(const RawKey &key) const {
189
187
return result;
190
188
}
191
189
192
- void KeypadDrawContext::toggle () {
193
- _keySet = static_cast <Keyset>((_keySet + 1 ) % kSize );
194
- }
195
-
196
190
KeyCode KeypadDrawContext::getKey (RawKey key) const {
197
191
KeyCode keyCode;
198
192
switch (_keySet) {
@@ -205,6 +199,32 @@ KeyCode KeypadDrawContext::getKey(RawKey key) const {
205
199
return keyCode;
206
200
}
207
201
202
+ void KeypadDrawContext::layoutHeight (int padding) {
203
+ const int imageSize = static_cast <int >((padding * 2 + _charHeight) * IMAGE_SIZE_FACTOR);
204
+ if (imageSize < _imageSize - 2 || imageSize > _imageSize + 2 ) {
205
+ _cutImage.resize (imageSize, imageSize);
206
+ _copyImage.resize (imageSize, imageSize);
207
+ _pasteImage.resize (imageSize, imageSize);
208
+ _saveImage.resize (imageSize, imageSize);
209
+ _runImage.resize (imageSize, imageSize);
210
+ _helpImage.resize (imageSize, imageSize);
211
+ _backImage.resize (imageSize, imageSize);
212
+ _enterImage.resize (imageSize, imageSize);
213
+ _searchImage.resize (imageSize, imageSize);
214
+ _lineUpImage.resize (imageSize, imageSize);
215
+ _pageUpImage.resize (imageSize, imageSize);
216
+ _lineDownImage.resize (imageSize, imageSize);
217
+ _pageDownImage.resize (imageSize, imageSize);
218
+ _toggleImage.resize (imageSize, imageSize);
219
+ _tagImage.resize (imageSize, imageSize);
220
+ _imageSize = imageSize;
221
+ }
222
+ }
223
+
224
+ void KeypadDrawContext::toggle () {
225
+ _keySet = static_cast <Keyset>((_keySet + 1 ) % kSize );
226
+ }
227
+
208
228
//
209
229
// Key
210
230
//
@@ -306,9 +326,9 @@ void Key::draw(const KeypadTheme *theme, const KeypadDrawContext *context, bool
306
326
307
327
bool Key::inside (int x, int y) const {
308
328
return (x >= _x &&
309
- x <= _x + _w &&
329
+ x <= _xEnd &&
310
330
y >= _y &&
311
- y <= _y + _h );
331
+ y <= _yEnd );
312
332
}
313
333
314
334
void Key::onClick (const KeypadDrawContext *context) const {
@@ -378,7 +398,7 @@ Keypad::Keypad(int charWidth, int charHeight, bool toolbar)
378
398
_height(0 ),
379
399
_padding(static_cast <int >(charHeight * PADDING_FACTOR)),
380
400
_theme(&MODERN_DARK_THEME),
381
- _context(charWidth, charHeight, _padding ),
401
+ _context(charWidth, charHeight),
382
402
_toolbar(toolbar),
383
403
_pressed(nullptr ) {
384
404
generateKeys ();
@@ -442,6 +462,8 @@ void Keypad::layout(int x, int y, int w, int h) {
442
462
key->_y = yPos;
443
463
key->_w = keyWidth;
444
464
key->_h = keyH;
465
+ key->_xEnd = key->_x + key->_w ;
466
+ key->_yEnd = key->_y + key->_h ;
445
467
xPos += keyWidth;
446
468
}
447
469
yPos += keyH;
@@ -450,16 +472,17 @@ void Keypad::layout(int x, int y, int w, int h) {
450
472
451
473
int Keypad::layoutHeight (int screenHeight) {
452
474
int charHeight = _context._charHeight ;
453
- int maxHeight = static_cast <int >(screenHeight * 0.45 );
475
+ int maxHeight = static_cast <int >(screenHeight * MAX_HEIGHT_FACTOR );
454
476
int padding = static_cast <int >(charHeight * PADDING_FACTOR);
455
477
int rows = _toolbar ? 1 : MAX_ROWS;
456
478
int height = rows * ((padding * 2 ) + charHeight);
457
479
if (height > maxHeight) {
458
- // h = r(ch + 2p) -> p = (h - r * ch) / (2 * r )
459
- height = maxHeight;
460
- padding = ((height - ( rows * charHeight)) / (rows * 2 ));
480
+ // h = r(ch + 2p) -> p = (h - r * ch) / (r * 2 )
481
+ padding = (( maxHeight - (rows * charHeight)) / (rows * 2 )) ;
482
+ height = rows * ((padding * 2 ) + charHeight );
461
483
}
462
484
_padding = padding;
485
+ _context.layoutHeight (_padding);
463
486
return height;
464
487
}
465
488
0 commit comments