Permalink
Browse files

cleanup bgmenu_display a little

  • Loading branch information...
1 parent 3d9bba1 commit 3b95d8162ad79fcd7cbc6b5a1b3f4fc41f6078e4 @beerriot committed Mar 3, 2011
Showing with 86 additions and 37 deletions.
  1. +8 −0 include/bgmenu.h
  2. +78 −37 src/bgmenu.c
View
@@ -5,5 +5,13 @@ void bgmenu_forb(int row, uint8_t focus);
void bgmenu_focus(int row);
void bgmenu_blur(int row);
void bgmenu_write_prompt(int row, int val);
+int bgmenu_previous_prompt(int current);
+int bgmenu_next_prompt(int current);
+int bgmenu_refocus(int blur, int focus);
+int bgmenu_field_and_limits(int prompt, game_t *game,
+ int **field, int *min, int *max);
+void bgmenu_increase_prompt(int prompt, game_t *game);
+void bgmenu_decrease_prompt(int prompt, game_t *game);
uint8_t bgmenu_display(game_t *game);
+
#endif
View
@@ -17,6 +17,13 @@
#include "bggame.h"
#include "bgmenu.h"
+// line numbers of prompts
+#define P_WIDTH 0
+#define P_HEIGHT 1
+#define P_VARIETY 2
+#define P_START 3
+#define P_LAST 3
+
void bgmenu_forb(int row, uint8_t focus) {
lcd_goto_position(row, 10);
lcd_write_data(focus ? 0x7e : ' ');
@@ -37,66 +44,100 @@ void bgmenu_write_prompt(int row, int val) {
lcd_write_int16(val);
}
+int bgmenu_previous_prompt(int current) {
+ return bgmenu_refocus(current, (current == 0) ? P_LAST : current-1);
+}
+
+int bgmenu_next_prompt(int current) {
+ return bgmenu_refocus(current, (current == P_LAST) ? 0 : current+1);
+}
+
+int bgmenu_refocus(int blur, int focus) {
+ bgmenu_blur(blur);
+ bgmenu_focus(focus);
+ return focus;
+}
+
+int bgmenu_field_and_limits(int prompt, game_t *game,
+ int **field, int *min, int *max) {
+ switch (prompt) {
+ case P_WIDTH:
+ *field = &game->width;
+ *min = 10;
+ *max = MAX_WIDTH;
+ return 1;
+ case P_HEIGHT:
+ *field = &game->height;
+ *min = 3;
+ *max = MAX_HEIGHT;
+ return 1;
+ case P_VARIETY:
+ *field = &game->variety;
+ *min = 5;
+ *max = 26;
+ return 1;
+ default:
+ return 0; // start doesn't increase
+ }
+}
+
+void bgmenu_increase_prompt(int prompt, game_t *game) {
+ int *field, min, max;
+ if (bgmenu_field_and_limits(prompt, game, &field, &min, &max)) {
+ if (*field < max)
+ *field = *field+1;
+ bgmenu_write_prompt(prompt, *field);
+ }
+}
+
+void bgmenu_decrease_prompt(int prompt, game_t *game) {
+ int *field, min, max;
+ if (bgmenu_field_and_limits(prompt, game, &field, &min, &max)) {
+ if (*field > min)
+ *field = *field-1;
+ bgmenu_write_prompt(prompt, *field);
+ }
+}
+
uint8_t bgmenu_display(game_t *game) {
int ready = 0, prompt = 0, inactivity = 0;
uint8_t pressed_buttons;
- int *field, min, max;
nkbuttons_t button_state;
nkbuttons_clear(&button_state);
nklcd_stop_blinking();
lcd_clear_and_home();
- lcd_goto_position(0, 3);
+ lcd_goto_position(P_WIDTH, 3);
lcd_write_string(PSTR("width:"));
- bgmenu_write_prompt(0, game->width);
- bgmenu_focus(0);
+ bgmenu_write_prompt(P_WIDTH, game->width);
+ bgmenu_focus(P_WIDTH);
- lcd_goto_position(1, 2);
+ lcd_goto_position(P_HEIGHT, 2);
lcd_write_string(PSTR("height:"));
- bgmenu_write_prompt(1, game->height);
+ bgmenu_write_prompt(P_HEIGHT, game->height);
- lcd_goto_position(2, 1);
+ lcd_goto_position(P_VARIETY, 1);
lcd_write_string(PSTR("variety:"));
- bgmenu_write_prompt(2, game->variety);
+ bgmenu_write_prompt(P_VARIETY, game->variety);
- lcd_goto_position(3, 11);
+ lcd_goto_position(P_START, 11);
lcd_write_string(PSTR("start"));
while(!ready) {
if (nktimer_animate()) {
pressed_buttons = nkbuttons_read(&button_state);
if(pressed_buttons) {
inactivity = 0;
- if (pressed_buttons & B_SELECT && prompt == 3) {
+ if (pressed_buttons & B_SELECT && prompt == P_START) {
ready = 1;
- } else if (pressed_buttons & (B_UP | B_DOWN | B_SELECT)) {
- bgmenu_blur(prompt);
- if (pressed_buttons & B_UP) {
- prompt--;
- if (prompt < 0) prompt = 3;
- } else {
- prompt++;
- if (prompt > 3) prompt = 0;
- }
- bgmenu_focus(prompt);
+ } else if (pressed_buttons & B_UP) {
+ prompt = bgmenu_previous_prompt(prompt);
+ } else if (pressed_buttons & (B_DOWN | B_SELECT)) {
+ prompt = bgmenu_next_prompt(prompt);
+ } else if (pressed_buttons & B_LEFT) {
+ bgmenu_decrease_prompt(prompt, game);
} else {
- switch (prompt) {
- case 0: field = &game->width;
- min=10; max=MAX_WIDTH;
- break;
- case 1: field = &game->height;
- min=3; max=MAX_HEIGHT;
- break;
- default: field = &game->variety;
- min=5; max=26;
- }
- if (pressed_buttons & B_RIGHT &&
- *field < max)
- *field = *field+1;
- else if (pressed_buttons & B_LEFT &&
- *field > min)
- *field = *field-1;
- bgmenu_write_prompt(prompt, *field);
+ bgmenu_increase_prompt(prompt, game);
}
} else if (++inactivity > 600) {
return 0; // leave menu for "screen saver" after 10sec

0 comments on commit 3b95d81

Please sign in to comment.