diff options
author | Kevin Ferrare <kevin@rockbox.org> | 2007-07-20 02:47:37 +0000 |
---|---|---|
committer | Kevin Ferrare <kevin@rockbox.org> | 2007-07-20 02:47:37 +0000 |
commit | 21cc9a6d761bf1d99a9938a13259c19abe23d281 (patch) | |
tree | 0e66da536e6f50b9c5b375b524348d1ce37f1026 | |
parent | b2585ce3de5180e6a690128de5540709c5bbf955 (diff) | |
download | rockbox-21cc9a6d761bf1d99a9938a13259c19abe23d281.tar.gz rockbox-21cc9a6d761bf1d99a9938a13259c19abe23d281.zip |
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
-rw-r--r-- | apps/plugins/dice.c | 289 | ||||
-rw-r--r-- | apps/plugins/lib/pluginlib_actions.h | 4 |
2 files changed, 108 insertions, 185 deletions
diff --git a/apps/plugins/dice.c b/apps/plugins/dice.c index 0875248bdc..73927372a6 100644 --- a/apps/plugins/dice.c +++ b/apps/plugins/dice.c | |||
@@ -17,213 +17,133 @@ | |||
17 | * | 17 | * |
18 | ****************************************************************************/ | 18 | ****************************************************************************/ |
19 | 19 | ||
20 | /***************************************************************************** | ||
21 | Dice by lostlogic | ||
22 | |||
23 | use left and right to pick between sides and number of dice | ||
24 | use up and down to select number of sides or number of dice | ||
25 | use select or play to roll the dice | ||
26 | use stop to exit | ||
27 | |||
28 | *****************************************************************************/ | ||
29 | |||
30 | #include "plugin.h" | 20 | #include "plugin.h" |
21 | #include "pluginlib_actions.h" | ||
31 | 22 | ||
32 | #define MAX_DICE 12 | 23 | #define MAX_DICES 12 |
33 | #define NUM_SIDE_CHOICES 8 | 24 | #define INITIAL_NB_DICES 1 |
34 | #if (CONFIG_KEYPAD == PLAYER_PAD) | ||
35 | #define START_DICE_ROW 1 | ||
36 | #define ROWS 1 | ||
37 | #define LINE_LENGTH 50 | ||
38 | #define SELECTIONS_ROW 0 | ||
39 | #else | ||
40 | #if (CONFIG_KEYPAD == RECORDER_PAD) || (CONFIG_KEYPAD == ONDIO_PAD) | ||
41 | #define START_DICE_ROW 0 | ||
42 | #define ROWS 3 | ||
43 | #define LINE_LENGTH 18 | ||
44 | #else | ||
45 | #define START_DICE_ROW 0 | ||
46 | #define ROWS 2 | ||
47 | #define LINE_LENGTH 26 | ||
48 | #endif | ||
49 | #endif | ||
50 | |||
51 | #define min(x,y) (x<y?x:y) | ||
52 | #define max(x,y) (x>y?x:y) | ||
53 | |||
54 | #if CONFIG_KEYPAD == RECORDER_PAD | ||
55 | #define DICE_BUTTON_ON BUTTON_ON | ||
56 | #define DICE_BUTTON_OFF BUTTON_OFF | ||
57 | #define DICE_BUTTON_SELECT BUTTON_PLAY | ||
58 | |||
59 | #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD | ||
60 | #define DICE_BUTTON_ON BUTTON_ON | ||
61 | #define DICE_BUTTON_OFF BUTTON_OFF | ||
62 | #define DICE_BUTTON_SELECT BUTTON_SELECT | ||
63 | |||
64 | #elif CONFIG_KEYPAD == ONDIO_PAD | ||
65 | #define DICE_BUTTON_ON (BUTTON_MENU|BUTTON_OFF) | ||
66 | #define DICE_BUTTON_OFF BUTTON_OFF | ||
67 | #define DICE_BUTTON_SELECT (BUTTON_MENU|BUTTON_REL) | ||
68 | |||
69 | #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD) | ||
70 | #define DICE_BUTTON_ON BUTTON_ON | ||
71 | #define DICE_BUTTON_OFF BUTTON_OFF | ||
72 | #define DICE_BUTTON_SELECT BUTTON_SELECT | ||
73 | |||
74 | #define DICE_BUTTON_RC_OFF BUTTON_RC_STOP | ||
75 | |||
76 | #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) | ||
77 | #define DICE_BUTTON_ON (BUTTON_SELECT|BUTTON_PLAY) | ||
78 | #define DICE_BUTTON_OFF (BUTTON_PLAY|BUTTON_REPEAT) | ||
79 | #define DICE_BUTTON_SELECT BUTTON_SELECT | ||
80 | |||
81 | #elif CONFIG_KEYPAD == PLAYER_PAD | ||
82 | #define DICE_BUTTON_ON (BUTTON_ON|BUTTON_REPEAT) | ||
83 | #define DICE_BUTTON_OFF BUTTON_MENU | ||
84 | #define DICE_BUTTON_SELECT BUTTON_ON | ||
85 | |||
86 | #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD | ||
87 | #define DICE_BUTTON_ON BUTTON_PLAY | ||
88 | #define DICE_BUTTON_OFF BUTTON_POWER | ||
89 | #define DICE_BUTTON_SELECT BUTTON_SELECT | ||
90 | |||
91 | #elif CONFIG_KEYPAD == GIGABEAT_PAD | ||
92 | #define DICE_BUTTON_ON BUTTON_A | ||
93 | #define DICE_BUTTON_OFF BUTTON_POWER | ||
94 | #define DICE_BUTTON_SELECT BUTTON_SELECT | ||
95 | |||
96 | #elif CONFIG_KEYPAD == SANSA_E200_PAD | ||
97 | #define DICE_BUTTON_ON BUTTON_UP | ||
98 | #define DICE_BUTTON_OFF BUTTON_POWER | ||
99 | #define DICE_BUTTON_SELECT BUTTON_SELECT | ||
100 | |||
101 | #elif CONFIG_KEYPAD == IRIVER_H10_PAD | ||
102 | #define DICE_BUTTON_ON BUTTON_PLAY | ||
103 | #define DICE_BUTTON_OFF BUTTON_POWER | ||
104 | #define DICE_BUTTON_SELECT BUTTON_REW | ||
105 | |||
106 | #else | ||
107 | #error DICE: Unsupported keypad | ||
108 | #endif | ||
109 | 25 | ||
26 | #define DICE_QUIT PLA_QUIT | ||
27 | #define DICE_ROLL PLA_START | ||
110 | 28 | ||
29 | const struct button_mapping* plugin_contexts[]={generic_actions}; | ||
111 | 30 | ||
31 | struct dices | ||
32 | { | ||
33 | int values[MAX_DICES]; | ||
34 | int total; | ||
35 | int nb_dices; | ||
36 | int nb_sides; | ||
37 | }; | ||
112 | 38 | ||
39 | #define PRINT_BUFFER_LENGTH MAX_DICES*4 | ||
113 | PLUGIN_HEADER | 40 | PLUGIN_HEADER |
114 | /* 0, 1, 2, 3, 4, 5, 6, 7 */ | ||
115 | static const int SIDES[] = { 3, 4, 6, 8, 10, 12, 20, 100 }; | ||
116 | 41 | ||
117 | static struct plugin_api* rb; | 42 | static struct plugin_api* rb; |
118 | 43 | ||
119 | static int roll_dice(int dice[], const int num_dice, const int side_index); | 44 | void dice_init(struct dices* dice); |
120 | static void print_dice(const int dice[], const int total); | 45 | void dice_roll(struct dices* dice); |
121 | static bool dice_menu(int *num_dice, int *side_index); | 46 | void dice_print(struct dices* dice, struct screen* display); |
47 | bool dice_menu(struct dices* dice); | ||
122 | 48 | ||
123 | /* plugin entry point */ | 49 | /* plugin entry point */ |
124 | enum plugin_status plugin_start(struct plugin_api* api, void* parameter) { | 50 | enum plugin_status plugin_start(struct plugin_api* api, void* parameter) { |
125 | int side_index = 6; | ||
126 | int num_dice = 1; | ||
127 | bool exit = false; | ||
128 | int dice[MAX_DICE]; | ||
129 | int total; | ||
130 | |||
131 | /* plugin init */ | ||
132 | (void)parameter; | 51 | (void)parameter; |
133 | rb = api; | 52 | rb = api; |
53 | int i, action; | ||
54 | struct dices dice; | ||
134 | 55 | ||
135 | rb->lcd_clear_display(); | 56 | dice_init(&dice); |
136 | rb->srand(*rb->current_tick); | 57 | rb->srand(*rb->current_tick); |
137 | /* end of plugin init */ | ||
138 | |||
139 | if(!dice_menu(&num_dice, &side_index)) | ||
140 | exit = true; | ||
141 | else { | ||
142 | total = roll_dice(dice, num_dice, side_index); | ||
143 | print_dice(dice, total); | ||
144 | } | ||
145 | while(!exit) { | ||
146 | switch(rb->button_get(true)) { | ||
147 | case DICE_BUTTON_ON: | ||
148 | case DICE_BUTTON_SELECT: | ||
149 | total = roll_dice(dice, num_dice, side_index); | ||
150 | print_dice(dice, total); | ||
151 | break; | ||
152 | #ifdef DICE_BUTTON_RC_OFF | ||
153 | case DICE_BUTTON_RC_OFF: | ||
154 | #endif | ||
155 | case DICE_BUTTON_OFF: | ||
156 | exit=true; | ||
157 | break; | ||
158 | 58 | ||
159 | default: | 59 | if(!dice_menu(&dice)) |
60 | return PLUGIN_OK; | ||
61 | dice_roll(&dice); | ||
62 | FOR_NB_SCREENS(i) | ||
63 | dice_print( &dice, rb->screens[i] ); | ||
64 | while(true) { | ||
65 | action = pluginlib_getaction(rb, TIMEOUT_BLOCK, | ||
66 | plugin_contexts, 1); | ||
67 | switch(action) { | ||
68 | case DICE_ROLL: | ||
69 | dice_roll(&dice); | ||
70 | FOR_NB_SCREENS(i) | ||
71 | dice_print( &dice, rb->screens[i] ); | ||
160 | break; | 72 | break; |
161 | } /* switch */ | 73 | case DICE_QUIT: |
162 | } /* while */ | 74 | return PLUGIN_OK; |
75 | } | ||
76 | } | ||
77 | } | ||
163 | 78 | ||
164 | return PLUGIN_OK; | 79 | void dice_init(struct dices* dice){ |
80 | dice->nb_dices=INITIAL_NB_DICES; | ||
165 | } | 81 | } |
166 | 82 | ||
167 | /* Clears the dice array, rolls the dice and returns the sum */ | 83 | void dice_roll(struct dices* dice) { |
168 | static int roll_dice(int dice[], const int num_dice, const int side_index) { | ||
169 | int i; | 84 | int i; |
170 | int total = 0; | 85 | dice->total = 0; |
171 | rb->memset((void*)dice,0,MAX_DICE*sizeof(int)); | 86 | for (i=0; i<dice->nb_dices; i++) { |
172 | for (i=0; i<num_dice; i++) { | 87 | dice->values[i] = rb->rand()%dice->nb_sides + 1; |
173 | dice[i] = rb->rand()%SIDES[side_index] + 1; | 88 | dice->total+=dice->values[i]; |
174 | total+=dice[i]; | ||
175 | } | 89 | } |
176 | return total; | ||
177 | } | 90 | } |
178 | 91 | ||
179 | /* Prints the dice, and the sum of the dice values */ | 92 | void dice_print_string_buffer(struct dices* dice, char* buffer, |
180 | static void print_dice(const int dice[], const int total) { | 93 | int start, int end){ |
181 | const int dice_per_row = MAX_DICE/ROWS + (MAX_DICE%ROWS?1:0); | 94 | int i, written; |
182 | char showdice[LINE_LENGTH]; | 95 | for (i=start; i<end; i++) { |
183 | int row; | 96 | written=rb->snprintf(buffer, PRINT_BUFFER_LENGTH, |
184 | rb->lcd_clear_display(); | 97 | " %3d", dice->values[i]); |
185 | for (row=0; /*break;*/; row++) { | 98 | buffer=&(buffer[written]); |
186 | const int start = row*dice_per_row; | ||
187 | const int end = min(MAX_DICE,start+dice_per_row); | ||
188 | char *pointer = showdice; | ||
189 | int space = LINE_LENGTH; | ||
190 | int i; | ||
191 | rb->memset((void*)showdice,0,LINE_LENGTH*sizeof(char)); | ||
192 | for (i=start; i<end && dice[i]>0; i++) { | ||
193 | int count = rb->snprintf(pointer,space," %3d",dice[i]); | ||
194 | pointer = &pointer[count]; | ||
195 | space -= count; | ||
196 | } | ||
197 | if (i > start) { | ||
198 | rb->lcd_puts_scroll(0,START_DICE_ROW+row,showdice); | ||
199 | } else { | ||
200 | row--; | ||
201 | break; | ||
202 | } | ||
203 | if (i < end || end == MAX_DICE) break; | ||
204 | } | 99 | } |
205 | #if (CONFIG_KEYPAD == PLAYER_PAD) | 100 | } |
206 | (void)total; | 101 | |
207 | #else | 102 | void dice_print(struct dices* dice, struct screen* display){ |
208 | rb->lcd_puts(0,START_DICE_ROW+(++row)," "); | 103 | char buffer[PRINT_BUFFER_LENGTH]; |
209 | if (total > 0) { | 104 | /* display characteristics */ |
210 | rb->snprintf(showdice,LINE_LENGTH,"Total: %4d",total); | 105 | int char_height, char_width; |
211 | rb->lcd_puts(1,START_DICE_ROW+(++row),showdice); | 106 | display->getstringsize("M", &char_width, &char_height); |
212 | } | 107 | int display_nb_row=display->height/char_height; |
213 | while (row < ROWS+2) { | 108 | int display_nb_col=display->width/char_width; |
214 | rb->lcd_puts(0,START_DICE_ROW+(++row)," "); | 109 | |
110 | int nb_dices_per_line=display_nb_col/4;/* 4 char per dice displayed*/ | ||
111 | int nb_lines_required=dice->nb_dices/nb_dices_per_line; | ||
112 | int current_row=0; | ||
113 | if(dice->nb_dices%nb_dices_per_line!=0) | ||
114 | nb_lines_required++; | ||
115 | display->clear_display(); | ||
116 | if(display_nb_row<nb_lines_required){ | ||
117 | /* Put everything on the same scrolling line */ | ||
118 | dice_print_string_buffer(dice, buffer, 0, dice->nb_dices); | ||
119 | display->puts_scroll(0, current_row, buffer); | ||
120 | current_row++; | ||
121 | }else{ | ||
122 | int start=0; | ||
123 | int end=0; | ||
124 | for(;current_row<nb_lines_required;current_row++){ | ||
125 | end=start+nb_dices_per_line; | ||
126 | if(end>dice->nb_dices) | ||
127 | end=dice->nb_dices; | ||
128 | dice_print_string_buffer(dice, buffer, start, end); | ||
129 | display->puts(0, current_row, buffer); | ||
130 | start=end; | ||
131 | } | ||
215 | } | 132 | } |
216 | #endif | 133 | rb->snprintf(buffer, PRINT_BUFFER_LENGTH, "Total: %4d", dice->total); |
217 | rb->lcd_update(); | 134 | display->puts(0, current_row, buffer); |
135 | display->update(); | ||
218 | } | 136 | } |
219 | static bool dice_menu(int *num_dice, int *side_index) { | 137 | |
138 | bool dice_menu(struct dices * dice) { | ||
220 | int selection; | 139 | int selection; |
140 | int side_index; | ||
221 | bool menu_quit = false, result = false; | 141 | bool menu_quit = false, result = false; |
222 | 142 | ||
223 | MENUITEM_STRINGLIST(menu,"Dice Menu",NULL,"Roll Dice","Number of Dice", | 143 | MENUITEM_STRINGLIST(menu,"Dice Menu",NULL,"Roll Dice","Number of Dice", |
224 | "Number of Sides","Quit"); | 144 | "Number of Sides","Quit"); |
225 | 145 | ||
226 | static const struct opt_items num_sides_option[8] = { | 146 | struct opt_items nb_sides_option[8] = { |
227 | { "3", -1 }, | 147 | { "3", -1 }, |
228 | { "4", -1 }, | 148 | { "4", -1 }, |
229 | { "6", -1 }, | 149 | { "6", -1 }, |
@@ -233,31 +153,32 @@ static bool dice_menu(int *num_dice, int *side_index) { | |||
233 | { "20", -1 }, | 153 | { "20", -1 }, |
234 | { "100", -1 } | 154 | { "100", -1 } |
235 | }; | 155 | }; |
236 | 156 | int nb_sides_values[] = { 3, 4, 6, 8, 10, 12, 20, 100 }; | |
237 | while (!menu_quit) { | 157 | while (!menu_quit) { |
238 | switch(rb->do_menu(&menu, &selection)) | 158 | switch(rb->do_menu(&menu, &selection)){ |
239 | { | ||
240 | case 0: | 159 | case 0: |
241 | menu_quit = true; | 160 | menu_quit = true; |
242 | result = true; | 161 | result = true; |
243 | break; | 162 | break; |
244 | 163 | ||
245 | case 1: | 164 | case 1: |
246 | rb->set_int("Number of Dice", "", UNIT_INT, num_dice, NULL, | 165 | rb->set_int("Number of Dice", "", UNIT_INT, &(dice->nb_dices), |
247 | 1, 1, MAX_DICE, NULL ); | 166 | NULL, 1, 1, MAX_DICES, NULL ); |
248 | break; | 167 | break; |
249 | 168 | ||
250 | case 2: | 169 | case 2: |
251 | rb->set_option("Number of Sides", side_index, INT, | 170 | side_index=6; |
252 | num_sides_option, 8, NULL); | 171 | rb->set_option("Number of Sides", &side_index, INT, |
172 | nb_sides_option, | ||
173 | sizeof(nb_sides_values)/sizeof(int), NULL); | ||
174 | dice->nb_sides=nb_sides_values[side_index]; | ||
253 | break; | 175 | break; |
254 | 176 | ||
255 | default: | 177 | default: |
256 | menu_quit = true; | 178 | menu_quit = true; |
257 | result = false; | 179 | result = false; |
258 | break; | 180 | break; |
259 | } | 181 | } |
260 | } | 182 | } |
261 | |||
262 | return result; | 183 | return result; |
263 | } | 184 | } |
diff --git a/apps/plugins/lib/pluginlib_actions.h b/apps/plugins/lib/pluginlib_actions.h index a5c199d209..26370d2b99 100644 --- a/apps/plugins/lib/pluginlib_actions.h +++ b/apps/plugins/lib/pluginlib_actions.h | |||
@@ -219,8 +219,9 @@ static const struct button_mapping generic_actions[] = | |||
219 | { | 219 | { |
220 | #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD) | 220 | #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD) |
221 | {PLA_QUIT, BUTTON_OFF, BUTTON_NONE}, | 221 | {PLA_QUIT, BUTTON_OFF, BUTTON_NONE}, |
222 | {PLA_QUIT, BUTTON_RC_STOP, BUTTON_NONE}, | 222 | {PLA_QUIT, BUTTON_RC_STOP, BUTTON_NONE}, |
223 | {PLA_START, BUTTON_ON, BUTTON_NONE}, | 223 | {PLA_START, BUTTON_ON, BUTTON_NONE}, |
224 | {PLA_START, BUTTON_RC_ON, BUTTON_NONE}, | ||
224 | {PLA_MENU, BUTTON_MODE, BUTTON_NONE}, | 225 | {PLA_MENU, BUTTON_MODE, BUTTON_NONE}, |
225 | {PLA_FIRE, BUTTON_SELECT, BUTTON_NONE}, | 226 | {PLA_FIRE, BUTTON_SELECT, BUTTON_NONE}, |
226 | {PLA_FIRE_REPEAT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_NONE}, | 227 | {PLA_FIRE_REPEAT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_NONE}, |
@@ -233,6 +234,7 @@ static const struct button_mapping generic_actions[] = | |||
233 | #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD | 234 | #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD |
234 | {PLA_QUIT, BUTTON_POWER, BUTTON_NONE}, | 235 | {PLA_QUIT, BUTTON_POWER, BUTTON_NONE}, |
235 | {PLA_START, BUTTON_PLAY, BUTTON_NONE}, | 236 | {PLA_START, BUTTON_PLAY, BUTTON_NONE}, |
237 | {PLA_START, BUTTON_RC_PLAY, BUTTON_NONE}, | ||
236 | {PLA_MENU, BUTTON_REC, BUTTON_NONE}, | 238 | {PLA_MENU, BUTTON_REC, BUTTON_NONE}, |
237 | {PLA_FIRE, BUTTON_SELECT, BUTTON_NONE}, | 239 | {PLA_FIRE, BUTTON_SELECT, BUTTON_NONE}, |
238 | {PLA_FIRE_REPEAT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_NONE}, | 240 | {PLA_FIRE_REPEAT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_NONE}, |