Dice plugin now works on remote and uses the action api ; cleaned up the code a little bit as well
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13935 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
b2585ce3de
commit
21cc9a6d76
2 changed files with 110 additions and 187 deletions
|
@ -17,213 +17,133 @@
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/*****************************************************************************
|
|
||||||
Dice by lostlogic
|
|
||||||
|
|
||||||
use left and right to pick between sides and number of dice
|
|
||||||
use up and down to select number of sides or number of dice
|
|
||||||
use select or play to roll the dice
|
|
||||||
use stop to exit
|
|
||||||
|
|
||||||
*****************************************************************************/
|
|
||||||
|
|
||||||
#include "plugin.h"
|
#include "plugin.h"
|
||||||
|
#include "pluginlib_actions.h"
|
||||||
|
|
||||||
#define MAX_DICE 12
|
#define MAX_DICES 12
|
||||||
#define NUM_SIDE_CHOICES 8
|
#define INITIAL_NB_DICES 1
|
||||||
#if (CONFIG_KEYPAD == PLAYER_PAD)
|
|
||||||
#define START_DICE_ROW 1
|
|
||||||
#define ROWS 1
|
|
||||||
#define LINE_LENGTH 50
|
|
||||||
#define SELECTIONS_ROW 0
|
|
||||||
#else
|
|
||||||
#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 0
|
|
||||||
#define ROWS 2
|
|
||||||
#define LINE_LENGTH 26
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define min(x,y) (x<y?x:y)
|
|
||||||
#define max(x,y) (x>y?x:y)
|
|
||||||
|
|
||||||
#if CONFIG_KEYPAD == RECORDER_PAD
|
|
||||||
#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_ON BUTTON_ON
|
|
||||||
#define DICE_BUTTON_OFF BUTTON_OFF
|
|
||||||
#define DICE_BUTTON_SELECT BUTTON_SELECT
|
|
||||||
|
|
||||||
#elif CONFIG_KEYPAD == ONDIO_PAD
|
|
||||||
#define DICE_BUTTON_ON (BUTTON_MENU|BUTTON_OFF)
|
|
||||||
#define DICE_BUTTON_OFF BUTTON_OFF
|
|
||||||
#define DICE_BUTTON_SELECT (BUTTON_MENU|BUTTON_REL)
|
|
||||||
|
|
||||||
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
|
|
||||||
#define DICE_BUTTON_ON BUTTON_ON
|
|
||||||
#define DICE_BUTTON_OFF BUTTON_OFF
|
|
||||||
#define DICE_BUTTON_SELECT BUTTON_SELECT
|
|
||||||
|
|
||||||
#define DICE_BUTTON_RC_OFF BUTTON_RC_STOP
|
|
||||||
|
|
||||||
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD)
|
|
||||||
#define DICE_BUTTON_ON (BUTTON_SELECT|BUTTON_PLAY)
|
|
||||||
#define DICE_BUTTON_OFF (BUTTON_PLAY|BUTTON_REPEAT)
|
|
||||||
#define DICE_BUTTON_SELECT BUTTON_SELECT
|
|
||||||
|
|
||||||
#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_ON BUTTON_PLAY
|
|
||||||
#define DICE_BUTTON_OFF BUTTON_POWER
|
|
||||||
#define DICE_BUTTON_SELECT BUTTON_SELECT
|
|
||||||
|
|
||||||
#elif CONFIG_KEYPAD == GIGABEAT_PAD
|
|
||||||
#define DICE_BUTTON_ON BUTTON_A
|
|
||||||
#define DICE_BUTTON_OFF BUTTON_POWER
|
|
||||||
#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_ON BUTTON_PLAY
|
|
||||||
#define DICE_BUTTON_OFF BUTTON_POWER
|
|
||||||
#define DICE_BUTTON_SELECT BUTTON_REW
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error DICE: Unsupported keypad
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
#define DICE_QUIT PLA_QUIT
|
||||||
|
#define DICE_ROLL PLA_START
|
||||||
|
|
||||||
|
const struct button_mapping* plugin_contexts[]={generic_actions};
|
||||||
|
|
||||||
|
struct dices
|
||||||
|
{
|
||||||
|
int values[MAX_DICES];
|
||||||
|
int total;
|
||||||
|
int nb_dices;
|
||||||
|
int nb_sides;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define PRINT_BUFFER_LENGTH MAX_DICES*4
|
||||||
PLUGIN_HEADER
|
PLUGIN_HEADER
|
||||||
/* 0, 1, 2, 3, 4, 5, 6, 7 */
|
|
||||||
static const int SIDES[] = { 3, 4, 6, 8, 10, 12, 20, 100 };
|
|
||||||
|
|
||||||
static struct plugin_api* rb;
|
static struct plugin_api* rb;
|
||||||
|
|
||||||
static int roll_dice(int dice[], const int num_dice, const int side_index);
|
void dice_init(struct dices* dice);
|
||||||
static void print_dice(const int dice[], const int total);
|
void dice_roll(struct dices* dice);
|
||||||
static bool dice_menu(int *num_dice, int *side_index);
|
void dice_print(struct dices* dice, struct screen* display);
|
||||||
|
bool dice_menu(struct dices* dice);
|
||||||
|
|
||||||
/* plugin entry point */
|
/* plugin entry point */
|
||||||
enum plugin_status plugin_start(struct plugin_api* api, void* parameter) {
|
enum plugin_status plugin_start(struct plugin_api* api, void* parameter) {
|
||||||
int side_index = 6;
|
|
||||||
int num_dice = 1;
|
|
||||||
bool exit = false;
|
|
||||||
int dice[MAX_DICE];
|
|
||||||
int total;
|
|
||||||
|
|
||||||
/* plugin init */
|
|
||||||
(void)parameter;
|
(void)parameter;
|
||||||
rb = api;
|
rb = api;
|
||||||
|
int i, action;
|
||||||
|
struct dices dice;
|
||||||
|
|
||||||
rb->lcd_clear_display();
|
dice_init(&dice);
|
||||||
rb->srand(*rb->current_tick);
|
rb->srand(*rb->current_tick);
|
||||||
/* end of plugin init */
|
|
||||||
|
|
||||||
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_ON:
|
|
||||||
case DICE_BUTTON_SELECT:
|
|
||||||
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:
|
|
||||||
exit=true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
if(!dice_menu(&dice))
|
||||||
|
return PLUGIN_OK;
|
||||||
|
dice_roll(&dice);
|
||||||
|
FOR_NB_SCREENS(i)
|
||||||
|
dice_print( &dice, rb->screens[i] );
|
||||||
|
while(true) {
|
||||||
|
action = pluginlib_getaction(rb, TIMEOUT_BLOCK,
|
||||||
|
plugin_contexts, 1);
|
||||||
|
switch(action) {
|
||||||
|
case DICE_ROLL:
|
||||||
|
dice_roll(&dice);
|
||||||
|
FOR_NB_SCREENS(i)
|
||||||
|
dice_print( &dice, rb->screens[i] );
|
||||||
break;
|
break;
|
||||||
} /* switch */
|
case DICE_QUIT:
|
||||||
} /* while */
|
return PLUGIN_OK;
|
||||||
|
}
|
||||||
return PLUGIN_OK;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Clears the dice array, rolls the dice and returns the sum */
|
void dice_init(struct dices* dice){
|
||||||
static int roll_dice(int dice[], const int num_dice, const int side_index) {
|
dice->nb_dices=INITIAL_NB_DICES;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dice_roll(struct dices* dice) {
|
||||||
int i;
|
int i;
|
||||||
int total = 0;
|
dice->total = 0;
|
||||||
rb->memset((void*)dice,0,MAX_DICE*sizeof(int));
|
for (i=0; i<dice->nb_dices; i++) {
|
||||||
for (i=0; i<num_dice; i++) {
|
dice->values[i] = rb->rand()%dice->nb_sides + 1;
|
||||||
dice[i] = rb->rand()%SIDES[side_index] + 1;
|
dice->total+=dice->values[i];
|
||||||
total+=dice[i];
|
|
||||||
}
|
}
|
||||||
return total;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prints the dice, and the sum of the dice values */
|
void dice_print_string_buffer(struct dices* dice, char* buffer,
|
||||||
static void print_dice(const int dice[], const int total) {
|
int start, int end){
|
||||||
const int dice_per_row = MAX_DICE/ROWS + (MAX_DICE%ROWS?1:0);
|
int i, written;
|
||||||
char showdice[LINE_LENGTH];
|
for (i=start; i<end; i++) {
|
||||||
int row;
|
written=rb->snprintf(buffer, PRINT_BUFFER_LENGTH,
|
||||||
rb->lcd_clear_display();
|
" %3d", dice->values[i]);
|
||||||
for (row=0; /*break;*/; row++) {
|
buffer=&(buffer[written]);
|
||||||
const int start = row*dice_per_row;
|
|
||||||
const int end = min(MAX_DICE,start+dice_per_row);
|
|
||||||
char *pointer = showdice;
|
|
||||||
int space = LINE_LENGTH;
|
|
||||||
int i;
|
|
||||||
rb->memset((void*)showdice,0,LINE_LENGTH*sizeof(char));
|
|
||||||
for (i=start; i<end && dice[i]>0; i++) {
|
|
||||||
int count = rb->snprintf(pointer,space," %3d",dice[i]);
|
|
||||||
pointer = &pointer[count];
|
|
||||||
space -= count;
|
|
||||||
}
|
|
||||||
if (i > start) {
|
|
||||||
rb->lcd_puts_scroll(0,START_DICE_ROW+row,showdice);
|
|
||||||
} else {
|
|
||||||
row--;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (i < end || end == MAX_DICE) break;
|
|
||||||
}
|
}
|
||||||
#if (CONFIG_KEYPAD == PLAYER_PAD)
|
|
||||||
(void)total;
|
|
||||||
#else
|
|
||||||
rb->lcd_puts(0,START_DICE_ROW+(++row)," ");
|
|
||||||
if (total > 0) {
|
|
||||||
rb->snprintf(showdice,LINE_LENGTH,"Total: %4d",total);
|
|
||||||
rb->lcd_puts(1,START_DICE_ROW+(++row),showdice);
|
|
||||||
}
|
|
||||||
while (row < ROWS+2) {
|
|
||||||
rb->lcd_puts(0,START_DICE_ROW+(++row)," ");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
rb->lcd_update();
|
|
||||||
}
|
}
|
||||||
static bool dice_menu(int *num_dice, int *side_index) {
|
|
||||||
|
void dice_print(struct dices* dice, struct screen* display){
|
||||||
|
char buffer[PRINT_BUFFER_LENGTH];
|
||||||
|
/* display characteristics */
|
||||||
|
int char_height, char_width;
|
||||||
|
display->getstringsize("M", &char_width, &char_height);
|
||||||
|
int display_nb_row=display->height/char_height;
|
||||||
|
int display_nb_col=display->width/char_width;
|
||||||
|
|
||||||
|
int nb_dices_per_line=display_nb_col/4;/* 4 char per dice displayed*/
|
||||||
|
int nb_lines_required=dice->nb_dices/nb_dices_per_line;
|
||||||
|
int current_row=0;
|
||||||
|
if(dice->nb_dices%nb_dices_per_line!=0)
|
||||||
|
nb_lines_required++;
|
||||||
|
display->clear_display();
|
||||||
|
if(display_nb_row<nb_lines_required){
|
||||||
|
/* Put everything on the same scrolling line */
|
||||||
|
dice_print_string_buffer(dice, buffer, 0, dice->nb_dices);
|
||||||
|
display->puts_scroll(0, current_row, buffer);
|
||||||
|
current_row++;
|
||||||
|
}else{
|
||||||
|
int start=0;
|
||||||
|
int end=0;
|
||||||
|
for(;current_row<nb_lines_required;current_row++){
|
||||||
|
end=start+nb_dices_per_line;
|
||||||
|
if(end>dice->nb_dices)
|
||||||
|
end=dice->nb_dices;
|
||||||
|
dice_print_string_buffer(dice, buffer, start, end);
|
||||||
|
display->puts(0, current_row, buffer);
|
||||||
|
start=end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rb->snprintf(buffer, PRINT_BUFFER_LENGTH, "Total: %4d", dice->total);
|
||||||
|
display->puts(0, current_row, buffer);
|
||||||
|
display->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool dice_menu(struct dices * dice) {
|
||||||
int selection;
|
int selection;
|
||||||
|
int side_index;
|
||||||
bool menu_quit = false, result = false;
|
bool menu_quit = false, result = false;
|
||||||
|
|
||||||
MENUITEM_STRINGLIST(menu,"Dice Menu",NULL,"Roll Dice","Number of Dice",
|
MENUITEM_STRINGLIST(menu,"Dice Menu",NULL,"Roll Dice","Number of Dice",
|
||||||
"Number of Sides","Quit");
|
"Number of Sides","Quit");
|
||||||
|
|
||||||
static const struct opt_items num_sides_option[8] = {
|
struct opt_items nb_sides_option[8] = {
|
||||||
{ "3", -1 },
|
{ "3", -1 },
|
||||||
{ "4", -1 },
|
{ "4", -1 },
|
||||||
{ "6", -1 },
|
{ "6", -1 },
|
||||||
|
@ -233,31 +153,32 @@ static bool dice_menu(int *num_dice, int *side_index) {
|
||||||
{ "20", -1 },
|
{ "20", -1 },
|
||||||
{ "100", -1 }
|
{ "100", -1 }
|
||||||
};
|
};
|
||||||
|
int nb_sides_values[] = { 3, 4, 6, 8, 10, 12, 20, 100 };
|
||||||
while (!menu_quit) {
|
while (!menu_quit) {
|
||||||
switch(rb->do_menu(&menu, &selection))
|
switch(rb->do_menu(&menu, &selection)){
|
||||||
{
|
|
||||||
case 0:
|
case 0:
|
||||||
menu_quit = true;
|
menu_quit = true;
|
||||||
result = true;
|
result = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
rb->set_int("Number of Dice", "", UNIT_INT, num_dice, NULL,
|
rb->set_int("Number of Dice", "", UNIT_INT, &(dice->nb_dices),
|
||||||
1, 1, MAX_DICE, NULL );
|
NULL, 1, 1, MAX_DICES, NULL );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
rb->set_option("Number of Sides", side_index, INT,
|
side_index=6;
|
||||||
num_sides_option, 8, NULL);
|
rb->set_option("Number of Sides", &side_index, INT,
|
||||||
|
nb_sides_option,
|
||||||
|
sizeof(nb_sides_values)/sizeof(int), NULL);
|
||||||
|
dice->nb_sides=nb_sides_values[side_index];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
menu_quit = true;
|
menu_quit = true;
|
||||||
result = false;
|
result = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -219,8 +219,9 @@ static const struct button_mapping generic_actions[] =
|
||||||
{
|
{
|
||||||
#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
|
#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
|
||||||
{PLA_QUIT, BUTTON_OFF, BUTTON_NONE},
|
{PLA_QUIT, BUTTON_OFF, BUTTON_NONE},
|
||||||
{PLA_QUIT, BUTTON_RC_STOP, BUTTON_NONE},
|
{PLA_QUIT, BUTTON_RC_STOP, BUTTON_NONE},
|
||||||
{PLA_START, BUTTON_ON, BUTTON_NONE},
|
{PLA_START, BUTTON_ON, BUTTON_NONE},
|
||||||
|
{PLA_START, BUTTON_RC_ON, BUTTON_NONE},
|
||||||
{PLA_MENU, BUTTON_MODE, BUTTON_NONE},
|
{PLA_MENU, BUTTON_MODE, BUTTON_NONE},
|
||||||
{PLA_FIRE, BUTTON_SELECT, BUTTON_NONE},
|
{PLA_FIRE, BUTTON_SELECT, BUTTON_NONE},
|
||||||
{PLA_FIRE_REPEAT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_NONE},
|
{PLA_FIRE_REPEAT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_NONE},
|
||||||
|
@ -233,6 +234,7 @@ static const struct button_mapping generic_actions[] =
|
||||||
#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
|
#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
|
||||||
{PLA_QUIT, BUTTON_POWER, BUTTON_NONE},
|
{PLA_QUIT, BUTTON_POWER, BUTTON_NONE},
|
||||||
{PLA_START, BUTTON_PLAY, BUTTON_NONE},
|
{PLA_START, BUTTON_PLAY, BUTTON_NONE},
|
||||||
|
{PLA_START, BUTTON_RC_PLAY, BUTTON_NONE},
|
||||||
{PLA_MENU, BUTTON_REC, BUTTON_NONE},
|
{PLA_MENU, BUTTON_REC, BUTTON_NONE},
|
||||||
{PLA_FIRE, BUTTON_SELECT, BUTTON_NONE},
|
{PLA_FIRE, BUTTON_SELECT, BUTTON_NONE},
|
||||||
{PLA_FIRE_REPEAT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_NONE},
|
{PLA_FIRE_REPEAT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_NONE},
|
||||||
|
|
Loading…
Reference in a new issue