13 MENU_SCROLL_TICKS
= 25
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" }
23 MENU_MARGIN_BOTTOM
= 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
29 static void badge_menu_show(char const *const *menu
,
31 size_t *first_visible
,
35 badge_framebuffer fb
= { { { 0 } } };
37 bool arrow_down
= true;
39 size_t first_used_row
= 0;
40 size_t used_rows
= MENU_ENTRIES_VISIBLE
;
42 if(n
<= MENU_ENTRIES_VISIBLE
) {
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;
54 if(*first_visible
== 0) {
58 if(*first_visible
+ MENU_ENTRIES_VISIBLE
>= n
) {
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
]);
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); }
73 badge_framebuffer_flush(&fb
);
76 size_t badge_menu(char const *const *menu
,
78 size_t *first_visible
,
81 unsigned scroll_ticks
= 0;
82 int scroll_direction
= 0;
85 if(scroll_ticks
== 0) {
86 if (scroll_direction
== 1 && selected
+ 1 < n
) {
88 } else if(scroll_direction
== -1 && selected
!= 0) {
92 badge_menu_show(menu
, n
, first_visible
, selected
, '*');
94 scroll_ticks
= MENU_SCROLL_TICKS
;
99 ev
= badge_event_wait();
100 switch(badge_event_type(ev
)) {
101 case BADGE_EVENT_USER_INPUT
:
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
);
107 if(new_buttons
& (BADGE_EVENT_KEY_BTN_A
| BADGE_EVENT_KEY_BTN_B
)) {
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;
114 scroll_direction
= 0;
120 case BADGE_EVENT_GAME_TICK
:
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;
135 if(scroll_ticks
== 0) {
136 if (scroll_direction
== 1 && first_visible
+ MENU_ENTRIES_VISIBLE
< n
) {
138 } else if(scroll_direction
== -1 && first_visible
!= 0) {
142 badge_menu_show(lines
, n
, &first_visible
, first_visible
+ (first_visible
+ 1 == n
? 0 : 1), ' ');
144 scroll_ticks
= MENU_SCROLL_TICKS
;
149 ev
= badge_event_wait();
150 switch(badge_event_type(ev
)) {
151 case BADGE_EVENT_USER_INPUT
:
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
);
157 if(new_buttons
& (BADGE_EVENT_KEY_BTN_A
| BADGE_EVENT_KEY_BTN_B
)) {
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;
164 scroll_direction
= 0;
170 case BADGE_EVENT_GAME_TICK
:
This page took 0.047355 seconds and 5 git commands to generate.