Skip to content

Commit

Permalink
Multiline toasts now scroll about.
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAbata committed May 2, 2023
1 parent aa78092 commit bf13d6e
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 66 deletions.
1 change: 1 addition & 0 deletions include/ui/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void ui_page_handle_close(const ui_page_t* p, void* arg);

extern const ui_page_t PAGE_SNAKE;
extern const ui_page_t PAGE_EDGING_STATS;
extern const ui_page_t EDGING_CHART_PAGE;
extern const ui_page_t PAGE_APP_RUNNER;
extern const ui_page_t SPLASH_PAGE;

Expand Down
9 changes: 9 additions & 0 deletions include/ui/toast.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {
#endif

#include "u8g2.h"
#include "ui/ui.h"
#include <stdint.h>

#define TOAST_MAX 256
Expand Down Expand Up @@ -86,6 +87,14 @@ void ui_toast_handle_close(void);
*/
void ui_toast_render(void);

/**
* @brief Scrolls the currently active toast.
*
* @param delta
* @return ui_render_flag_t
*/
ui_render_flag_t ui_toast_scroll(int delta);

/**
* @brief Checks if a toast is currently active and rendering.
*
Expand Down
31 changes: 31 additions & 0 deletions src/pages/edging_chart.c
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@

#include "application.h"
#include "assets.h"
#include "eom-hal.h"
#include "esp_log.h"
#include "ui/graphics.h"
#include "ui/page.h"
#include "util/i18n.h"
#include "version.h"

static const char* TAG = "page:splash";

static void on_open(void* arg) {
u8g2_t* d = eom_hal_get_display_ptr();
u8g2_ClearBuffer(d);
u8g2_SetDrawColor(d, 1);
u8g2_DrawBitmap(d, 0, 0, 128 / 8, 64, SPLASH_IMG);
u8g2_SetFontPosBottom(d);
u8g2_SetFont(d, UI_FONT_SMALL);
u8g2_DrawStr(d, 0, EOM_DISPLAY_HEIGHT, EOM_VERSION);
u8g2_SendBuffer(d);
}

const struct ui_page EDGING_CHART_PAGE = {
.title = "BOOT",
.on_open = on_open,
.on_render = NULL,
.on_close = NULL,
.on_loop = NULL,
.on_encoder = NULL,
.on_button = NULL,
};
14 changes: 13 additions & 1 deletion src/pages/edging_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "esp_log.h"
#include "esp_timer.h"
#include "orgasm_control.h"
#include "ui/toast.h"
#include "ui/ui.h"
#include "util/i18n.h"
#include <math.h>
Expand All @@ -22,6 +23,17 @@ static struct {
} state;

static void on_open(void* arg) {
// ui_toast_multiline(
// "Richter: Die monster. You don't belong in this world!\n\n"
// "Dracula: It was not by my hand that I am once again given flesh. I was called here by "
// "humans who wish to pay me tribute.\n\n"
// "Richter: Tribute!?! You steal men's souls and make them your slaves!\n\n"
// "Dracula: Perhaps the same could be said of all religions...\n\n"
// "Richter: Your words are as empty as your soul! Mankind ill needs a savior such as
// you!\n\n" "Dracula: What is a man? (throws his wine glass to the floor) A miserable
// little pile of " "secrets. But enough talk... Have at you!\n\n" "Belmont:
// alsdjf;lskdfj;alskdjf;laskdjf;alskdjf;alksdjflsdkf <3"
// );
}

static ui_render_flag_t on_loop(void* arg) {
Expand Down Expand Up @@ -246,7 +258,7 @@ on_button(eom_hal_button_t button, eom_hal_button_event_t event, void* arg) {
if (event != EOM_HAL_BUTTON_PRESS) return PASS;

if (button == EOM_HAL_BUTTON_BACK) {
// ui open chart
ui_open_page(&EDGING_CHART_PAGE, NULL);
} else if (button == EOM_HAL_BUTTON_MID) {
if (locked) return NORENDER;

Expand Down
2 changes: 1 addition & 1 deletion src/ui/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void ui_draw_scrollbar(u8g2_t* d, size_t index, size_t count, size_t window_size
int offset_top = count > 1 ? (track_height - bar_height) * index / (count - 1) : 0;

u8g2_SetDrawColor(d, 0);
u8g2_DrawBox(d, EOM_DISPLAY_WIDTH - 3, title_bar_height + 1, 3, track_height);
u8g2_DrawBox(d, EOM_DISPLAY_WIDTH - 3, title_bar_height, 3, track_height + 1);
u8g2_SetDrawColor(d, 1);
u8g2_DrawBox(d, EOM_DISPLAY_WIDTH - 2, title_bar_height + 1 + offset_top, 2, bar_height);
}
Expand Down
154 changes: 91 additions & 63 deletions src/ui/toast.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ static struct ui_toast {
uint8_t blocking;
void (*on_close)(void*);
void* on_close_arg;
int start_line;
int text_lines;
} current_toast;

void ui_toast(const char* fmt, ...) {
Expand Down Expand Up @@ -98,6 +100,20 @@ void ui_toast_clear(void) {
current_toast.progress = -1;
current_toast.on_close = NULL;
current_toast.on_close_arg = NULL;
current_toast.start_line = 0;
current_toast.text_lines = 0;
}

ui_render_flag_t ui_toast_scroll(int delta) {
size_t old = current_toast.start_line;
current_toast.start_line += delta;

if (current_toast.start_line > current_toast.text_lines - 4)
current_toast.start_line = current_toast.text_lines - 4;

if (current_toast.start_line < 0) current_toast.start_line = 0;

return current_toast.start_line == old ? NORENDER : RENDER;
}

void ui_toast_on_close(void (*cb)(void*), void* arg) {
Expand Down Expand Up @@ -142,6 +158,66 @@ void ui_toast_draw_frame(
);
}

typedef void (*text_wrap_cb_t)(const char* start, size_t len, size_t line, void* arg);

static size_t text_wrap(size_t col, const char* text, text_wrap_cb_t cb, void* cb_arg) {
size_t text_lines = 0;
size_t i = 0;
const char* line = text;
const char* space = text;

for (const char* ptr = text; *ptr != '\0'; ptr++) {
i++;

if (*ptr == '\n') {
if (cb != NULL) cb(line, ptr - line, text_lines, cb_arg);

i = 0;
text_lines++;
line = ptr + 1;
space = line;
} else if (i > col) {
if (space != line) {
if (cb != NULL) cb(line, space - line, text_lines, cb_arg);

ptr = space;
line = ptr;
} else {
if (cb != NULL) cb(line, ptr - line, text_lines, cb_arg);
}

i = 0;
text_lines++;
} else if (*ptr == ' ') {
space = ptr + 1;
}
}

if (*line != '\0' && cb != NULL) cb(line, strlen(line), text_lines, cb_arg);

return text_lines + 1;
}

struct toast_line_cb_arg {
u8g2_t* display;
int x;
int* y;
};

static void ui_toast_render_line(const char* start, size_t len, size_t i, void* arg) {
static char line[UI_TOAST_LINE_WIDTH + 1];

if (i - current_toast.start_line > 3 || i < current_toast.start_line) return;

struct toast_line_cb_arg* cbarg = (struct toast_line_cb_arg*)arg;

memcpy(line, start, len);
line[len] = '\0';

u8g2_DrawUTF8(cbarg->display, cbarg->x, *cbarg->y, line);
*cbarg->y += 9;
}

void ui_toast_render(void) {
if (!ui_toast_is_active()) return;

Expand All @@ -158,31 +234,11 @@ void ui_toast_render(void) {
const char* str =
current_toast.multiline_msg == NULL ? current_toast.str : current_toast.multiline_msg;

int text_lines = 1;

{
size_t i = 0;
const char* line = str;
const char* space = str;

for (const char* ptr = str; *ptr != '\0'; ptr++) {
i++;

if (current_toast.str[i] == '\n') {
i = 0;
text_lines++;
} else if (i > UI_TOAST_LINE_WIDTH) {
if (space != line) {
ptr = space;
line = ptr;
}

i = 0;
text_lines++;
} else if (*ptr == ' ') {
space = ptr;
}
}
size_t total_lines = text_wrap(UI_TOAST_LINE_WIDTH, str, NULL, NULL);
size_t text_lines = total_lines;

if (current_toast.text_lines == 0) {
current_toast.text_lines = total_lines;
}

ESP_LOGD(TAG, "Text Lines: %d", text_lines);
Expand All @@ -203,45 +259,13 @@ void ui_toast_render(void) {

ui_toast_draw_frame(display, margin, start_x, start_y, (EOM_DISPLAY_HEIGHT - (start_y * 2)));

char line[UI_TOAST_LINE_WIDTH + 1];

size_t idx = 0;
size_t space_idx = 0;
uint8_t col = 0;

do {
if (str[idx] == ' ') {
space_idx = idx;
}

if (str[idx] == '\n') {
space_idx = idx;
line[col] = '\0';
col = UI_TOAST_LINE_WIDTH;
}

else {
line[col] = str[idx];
line[col + 1] = '\0';
col++;
}

if (str[idx + 1] == '\0') {
space_idx = idx;
line[col] = '\0';
col = UI_TOAST_LINE_WIDTH;
}

if (col >= UI_TOAST_LINE_WIDTH) {
line[col - (idx - space_idx)] = '\0';
u8g2_DrawUTF8(display, start_x + margin + padding + 1, text_start_y, line);
text_start_y += 7 + padding;
col = 0;
idx = space_idx;
}
struct toast_line_cb_arg cb_args = {
.display = display,
.x = start_x + margin + padding + 1,
.y = &text_start_y,
};

idx++;
} while (str[idx] != '\0');
text_wrap(UI_TOAST_LINE_WIDTH, str, ui_toast_render_line, &cb_args);

if (current_toast.progress >= 0) {
int progress_x = start_x + margin + padding + 5;
Expand All @@ -254,6 +278,10 @@ void ui_toast_render(void) {
);
}

if (total_lines > text_lines) {
ui_draw_scrollbar(display, current_toast.start_line, total_lines, text_lines);
}

if (!current_toast.blocking) {
// render press-any-key message
u8g2_SetDrawColor(display, 1);
Expand Down
8 changes: 7 additions & 1 deletion src/ui/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ static void handle_encoder(int delta) {
ui_reset_idle_timer();

// Toasts always eat encoder...
if (ui_toast_is_active()) return;
if (ui_toast_is_active()) {
if (ui_toast_scroll(delta) == RENDER) {
UI.force_rerender = 1;
}

return;
}

if (UI.current_input != NULL) {
const ui_input_t* i = UI.current_input;
Expand Down

0 comments on commit bf13d6e

Please sign in to comment.