summaryrefslogtreecommitdiff
path: root/apps/plugins/dice.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/dice.c')
-rw-r--r--apps/plugins/dice.c235
1 files changed, 77 insertions, 158 deletions
diff --git a/apps/plugins/dice.c b/apps/plugins/dice.c
index 7c265dac4b..cfeceae539 100644
--- a/apps/plugins/dice.c
+++ b/apps/plugins/dice.c
@@ -28,129 +28,80 @@ use stop to exit
*****************************************************************************/
#include "plugin.h"
-#include "button.h"
-#include "lcd.h"
#define MAX_DICE 12
#define NUM_SIDE_CHOICES 8
#if (CONFIG_KEYPAD == PLAYER_PAD)
- #define SELECTIONS_SIZE 9
#define START_DICE_ROW 1
#define ROWS 1
#define LINE_LENGTH 50
#define SELECTIONS_ROW 0
#else
- #define SELECTIONS_SIZE 7
- #define SELECTIONS_ROW 4
#if (CONFIG_KEYPAD == RECORDER_PAD) || (CONFIG_KEYPAD == ONDIO_PAD)
#define START_DICE_ROW 0
#define ROWS 3
#define LINE_LENGTH 18
#else
- #define START_DICE_ROW 7
+ #define START_DICE_ROW 0
#define ROWS 2
#define LINE_LENGTH 26
#endif
#endif
-/* Values for selected */
-#define CHANGE_DICE 0
-#define CHANGE_SIDES 1
-#define EXIT 2
-
-
+#define min(x,y) (x<y?x:y)
+#define max(x,y) (x>y?x:y)
#if CONFIG_KEYPAD == RECORDER_PAD
-#define DICE_BUTTON_UP BUTTON_UP
-#define DICE_BUTTON_DOWN BUTTON_DOWN
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
-#define DICE_BUTTON_OFF BUTTON_OFF
#define DICE_BUTTON_ON BUTTON_ON
+#define DICE_BUTTON_OFF BUTTON_OFF
#define DICE_BUTTON_SELECT BUTTON_PLAY
#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
-#define DICE_BUTTON_UP BUTTON_UP
-#define DICE_BUTTON_DOWN BUTTON_DOWN
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
-#define DICE_BUTTON_OFF BUTTON_OFF
#define DICE_BUTTON_ON BUTTON_ON
+#define DICE_BUTTON_OFF BUTTON_OFF
#define DICE_BUTTON_SELECT BUTTON_SELECT
#elif CONFIG_KEYPAD == ONDIO_PAD
-#define DICE_BUTTON_UP BUTTON_UP
-#define DICE_BUTTON_DOWN BUTTON_DOWN
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
+#define DICE_BUTTON_ON (BUTTON_MENU|BUTTON_OFF)
#define DICE_BUTTON_OFF BUTTON_OFF
#define DICE_BUTTON_SELECT (BUTTON_MENU|BUTTON_REL)
-#define DICE_BUTTON_ON (BUTTON_MENU|BUTTON_OFF)
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
-#define DICE_BUTTON_UP BUTTON_UP
-#define DICE_BUTTON_DOWN BUTTON_DOWN
-#define DICE_BUTTON_OFF BUTTON_OFF
#define DICE_BUTTON_ON BUTTON_ON
+#define DICE_BUTTON_OFF BUTTON_OFF
#define DICE_BUTTON_SELECT BUTTON_SELECT
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
#define DICE_BUTTON_RC_OFF BUTTON_RC_STOP
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD)
-#define DICE_BUTTON_UP BUTTON_SCROLL_FWD
-#define DICE_BUTTON_DOWN BUTTON_SCROLL_BACK
-#define DICE_BUTTON_OFF (BUTTON_PLAY|BUTTON_REPEAT)
#define DICE_BUTTON_ON (BUTTON_SELECT|BUTTON_PLAY)
+#define DICE_BUTTON_OFF (BUTTON_PLAY|BUTTON_REPEAT)
#define DICE_BUTTON_SELECT BUTTON_SELECT
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
-#elif (CONFIG_KEYPAD == PLAYER_PAD)
-#define DICE_BUTTON_UP BUTTON_PLAY
-#define DICE_BUTTON_DOWN BUTTON_STOP
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
-#define DICE_BUTTON_SELECT BUTTON_ON
+#elif CONFIG_KEYPAD == PLAYER_PAD
#define DICE_BUTTON_ON (BUTTON_ON|BUTTON_REPEAT)
#define DICE_BUTTON_OFF BUTTON_MENU
+#define DICE_BUTTON_SELECT BUTTON_ON
-#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
-#define DICE_BUTTON_UP BUTTON_UP
-#define DICE_BUTTON_DOWN BUTTON_DOWN
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
-#define DICE_BUTTON_SELECT BUTTON_SELECT
+#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
#define DICE_BUTTON_ON BUTTON_PLAY
#define DICE_BUTTON_OFF BUTTON_POWER
-
-#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
-#define DICE_BUTTON_UP BUTTON_UP
-#define DICE_BUTTON_DOWN BUTTON_DOWN
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
#define DICE_BUTTON_SELECT BUTTON_SELECT
+
+#elif CONFIG_KEYPAD == GIGABEAT_PAD
#define DICE_BUTTON_ON BUTTON_POWER
#define DICE_BUTTON_OFF BUTTON_A
-
-#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
-#define DICE_BUTTON_UP BUTTON_SCROLL_UP
-#define DICE_BUTTON_DOWN BUTTON_SCROLL_DOWN
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
#define DICE_BUTTON_SELECT BUTTON_SELECT
+
+#elif CONFIG_KEYPAD == SANSA_E200_PAD
#define DICE_BUTTON_ON BUTTON_UP
#define DICE_BUTTON_OFF BUTTON_POWER
+#define DICE_BUTTON_SELECT BUTTON_SELECT
-#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
-#define DICE_BUTTON_UP BUTTON_SCROLL_UP
-#define DICE_BUTTON_DOWN BUTTON_SCROLL_DOWN
-#define DICE_BUTTON_LEFT BUTTON_LEFT
-#define DICE_BUTTON_RIGHT BUTTON_RIGHT
-#define DICE_BUTTON_SELECT BUTTON_REW
+#elif CONFIG_KEYPAD == IRIVER_H10_PAD
#define DICE_BUTTON_ON BUTTON_PLAY
#define DICE_BUTTON_OFF BUTTON_POWER
+#define DICE_BUTTON_SELECT BUTTON_REW
#else
#error DICE: Unsupported keypad
@@ -167,16 +118,15 @@ static struct plugin_api* rb;
static int roll_dice(int dice[], const int num_dice, const int side_index);
static void print_dice(const int dice[], const int total);
-static void print_selections(const int selected,
- const int num_dice,
- const int side_index);
-static void print_help(void);
+static bool dice_menu(int *num_dice, int *side_index);
/* plugin entry point */
enum plugin_status plugin_start(struct plugin_api* api, void* parameter) {
int side_index = 6;
int num_dice = 1;
- int selected = CHANGE_DICE;
+ bool exit = false;
+ int dice[MAX_DICE];
+ int total;
/* plugin init */
(void)parameter;
@@ -185,60 +135,25 @@ enum plugin_status plugin_start(struct plugin_api* api, void* parameter) {
rb->lcd_clear_display();
rb->srand(*rb->current_tick);
/* end of plugin init */
-
- print_selections(selected, num_dice, side_index);
- print_help();
- while(selected!=EXIT) {
+
+ if(!dice_menu(&num_dice, &side_index))
+ exit = true;
+ else {
+ total = roll_dice(dice, num_dice, side_index);
+ print_dice(dice, total);
+ }
+ while(!exit) {
switch(rb->button_get(true)) {
- case DICE_BUTTON_UP:
- if (selected==CHANGE_DICE) {
- num_dice%=MAX_DICE;
- num_dice++;
- } else if (selected==CHANGE_SIDES) {
- side_index++;
- side_index%=NUM_SIDE_CHOICES;
- }
- print_selections(selected, num_dice, side_index);
- break;
-
- case DICE_BUTTON_DOWN:
- if (selected==CHANGE_DICE) {
- num_dice--;
- if (num_dice==0) {
- num_dice=MAX_DICE;
- }
- } else if (selected==CHANGE_SIDES) {
- if (side_index==0) {
- side_index=NUM_SIDE_CHOICES;
- }
- side_index--;
- }
- print_selections(selected, num_dice, side_index);
- break;
-
- case DICE_BUTTON_LEFT:
- selected = CHANGE_DICE;
- print_selections(selected, num_dice, side_index);
- break;
-
- case DICE_BUTTON_RIGHT:
- selected = CHANGE_SIDES;
- print_selections(selected, num_dice, side_index);
- break;
-
case DICE_BUTTON_ON:
case DICE_BUTTON_SELECT:
- {
- int dice[MAX_DICE];
- int total = roll_dice(dice, num_dice, side_index);
- print_dice(dice, total);
- }
+ total = roll_dice(dice, num_dice, side_index);
+ print_dice(dice, total);
break;
#ifdef DICE_BUTTON_RC_OFF
case DICE_BUTTON_RC_OFF:
#endif
case DICE_BUTTON_OFF:
- selected = EXIT;
+ exit=true;
break;
default:
@@ -261,14 +176,12 @@ static int roll_dice(int dice[], const int num_dice, const int side_index) {
return total;
}
-#define min(x,y) (x<y?x:y)
-#define max(x,y) (x>y?x:y)
-
/* Prints the dice, and the sum of the dice values */
static void print_dice(const int dice[], const int total) {
const int dice_per_row = MAX_DICE/ROWS + (MAX_DICE%ROWS?1:0);
char showdice[LINE_LENGTH];
int row;
+ rb->lcd_clear_display();
for (row=0; /*break;*/; row++) {
const int start = row*dice_per_row;
const int end = min(MAX_DICE,start+dice_per_row);
@@ -305,43 +218,49 @@ static void print_dice(const int dice[], const int total) {
rb->lcd_update();
#endif
}
-
-/* Print the current user input choices */
-static void print_selections(const int selected,
- const int num_dice,
- const int side_index) {
- char buffer[SELECTIONS_SIZE];
-#if (CONFIG_KEYPAD == PLAYER_PAD)
- rb->snprintf(buffer, SELECTIONS_SIZE, "%c%2dd%c%3d",
- selected==CHANGE_DICE?'*':' ', num_dice,
- selected==CHANGE_SIDES?'*':' ', SIDES[side_index]);
- rb->lcd_puts(1,SELECTIONS_ROW,buffer);
-#else
- rb->snprintf(
- buffer,SELECTIONS_SIZE,"%2dd%3d",num_dice,SIDES[side_index]);
- rb->lcd_puts(1,SELECTIONS_ROW,buffer);
- if (selected==CHANGE_DICE) {
- rb->lcd_puts(1,SELECTIONS_ROW+1,"--");
- } else if (selected==CHANGE_SIDES) {
- rb->lcd_puts(1,SELECTIONS_ROW+1," ---");
+static bool dice_menu(int *num_dice, int *side_index) {
+ int selection;
+ bool menu_quit = false, result = false;
+
+ MENUITEM_STRINGLIST(menu,"Dice Menu",NULL,"Roll Dice","Number of Dice",
+ "Number of Sides","Quit");
+
+ static const struct opt_items num_sides_option[8] = {
+ { "3", -1 },
+ { "4", -1 },
+ { "6", -1 },
+ { "8", -1 },
+ { "10", -1 },
+ { "12", -1 },
+ { "20", -1 },
+ { "100", -1 }
+ };
+
+ while (!menu_quit) {
+ selection = rb->do_menu(&menu, &selection);
+ switch(selection)
+ {
+ case 0:
+ menu_quit = true;
+ result = true;
+ break;
+
+ case 1:
+ rb->set_int("Number of Dice", "", UNIT_INT, num_dice, NULL,
+ 1, 1, MAX_DICE, NULL );
+ break;
+
+ case 2:
+ rb->set_option("Number of Sides", side_index, INT,
+ num_sides_option, 8, NULL);
+ break;
+
+ default:
+ menu_quit = true;
+ result = false;
+ break;
+ }
}
-#endif
-#ifdef HAVE_LCD_BITMAP
- rb->lcd_update();
-#endif
-}
-static void print_help() {
-#if (CONFIG_KEYPAD == PLAYER_PAD)
- rb->lcd_puts_scroll(1,SELECTIONS_ROW, "</>, +/- setup");
- rb->lcd_puts_scroll(1,SELECTIONS_ROW+1, "on roll, menu exit");
-#else
- rb->lcd_puts(1,START_DICE_ROW,"</> pick dice/sides");
- rb->lcd_puts(1,START_DICE_ROW+1,"+/- change");
- rb->lcd_puts(1,START_DICE_ROW+2,"on/select roll");
- rb->lcd_puts(1,START_DICE_ROW+3,"off exit");
-#endif
-#ifdef HAVE_LCD_BITMAP
- rb->lcd_update();
-#endif
+ return result;
}