1
0
Fork 0
forked from len0rd/rockbox

x1000: bootloader: add GUI list widget

Change-Id: Ic5bf4747ed99b713b7c035153865ed9bdebd89b0
This commit is contained in:
Aidan MacDonald 2022-03-10 14:16:39 +00:00
parent 8b4949381c
commit 4b51ca5ce6
2 changed files with 88 additions and 0 deletions

View file

@ -111,3 +111,67 @@ void gui_shutdown(void)
backlight_hw_off();
}
void gui_list_init(struct bl_list* list, struct viewport* vp)
{
list->vp = vp;
list->num_items = 0;
list->selected_item = 0;
list->top_item = 0;
list->item_height = SYSFONT_HEIGHT;
list->draw_item = NULL;
}
void gui_list_draw(struct bl_list* list)
{
struct bl_listitem item = {
.list = list,
.x = 0, .y = 0,
.width = list->vp->width,
.height = list->item_height,
};
struct viewport* old_vp = lcd_set_viewport(list->vp);
lcd_clear_viewport();
int items_on_screen = list->vp->height / list->item_height;
for(int i = 0; i < items_on_screen; ++i) {
item.index = list->top_item + i;
if(item.index >= list->num_items)
break;
list->draw_item(&item);
item.y += item.height;
}
lcd_set_viewport(old_vp);
}
void gui_list_select(struct bl_list* list, int item_index)
{
/* clamp the selection */
list->selected_item = item_index;
if(list->selected_item < 0)
list->selected_item = 0;
else if(list->selected_item >= list->num_items)
list->selected_item = list->num_items - 1;
/* handle scrolling the list view */
int items_on_screen = list->vp->height / list->item_height;
int bottom_item = list->top_item + items_on_screen;
if(list->selected_item < list->top_item) {
list->top_item = list->selected_item;
} else if(list->selected_item >= bottom_item) {
list->top_item = list->selected_item - items_on_screen + 1;
if(list->top_item < 0)
list->top_item = 0;
}
}
void gui_list_scroll(struct bl_list* list, int delta)
{
gui_list_select(list, list->selected_item + delta);
}