Skip to content

Commit

Permalink
lib: smooth menus
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Feb 8, 2024
1 parent cea5c9d commit b1045b4
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/lilka/src/lilka/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ namespace lilka {
int ui_menu(String title, String menu[], int menu_size, int cursor, const menu_icon_t *icons[]) {
Canvas canvas;
canvas.begin();
canvas.fillScreen(canvas.color565(0, 0, 0));
canvas.fillTriangle(0, 0, 64, 0, 0, 32, canvas.color565(0, 0, 255));
canvas.fillTriangle(LILKA_DISPLAY_WIDTH, LILKA_DISPLAY_HEIGHT, LILKA_DISPLAY_WIDTH - 64, LILKA_DISPLAY_HEIGHT, LILKA_DISPLAY_WIDTH, LILKA_DISPLAY_HEIGHT - 32, canvas.color565(255, 255, 0));
controller.resetState();
uint8_t cursorY = cursor * 24 + 96 - 20;
while (1) {
uint8_t desiredCursorY = cursor * 24 + 96 - 20;
canvas.fillScreen(canvas.color565(0, 0, 0));
canvas.fillTriangle(0, 0, 64, 0, 0, 32, canvas.color565(0, 0, 255));
canvas.fillTriangle(LILKA_DISPLAY_WIDTH, LILKA_DISPLAY_HEIGHT, LILKA_DISPLAY_WIDTH - 64, LILKA_DISPLAY_HEIGHT, LILKA_DISPLAY_WIDTH, LILKA_DISPLAY_HEIGHT - 32, canvas.color565(255, 255, 0));
canvas.setCursor(32, 48);
canvas.setTextColor(canvas.color565(255, 255, 255));
canvas.setFont(u8g2_font_6x13_t_cyrillic);
Expand All @@ -22,8 +24,21 @@ int ui_menu(String title, String menu[], int menu_size, int cursor, const menu_i
canvas.setTextSize(1);
canvas.setFont(u8g2_font_10x20_t_cyrillic);

canvas.fillRect(0, cursorY, LILKA_DISPLAY_WIDTH, 24, canvas.color565(255, 64, 0));
if (cursorY < desiredCursorY) {
cursorY += ceil((float)(desiredCursorY - cursorY) / 2);
if (cursorY > desiredCursorY) {
cursorY = desiredCursorY;
}
} else if (cursorY > desiredCursorY) {
cursorY -= floor((float)(cursorY - desiredCursorY) / 2);
if (cursorY < desiredCursorY) {
cursorY = desiredCursorY;
}
}

for (int i = 0; i < menu_size; i++) {
canvas.fillRect(0, 96 + i * 24 - 20, LILKA_DISPLAY_WIDTH, 24, i == cursor ? canvas.color565(255, 64, 0) : canvas.color565(0, 0, 0));
// canvas.fillRect(0, 96 + i * 24 - 20, LILKA_DISPLAY_WIDTH, 24, i == cursor ? canvas.color565(255, 64, 0) : canvas.color565(0, 0, 0));
canvas.setTextBound(0, 96 + i * 24 - 20, LILKA_DISPLAY_WIDTH, 24);
if (icons != NULL && icons[i] != NULL) {
canvas.draw16bitRGBBitmap(0, 96 + i * 24 - 20, const_cast<uint16_t *>(*icons[i]), 24, 24);
Expand All @@ -37,11 +52,6 @@ int ui_menu(String title, String menu[], int menu_size, int cursor, const menu_i

State state = controller.getState();

while (!state.up.justPressed && !state.down.justPressed && !state.start.justPressed) {
state = controller.getState();
delay(10);
}

if (state.up.justPressed) {
// Move cursor up
cursor--;
Expand Down

0 comments on commit b1045b4

Please sign in to comment.