8000 12 29 by chrisws · Pull Request #244 · smallbasic/SmallBASIC · GitHub
[go: up one dir, main page]

Skip to content

12 29 #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Jun 26, 2025
Merged

12 29 #244

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e1dab04
ANDROID: update USB api to allow setting baud rate and timeout
chrisws Mar 30, 2025
82c463c
ANDROID: experimental bluetooth support
chrisws Apr 5, 2025
1f9b998
ANDROID: fixed some intellij inspection issues
chrisws Apr 14, 2025
c7c3566
Removes IOIO-OTG support for now
chrisws Apr 14, 2025
2907bde
TEENSY: serial receive now takes buffer size arg
chrisws Apr 14, 2025
75438c3
TEENSY: update demo
chrisws Apr 14, 2025
a0b0784
ANDROID: update sound sample
chrisws Apr 19, 2025
e8e768e
COMMON: bump version
chrisws Apr 19, 2025
ac35055
ANDROID: update file UI dependencies
chrisws Apr 19, 2025
d978842
ANDROID: allow android.speak to take addtional non-string args
chrisws Apr 19, 2025
6c38f2b
ANDROID: update SOUND implementation to avoid skips
chrisws Apr 19, 2025
aad80fc
ANDROID: update SOUND implementation to avoid skips
chrisws Apr 19, 2025
cec6cb7
ANDROID: minor fix for PEN(3)
chrisws Apr 26, 2025
2c26e10
Merge branch 'smallbasic:master' into 12_29
chrisws May 3, 2025
fedfbcc
SDL: upgrade to SDL v 3.x
chrisws May 5, 2025
6d2bf2d
ANDROID: update bluetooth permissions for older android versions #242
chrisws May 6, 2025
c42cba8
ANDROID: updated file selection handling for new MUI grid version
chrisws May 6, 2025
9c9ae77
ANDROID: update bluetooth handling for older android devices
chrisws May 10, 2025
ad5a33b
COMMON: fix issue with space char before import 'c-module'
chrisws May 17, 2025
e46505d
ANDROID: implemented find command
chrisws May 18, 2025
ac23ca3
UI: update help display
chrisws May 24, 2025
77c221f
UI: update help display
chrisws May 24, 2025
bed7c3d
UI: update help display
chrisws May 24, 2025
740decc
UI: experimental keypad to replace android keypad
chrisws May 27, 2025
8955be7
UI: experimental keypad to replace android keypad
chrisws May 28, 2025
99742e9
UI: experimental keypad to replace android keypad
chrisws May 29, 2025
fb6b2c3
UI: experimental keypad to replace android keypad
chrisws Jun 5, 2025
02364ef
COMMON: resolved some compiler warnings
chrisws Jun 6, 2025
bd05aac
COMMON: update dependencies
chrisws Jun 6, 2025
230060f
SDL: added cmake build scripts for use with clion (free version)
chrisws Jun 7, 2025
1c22dfa
UI: experimental keypad to replace android keypad
chrisws Jun 7, 2025
2709c3f
UI: experimental keypad to replace android keypad
chrisws Jun 8, 2025
8c6752f
UI: experimental keypad to replace android keypad
chrisws Jun 11, 2025
da9d224
UI: experimental keypad to replace android keypad
chrisws Jun 13, 2025
05e366d
UI: experimental keypad to replace android keypad
chrisws Jun 14, 2025
6094d3a
UI: experimental keypad to replace android keypad
chrisws Jun 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
UI: experimental keypad to replace android keypad
  • Loading branch information
chrisws committed May 27, 2025
commit 740deccf83497e21424f72f3adbe47ffb6df5555
1 change: 1 addition & 0 deletions src/platform/sdl/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sbasicg_SOURCES = \
../../ui/form.cpp \
../../ui/inputs.cpp \
../../ui/textedit.cpp \
../../ui/keypad.cpp \
../../ui/image.cpp \
../../ui/strlib.cpp \
../../ui/audio.cpp \
Expand Down
226 changes: 226 additions & 0 deletions src/ui/keypad.cpp
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// This file is part of SmallBASIC
//
// Copyright(C) 2001-2025 Chris Warren-Smith.
//
// This program is distributed under the terms of the GPL v2.0 or later
// Download the GNU Public License (GPL) from www.gnu.org
//

#include "config.h"
#include "keypad.h"
#include "lib/maapi.h"

constexpr char cutIcon[] = {'\367', '\0'};
constexpr char copyIcon[] = {'\251', '\0'};
constexpr char pasteIcon[] = {'\273', '\0'};
constexpr char saveIcon[] = {'\247', '\0'};
constexpr char runIcon[] = {'\245', '\0'};
constexpr char helpIcon[] = {'\277', '\0'};
constexpr char shiftKey[] = "Shift";
constexpr char backKey[] = "Back";
constexpr char spaceKey[] = "SPACE";
constexpr char enterKey[] = "Enter";
constexpr char toggleKey[] = "?123";

constexpr RawKey letters[][10] = {
{{cutIcon, "Cut"}, {copyIcon, "Copy"}, {pasteIcon, "Paste"}, {saveIcon, "Save"}, {runIcon, "Run"}, {helpIcon, "Help"}},
{{"q", "Q"}, {"w", "W"}, {"e", "E"}, {"r", "R"}, {"t", "T"}, {"y", "Y"}, {"u", "U"}, {"i", "I"}, {"o", "O"}, {"p", "P"}},
{{"a", "A"}, {"s", "S"}, {"d", "D"}, {"f", "F"}, {"g", "G"}, {"h", "H"}, {"j", "J"}, {"k", "K"}, {"l", "L"}},
{{shiftKey, shiftKey}, {"z", "Z"}, {"x", "X"}, {"c", "C"}, {"v", "V"}, {"b", "B"}, {"n", "N"}, {"m", "M"}, {backKey, backKey}},
{{toggleKey, toggleKey}, {spaceKey, spaceKey}, {enterKey, enterKey}}
};

constexpr RawKey numbers[][10] = {
{{cutIcon, "Cut"}, {copyIcon, "Copy"}, {pasteIcon, "Paste"}, {saveIcon, "Save"}, {runIcon, "Run"}, {helpIcon, "Help"}},
{{"1", "!"}, {"2", "@"}, {"3", "#"}, {"4", "$"}, {"5", "%"}, {"6", "^"}, {"7", "&"}, {"8", "*"}, {"9", "("}, {"0", ")"}},
{{"-", "_"}, {"/", "\\"}, {":", ";"}, {"(", ")"}, {"$", "€"}, {"&", "|"}, {"@", "~"}, {"\"", "'"}, {backKey, backKey}},
{{toggleKey, toggleKey}, {spaceKey, spaceKey}, {enterKey, enterKey}}
};

constexpr RawKey symbols[][10] = {
{{cutIcon, "Cut"}, {copyIcon, "Copy"}, {pasteIcon, "Paste"}, {saveIcon, "Save"}, {runIcon, "Run"}, {helpIcon, "Help"}},
{{"[", "{"}, {"]", "}"}, {"{", "{"}, {"}", "}"}, {"#", "#"}, {"%", "%"}, {"^", "^"}, {"*", "*"}, {"+", "+"}, {"=", "="}},
{{"_", "_"}, {"\\", "\\"}, {"|", "|"}, {"~", "~"}, {"<", "<"}, {">", ">"}, {"`", "`"}, {backKey, backKey}},
{{toggleKey, toggleKey}, {spaceKey, spaceKey}, {enterKey, enterKey}}
};

constexpr int rowLengths[][5] = {
{6, 10, 9, 9, 3}, // letters
{6, 10, 9, 3, 0}, // numbers
{6, 10, 8, 3, 0}, // symbols
};

constexpr int maxRows = 5;
constexpr int maxCols = 10;
constexpr int numRows = 6;
constexpr int keyMargin = 4;

Key::Key(const RawKey &k) :
_label(k._normal),
_altLabel(k._shifted) {
_pressed = false;
_special = k._normal[0] > 128 || _label.length() > 1;
}

void Key::draw(EditTheme *theme, bool shiftActive, bool capsLockActive) {
int rx = _w / 2;
int ry = _h / 2;

int color = theme->_number_color;
if (_pressed || (_label.equals(shiftKey) && shiftActive)) {
color = _special ? theme->_selection_background : theme->_number_selection_background;
} else if (_special) {
color = theme->_selection_color;
}

maSetColor(color);
maEllipse(_x + rx, _y + ry, rx, ry, 1);
maSetColor(theme->_row_marker);
maEllipse(_x + rx, _y + ry, rx, ry, 0);
maSetColor(theme->_syntax_text);

String label;
if (_special) {
label = _label;
} else {
bool useShift = shiftActive ^ capsLockActive;
label = useShift ? _altLabel : _label;
}

int textX = _x + _w / 4;
int textY = _y + _h / 4;
maDrawText(textX, textY, label.c_str(), label.length());
}

bool Key::inside(int x, int y) const {
return (x >= _x &&
x <= _x + _w &&
y >= _y &&
y <= _y + _h);
}

Keypad::Keypad(int charWidth, int charHeight)
: _posX(0),
_posY(0),
_width(0),
_height(0),
_charWidth(charWidth),
_charHeight(charHeight),
_shiftActive(false),
_capsLockActive(false),
_visible(true),
_theme(nullptr),
_currentLayout(LayoutLetters) {
generateKeys();
}

int Keypad::outerHeight(int charHeight) const {
return !_visible ? 0 : numRows * ((keyMargin * 4) + charHeight);
}

void Keypad::generateKeys() {
_keys.clear();

const RawKey (*activeLayout)[10] = nullptr;
switch (_currentLayout) {
case LayoutLetters:
activeLayout = letters;
break;
case LayoutNumbers:
activeLayout = numbers;
break;
case LayoutSymbols:
activeLayout = symbols;
break;
}

for (int row = 0; row < maxRows; ++row) {
int cols = rowLengths[_currentLayout][row];
for (int col = 0; col < cols; col++) {
const RawKey &k = activeLayout[row][col];
if (k._normal != nullptr) {
_keys.add(new Key(k));
}
}
}
}

void Keypad::layout(int x, int y, int w, int h) {
_posX = x;
_posY = y;
_width = w;
_height = h;

// start with optimum padding, then reduce to fit width
int padding = 12;
int width = maxCols * (_charWidth + (padding * 2));

while (width > w && padding > 0) {
padding--;
width = maxCols * (_charWidth + (padding * 2));
}

int keyW = _charWidth + (padding * 2);
int keyH = _charHeight + (padding * 2);
int index = 0;
int xStart = _posX + ((w - width) / 2);
int yPos = _posY;

for (int row = 0; row < maxRows; ++row) {
int cols = rowLengths[_currentLayout][row];
int xPos = xStart;
if (cols < maxCols) {
// 8/9th of width / 2
int xOffs = (cols * width / maxCols) / 2;
xPos += xOffs;
}
for (int col = 0; col < cols; col++) {
if (index >= (int)_keys.size()) {
break;
}
Key *key = _keys[index++];
key->_x = xPos;
key->_y = yPos;
key->_w = keyW;
key->_h = keyH;
xPos += keyW;
}
yPos += keyH;
}
}

void Keypad::clicked(int x, int y, bool pressed) {
for (auto key : _keys) {
bool inside = key->inside(x, y);
key->_pressed = pressed && inside;

if (pressed && inside) {
if (key->_label.equals(shiftKey)) {
toggleShift();
} else if (key->_label.equals(toggleKey)) {
_currentLayout = static_cast<KeypadLayout>((_currentLayout + 1) % 3);
generateKeys();
layout(_posX, _posY, _width, _height);
break;
} else {
if (_shiftActive && !key->_special) {
_shiftActive = false;
}
// TODO: Output the key's character/command
}
}
}
}

void Keypad::toggleShift() {
_shiftActive = !_shiftActive;
}

void Keypad::draw() {
maSetColor(_theme->_background);
maFillRect(_posX, _posY, _width, _height);
for (const auto &key : _keys) {
key->draw(_theme, _shiftActive, _capsLockActive);
}
}

70 changes: 70 additions & 0 deletions src/ui/keypad.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// This file is part of SmallBASIC
//
// Copyright(C) 2001-2025 Chris Warren-Smith.
//
// This program is distributed under the terms of the GPL v2.0 or later
// Download the GNU Public License (GPL) from www.gnu.org
//

#pragma once

#include "ui/strlib.h"
#include "ui/theme.h"

using namespace strlib;

enum KeypadLayout {
LayoutLetters = 0, LayoutNumbers = 1, LayoutSymbols = 2
};

struct RawKey {
const char *_normal;
const char *_shifted;
};

struct Key {
Key(const RawKey &k);

void draw(EditTheme *theme, bool shiftActive, bool capsLockActive);
bool inside(int x, int y) const;

String _label;
String _altLabel;
int _x;
int _y;
int _w;
int _h;
bool _pressed;
bool _special;
};

struct Keypad {
Keypad(int charWidth, int charHeight);
~Keypad() = default;

bool visible() const { return _visible; }
int outerHeight(int ch) const;
void clicked(int x, int y, bool pressed);
void draw();
void hide() { _visible = false; }
void layout(int x, int y, int w, int h);
void show() { _visible = true; }
void setTheme(EditTheme *theme) { _theme = theme; }

private:
int _posX;
int _posY;
int _width;
int _height;
int _charWidth;
int _charHeight;
strlib::List<Key *> _keys;
bool _shiftActive;
bool _capsLockActive;
bool _visible;
EditTheme *_theme;
KeypadLayout _currentLayout;

void generateKeys();
void toggleShift();
};
0