Skip to content

Commit fc1d8b7

Browse files
committed
UI: experimental keypad to replace android keypad
1 parent 12230e6 commit fc1d8b7

File tree

5 files changed

+43
-70
lines changed

5 files changed

+43
-70
lines changed

src/platform/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
mavenCentral()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:8.10.0'
8+
classpath 'com.android.tools.build:gradle:8.11.0'
99
}
1010
}
1111

src/platform/android/jni/Android.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ LOCAL_SRC_FILES := main.cpp \
4747
../../../ui/image.cpp \
4848
../../../ui/inputs.cpp \
4949
../../../ui/textedit.cpp \
50+
../../../ui/keypad.cpp \
51+
../../../ui/image_codec.cpp\
5052
../../../ui/strlib.cpp \
5153
../../../ui/graphics.cpp \
5254
../../../ui/system.cpp

src/platform/android/jni/editor.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "ui/textedit.h"
1212
#include "platform/android/jni/runtime.h"
1313
#include "common/device.h"
14+
#include "ui/keypad.h"
1415

1516
extern Runtime *runtime;
1617

@@ -59,6 +60,10 @@ void System::editSource(strlib::String loadPath, bool restoreOnExit) {
5960
_output->clearScreen();
6061
_output->addInput(editWidget);
6162
_output->addInput(helpWidget);
63+
_output->addInput(new KeypadInput(false, false, charWidth, charHeight));
64+
65+
// to layout inputs
66+
_output->resize(w, h);
6267

6368
if (gsb_last_line && isBreak()) {
6469
String msg = "Break at line: ";
@@ -75,7 +80,6 @@ void System::editSource(strlib::String loadPath, bool restoreOnExit) {
7580
_output->setStatus(showStatus ? cleanFile : "");
7681
_output->redraw();
7782
_state = kEditState;
78-
runtime->showKeypad(true);
7983

8084
while (_state == kEditState) {
8185
MAEvent event = getNextEvent();
@@ -135,10 +139,10 @@ void System::editSource(strlib::String loadPath, bool restoreOnExit) {
135139
helpWidget->createKeywordIndex();
136140
helpWidget->showPopup(-4, -2);
137141
helpWidget->setFocus(true);
138-
runtime->showKeypad(false);
139142
showStatus = false;
140143
break;
141144
case SB_KEY_F(9):
145+
case SB_KEY_CTRL('r'):
142146
_state = kRunState;
143147
if (editWidget->isDirty()) {
144148
saveFile(editWidget, loadPath);
@@ -171,7 +175,6 @@ void System::editSource(strlib::String loadPath, bool restoreOnExit) {
171175
_output->redraw();
172176
_state = kActiveState;
173177
waitForBack();
174-
runtime->showKeypad(true);
175178
_output->selectScreen(SOURCE_SCREEN);
176179
_state = kEditState;
177180
break;
@@ -189,7 +192,6 @@ void System::editSource(strlib::String loadPath, bool restoreOnExit) {
189192
}
190193

191194
if (isBack() && widget == helpWidget) {
192-
runtime->showKeypad(true);
193195
widget = editWidget;
194196
helpWidget->hide();
195197
editWidget->setFocus(true);
@@ -220,7 +222,6 @@ void System::editSource(strlib::String loadPath, bool restoreOnExit) {
220222
if (!_output->removeInput(editWidget)) {
221223
trace("Failed to remove editor input");
222224
}
223-
runtime->showKeypad(false);
224225
_editor = editWidget;
225226
_editor->setFocus(false);
226227
} else {

src/ui/keypad.cpp

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
#include "ui/keypad_icons.h"
1515
#include "keypad.h"
1616

17+
constexpr int ROW_LENGTHS[] = {6, 10, 9, 9, 7};
1718
constexpr int MAX_ROWS = 5;
1819
constexpr int MAX_COLS = 10;
19-
constexpr int NARROW_ROW = 2;
20+
constexpr int QWERTY_ROW = 1;
21+
constexpr int ASDF_ROW = 2;
2022
constexpr int SPACE_COLS = 3;
21-
constexpr int DEFAULT_PADDING = 24;
23+
constexpr int PADDING = 16;
2224
constexpr double PI = 3.14159;
2325

2426
KeypadTheme MODERN_DARK_THEME = {
@@ -27,7 +29,7 @@ KeypadTheme MODERN_DARK_THEME = {
2729
._keyHighlight = 0x333333, // Slightly lighter for key press
2830
._text = 0xe0e0e0, // Soft white text
2931
._outline = 0x2a2a2a, // Subtle key outlines
30-
._funcKeyBg = 0x9f7aea, // Soft faint purple (Material Purple 300~400)
32+
._funcKeyBg = 0x59422c, // Soft faint purple (Material Purple 300~400)
3133
._funcKeyHighlight = 0xd1c4e9, // Pale lavender for highlight effect
3234
};
3335

@@ -36,20 +38,15 @@ constexpr RawKey LETTERS[][MAX_COLS] = {
3638
{{K_q, K_Q}, {K_w, K_W}, {K_e, K_E}, {K_r, K_R}, {K_t, K_T}, {K_y, K_Y}, {K_u, K_U}, {K_i, K_I}, {K_o, K_O}, {K_p, K_P}},
3739
{{K_a, K_A}, {K_s, K_S}, {K_d, K_D}, {K_f, K_F}, {K_g, K_G}, {K_h, K_H}, {K_j, K_J}, {K_k, K_K}, {K_l, K_L}},
3840
{{K_SHIFT, K_SHIFT}, {K_z, K_Z}, {K_x, K_X}, {K_c, K_C}, {K_v, K_V}, {K_b, K_B}, {K_n, K_N}, {K_m, K_M}, {K_BACKSPACE, K_BACKSPACE}},
39-
{{K_TOGGLE, K_TOGGLE}, {K_LBRACKET, K_LPAREN}, {K_SPACE, K_SPACE}, {K_RBRACKET, K_RPAREN}, {K_ENTER, K_ENTER}}
41+
{{K_TOGGLE, K_TOGGLE}, {K_LPAREN, K_LBRACKET}, {K_SPACE, K_SPACE}, {K_RPAREN, K_RBRACKET}, {K_ENTER, K_ENTER}}
4042
};
4143

4244
constexpr RawKey SYMBOLS[][MAX_COLS] = {
4345
{{K_CUT, K_CUT}, {K_COPY, K_COPY}, {K_PASTE, K_PASTE}, {K_SAVE, K_SAVE}, {K_RUN, K_RUN}, {K_HELP, K_HELP}},
4446
{{K_1, K_EXCLAIM}, {K_2, K_AT}, {K_3, K_HASH}, {K_4, K_DOLLAR}, {K_5, K_PERCENT}, {K_6, K_CARET}, {K_7, K_AMPERSAND}, {K_8, K_ASTERISK}, {K_9, K_LPAREN}, {K_0, K_RPAREN}},
4547
{{K_BACKTICK, K_TILDE}, {K_MINUS, K_UNDERSCORE}, {K_EQUALS, K_PLUS}, {K_LBRACKET, K_LBRACE}, {K_RBRACKET, K_RBRACE}, {K_BACKSLASH, K_PIPE}, {K_SEMICOLON, K_COLON}, {K_APOSTROPHE, K_QUOTE}, {K_HASH, K_EXT1}},
4648
{{K_SHIFT, K_SHIFT}, {K_LESS, K_COMMA}, {K_GREATER, K_PERIOD}, {K_QUESTION, K_SLASH}, {K_PLUS, K_EXT2}, {K_ASTERISK, K_EXT3}, {K_LPAREN, K_EXT4}, {K_RPAREN, K_EXT5}, {K_BACKSPACE, K_BACKSPACE}},
47-
{{K_TOGGLE, K_TOGGLE}, {K_LBRACKET, K_LPAREN}, {K_SPACE, K_SPACE}, {K_RBRACKET, K_RPAREN}, {K_ENTER, K_ENTER}}
48-
};
49-
50-
constexpr int ROW_LENGTHS[][5] = {
51-
{6, 10, 9, 9, 7}, // letters
52-
{6, 10, 9, 9, 7}, // symbols
49+
{{K_TOGGLE, K_TOGGLE}, {K_LPAREN, K_LBRACKET}, {K_SPACE, K_SPACE}, {K_RPAREN, K_RBRACKET}, {K_ENTER, K_ENTER}}
5350
};
5451

5552
//
@@ -77,17 +74,7 @@ KeypadDrawContext::KeypadDrawContext(int charWidth, int charHeight) :
7774
_charWidth(charWidth),
7875
_charHeight(charHeight),
7976
_shiftActive(false),
80-
_capsLockActive(false),
81-
_cutImage(),
82-
_copyImage(),
83-
_pasteImage(),
84-
_saveImage(),
85-
_runImage(),
86-
_helpImage(),
87-
_backImage(),
88-
_enterImage(),
89-
_searchImage(),
90-
_shiftImage() {
77+
_capsLockActive(false) {
9178

9279
if (!_cutImage.decode(img_cut, img_cut_len) ||
9380
!_copyImage.decode(img_copy, img_copy_len) ||
@@ -196,9 +183,9 @@ void Key::draw(const KeypadTheme *theme, const KeypadDrawContext *context) const
196183
if (_printable || _pressed) {
197184
if (_printable && _pressed) {
198185
maSetColor(theme->_outline);
199-
maFillRect(_x + 4, _y + 4, _w - 8, _h - 8);
186+
maFillRect(_x + 4, _y + 4, _w - 7, _h - 7);
200187
}
201-
188+
202189
maSetColor(_printable ? theme->_keyHighlight : theme->_funcKeyHighlight);
203190

204191
// Draw edges (excluding the rounded corners)
@@ -214,7 +201,7 @@ void Key::draw(const KeypadTheme *theme, const KeypadDrawContext *context) const
214201
maArc(xcR, ycT, rc, PI * 3 / 2, 0, aspect); // Top-right corner
215202
maArc(xcR, ycB, rc, 0, PI / 2, aspect); // Bottom-right corner
216203
maArc(xcL, ycB, rc, PI / 2, PI, aspect); // Bottom-left corner
217-
}
204+
}
218205

219206
if (_printable) {
220207
bool useShift = context->_shiftActive ^ context->_capsLockActive;
@@ -243,7 +230,7 @@ bool Key::inside(int x, int y) const {
243230
y <= _y + _h);
244231
}
245232

246-
void Key::onClick(bool useShift) const {
233+
void Key::onClick(const bool useShift) const {
247234
auto *event = new MAEvent();
248235
event->type = EVENT_TYPE_KEY_PRESSED;
249236
event->nativeKey = 0;
@@ -293,14 +280,14 @@ Keypad::Keypad(int charWidth, int charHeight)
293280
_posY(0),
294281
_width(0),
295282
_height(0),
296-
_context(charWidth, charHeight),
297283
_theme(&MODERN_DARK_THEME),
284+
_context(charWidth, charHeight),
298285
_currentLayout(LayoutLetters) {
299286
generateKeys();
300287
}
301288

302289
int Keypad::outerHeight(int charHeight) {
303-
return MAX_ROWS * ((DEFAULT_PADDING * 2) + charHeight);
290+
return MAX_ROWS * ((PADDING * 2) + charHeight);
304291
}
305292

306293
void Keypad::generateKeys() {
@@ -317,7 +304,7 @@ void Keypad::generateKeys() {
317304
}
318305

319306
for (int row = 0; row < MAX_ROWS; ++row) {
320-
int cols = ROW_LENGTHS[_currentLayout][row];
307+
int cols = ROW_LENGTHS[row];
321308
for (int col = 0; col < cols; col++) {
322309
const RawKey &k = activeLayout[row][col];
323310
if (k._normal != K_NULL) {
@@ -333,56 +320,36 @@ void Keypad::layout(int x, int y, int w, int h) {
333320
_width = w;
334321
_height = h;
335322

336-
const int charWidth = _context._charWidth;
337-
const int charHeight = _context._charHeight;
338-
339-
// start with optimum padding, then reduce to fit width
340-
int padding = DEFAULT_PADDING;
341-
int width = MAX_COLS * (charWidth + (padding * 2));
342-
343-
while (width > w && padding > 0) {
344-
padding -= 2;
345-
width = MAX_COLS * (charWidth + (padding * 2));
346-
}
347-
padding *= 2;
348-
349-
int keyW = charWidth + (padding);
350-
int keyH = charHeight + (padding);
351-
int xStart = _posX + ((w - width) / 2);
323+
const int width = _width - PADDING;
324+
const int keyW = width / ROW_LENGTHS[QWERTY_ROW];
325+
const int keyH = _context._charHeight + PADDING * 2;
326+
const int xStart = _posX + ((w - _width) / 2);
352327
int yPos = _posY;
353328
int index = 0;
354-
int boardWidth = (MAX_COLS * charWidth) + (MAX_COLS * padding);
355329

356330
for (int row = 0; row < MAX_ROWS; ++row) {
357-
int cols = ROW_LENGTHS[_currentLayout][row];
331+
const int cols = ROW_LENGTHS[row];
358332
int xPos = xStart;
359-
if (row == NARROW_ROW) {
360-
int rowWidth = (cols * charWidth) + (cols * padding);
361-
if (rowWidth > width) {
362-
xPos -= (rowWidth - width) / 2;
363-
} else {
364-
xPos += (width - rowWidth) / 2;
365-
}
333+
if (row == QWERTY_ROW || row == ASDF_ROW) {
334+
const int rowWidth = keyW * cols;
335+
xPos += (_width - rowWidth) / 2;
366336
}
367337
for (int col = 0; col < cols; col++) {
368338
if (index >= (int)_keys.size()) {
369339
break;
370340
}
371341
Key *key = _keys[index++];
372-
int length = key->_printable ? 1 : 2;
373342
int keyWidth = keyW;
374343
if (row == 0) {
375-
keyWidth = (boardWidth / cols);
344+
keyWidth = _width / cols;
376345
if (col < 2 || col > 3) {
377346
keyWidth += 1;
378347
}
379348
} else if (!key->_printable) {
380-
int numKeys = 2;
381-
keyWidth = (boardWidth - ((cols - numKeys) * keyW)) / numKeys;
349+
const int numKeys = 2;
350+
keyWidth = (_width - ((cols - numKeys) * keyW)) / numKeys;
382351
} else if (key->_key == K_SPACE) {
383352
keyWidth = (SPACE_COLS * keyW);
384-
} else if (length > 1) {
385-
keyWidth = (length * charWidth) + (padding);
386353
}
387354
key->_x = xPos;
388355
key->_y = yPos;
@@ -395,8 +362,8 @@ void Keypad::layout(int x, int y, int w, int h) {
395362
}
396363

397364
void Keypad::clicked(int x, int y, bool pressed) {
398-
for (auto key : _keys) {
399-
bool inside = key->inside(x, y);
365+
for (const auto key : _keys) {
366+
const bool inside = key->inside(x, y);
400367
key->_pressed = pressed && inside;
401368

402369
if (pressed && inside) {

src/ui/keypad.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct Keypad {
8888
Keypad(int charWidth, int charHeight);
8989
~Keypad() = default;
9090

91-
static int outerHeight(int ch) ;
91+
static int outerHeight(int charHeight) ;
9292
void clicked(int x, int y, bool pressed);
9393
void draw() const;
9494
void layout(int x, int y, int w, int h);
@@ -115,10 +115,13 @@ struct KeypadInput : public FormInput {
115115
bool floatTop() override { return _floatTop; }
116116
bool floatBottom() override { return !_floatTop; }
117117
void layout(int x, int y, int w, int h) override;
118-
bool hasHover() override { return true; }
119118
void drawHover(int dx, int dy, bool selected) override {};
119+
#if defined(_SDL)
120+
// for cursor display
121+
bool hasHover() override { return true; }
122+
#endif
120123

121-
private:
124+
private:
122125
bool _floatTop;
123126
bool _toolbar;
124127
Keypad *_keypad;

0 commit comments

Comments
 (0)