Skip to content

Commit 0b500d3

Browse files
committed
UI: experimental keypad to replace android keypad
1 parent cfefaf9 commit 0b500d3

File tree

5 files changed

+67
-40
lines changed

5 files changed

+67
-40
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dnl This program is distributed under the terms of the GPL v2.0
77
dnl Download the GNU Public License (GPL) from www.gnu.org
88
dnl
99

10-
AC_INIT([smallbasic], [12.29])
10+
AC_INIT([smallbasic], [12.30])
1111
AC_CONFIG_SRCDIR([configure.ac])
1212

1313
AC_CANONICAL_TARGET

src/platform/android/jni/editor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ void Runtime::editSource(strlib::String loadPath, bool restoreOnExit) {
160160
_output->addInput(_keypad);
161161
}
162162

163-
// to layout inputs
163+
statusMessage.update(editWidget, _output);
164+
165+
// layout inputs and redraw
164166
_output->resize(w, h);
165167

166168
if (gsb_last_line && isBreak()) {
@@ -173,9 +175,7 @@ void Runtime::editSource(strlib::String loadPath, bool restoreOnExit) {
173175
alert(gsb_last_errmsg);
174176
}
175177

176-
statusMessage.update(editWidget, _output);
177178
_srcRendered = false;
178-
_output->redraw();
179179
_state = kEditState;
180180

181181
while (_state == kEditState) {

src/ui/keypad.cpp

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ constexpr int QWERTY_ROW = 1;
2121
constexpr int ASDF_ROW = 2;
2222
constexpr int SPACE_COLS = 3;
2323
constexpr int IMAGE_SIZE = 30;
24-
constexpr int MIN_DENSITY = 280;
2524
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;
2734

2835
// https://materialui.co/colors
2936
KeypadTheme MODERN_DARK_THEME = {
@@ -113,24 +120,31 @@ KeypadImage::KeypadImage() : ImageCodec() {
113120
void 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-
196190
KeyCode 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

307327
bool 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

314334
void 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

451473
int 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

src/ui/keypad.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ struct RawKey {
5050
};
5151

5252
struct KeypadDrawContext {
53-
explicit KeypadDrawContext(int charWidth, int charHeight, int padding);
53+
explicit KeypadDrawContext(int charWidth, int charHeight);
5454
const KeypadImage *getImage(const RawKey &keycode) const;
5555
KeyCode getKey(RawKey rawKey) const;
56+
void layoutHeight(int padding);
5657
void toggle();
5758

5859
int _charWidth;
5960
int _charHeight;
61+
int _imageSize;
6062
Keyset _keySet;
6163

6264
KeypadImage _cutImage;
@@ -89,6 +91,8 @@ struct Key {
8991
int _y{};
9092
int _w{};
9193
int _h{};
94+
int _xEnd{};
95+
int _yEnd{};
9296
RawKey _key;
9397
bool _printable;
9498
};

src/ui/system.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
#define FONT_SCALE_INTERVAL 10
6161
#define FONT_MIN 80
62-
#define FONT_MAX 200
62+
#define FONT_MAX 160
6363

6464
#define OPTIONS_BOX_WIDTH_EXTRA 1
6565
#define OPTIONS_BOX_BG 0xd2d1d0

0 commit comments

Comments
 (0)