@@ -21,9 +21,16 @@ constexpr int QWERTY_ROW = 1;
2121constexpr int ASDF_ROW = 2 ;
2222constexpr int SPACE_COLS = 3 ;
2323constexpr int IMAGE_SIZE = 30 ;
24- constexpr int MIN_DENSITY = 280 ;
2524constexpr 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 ;
2734
2835// https://materialui.co/colors
2936KeypadTheme MODERN_DARK_THEME = {
@@ -113,24 +120,31 @@ KeypadImage::KeypadImage() : ImageCodec() {
113120void KeypadImage::draw (int x, int y, int w, int h) const {
114121 MAPoint2d dstPoint;
115122 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 ;
117126 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+ }
118133 srcRect.left = 0 ;
119134 srcRect.top = 0 ;
120- srcRect.width = _width ;
135+ srcRect.width = width ;
121136 srcRect.height = _height;
122137 maDrawRGB (&dstPoint, _pixels, &srcRect, 0 , _width);
123138}
124139
125140//
126141// KeypadDrawContext
127142//
128- KeypadDrawContext::KeypadDrawContext (int charWidth, int charHeight, int padding ) :
143+ KeypadDrawContext::KeypadDrawContext (int charWidth, int charHeight) :
129144 _charWidth(charWidth),
130145 _charHeight(charHeight),
146+ _imageSize(IMAGE_SIZE),
131147 _keySet(kLower ) {
132- const int imageSize = static_cast <int >(MAX (padding * 1.4 , charHeight) * 1.2 );
133-
134148 if (!_cutImage.decode (img_cut, img_cut_len) ||
135149 !_copyImage.decode (img_copy, img_copy_len) ||
136150 !_pasteImage.decode (img_clipboard_paste, img_clipboard_paste_len) ||
@@ -147,22 +161,6 @@ KeypadDrawContext::KeypadDrawContext(int charWidth, int charHeight, int padding)
147161 !_tagImage.decode (img_tag, img_tag_len) ||
148162 !_toggleImage.decode (img_layers, img_layers_len)) {
149163 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);
166164 }
167165}
168166
@@ -189,10 +187,6 @@ const KeypadImage *KeypadDrawContext::getImage(const RawKey &key) const {
189187 return result;
190188}
191189
192- void KeypadDrawContext::toggle () {
193- _keySet = static_cast <Keyset>((_keySet + 1 ) % kSize );
194- }
195-
196190KeyCode KeypadDrawContext::getKey (RawKey key) const {
197191 KeyCode keyCode;
198192 switch (_keySet) {
@@ -205,6 +199,32 @@ KeyCode KeypadDrawContext::getKey(RawKey key) const {
205199 return keyCode;
206200}
207201
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+
208228//
209229// Key
210230//
@@ -306,9 +326,9 @@ void Key::draw(const KeypadTheme *theme, const KeypadDrawContext *context, bool
306326
307327bool Key::inside (int x, int y) const {
308328 return (x >= _x &&
309- x <= _x + _w &&
329+ x <= _xEnd &&
310330 y >= _y &&
311- y <= _y + _h );
331+ y <= _yEnd );
312332}
313333
314334void Key::onClick (const KeypadDrawContext *context) const {
@@ -378,7 +398,7 @@ Keypad::Keypad(int charWidth, int charHeight, bool toolbar)
378398 _height(0 ),
379399 _padding(static_cast <int >(charHeight * PADDING_FACTOR)),
380400 _theme(&MODERN_DARK_THEME),
381- _context(charWidth, charHeight, _padding ),
401+ _context(charWidth, charHeight),
382402 _toolbar(toolbar),
383403 _pressed(nullptr ) {
384404 generateKeys ();
@@ -442,6 +462,8 @@ void Keypad::layout(int x, int y, int w, int h) {
442462 key->_y = yPos;
443463 key->_w = keyWidth;
444464 key->_h = keyH;
465+ key->_xEnd = key->_x + key->_w ;
466+ key->_yEnd = key->_y + key->_h ;
445467 xPos += keyWidth;
446468 }
447469 yPos += keyH;
@@ -450,16 +472,17 @@ void Keypad::layout(int x, int y, int w, int h) {
450472
451473int Keypad::layoutHeight (int screenHeight) {
452474 int charHeight = _context._charHeight ;
453- int maxHeight = static_cast <int >(screenHeight * 0.45 );
475+ int maxHeight = static_cast <int >(screenHeight * MAX_HEIGHT_FACTOR );
454476 int padding = static_cast <int >(charHeight * PADDING_FACTOR);
455477 int rows = _toolbar ? 1 : MAX_ROWS;
456478 int height = rows * ((padding * 2 ) + charHeight);
457479 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 );
461483 }
462484 _padding = padding;
485+ _context.layoutHeight (_padding);
463486 return height;
464487}
465488
0 commit comments