MenĂ¼position merken.
[hackover2013-badge-firmware.git] / badge / ui / menu.c
1 #include "menu.h"
2
3 #include "event.h"
4 #include "font.h"
5 #include "sprite.h"
6
7 #include <stdbool.h>
8
9 enum {
10 MENU_ARROW_UP,
11 MENU_ARROW_DOWN
12 };
13
14 static badge_sprite const arrows[] = {
15 { 5, 7, (uint8_t const *) "\x04\xc3\xdf\x40" },
16 { 5, 7, (uint8_t const *) "\x10\xd8\x1f\x06\x01" }
17 };
18
19 enum {
20 MENU_MARGIN_TOP = 2,
21 MENU_MARGIN_BOTTOM = 3,
22 MENU_MARGIN_LEFT = 3,
23 MENU_ENTRIES_HEIGHT = 1 + BADGE_FONT_HEIGHT,
24 MENU_ENTRIES_VISIBLE = (BADGE_DISPLAY_HEIGHT - MENU_MARGIN_TOP - MENU_MARGIN_BOTTOM) / MENU_ENTRIES_HEIGHT
25 };
26
27 static void badge_menu_show(char const *const *menu,
28 size_t n,
29 size_t first_visible,
30 size_t selected)
31 {
32 badge_framebuffer fb = { { { 0 } } };
33 bool arrow_up = true;
34 bool arrow_down = true;
35
36 size_t first_used_row = 0;
37 size_t used_rows = MENU_ENTRIES_VISIBLE;
38
39 if(n <= MENU_ENTRIES_VISIBLE) {
40 first_visible = 0;
41 used_rows = n;
42 first_used_row = (MENU_ENTRIES_VISIBLE - used_rows) / 2;
43 }
44
45 if(first_visible == 0) {
46 arrow_up = false;
47 }
48
49 if(first_visible + MENU_ENTRIES_VISIBLE >= n) {
50 arrow_down = false;
51 }
52
53 for(size_t i = 0; i < used_rows; ++i) {
54 badge_framebuffer_render_text(&fb,
55 (int8_t) (MENU_MARGIN_LEFT + BADGE_FONT_WIDTH),
56 (int8_t) (MENU_MARGIN_TOP + (first_used_row + i) * MENU_ENTRIES_HEIGHT),
57 menu[first_visible + i]);
58 }
59
60 badge_framebuffer_render_char(&fb, MENU_MARGIN_LEFT, MENU_MARGIN_TOP + MENU_ENTRIES_HEIGHT * (selected - first_visible + first_used_row), '*');
61 if(arrow_up ) { badge_framebuffer_blt(&fb, MENU_MARGIN_LEFT, MENU_MARGIN_TOP, &arrows[MENU_ARROW_UP ], 0); }
62 if(arrow_down) { badge_framebuffer_blt(&fb, MENU_MARGIN_LEFT, MENU_MARGIN_TOP + (MENU_ENTRIES_VISIBLE - 1) * MENU_ENTRIES_HEIGHT, &arrows[MENU_ARROW_DOWN], 0); }
63
64 badge_framebuffer_flush(&fb);
65 }
66
67 size_t badge_menu(char const *const *menu,
68 size_t n,
69 size_t *first_visible,
70 size_t selected)
71 {
72 for(;;) {
73 badge_menu_show(menu, n, *first_visible, selected);
74
75 badge_event_t ev;
76
77 do {
78 ev = badge_event_wait();
79 } while(badge_event_type(ev) != BADGE_EVENT_USER_INPUT);
80
81 uint8_t old_state = badge_event_old_input_state(ev);
82 uint8_t new_state = badge_event_new_input_state(ev);
83 uint8_t new_buttons = new_state & (old_state ^ new_state);
84
85 if(new_buttons & (BADGE_EVENT_KEY_BTN_A | BADGE_EVENT_KEY_BTN_B)) {
86 return selected;
87 } else if((new_buttons & BADGE_EVENT_KEY_UP ) && selected != 0) {
88 --selected;
89 if(*first_visible != 0 && selected <= *first_visible) {
90 *first_visible = selected - 1;
91 }
92 } else if(new_buttons & BADGE_EVENT_KEY_DOWN && selected + 1 < n) {
93 ++selected;
94 if(*first_visible + (MENU_ENTRIES_VISIBLE - 2 + (selected + 1 == n)) < selected) {
95 *first_visible = selected - (MENU_ENTRIES_VISIBLE - 2 + (selected + 1 == n));
96 }
97 }
98 }
99 }
This page took 0.058924 seconds and 5 git commands to generate.