diff options
Diffstat (limited to 'apps/plugins/reversi/reversi-gui.c')
-rw-r--r-- | apps/plugins/reversi/reversi-gui.c | 218 |
1 files changed, 149 insertions, 69 deletions
diff --git a/apps/plugins/reversi/reversi-gui.c b/apps/plugins/reversi/reversi-gui.c index 2ad27caf01..fa20e46bb6 100644 --- a/apps/plugins/reversi/reversi-gui.c +++ b/apps/plugins/reversi/reversi-gui.c | |||
@@ -32,11 +32,9 @@ Use the arrow keys to move cursor, and press TOGGLE to place a stone. | |||
32 | 32 | ||
33 | At any time during the game, press MENU to bring up the game menu with | 33 | At any time during the game, press MENU to bring up the game menu with |
34 | further options: | 34 | further options: |
35 | |||
36 | - Save | 35 | - Save |
37 | - Reload | 36 | - Reload |
38 | - Clear | 37 | - Clear |
39 | |||
40 | */ | 38 | */ |
41 | 39 | ||
42 | #include "plugin.h" | 40 | #include "plugin.h" |
@@ -139,6 +137,75 @@ static cursor_wrap_mode_t cursor_wrap_mode; | |||
139 | static bool quit_plugin; | 137 | static bool quit_plugin; |
140 | static bool game_finished; | 138 | static bool game_finished; |
141 | 139 | ||
140 | #ifdef HAVE_TOUCHSCREEN | ||
141 | #include "lib/pluginlib_touchscreen.h" | ||
142 | /* This uses the touchscreen library functions/structures. */ | ||
143 | |||
144 | /* This defines the number of buttons used; only used in this C file. */ | ||
145 | #define TOUCHBUTTON_COUNT 3 | ||
146 | |||
147 | /* Define the button locations, widths and heights */ | ||
148 | |||
149 | #if LCD_HEIGHT < LCD_WIDTH | ||
150 | /* Define Menu button x, y, width, height */ | ||
151 | #define B_MENU_X LEGEND_X(0) | ||
152 | #define B_MENU_Y (LCD_HEIGHT/4) | ||
153 | #define B_MENU_W (LCD_WIDTH-LEGEND_X(0)) | ||
154 | #define B_MENU_H (LCD_HEIGHT/4) | ||
155 | /* Define Quit Button x, y, width, height */ | ||
156 | #define B_QUIT_X LEGEND_X(0) | ||
157 | #define B_QUIT_Y (LCD_HEIGHT/2) | ||
158 | #define B_QUIT_W (LCD_WIDTH-LEGEND_X(0)) | ||
159 | #define B_QUIT_H (LCD_HEIGHT/4) | ||
160 | #else | ||
161 | /* Define Menu button x, y, width, height */ | ||
162 | #define B_MENU_X (LCD_WIDTH/2) | ||
163 | #define B_MENU_Y LEGEND_Y(0) | ||
164 | #define B_MENU_W (LCD_WIDTH/4) | ||
165 | #define B_MENU_H (2*CELL_HEIGHT) | ||
166 | /* Define Quit Button x, y, width, height */ | ||
167 | #define B_QUIT_X (LCD_WIDTH-LCD_WIDTH/4) | ||
168 | #define B_QUIT_Y LEGEND_Y(0) | ||
169 | #define B_QUIT_W (LCD_WIDTH/4) | ||
170 | #define B_QUIT_H (2*CELL_HEIGHT) | ||
171 | #endif | ||
172 | |||
173 | /* This is the button initialization/definition. The first element is the | ||
174 | * Viewport. This is defined in lcd.h, but the elements are: | ||
175 | * int x - X location of button/viewport | ||
176 | * int y - Y location of button/viewport | ||
177 | * int width - Width of button/viewport | ||
178 | * int height - Height of button/viewport | ||
179 | * int font - Font to be used on button/viewport | ||
180 | * int drawmode- Modes defined in lcd.h | ||
181 | * unsigned fg_pattern - foreground color | ||
182 | * unsigned bg_pattern - backbround color | ||
183 | * unsigned lss_pattern - Selector colors (currently unused) | ||
184 | * unsigned lse_pattern - | | ||
185 | * unsigned lst_pattern - \/ | ||
186 | * | ||
187 | * The rest of the touch button elements are: | ||
188 | * bool repeat - requires the area be held for the action | ||
189 | * int action - action this button will return | ||
190 | * bool invisible - Is this an invisible button? | ||
191 | * char *title - Specify a title | ||
192 | * fb_data *pixmap- Currently unused, but will allow for a graphic | ||
193 | */ | ||
194 | struct touchbutton reversi_buttons[TOUCHBUTTON_COUNT] = | ||
195 | { | ||
196 | { {B_MENU_X, B_MENU_Y, B_MENU_W, B_MENU_H, FONT_UI, | ||
197 | STYLE_DEFAULT, 0, 0xFFFF, 0, 0, 0}, | ||
198 | false, REVERSI_BUTTON_MENU, false, "Menu", NULL }, | ||
199 | |||
200 | { {B_QUIT_X, B_QUIT_Y, B_QUIT_W, B_QUIT_H, FONT_UI, | ||
201 | STYLE_DEFAULT, 0, 0xFFFF, 0, 0, 0}, | ||
202 | false, REVERSI_BUTTON_QUIT, false, "Quit", NULL }, | ||
203 | |||
204 | { {0, 0, XOFS+BOARD_WIDTH, YOFS+BOARD_HEIGHT, 0, | ||
205 | STYLE_DEFAULT, 0, 0xFFFF, 0, 0, 0}, | ||
206 | false, REVERSI_BUTTON_MAKE_MOVE, true, NULL, NULL } | ||
207 | }; | ||
208 | #endif | ||
142 | 209 | ||
143 | /* Initialises the state of the game (starts a new game) */ | 210 | /* Initialises the state of the game (starts a new game) */ |
144 | static void reversi_gui_init(void) { | 211 | static void reversi_gui_init(void) { |
@@ -184,8 +251,12 @@ static void reversi_gui_draw_cell(int x, int y, int color) { | |||
184 | int i; | 251 | int i; |
185 | if (color == WHITE) { | 252 | if (color == WHITE) { |
186 | for (i = 0; i < CELL_LINE_THICKNESS; i++) { | 253 | for (i = 0; i < CELL_LINE_THICKNESS; i++) { |
187 | rb->lcd_drawrect(x+STONE_MARGIN+i, y+STONE_MARGIN+i, | 254 | rb->lcd_drawrect( |
188 | CELL_WIDTH+1-2*(STONE_MARGIN+i), CELL_HEIGHT+1-2*(STONE_MARGIN+i)); | 255 | x+STONE_MARGIN+i, |
256 | y+STONE_MARGIN+i, | ||
257 | CELL_WIDTH+1-2*(STONE_MARGIN+i), | ||
258 | CELL_HEIGHT+1-2*(STONE_MARGIN+i) | ||
259 | ); | ||
189 | } | 260 | } |
190 | } else if (color == BLACK) { | 261 | } else if (color == BLACK) { |
191 | rb->lcd_fillrect(x+STONE_MARGIN, y+STONE_MARGIN, | 262 | rb->lcd_fillrect(x+STONE_MARGIN, y+STONE_MARGIN, |
@@ -249,6 +320,10 @@ static void reversi_gui_display_board(void) { | |||
249 | y = LEGEND_Y(r); | 320 | y = LEGEND_Y(r); |
250 | rb->lcd_drawrect(x, y, CELL_WIDTH+1, CELL_HEIGHT+1); | 321 | rb->lcd_drawrect(x, y, CELL_WIDTH+1, CELL_HEIGHT+1); |
251 | 322 | ||
323 | #if defined(HAVE_TOUCHSCREEN) | ||
324 | touchbutton_draw(reversi_buttons, TOUCHBUTTON_COUNT); | ||
325 | #endif | ||
326 | |||
252 | /* Update the screen */ | 327 | /* Update the screen */ |
253 | rb->lcd_update(); | 328 | rb->lcd_update(); |
254 | } | 329 | } |
@@ -299,7 +374,10 @@ static bool reversi_gui_choose_strategy( | |||
299 | break; | 374 | break; |
300 | } | 375 | } |
301 | } | 376 | } |
302 | result = rb->set_option(prompt, &index, INT, strategy_settings, num_items, NULL); | 377 | |
378 | result = | ||
379 | rb->set_option(prompt, &index, INT, strategy_settings, num_items, NULL); | ||
380 | |||
303 | (*player) = strategy_values[index]; | 381 | (*player) = strategy_values[index]; |
304 | 382 | ||
305 | if((*player)->init_func) | 383 | if((*player)->init_func) |
@@ -339,7 +417,8 @@ static bool reversi_gui_menu(void) { | |||
339 | break; | 417 | break; |
340 | 418 | ||
341 | case 4: /* Cursor wrap mode */ | 419 | case 4: /* Cursor wrap mode */ |
342 | num_items = sizeof(cursor_wrap_mode_values)/sizeof(cursor_wrap_mode_values[0]); | 420 | num_items = sizeof(cursor_wrap_mode_values) / |
421 | sizeof(cursor_wrap_mode_values[0]); | ||
343 | index = 0; | 422 | index = 0; |
344 | for (i = 0; i < num_items; i++) { | 423 | for (i = 0; i < num_items; i++) { |
345 | if (cursor_wrap_mode == cursor_wrap_mode_values[i]) { | 424 | if (cursor_wrap_mode == cursor_wrap_mode_values[i]) { |
@@ -372,7 +451,8 @@ static bool reversi_gui_menu(void) { | |||
372 | * Returns true iff the cursor would be really moved. In any case, the | 451 | * Returns true iff the cursor would be really moved. In any case, the |
373 | * new cursor position is stored in (new_row, new_col). | 452 | * new cursor position is stored in (new_row, new_col). |
374 | */ | 453 | */ |
375 | static bool reversi_gui_cursor_pos_vmove(int row_delta, int *new_row, int *new_col) { | 454 | static bool |
455 | reversi_gui_cursor_pos_vmove(int row_delta, int *new_row, int *new_col) { | ||
376 | *new_row = cur_row + row_delta; | 456 | *new_row = cur_row + row_delta; |
377 | *new_col = cur_col; | 457 | *new_col = cur_col; |
378 | 458 | ||
@@ -421,7 +501,8 @@ static bool reversi_gui_cursor_pos_vmove(int row_delta, int *new_row, int *new_c | |||
421 | * Returns true iff the cursor would be really moved. In any case, the | 501 | * Returns true iff the cursor would be really moved. In any case, the |
422 | * new cursor position is stored in (new_row, new_col). | 502 | * new cursor position is stored in (new_row, new_col). |
423 | */ | 503 | */ |
424 | static bool reversi_gui_cursor_pos_hmove(int col_delta, int *new_row, int *new_col) { | 504 | static bool |
505 | reversi_gui_cursor_pos_hmove(int col_delta, int *new_row, int *new_col) { | ||
425 | *new_row = cur_row; | 506 | *new_row = cur_row; |
426 | *new_col = cur_col + col_delta; | 507 | *new_col = cur_col + col_delta; |
427 | 508 | ||
@@ -554,39 +635,62 @@ enum plugin_status plugin_start(const void *parameter) { | |||
554 | continue; | 635 | continue; |
555 | } | 636 | } |
556 | 637 | ||
638 | /*********************************************************************** | ||
639 | * Button handling code happens below here | ||
640 | **********************************************************************/ | ||
557 | button = rb->button_get(true); | 641 | button = rb->button_get(true); |
642 | |||
643 | /* The touchscreen buttons can act as true buttons so OR them in */ | ||
644 | #ifdef HAVE_TOUCHSCREEN | ||
645 | button |= touchbutton_get(reversi_buttons, button, TOUCHBUTTON_COUNT); | ||
646 | #endif | ||
558 | 647 | ||
559 | switch (button) { | 648 | /* All of these button presses wait for the release event */ |
649 | if(button&BUTTON_REL) { | ||
560 | #ifdef REVERSI_BUTTON_QUIT | 650 | #ifdef REVERSI_BUTTON_QUIT |
561 | /* Exit game */ | 651 | if(button&REVERSI_BUTTON_QUIT) { |
562 | case REVERSI_BUTTON_QUIT: | ||
563 | exit = true; | 652 | exit = true; |
564 | break; | 653 | } |
565 | #endif | 654 | #endif |
566 | 655 | ||
567 | #ifdef HAVE_TOUCHSCREEN | 656 | #ifdef HAVE_TOUCHSCREEN |
568 | case BUTTON_TOUCHSCREEN: | 657 | if(button&BUTTON_TOUCHSCREEN) { |
569 | button_x = rb->button_get_data() >> 16; | 658 | button_x = rb->button_get_data(); |
570 | button_y = rb->button_get_data() & 0xffff; | 659 | button_y = button_x & 0xffff; |
571 | if( (CELL_R(button_y)>(BOARD_SIZE-1)) || | 660 | button_x >>= 16; |
572 | (CELL_C(button_x)>(BOARD_SIZE-1)) ) | 661 | |
662 | /* Check if the click was in the gameboard, if so move cursor. | ||
663 | * This has to happen before MAKE_MOVE is processed. | ||
664 | */ | ||
665 | if( (CELL_R(button_y)<BOARD_SIZE) && | ||
666 | (CELL_C(button_x)<BOARD_SIZE) ) | ||
573 | { | 667 | { |
574 | break; | ||
575 | } else { | ||
576 | reversi_gui_move_cursor(CELL_R(button_y), CELL_C(button_x)); | 668 | reversi_gui_move_cursor(CELL_R(button_y), CELL_C(button_x)); |
577 | } | 669 | } |
670 | } | ||
578 | #endif | 671 | #endif |
579 | 672 | ||
580 | #ifdef REVERSI_BUTTON_ALT_MAKE_MOVE | 673 | if( (button&REVERSI_BUTTON_MENU) |
581 | case REVERSI_BUTTON_ALT_MAKE_MOVE: | 674 | #if defined(REVERSI_BUTTON_MENU_LONGPRESS) |
675 | && (lastbutton&BUTTON_REPEAT) | ||
582 | #endif | 676 | #endif |
583 | case REVERSI_BUTTON_MAKE_MOVE: | 677 | ) { |
584 | #ifdef REVERSI_BUTTON_MAKE_MOVE_PRE | 678 | if (reversi_gui_menu()) { |
585 | if ((button == REVERSI_BUTTON_MAKE_MOVE) | 679 | return PLUGIN_USB_CONNECTED; |
586 | && (lastbutton != REVERSI_BUTTON_MAKE_MOVE_PRE)) | 680 | } |
587 | break; | 681 | draw_screen = true; |
682 | } | ||
683 | |||
684 | if(button&REVERSI_BUTTON_MAKE_MOVE | ||
685 | #if defined(REVERSI_BUTTON_MAKE_MOVE_SHORTPRESS) | ||
686 | && !(lastbutton&BUTTON_REPEAT) | ||
588 | #endif | 687 | #endif |
589 | if (game_finished) break; | 688 | ) { |
689 | /* If you touch the game board instead of hitting menu after it | ||
690 | * has completed the game will exit out. | ||
691 | */ | ||
692 | if (game_finished) | ||
693 | break; | ||
590 | if (reversi_make_move(&game, cur_row, cur_col, cur_player) > 0) { | 694 | if (reversi_make_move(&game, cur_row, cur_col, cur_player) > 0) { |
591 | /* Move was made. Global changes on the board are possible */ | 695 | /* Move was made. Global changes on the board are possible */ |
592 | draw_screen = true; /* Redraw the screen next time */ | 696 | draw_screen = true; /* Redraw the screen next time */ |
@@ -607,66 +711,42 @@ enum plugin_status plugin_start(const void *parameter) { | |||
607 | /* Ignore any button presses during the splash */ | 711 | /* Ignore any button presses during the splash */ |
608 | rb->button_clear_queue(); | 712 | rb->button_clear_queue(); |
609 | } | 713 | } |
610 | break; | 714 | } |
715 | } | ||
716 | |||
717 | /* These button presses will run on a release or a repeat event */ | ||
718 | if(button&BUTTON_REL || button&BUTTON_REPEAT) { | ||
611 | /* Move cursor left */ | 719 | /* Move cursor left */ |
612 | #ifdef REVERSI_BUTTON_ALT_LEFT | 720 | if(button&REVERSI_BUTTON_LEFT) { |
613 | case REVERSI_BUTTON_ALT_LEFT: | ||
614 | case (REVERSI_BUTTON_ALT_LEFT | BUTTON_REPEAT): | ||
615 | #endif | ||
616 | case REVERSI_BUTTON_LEFT: | ||
617 | case (REVERSI_BUTTON_LEFT | BUTTON_REPEAT): | ||
618 | if (reversi_gui_cursor_pos_hmove(-1, &row, &col)) { | 721 | if (reversi_gui_cursor_pos_hmove(-1, &row, &col)) { |
619 | reversi_gui_move_cursor(row, col); | 722 | reversi_gui_move_cursor(row, col); |
620 | } | 723 | } |
621 | break; | 724 | } |
622 | |||
623 | /* Move cursor right */ | 725 | /* Move cursor right */ |
624 | #ifdef REVERSI_BUTTON_ALT_RIGHT | 726 | if(button&REVERSI_BUTTON_RIGHT) { |
625 | case REVERSI_BUTTON_ALT_RIGHT: | ||
626 | case (REVERSI_BUTTON_ALT_RIGHT | BUTTON_REPEAT): | ||
627 | #endif | ||
628 | case REVERSI_BUTTON_RIGHT: | ||
629 | case (REVERSI_BUTTON_RIGHT | BUTTON_REPEAT): | ||
630 | if (reversi_gui_cursor_pos_hmove(1, &row, &col)) { | 727 | if (reversi_gui_cursor_pos_hmove(1, &row, &col)) { |
631 | reversi_gui_move_cursor(row, col); | 728 | reversi_gui_move_cursor(row, col); |
632 | } | 729 | } |
633 | break; | 730 | } |
634 | |||
635 | /* Move cursor up */ | 731 | /* Move cursor up */ |
636 | case REVERSI_BUTTON_UP: | 732 | if(button&REVERSI_BUTTON_UP) { |
637 | case (REVERSI_BUTTON_UP | BUTTON_REPEAT): | ||
638 | if (reversi_gui_cursor_pos_vmove(-1, &row, &col)) { | 733 | if (reversi_gui_cursor_pos_vmove(-1, &row, &col)) { |
639 | reversi_gui_move_cursor(row, col); | 734 | reversi_gui_move_cursor(row, col); |
640 | } | 735 | } |
641 | break; | 736 | } |
642 | |||
643 | /* Move cursor down */ | 737 | /* Move cursor down */ |
644 | case REVERSI_BUTTON_DOWN: | 738 | if(button&REVERSI_BUTTON_DOWN) { |
645 | case (REVERSI_BUTTON_DOWN | BUTTON_REPEAT): | ||
646 | if (reversi_gui_cursor_pos_vmove(1, &row, &col)) { | 739 | if (reversi_gui_cursor_pos_vmove(1, &row, &col)) { |
647 | reversi_gui_move_cursor(row, col); | 740 | reversi_gui_move_cursor(row, col); |
648 | } | 741 | } |
649 | break; | 742 | } |
650 | 743 | } | |
651 | case REVERSI_BUTTON_MENU: | ||
652 | #ifdef REVERSI_BUTTON_MENU_PRE | ||
653 | if (lastbutton != REVERSI_BUTTON_MENU_PRE) { | ||
654 | break; | ||
655 | } | ||
656 | #endif | ||
657 | if (reversi_gui_menu()) { | ||
658 | return PLUGIN_USB_CONNECTED; | ||
659 | } | ||
660 | draw_screen = true; | ||
661 | break; | ||
662 | 744 | ||
663 | default: | 745 | if (rb->default_event_handler(button) == SYS_USB_CONNECTED) { |
664 | if (rb->default_event_handler(button) == SYS_USB_CONNECTED) { | 746 | /* Quit if USB has been connected */ |
665 | /* Quit if USB has been connected */ | 747 | return PLUGIN_USB_CONNECTED; |
666 | return PLUGIN_USB_CONNECTED; | ||
667 | } | ||
668 | break; | ||
669 | } | 748 | } |
749 | |||
670 | if (button != BUTTON_NONE) { | 750 | if (button != BUTTON_NONE) { |
671 | lastbutton = button; | 751 | lastbutton = button; |
672 | } | 752 | } |