parent
d7cd0e9cfa
commit
2b92a6832d
@ -0,0 +1,68 @@ |
||||
#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(); |
||||
} |
||||
} |
||||
|
||||
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->items = items; |
||||
scene->items_len = items_len; |
||||
|
||||
return scene; |
||||
} |
@ -0,0 +1,37 @@ |
||||
/**
|
||||
* TODO file description |
||||
*
|
||||
* Created on 2020/01/05. |
||||
*/ |
||||
|
||||
#ifndef REFLOWER_SCENE_MENU_H |
||||
#define REFLOWER_SCENE_MENU_H |
||||
|
||||
#define MENUITEM_LABEL_LEN 30 |
||||
|
||||
struct MenuItem { |
||||
char label[MENUITEM_LABEL_LEN]; |
||||
uint32_t tag; |
||||
}; |
||||
|
||||
struct MenuScene; |
||||
|
||||
typedef struct SceneEvent (*MenuScene_onSelect_t)(struct MenuScene *scene); |
||||
|
||||
struct MenuScene { |
||||
struct Scene base; |
||||
// items array
|
||||
struct MenuItem *items; |
||||
// items count
|
||||
size_t items_len; |
||||
// Vertical scroll offset (how many steps is the topmost item moved above the screen)
|
||||
int32_t scroll_up; |
||||
// Selected item number
|
||||
int32_t selected; |
||||
// selection handler
|
||||
MenuScene_onSelect_t onSelect; |
||||
}; |
||||
|
||||
struct MenuScene *NewScene_Menu(struct MenuItem *items, size_t items_len); |
||||
|
||||
#endif //REFLOWER_SCENE_MENU_H
|
@ -0,0 +1,52 @@ |
||||
#include <malloc.h> |
||||
#include <graphics/bitmaps.h> |
||||
#include <sys/param.h> |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
|
||||
#include "liquid.h" |
||||
#include "graphics/drawing.h" |
||||
|
||||
#include "scenes.h" |
||||
#include "scene_menu.h" |
||||
|
||||
|
||||
static struct SceneEvent onSelect(struct MenuScene *self) { |
||||
if (self->selected == 0) { |
||||
return SceneEvent_Close(0, NULL); |
||||
} |
||||
|
||||
if (self->selected == 8) { |
||||
return SceneEvent_OpenChild(NewScene_Car(), 0); |
||||
} |
||||
if (self->selected == 9) { |
||||
return SceneEvent_OpenChild(NewScene_Demo(), 0); |
||||
} |
||||
if (self->selected == 5) { |
||||
self->items[5].tag++; |
||||
snprintf(self->items[5].label, MENUITEM_LABEL_LEN, "Count = %d", self->items[5].tag); |
||||
return SceneEvent_Repaint(); |
||||
} |
||||
|
||||
return SceneEvent_None(); |
||||
} |
||||
|
||||
struct Scene *NewScene_MenuTest() { |
||||
struct MenuItem *items = calloc(10, sizeof(struct MenuItem)); |
||||
|
||||
strncpy(items[0].label, "🔙Back", MENUITEM_LABEL_LEN); |
||||
strncpy(items[1].label, "▶#1", MENUITEM_LABEL_LEN); |
||||
strncpy(items[2].label, "▶#2", MENUITEM_LABEL_LEN); |
||||
strncpy(items[3].label, "▶#3", MENUITEM_LABEL_LEN); |
||||
strncpy(items[4].label, "▶#4", MENUITEM_LABEL_LEN); |
||||
strncpy(items[5].label, "Count = 0", MENUITEM_LABEL_LEN); |
||||
strncpy(items[6].label, "▶#6", MENUITEM_LABEL_LEN); |
||||
strncpy(items[7].label, "▶#7", MENUITEM_LABEL_LEN); |
||||
strncpy(items[8].label, "Car", MENUITEM_LABEL_LEN); |
||||
strncpy(items[9].label, "Demo Scene", MENUITEM_LABEL_LEN); |
||||
|
||||
struct MenuScene * scene = NewScene_Menu(items, 10); |
||||
scene->onSelect = onSelect; |
||||
|
||||
return (struct Scene *) scene; |
||||
} |
Loading…
Reference in new issue