Textscroller f. Dateibrowser
[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 = 2,
23 MENU_MARGIN_BOTTOM = 3,
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(n <= MENU_ENTRIES_VISIBLE) {
43 *first_visible = 0;
44 used_rows = n;
45 first_used_row = (MENU_ENTRIES_VISIBLE - used_rows) / 2;
46 } else if(selected + 1 == n) {
47 *first_visible = n - MENU_ENTRIES_VISIBLE;
48 } else if(selected <= *first_visible) {
49 *first_visible = selected == 0 ? 0 : selected - 1;
50 } else if(selected - *first_visible + 2 > MENU_ENTRIES_VISIBLE) {
51 *first_visible = selected - MENU_ENTRIES_VISIBLE + 2;
52 }
53
54 if(*first_visible == 0) {
55 arrow_up = false;
56 }
57
58 if(*first_visible + MENU_ENTRIES_VISIBLE >= n) {
59 arrow_down = false;
60 }
61
62 for(size_t i = 0; i < used_rows; ++i) {
63 badge_framebuffer_render_text(&fb,
64 (int8_t) (MENU_MARGIN_LEFT + BADGE_FONT_WIDTH),
65 (int8_t) (MENU_MARGIN_TOP + (first_used_row + i) * MENU_ENTRIES_HEIGHT),
66 menu[*first_visible + i]);
67 }
68
69 badge_framebuffer_render_char(&fb, MENU_MARGIN_LEFT, MENU_MARGIN_TOP + MENU_ENTRIES_HEIGHT * (selected - *first_visible + first_used_row), selector);
70 if(arrow_up ) { badge_framebuffer_blt(&fb, MENU_MARGIN_LEFT, MENU_MARGIN_TOP, &arrows[MENU_ARROW_UP ], 0); }
71 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); }
72
73 badge_framebuffer_flush(&fb);
74 }
75
76 size_t badge_menu(char const *const *menu,
77 size_t n,
78 size_t *first_visible,
79 size_t selected)
80 {
81 unsigned scroll_ticks = 0;
82 int scroll_direction = 0;
83
84 for(;;) {
85 if(scroll_ticks == 0) {
86 if (scroll_direction == 1 && selected + 1 < n) {
87 ++selected;
88 } else if(scroll_direction == -1 && selected != 0) {
89 --selected;
90 }
91
92 badge_menu_show(menu, n, first_visible, selected, '*');
93
94 scroll_ticks = MENU_SCROLL_TICKS;
95 }
96
97 badge_event_t ev;
98
99 ev = badge_event_wait();
100 switch(badge_event_type(ev)) {
101 case BADGE_EVENT_USER_INPUT:
102 {
103 uint8_t old_state = badge_event_old_input_state(ev);
104 uint8_t new_state = badge_event_new_input_state(ev);
105 uint8_t new_buttons = new_state & (old_state ^ new_state);
106
107 if(new_buttons & (BADGE_EVENT_KEY_BTN_A | BADGE_EVENT_KEY_BTN_B)) {
108 return selected;
109 } else if((new_buttons & BADGE_EVENT_KEY_UP )) {
110 scroll_direction = -1;
111 } else if((new_buttons & BADGE_EVENT_KEY_DOWN)) {
112 scroll_direction = 1;
113 } else {
114 scroll_direction = 0;
115 }
116
117 scroll_ticks = 0;
118 break;
119 }
120 case BADGE_EVENT_GAME_TICK:
121 {
122 --scroll_ticks;
123 break;
124 }
125 }
126 }
127 }
128
129 void badge_scroll_text(char const *const *lines, size_t n) {
130 size_t first_visible = 0;
131 int scroll_direction = 0;
132 unsigned scroll_ticks = 0;
133
134 for(;;) {
135 if(scroll_ticks == 0) {
136 if (scroll_direction == 1 && first_visible + MENU_ENTRIES_VISIBLE < n) {
137 ++first_visible;
138 } else if(scroll_direction == -1 && first_visible != 0) {
139 --first_visible;
140 }
141
142 badge_menu_show(lines, n, &first_visible, first_visible + (first_visible + 1 == n ? 0 : 1), ' ');
143
144 scroll_ticks = MENU_SCROLL_TICKS;
145 }
146
147 badge_event_t ev;
148
149 ev = badge_event_wait();
150 switch(badge_event_type(ev)) {
151 case BADGE_EVENT_USER_INPUT:
152 {
153 uint8_t old_state = badge_event_old_input_state(ev);
154 uint8_t new_state = badge_event_new_input_state(ev);
155 uint8_t new_buttons = new_state & (old_state ^ new_state);
156
157 if(new_buttons & (BADGE_EVENT_KEY_BTN_A | BADGE_EVENT_KEY_BTN_B)) {
158 return;
159 } else if((new_buttons & BADGE_EVENT_KEY_UP )) {
160 scroll_direction = -1;
161 } else if((new_buttons & BADGE_EVENT_KEY_DOWN)) {
162 scroll_direction = 1;
163 } else {
164 scroll_direction = 0;
165 }
166
167 scroll_ticks = 0;
168 break;
169 }
170 case BADGE_EVENT_GAME_TICK:
171 {
172 --scroll_ticks;
173 break;
174 }
175 }
176 }
177 }
This page took 0.060051 seconds and 5 git commands to generate.