From 4b51ca5ce60a874276579d9b4657de1edc36f31d Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Thu, 10 Mar 2022 14:16:39 +0000 Subject: x1000: bootloader: add GUI list widget Change-Id: Ic5bf4747ed99b713b7c035153865ed9bdebd89b0 --- bootloader/x1000/gui.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'bootloader/x1000/gui.c') diff --git a/bootloader/x1000/gui.c b/bootloader/x1000/gui.c index da20a9b128..a672c30380 100644 --- a/bootloader/x1000/gui.c +++ b/bootloader/x1000/gui.c @@ -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); +} -- cgit v1.2.3