Menu work

This commit is contained in:
Dylan Smith
2026-01-15 15:53:02 -05:00
parent 66cb2436c7
commit b48cb11197
9 changed files with 248 additions and 63 deletions

View File

@@ -2,8 +2,7 @@
#include "graphics.h"
#include "fonts/font.h"
#include "display.h"
static uint8_t selected_entry_idx = 0;
#include "ui.h"
void draw_menu(const graphical_menu_t *const menu)
{
@@ -15,18 +14,14 @@ void draw_menu(const graphical_menu_t *const menu)
const pixel_t entry_bg_color = MAKE_PIXEL(255, 255, 255);
const pixel_t highlighted_bg_color = MAKE_PIXEL(0, 0, 0);
const pixel_t highlighted_text_color = MAKE_PIXEL(255, 255, 0);
DrawBox(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, entry_bg_color);
// Ensure selected_entry_idx is within bounds
if (selected_entry_idx >= menu->num_entries)
{
selected_entry_idx = 0;
}
for (uint8_t i = 0; i < menu->num_entries; i++)
for (uint8_t i = 0; i < menu->layout->num_entries; i++)
{
const uint16_t y_pos = i * entry_height;
const graphical_menu_entry_t *entry = &menu->entries[i];
const bool is_highlighted = (i == selected_entry_idx) && entry->enabled;
const graphical_menu_entry_t *entry = &menu->layout->entries[i];
const bool is_highlighted = (i == menu->selected_entry_idx) && entry->enabled;
// Draw entry background - use highlighted color if this is the selected entry
pixel_t bg_color = is_highlighted ? highlighted_bg_color : entry_bg_color;
@@ -51,15 +46,15 @@ void draw_menu(const graphical_menu_t *const menu)
}
}
void select_menu_entry(const graphical_menu_t *const menu)
void select_menu_entry(graphical_menu_t *const menu)
{
// Ensure selected_entry_idx is within bounds
if (selected_entry_idx >= menu->num_entries)
if (menu->selected_entry_idx >= menu->layout->num_entries)
{
return;
}
const graphical_menu_entry_t *entry = &menu->entries[selected_entry_idx];
const graphical_menu_entry_t *entry = &menu->layout->entries[menu->selected_entry_idx];
// Only select if the entry is enabled
if (entry->enabled && entry->selected_callback_function != NULL)
@@ -68,28 +63,37 @@ void select_menu_entry(const graphical_menu_t *const menu)
}
}
uint8_t get_selected_menu_entry_idx(void)
void set_selected_menu_entry_idx(graphical_menu_t *const menu, int8_t idx)
{
return selected_entry_idx;
}
void set_selected_menu_entry_idx(const graphical_menu_t *const menu, uint8_t idx)
{
// Clamp the index to valid range
if (idx >= menu->num_entries)
{
idx = menu->num_entries > 0 ? menu->num_entries - 1 : 0;
}
// Only allow selecting enabled entries
if (menu->entries[idx].enabled)
while (!menu->layout->entries[idx].enabled)
{
selected_entry_idx = idx;
// Call highlighted callback if it exists
if (menu->entries[idx].highlighted_callback_function != NULL)
if (idx >menu->selected_entry_idx)
{
menu->entries[idx].highlighted_callback_function(menu->entries[idx].highlighted_callback_function_args);
idx--;
}
else
{
idx++;
}
}
// Clamp the index to valid range
if (idx < 0 || idx >= menu->layout->num_entries)
{
idx = menu->layout->num_entries > 0 ? menu->layout->num_entries - 1 : 0;
}
menu->selected_entry_idx = idx;
ui_request_redraw();
}
void decrement_selected_menu_entry_idx(graphical_menu_t *const menu)
{
set_selected_menu_entry_idx(menu, menu->selected_entry_idx - 1);
}
void increment_selected_menu_entry_idx(graphical_menu_t *const menu)
{
set_selected_menu_entry_idx(menu, menu->selected_entry_idx + 1);
}