You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.1 KiB
75 lines
2.1 KiB
#include <malloc.h>
|
|
#include <graphics/bitmaps.h>
|
|
#include <sys/param.h>
|
|
#include <stdbool.h>
|
|
#include <liquid/input_event.h>
|
|
|
|
#include "liquid.h"
|
|
#include "graphics/drawing.h"
|
|
|
|
#include "scene_menu.h"
|
|
|
|
static void paint(struct MenuScene *self)
|
|
{
|
|
LCD_clearDisplay(0);
|
|
|
|
int row = 0;
|
|
for (int i = self->scroll_up; i < MIN(self->items_len, self->scroll_up + 6); i++, row++) {
|
|
bool selected = self->selected==i;
|
|
LCD_setStrEx(self->items[i].label, 0, row * 8, selected?WHITE:BLACK,FONT_NORMAL|(selected?FONT_BG:0));
|
|
}
|
|
}
|
|
|
|
static struct SceneEvent onInput(struct MenuScene *self, struct InputEvent event) {
|
|
switch (event.kind) {
|
|
case InputEventKind_Wheel:;
|
|
self->selected += event.wheel.direction;
|
|
if (self->selected < 0) {
|
|
self->selected = 0;
|
|
}
|
|
else if (self->selected >= self->items_len) {
|
|
self->selected = self->items_len - 1;
|
|
}
|
|
|
|
// hacky scroll adjust
|
|
while (self->selected - self->scroll_up < 1 && self->scroll_up > 0) {
|
|
self->scroll_up--;
|
|
}
|
|
|
|
while (self->selected - self->scroll_up >= 5 && self->scroll_up < self->items_len - 6) {
|
|
self->scroll_up++;
|
|
}
|
|
|
|
return SceneEvent_Repaint();
|
|
|
|
case InputEventKind_Button:
|
|
if (event.button.state) {
|
|
if (self->onSelect) {
|
|
return self->onSelect(self);
|
|
}
|
|
return SceneEvent_Close(0, NULL);
|
|
}
|
|
// fall through
|
|
default:
|
|
return SceneEvent_None();
|
|
}
|
|
}
|
|
|
|
static void deinit(struct MenuScene *self)
|
|
{
|
|
free(self->items);
|
|
self->items = NULL;
|
|
}
|
|
|
|
struct MenuScene *NewScene_Menu(struct MenuItem *items, size_t items_len) {
|
|
struct MenuScene *scene = calloc(1, sizeof(struct MenuScene));
|
|
|
|
scene->base.paint = (Scene_paint_t) paint;
|
|
scene->base.onInput = (Scene_onInput_t) onInput;
|
|
scene->base.deinit = (Scene_deinit_t) deinit;
|
|
|
|
scene->items = items;
|
|
scene->items_len = items_len;
|
|
|
|
return scene;
|
|
}
|
|
|