menu: simplify menu sliding logic

This commit is contained in:
Lephenixnoir 2022-09-04 11:53:13 +02:00
parent d1143fc995
commit b3ebb83230
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
1 changed files with 29 additions and 48 deletions

View File

@ -5,6 +5,7 @@
#include <gint/timer.h>
#include <gint/gint.h>
#include <gint/cpu.h>
#include <gint/defs/util.h>
//---
// Internal timers routines
@ -12,26 +13,11 @@
static int __menu_offset_update(int *menu_offset)
{
int *direction;
int *target;
int *offset;
if(*menu_offset < 0)
*menu_offset = min(*menu_offset + 4, 0);
if(*menu_offset > 0)
*menu_offset = max(*menu_offset - 4, 0);
for (int i = 0 ; i < 2; ++i)
{
offset = &menu_offset[(i * 3) + 0];
target = &menu_offset[(i * 3) + 1];
direction = &menu_offset[(i * 3) + 2];
if (*direction < 0) {
*offset += *direction;
if (*offset < *target)
*offset = *target;
} else {
*offset += *direction;
if (*offset > *target)
*offset = *target;
}
}
return TIMER_CONTINUE;
}
@ -45,7 +31,7 @@ static int __title_blink_update(int *blink)
// Internal menu content management
//---
#define annimX(x) ((x) - offset)
#define annimX(x) ((x) + offset)
#define annimY(y) (y)
static void menu_display_start(int offset, int title_blink)
@ -132,20 +118,14 @@ static void menu_display_about(int offset)
int menu_title(void)
{
volatile int title_blink;
int selected;
int selected = 0;
int timer0;
int timer1;
unsigned int exit;
uint16_t start_col;
uint16_t about_col;
volatile int menu_offset[6];
volatile int menu_offset = 0;
menu_offset[0] = DWIDTH;
menu_offset[1] = 0;
menu_offset[2] = -4;
menu_offset[3] = -DWIDTH;
menu_offset[4] = -DWIDTH;
menu_offset[5] = 0;
timer0 = timer_configure(
TIMER_ANY,
3300,
@ -168,10 +148,21 @@ int menu_title(void)
{
dclear(RGB24(0x49759f));
cpu_atomic_start();
menu_display_start(menu_offset[0], title_blink);
menu_display_about(menu_offset[3]);
cpu_atomic_end();
/* Load atomically to avoid data races */
int off = menu_offset;
/* Display the current screen, and the one next to it if sliding */
int show_left = selected - (selected > 0 && off > 0);
int show_right = show_left + (off != 0);
for(int i = show_left; i <= show_right; i++)
{
int offset = off + DWIDTH * (i - selected);
if(i == 0)
menu_display_start(offset, title_blink);
if(i == 1)
menu_display_about(offset);
}
drect(0, DHEIGHT - 32, DWIDTH, DHEIGHT, C_WHITE);
drect_border(
@ -212,23 +203,13 @@ int menu_title(void)
if(ev.type == KEYEV_UP || ev.type == KEYEV_HOLD)
continue;
if (ev.key == KEY_SHIFT) exit = 0;
if (ev.key == KEY_LEFT && selected != 0) {
selected = 0;
cpu_atomic_start();
menu_offset[1] = 0;
menu_offset[2] = -4;
menu_offset[4] = -DWIDTH;
menu_offset[5] = -4;
cpu_atomic_end();
if (ev.key == KEY_LEFT && selected > 0) {
selected--;
menu_offset = -DWIDTH;
}
if (ev.key == KEY_RIGHT && selected != 1) {
selected = 1;
cpu_atomic_start();
menu_offset[1] = DWIDTH;
menu_offset[2] = 4;
menu_offset[4] = 0;
menu_offset[5] = 4;
cpu_atomic_end();
if (ev.key == KEY_RIGHT && selected < 1) {
selected++;
menu_offset = DWIDTH;
}
if (ev.key == KEY_MENU || ev.key == KEY_EXIT) exit = -1;
}