@@ -23,6 +23,7 @@ constexpr int SPACE_COLS = 3;
2323constexpr int IMAGE_SIZE = 30 ;
2424constexpr int MIN_DENSITY = 280 ;
2525constexpr double PI = 3.14159 ;
26+ constexpr double PADDING_FACTOR = 0.95 ;
2627
2728// https://materialui.co/colors
2829KeypadTheme MODERN_DARK_THEME = {
@@ -128,7 +129,7 @@ KeypadDrawContext::KeypadDrawContext(int charWidth, int charHeight, int padding)
128129 _charWidth(charWidth),
129130 _charHeight(charHeight),
130131 _keySet(kLower ) {
131- const int imageSize = static_cast <int >(MAX (padding * 1.5 , charHeight) * 1.2 );
132+ const int imageSize = static_cast <int >(MAX (padding * 1.4 , charHeight) * 1.2 );
132133
133134 if (!_cutImage.decode (img_cut, img_cut_len) ||
134135 !_copyImage.decode (img_copy, img_copy_len) ||
@@ -370,23 +371,19 @@ void Key::onClick(const KeypadDrawContext *context) const {
370371//
371372// Keypad
372373//
373- Keypad::Keypad (int charWidth, int charHeight, bool toolbar, int density )
374+ Keypad::Keypad (int charWidth, int charHeight, bool toolbar)
374375 : _posX(0 ),
375376 _posY(0 ),
376377 _width(0 ),
377378 _height(0 ),
378- _padding(density < MIN_DENSITY ? PADDING : (PADDING * 2 )),
379+ _padding(static_cast < int >(charHeight * PADDING_FACTOR )),
379380 _theme(&MODERN_DARK_THEME),
380381 _context(charWidth, charHeight, _padding),
381382 _toolbar(toolbar),
382383 _pressed(nullptr ) {
383384 generateKeys ();
384385}
385386
386- int Keypad::outerHeight (int charHeight) const {
387- return (_toolbar ? 1 : MAX_ROWS) * ((_padding * 2 ) + charHeight);
388- }
389-
390387void Keypad::generateKeys () {
391388 _keys.clear ();
392389
@@ -451,15 +448,31 @@ void Keypad::layout(int x, int y, int w, int h) {
451448 }
452449}
453450
451+ int Keypad::layoutHeight (int screenHeight) {
452+ int charHeight = _context._charHeight ;
453+ int maxHeight = static_cast <int >(screenHeight * 0.45 );
454+ int padding = static_cast <int >(charHeight * PADDING_FACTOR);
455+ int rows = _toolbar ? 1 : MAX_ROWS;
456+ int height = rows * ((padding * 2 ) + charHeight);
457+ if (height > maxHeight) {
458+ // h = r(ch + 2p) -> p = (h - r * ch) / (2 * r)
459+ height = maxHeight;
460+ padding = ((height - (rows * charHeight)) / (rows * 2 ));
461+ }
462+ _padding = padding;
463+ return height;
464+ }
465+
454466void Keypad::clicked (int x, int y, bool pressed) {
467+ Key *down = _pressed;
455468 _pressed = nullptr ;
456469 for (const auto key : _keys) {
457470 if (key->inside (x, y)) {
458471 if (pressed) {
459472 _pressed = key;
460473 } else if (key->_key ._lower == K_TOGGLE) {
461474 _context.toggle ();
462- } else {
475+ } else if (key == down) {
463476 key->onClick (&_context);
464477 }
465478 break ;
@@ -478,11 +491,10 @@ void Keypad::draw() const {
478491//
479492// KeypadInput
480493//
481- KeypadInput::KeypadInput (bool floatTop, bool toolbar, int charWidth, int charHeight, int density ) :
494+ KeypadInput::KeypadInput (bool floatTop, bool toolbar, int charWidth, int charHeight) :
482495 FormInput(0 , 0 , 0 , charHeight * 2 ),
483496 _floatTop(floatTop) {
484- _keypad = new Keypad (charWidth, charHeight, toolbar, density);
485- _height = _keypad->outerHeight (charHeight);
497+ _keypad = new Keypad (charWidth, charHeight, toolbar);
486498}
487499
488500KeypadInput::~KeypadInput () {
@@ -504,3 +516,8 @@ void KeypadInput::layout(int x, int y, int w, int h) {
504516 _width = w;
505517 _keypad->layout (_x, _y, _width, _height);
506518}
519+
520+ int KeypadInput::layoutHeight (int screenHeight) {
521+ _height = _keypad->layoutHeight (screenHeight);
522+ return _height;
523+ }
0 commit comments