add wrap-around option to the menu

This commit is contained in:
2020-01-12 00:23:32 +01:00
parent 202b9e49f7
commit ad3eb59d58
3 changed files with 16 additions and 4 deletions
+1
View File
@@ -104,6 +104,7 @@ struct Scene *NewScene_MenuTest() {
scene->base.paint = (Scene_paint_t) paint; scene->base.paint = (Scene_paint_t) paint;
scene->extra = priv; scene->extra = priv;
scene->extra_deinit = (Scene_deinit_t) priv_deinit; scene->extra_deinit = (Scene_deinit_t) priv_deinit;
scene->wraparound = true;
// add a child return handler (base does not use this) // add a child return handler (base does not use this)
scene->base.onChildReturn = (Scene_onChildReturn_t) onChildReturn; scene->base.onChildReturn = (Scene_onChildReturn_t) onChildReturn;
+8
View File
@@ -57,11 +57,19 @@ static struct SceneEvent onInput(struct MenuScene *self, struct InputEvent event
case InputEventKind_Wheel:; case InputEventKind_Wheel:;
self->selected += event.wheel.direction; self->selected += event.wheel.direction;
if (self->selected < 0) { if (self->selected < 0) {
if (self->wraparound) {
self->selected = self->items_len - 1;
} else {
self->selected = 0; self->selected = 0;
} }
}
else if (self->selected >= self->items_len) { else if (self->selected >= self->items_len) {
if (self->wraparound) {
self->selected = 0;
} else {
self->selected = self->items_len - 1; self->selected = self->items_len - 1;
} }
}
// hacky scroll adjust // hacky scroll adjust
while (self->selected - self->scroll_up < 1 && self->scroll_up > 0) { while (self->selected - self->scroll_up < 1 && self->scroll_up > 0) {
+5 -2
View File
@@ -8,6 +8,7 @@
#define REFLOWER_SCENE_MENU_H #define REFLOWER_SCENE_MENU_H
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#define MENUITEM_LABEL_LEN 30 #define MENUITEM_LABEL_LEN 30
@@ -38,10 +39,10 @@ struct MenuScene {
struct Scene base; struct Scene base;
// items array // items array
struct MenuItem *items; struct MenuItem *items;
// allow wrap-around
bool wraparound;
// items count // items count
size_t items_len; size_t items_len;
// number of visible characters in a line at a time
uint8_t ncols;
// Vertical scroll offset (how many steps is the topmost item moved above the screen) // Vertical scroll offset (how many steps is the topmost item moved above the screen)
int32_t scroll_up; int32_t scroll_up;
// Start X // Start X
@@ -56,6 +57,8 @@ struct MenuScene {
int32_t hscroll_cnt; int32_t hscroll_cnt;
// Number of lines visible at a time // Number of lines visible at a time
uint8_t nlines; uint8_t nlines;
// number of visible characters in a line at a time
uint8_t ncols;
// selection handler // selection handler
MenuScene_onSelect_t onSelect; MenuScene_onSelect_t onSelect;
// Extra field for the subclass's private use. // Extra field for the subclass's private use.