Merge branch 'master' of gitlab:wintermute/hackover2013-badge-firmware
[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 MENU_SCROLL_TICKS = 25
14 };
15
16 static badge_sprite const arrows[] = {
17 { 5, 7, (uint8_t const *) "\x04\xc3\xdf\x40" },
18 { 5, 7, (uint8_t const *) "\x10\xd8\x1f\x06\x01" }
19 };
20
21 enum {
22 MENU_MARGIN_TOP = 3,
23 MENU_MARGIN_BOTTOM = 2,
24 MENU_MARGIN_LEFT = 3,
25 MENU_ENTRIES_HEIGHT = 1 + BADGE_FONT_HEIGHT,
26 MENU_ENTRIES_VISIBLE = (BADGE_DISPLAY_HEIGHT - MENU_MARGIN_TOP - MENU_MARGIN_BOTTOM) / MENU_ENTRIES_HEIGHT
27 };
28
29 static void badge_menu_show(char const *const *menu,
30 size_t n,
31 size_t *first_visible,
32 size_t selected,
33 char selector)
34 {
35 badge_framebuffer fb = { { { 0 } } };
36 bool arrow_up = true;
37 bool arrow_down = true;
38
39 size_t first_used_row = 0;
40 size_t used_rows = MENU_ENTRIES_VISIBLE;
41
42 if(selected >= n) {
43 selected = n - 1;
44 }
45
46 if(n <= MENU_ENTRIES_VISIBLE) {
47 *first_visible = 0;
48 used_rows = n;
49 first_used_row = (MENU_ENTRIES_VISIBLE - used_rows) / 2;
50 } else if(selected + 1 == n) {
51 *first_visible = n - MENU_ENTRIES_VISIBLE;
52 } else if(selected <= *first_visible) {
53 *first_visible = selected == 0 ? 0 : selected - 1;
54 } else if(selected - *first_visible + 2 > MENU_ENTRIES_VISIBLE) {
55 *first_visible = selected - MENU_ENTRIES_VISIBLE + 2;
56 }
57
58 if(*first_visible == 0) {
59 arrow_up = false;
60 }
61
62 if(*first_visible + MENU_ENTRIES_VISIBLE >= n) {
63 arrow_down = false;
64 }
65
66 for(size_t i = 0; i < used_rows; ++i) {
67 badge_framebuffer_render_text(&fb,
68 (int8_t) (MENU_MARGIN_LEFT + BADGE_FONT_WIDTH),
69 (int8_t) (MENU_MARGIN_TOP + (first_used_row + i) * MENU_ENTRIES_HEIGHT),
70 menu[*first_visible + i]);
71 }
72
73 badge_framebuffer_render_char(&fb, MENU_MARGIN_LEFT, MENU_MARGIN_TOP + MENU_ENTRIES_HEIGHT * (selected - *first_visible + first_used_row), selector);
74 if(arrow_up ) { badge_framebuffer_blt(&fb, MENU_MARGIN_LEFT, MENU_MARGIN_TOP, &arrows[MENU_ARROW_UP ], 0); }
75 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); }
76
77 badge_framebuffer_flush(&fb);
78 }
79
80 size_t badge_menu(char const *const *menu,
81 size_t n,
82 size_t *first_visible,
83 size_t selected)
84 {
85 unsigned scroll_ticks = 0;
86 int scroll_direction = 0;
87
88 for(;;) {
89 if(scroll_ticks == 0) {
90 if (scroll_direction == 1 && selected + 1 < n) {
91 ++selected;
92 } else if(scroll_direction == -1 && selected != 0) {
93 --selected;
94 }
95
96 badge_menu_show(menu, n, first_visible, selected, '*');
97
98 scroll_ticks = MENU_SCROLL_TICKS;
99 }
100
101 badge_event_t ev;
102
103 ev = badge_event_wait();
104 switch(badge_event_type(ev)) {
105 case BADGE_EVENT_USER_INPUT:
106 {
107 uint8_t old_state = badge_event_old_input_state(ev);
108 uint8_t new_state = badge_event_new_input_state(ev);
109 uint8_t new_buttons = new_state & (old_state ^ new_state);
110
111 if(new_buttons & (BADGE_EVENT_KEY_BTN_A | BADGE_EVENT_KEY_BTN_B)) {
112 return selected;
113 } else if((new_buttons & BADGE_EVENT_KEY_UP )) {
114 scroll_direction = -1;
115 } else if((new_buttons & BADGE_EVENT_KEY_DOWN)) {
116 scroll_direction = 1;
117 } else {
118 scroll_direction = 0;
119 }
120
121 scroll_ticks = 0;
122 break;
123 }
124 case BADGE_EVENT_GAME_TICK:
125 {
126 --scroll_ticks;
127 break;
128 }
129 }
130 }
131 }
132
133 void badge_scroll_text(char const *const *lines, size_t n) {
134 size_t first_visible = 0;
135 int scroll_direction = 0;
136 unsigned scroll_ticks = 0;
137
138 for(;;) {
139 if(scroll_ticks == 0) {
140 if (scroll_direction == 1 && first_visible + MENU_ENTRIES_VISIBLE < n) {
141 ++first_visible;
142 } else if(scroll_direction == -1 && first_visible != 0) {
143 --first_visible;
144 }
145
146 badge_menu_show(lines, n, &first_visible, first_visible + (first_visible + 1 == n ? 0 : 1), ' ');
147
148 scroll_ticks = MENU_SCROLL_TICKS;
149 }
150
151 badge_event_t ev;
152
153 ev = badge_event_wait();
154 switch(badge_event_type(ev)) {
155 case BADGE_EVENT_USER_INPUT:
156 {
157 uint8_t old_state = badge_event_old_input_state(ev);
158 uint8_t new_state = badge_event_new_input_state(ev);
159 uint8_t new_buttons = new_state & (old_state ^ new_state);
160
161 if(new_buttons & (BADGE_EVENT_KEY_BTN_A | BADGE_EVENT_KEY_BTN_B)) {
162 return;
163 } else if((new_buttons & BADGE_EVENT_KEY_UP )) {
164 scroll_direction = -1;
165 } else if((new_buttons & BADGE_EVENT_KEY_DOWN)) {
166 scroll_direction = 1;
167 } else {
168 scroll_direction = 0;
169 }
170
171 scroll_ticks = 0;
172 break;
173 }
174 case BADGE_EVENT_GAME_TICK:
175 {
176 --scroll_ticks;
177 break;
178 }
179 }
180 }
181 }
This page took 0.061865 seconds and 5 git commands to generate.