reversi: Fix viewport initialization. Improve button layout for portrait. Enable...
[maemo-rb.git] / apps / plugins / reversi / reversi-gui.c
blobe59a91e341dc9edf7f682d4635bf1a1aca73622c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2006 Alexander Levin
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 GUI part of reversi. Code is inspired by sudoku code by Dave Chapman
24 which is copyright (c) 2005 Dave Chapman and is released under the
25 GNU General Public License.
28 User instructions
29 -----------------
31 Use the arrow keys to move cursor, and press TOGGLE to place a stone.
33 At any time during the game, press MENU to bring up the game menu with
34 further options:
35 - Save
36 - Reload
37 - Clear
40 #include "plugin.h"
42 #include "reversi-game.h"
43 #include "reversi-strategy.h"
44 #include "reversi-gui.h"
46 #include "lib/playback_control.h"
50 /* This is initialized at the start of the plugin and used to determine the
51 * Appropriate game board size/legend spacing if the font is larger than a cell
52 * height/width.
54 static int font_width;
55 static int font_height;
57 /* Where the board begins */
58 #define XOFS 4
59 #define YOFS 4
61 #if LCD_HEIGHT > LCD_WIDTH
62 #define MARGIN_W (XOFS*2+1)
63 #define MARGIN_H (YOFS*2+1)
64 #define MARGIN_C_W 0
65 #define MARGIN_C_H 2
66 #else
67 #define MARGIN_W (XOFS*2 + 16)
68 #define MARGIN_H (YOFS*2+1)
69 #define MARGIN_C_W 1
70 #define MARGIN_C_H 0
71 #endif
73 #if ( (LCD_WIDTH - MARGIN_W) / (BOARD_SIZE+MARGIN_C_W)) < \
74 ( (LCD_HEIGHT - MARGIN_H) / (BOARD_SIZE+MARGIN_C_H))
76 #define CELL_PRE ( ( (LCD_WIDTH * LCD_PIXEL_ASPECT_WIDTH / \
77 LCD_PIXEL_ASPECT_HEIGHT) - MARGIN_W) / \
78 (BOARD_SIZE+MARGIN_C_W) )
80 #define CELL_WIDTH (CELL_PRE*LCD_PIXEL_ASPECT_HEIGHT / LCD_PIXEL_ASPECT_WIDTH)
81 #define CELL_HEIGHT (CELL_PRE)
82 #else
83 #define CELL_PRE ( ( (LCD_HEIGHT * LCD_PIXEL_ASPECT_HEIGHT / \
84 LCD_PIXEL_ASPECT_WIDTH) - MARGIN_H) / \
85 (BOARD_SIZE+MARGIN_C_H) )
87 #define CELL_WIDTH (CELL_PRE)
88 #define CELL_HEIGHT (CELL_PRE*LCD_PIXEL_ASPECT_WIDTH / LCD_PIXEL_ASPECT_HEIGHT)
89 #endif
91 /* Total width and height of the board without enclosing box */
92 #define BOARD_WIDTH (CELL_WIDTH*BOARD_SIZE)
93 #define BOARD_HEIGHT (CELL_HEIGHT*BOARD_SIZE)
95 /* Thickness of the white cells' lines */
96 #if (CELL_WIDTH >= 10) && (CELL_HEIGHT >= 10)
97 #define CELL_LINE_THICKNESS CELL_WIDTH/5
98 #else
99 #define CELL_LINE_THICKNESS 1
100 #endif
102 /* Margins within a cell */
103 #define STONE_MARGIN 2
105 #define CURSOR_MARGIN 1
107 /* Upper left corner of a cell */
108 #define CELL_X(c) (XOFS + (c)*CELL_WIDTH)
109 #define CELL_Y(r) (YOFS + (r)*CELL_HEIGHT)
111 /* Used for touchscreen to convert an X/Y location to a cell location */
112 #define CELL_C(x) (((x)-XOFS)/CELL_WIDTH)
113 #define CELL_R(y) (((y)-YOFS)/CELL_HEIGHT)
115 #if LCD_HEIGHT > LCD_WIDTH
116 #define LEGEND_X(lc) (CELL_X(lc))
117 #define LEGEND_Y(lr) ((CELL_HEIGHT > font_height) ? \
118 CELL_Y(BOARD_SIZE+lr) + YOFS + 1 : \
119 BOARD_HEIGHT + 2*YOFS + font_height*(lr-BOARD_SIZE))
120 #else
121 #define LEGEND_X(lc) (CELL_X(BOARD_SIZE+lc) + XOFS + 1)
122 #define LEGEND_Y(lr) (CELL_HEIGHT > font_height ? \
123 CELL_Y(lr) : font_height*(lr)+YOFS)
124 #endif
127 /* Board state */
128 static reversi_board_t game;
130 /* --- Setting values --- */
132 /* Playing strategies used by white and black players */
133 const game_strategy_t *white_strategy;
134 const game_strategy_t *black_strategy;
136 /* Cursor position */
137 static int cur_row, cur_col;
139 /* Color for the next move (BLACK/WHITE) */
140 static int cur_player;
142 /* Active cursor wrapping mode */
143 static cursor_wrap_mode_t cursor_wrap_mode;
145 static bool quit_plugin;
146 static bool game_finished;
148 #ifdef HAVE_TOUCHSCREEN
149 #include "lib/pluginlib_touchscreen.h"
150 /* This uses the touchscreen library functions/structures. */
152 /* This defines the number of buttons used; only used in this C file. */
153 #define TOUCHBUTTON_COUNT 3
155 /* Define the button locations, widths and heights */
157 #if LCD_HEIGHT < LCD_WIDTH
158 /* Define Menu button x, y, width, height */
159 #define B_MENU_X LEGEND_X(0)
160 #define B_MENU_Y (LCD_HEIGHT-LCD_HEIGHT/2)
161 #define B_MENU_W (LCD_WIDTH-LEGEND_X(0))
162 #define B_MENU_H (LCD_HEIGHT/4)
163 /* Define Quit Button x, y, width, height */
164 #define B_QUIT_X LEGEND_X(0)
165 #define B_QUIT_Y (LCD_HEIGHT-LCD_HEIGHT/4)
166 #define B_QUIT_W (LCD_WIDTH-LEGEND_X(0))
167 #define B_QUIT_H (LCD_HEIGHT/4)
168 #else
169 /* Define Menu button x, y, width, height */
170 #define B_MENU_X (LCD_WIDTH/2 - XOFS)
171 #define B_MENU_Y (CELL_HEIGHT*BOARD_SIZE+YOFS*2)
172 #define B_MENU_W (LCD_WIDTH/4 - XOFS)
173 #define B_MENU_H (2*CELL_HEIGHT)
174 /* Define Quit Button x, y, width, height */
175 #define B_QUIT_X (B_MENU_X + B_MENU_W + 1)
176 #define B_QUIT_Y (CELL_HEIGHT*BOARD_SIZE+YOFS*2)
177 #define B_QUIT_W (LCD_WIDTH/4 - XOFS)
178 #define B_QUIT_H (2*CELL_HEIGHT)
179 #endif
181 /* This is the button initialization/definition. The first element is the
182 * Viewport. This is defined in lcd.h, but the elements are:
183 * int x - X location of button/viewport
184 * int y - Y location of button/viewport
185 * int width - Width of button/viewport
186 * int height - Height of button/viewport
187 * int font - Font to be used on button/viewport
188 * int drawmode- Modes defined in lcd.h
189 * unsigned fg_pattern - foreground color
190 * unsigned bg_pattern - backbround color
191 * unsigned lss_pattern - Selector colors (currently unused)
192 * unsigned lse_pattern - |
193 * unsigned lst_pattern - \/
195 * The rest of the touch button elements are:
196 * bool repeat - requires the area be held for the action
197 * int action - action this button will return
198 * bool invisible - Is this an invisible button?
199 * char *title - Specify a title
200 * fb_data *pixmap- Currently unused, but will allow for a graphic
202 struct touchbutton reversi_buttons[TOUCHBUTTON_COUNT] =
204 { {B_MENU_X, B_MENU_Y, B_MENU_W, B_MENU_H, 0, FONT_UI,
205 -1, STYLE_DEFAULT, 0, 0xFFFF, 0, 0, 0},
206 false, REVERSI_BUTTON_MENU, false, "Menu", NULL },
208 { {B_QUIT_X, B_QUIT_Y, B_QUIT_W, B_QUIT_H, 0, FONT_UI,
209 -1, STYLE_DEFAULT, 0, 0xFFFF, 0, 0, 0},
210 false, REVERSI_BUTTON_QUIT, false, "Quit", NULL },
212 { {0, 0, XOFS+BOARD_WIDTH, YOFS+BOARD_HEIGHT, 0, 0,
213 -1, STYLE_DEFAULT, 0, 0xFFFF, 0, 0, 0},
214 false, REVERSI_BUTTON_MAKE_MOVE, true, NULL, NULL }
216 #endif
218 /* Initialises the state of the game (starts a new game) */
219 static void reversi_gui_init(void) {
220 reversi_init_game(&game);
221 game_finished = false;
222 cur_player = BLACK;
224 /* Place the cursor so that WHITE can make a move */
225 cur_row = 2;
226 cur_col = 3;
230 /* Draws the cursor in the specified cell. Cursor is drawn in the complement
231 * mode, i.e. drawing it twice will result in no changes on the screen.
233 static void reversi_gui_display_cursor(int row, int col) {
234 int old_mode, x, y;
235 old_mode = rb->lcd_get_drawmode();
236 x = CELL_X(col);
237 y = CELL_Y(row);
239 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
240 rb->lcd_drawline(x+CURSOR_MARGIN, y+CURSOR_MARGIN,
241 x+CELL_WIDTH-CURSOR_MARGIN, y+CELL_HEIGHT-CURSOR_MARGIN);
242 rb->lcd_drawline(x+CURSOR_MARGIN, y+CELL_HEIGHT-CURSOR_MARGIN,
243 x+CELL_WIDTH-CURSOR_MARGIN, y+CURSOR_MARGIN);
245 /* Draw the shadows */
246 rb->lcd_hline(x, x+CELL_WIDTH-1, YOFS-3);
247 rb->lcd_hline(x, x+CELL_WIDTH-1, YOFS+BOARD_HEIGHT+3);
248 rb->lcd_vline(XOFS-3, y, y+CELL_HEIGHT-1);
249 rb->lcd_vline(XOFS+BOARD_WIDTH+3, y, y+CELL_HEIGHT-1);
251 rb->lcd_set_drawmode(old_mode);
252 rb->lcd_update();
256 /* Draws the cell of the specified color (WHITE/BLACK) assuming that
257 * the upper left corner of the cell is at (x, y) */
258 static void reversi_gui_draw_cell(int x, int y, int color) {
259 int i;
260 if (color == WHITE) {
261 for (i = 0; i < CELL_LINE_THICKNESS; i++) {
262 rb->lcd_drawrect(
263 x+STONE_MARGIN+i,
264 y+STONE_MARGIN+i,
265 CELL_WIDTH+1-2*(STONE_MARGIN+i),
266 CELL_HEIGHT+1-2*(STONE_MARGIN+i)
269 } else if (color == BLACK) {
270 rb->lcd_fillrect(x+STONE_MARGIN, y+STONE_MARGIN,
271 CELL_WIDTH-STONE_MARGIN-1, CELL_HEIGHT-1-STONE_MARGIN);
272 } else {
273 /* Cell is free -> nothing to do */
278 /* Draws the complete screen */
279 static void reversi_gui_display_board(void) {
280 int x, y, r, c, x_width, x_height;
281 /* This viewport is used to draw a scrolling score */
282 struct viewport tempvp;
283 char buf[8];
285 /* Clear the display buffer */
286 rb->lcd_clear_display();
287 rb->lcd_set_drawmode(DRMODE_FG);
289 /* Thicker board box */
290 rb->lcd_drawrect(XOFS-1, YOFS-1, BOARD_WIDTH+3, BOARD_HEIGHT+3);
292 /* Draw the gridlines */
293 for (r=0, x=XOFS, y=YOFS; r<=BOARD_SIZE;
294 r++, x+=CELL_WIDTH, y+=CELL_HEIGHT) {
295 rb->lcd_hline(XOFS, XOFS+BOARD_WIDTH, y);
296 rb->lcd_vline(x, YOFS, YOFS+BOARD_HEIGHT);
299 /* Draw the stones. This is not the most efficient way but more readable */
300 for (r=0; r<BOARD_SIZE; r++) {
301 y = CELL_Y(r);
302 for (c=0; c<BOARD_SIZE; c++) {
303 x = CELL_X(c);
304 reversi_gui_draw_cell(x, y, game.board[r][c]);
308 /* Draw the cursor */
309 reversi_gui_display_cursor(cur_row, cur_col);
311 /* Draw the current score */
312 reversi_count_occupied_cells(&game, &r, &c);
313 rb->lcd_getstringsize("x", &x_width, &x_height);
315 x = LEGEND_X(0);
316 y = LEGEND_Y(0);
317 reversi_gui_draw_cell(x, y+(LEGEND_Y(1)-LEGEND_Y(0))/2-CELL_WIDTH/2, BLACK);
318 rb->snprintf(buf, sizeof(buf), "%01d", c);
320 rb->viewport_set_defaults(&tempvp, SCREEN_MAIN);
322 tempvp.x=x+CELL_WIDTH+2;
323 tempvp.y=y;
324 tempvp.width=LCD_WIDTH-tempvp.x;
325 tempvp.height=LEGEND_Y(1);
326 #if LCD_DEPTH > 1
327 tempvp.fg_pattern = LCD_BLACK;
328 tempvp.bg_pattern = LCD_WHITE;
329 #endif
331 rb->screens[SCREEN_MAIN]->set_viewport(&tempvp);
332 rb->lcd_puts_scroll(0, 0, buf);
333 rb->screens[SCREEN_MAIN]->set_viewport(NULL);
335 y = LEGEND_Y(1);
337 reversi_gui_draw_cell(x, y+(LEGEND_Y(1)-LEGEND_Y(0))/2-CELL_WIDTH/2, WHITE);
338 rb->snprintf(buf, sizeof(buf), "%01d", r);
340 tempvp.y=y;
341 rb->screens[SCREEN_MAIN]->set_viewport(&tempvp);
342 rb->lcd_puts_scroll(0, 0, buf);
343 rb->screens[SCREEN_MAIN]->set_viewport(NULL);
345 /* Draw the box around the current player */
346 r = (cur_player == BLACK ? 0 : 1);
347 y = LEGEND_Y(r)+(LEGEND_Y(1)-LEGEND_Y(0))/2-CELL_WIDTH/2;
348 rb->lcd_drawrect(x, y, CELL_WIDTH+1, CELL_HEIGHT+1);
350 #if defined(HAVE_TOUCHSCREEN)
351 touchbutton_draw(reversi_buttons, TOUCHBUTTON_COUNT);
352 #endif
354 /* Update the screen */
355 rb->lcd_update();
360 * Menu related stuff
363 /* Menu entries and the corresponding values for cursor wrap mode */
364 #define MENU_TEXT_WRAP_MODE "Cursor wrap mode"
365 static const struct opt_items cursor_wrap_mode_settings[] = {
366 { "Flat board", -1 },
367 { "Sphere", -1 },
368 { "Torus", -1 },
370 static const cursor_wrap_mode_t cursor_wrap_mode_values[3] = {
371 WRAP_FLAT, WRAP_SPHERE, WRAP_TORUS };
374 /* Menu entries and the corresponding values for available strategies */
375 #define MENU_TEXT_STRAT_WHITE "Strategy for white"
376 #define MENU_TEXT_STRAT_BLACK "Strategy for black"
378 static struct opt_items strategy_settings[] = {
379 { "Human", -1 },
380 { "Naive robot", -1 },
381 { "Simple robot", -1 },
382 //{ "AB robot", -1 },
384 static const game_strategy_t * const strategy_values[] = {
385 &strategy_human, &strategy_naive, &strategy_simple, /*&strategy_ab*/ };
388 /* Sets the strategy for the specified player. 'player' is the
389 pointer to the player to set the strategy for (actually,
390 either white_strategy or black_strategy). propmpt is the
391 text to show as the prompt in the menu */
392 static bool reversi_gui_choose_strategy(
393 const game_strategy_t **player, const char *prompt) {
394 int index = 0, i;
395 int num_items = sizeof(strategy_settings)/sizeof(strategy_settings[0]);
396 bool result;
398 for (i = 0; i < num_items; i++) {
399 if ((*player) == strategy_values[i]) {
400 index = i;
401 break;
405 result =
406 rb->set_option(prompt, &index, INT, strategy_settings, num_items, NULL);
408 (*player) = strategy_values[index];
410 if((*player)->init_func)
411 (*player)->init_func(&game);
413 return result;
417 /* Returns true iff USB ws connected while in the menu */
418 static bool reversi_gui_menu(void) {
419 int index, num_items, i;
420 int result;
422 MENUITEM_STRINGLIST(menu, "Reversi Menu", NULL,
423 "Start new game", "Pass the move",
424 MENU_TEXT_STRAT_BLACK, MENU_TEXT_STRAT_WHITE,
425 MENU_TEXT_WRAP_MODE, "Playback Control", "Quit");
427 result = rb->do_menu(&menu, NULL, NULL, false);
429 switch (result) {
430 case 0: /* Start a new game */
431 reversi_gui_init();
432 break;
434 case 1: /* Pass the move to the partner */
435 cur_player = reversi_flipped_color(cur_player);
436 break;
438 case 2: /* Strategy for black */
439 reversi_gui_choose_strategy(&black_strategy, MENU_TEXT_STRAT_BLACK);
440 break;
442 case 3: /* Strategy for white */
443 reversi_gui_choose_strategy(&white_strategy, MENU_TEXT_STRAT_WHITE);
444 break;
446 case 4: /* Cursor wrap mode */
447 num_items = sizeof(cursor_wrap_mode_values) /
448 sizeof(cursor_wrap_mode_values[0]);
449 index = 0;
450 for (i = 0; i < num_items; i++) {
451 if (cursor_wrap_mode == cursor_wrap_mode_values[i]) {
452 index = i;
453 break;
456 rb->set_option(MENU_TEXT_WRAP_MODE, &index, INT,
457 cursor_wrap_mode_settings, 3, NULL);
458 cursor_wrap_mode = cursor_wrap_mode_values[index];
459 break;
461 case 5:
462 playback_control(NULL);
463 break;
465 case 6: /* Quit */
466 quit_plugin = true;
467 break;
470 return (result == MENU_ATTACHED_USB);
474 /* Calculates the new cursor position if the user wants to move it
475 * vertically as specified by delta. Current wrap mode is respected.
476 * The cursor is not actually moved.
478 * Returns true iff the cursor would be really moved. In any case, the
479 * new cursor position is stored in (new_row, new_col).
481 static bool
482 reversi_gui_cursor_pos_vmove(int row_delta, int *new_row, int *new_col) {
483 *new_row = cur_row + row_delta;
484 *new_col = cur_col;
486 if (*new_row < 0) {
487 switch (cursor_wrap_mode) {
488 case WRAP_FLAT:
489 *new_row = cur_row;
490 break;
491 case WRAP_SPHERE:
492 *new_row = BOARD_SIZE - 1;
493 break;
494 case WRAP_TORUS:
495 *new_row = BOARD_SIZE - 1;
496 (*new_col)--;
497 if (*new_col < 0) {
498 *new_col = BOARD_SIZE - 1;
500 break;
502 } else if (*new_row >= BOARD_SIZE) {
503 switch (cursor_wrap_mode) {
504 case WRAP_FLAT:
505 *new_row = cur_row;
506 break;
507 case WRAP_SPHERE:
508 *new_row = 0;
509 break;
510 case WRAP_TORUS:
511 *new_row = 0;
512 (*new_col)++;
513 if (*new_col >= BOARD_SIZE) {
514 *new_col = 0;
516 break;
520 return (cur_row != (*new_row)) || (cur_col != (*new_col));
524 /* Calculates the new cursor position if the user wants to move it
525 * horisontally as specified by delta. Current wrap mode is respected.
526 * The cursor is not actually moved.
528 * Returns true iff the cursor would be really moved. In any case, the
529 * new cursor position is stored in (new_row, new_col).
531 static bool
532 reversi_gui_cursor_pos_hmove(int col_delta, int *new_row, int *new_col) {
533 *new_row = cur_row;
534 *new_col = cur_col + col_delta;
536 if (*new_col < 0) {
537 switch (cursor_wrap_mode) {
538 case WRAP_FLAT:
539 *new_col = cur_col;
540 break;
541 case WRAP_SPHERE:
542 *new_col = BOARD_SIZE - 1;
543 break;
544 case WRAP_TORUS:
545 *new_col = BOARD_SIZE - 1;
546 (*new_row)--;
547 if (*new_row < 0) {
548 *new_row = BOARD_SIZE - 1;
550 break;
552 } else if (*new_col >= BOARD_SIZE) {
553 switch (cursor_wrap_mode) {
554 case WRAP_FLAT:
555 *new_col = cur_col;
556 break;
557 case WRAP_SPHERE:
558 *new_col = 0;
559 break;
560 case WRAP_TORUS:
561 *new_col = 0;
562 (*new_row)++;
563 if (*new_row >= BOARD_SIZE) {
564 *new_row = 0;
566 break;
570 return (cur_row != (*new_row)) || (cur_col != (*new_col));
574 /* Actually moves the cursor to the new position and updates the screen */
575 static void reversi_gui_move_cursor(int new_row, int new_col) {
576 int old_row, old_col;
578 old_row = cur_row;
579 old_col = cur_col;
581 cur_row = new_row;
582 cur_col = new_col;
584 /* Only update the changed cells since there are no global changes */
585 reversi_gui_display_cursor(old_row, old_col);
586 reversi_gui_display_cursor(new_row, new_col);
590 /* plugin entry point */
591 enum plugin_status plugin_start(const void *parameter) {
592 bool exit, draw_screen;
593 int button;
594 #ifdef HAVE_TOUCHSCREEN
595 int button_x, button_y;
596 #endif
597 #if defined(REVERSI_BUTTON_MENU_LONGPRESS) || \
598 defined(REVERSI_BUTTON_MAKE_MOVE_SHORTPRESS)
599 int lastbutton = BUTTON_NONE;
600 #endif
601 int row, col;
602 int w_cnt, b_cnt;
604 /* Initialize Font Width and height */
605 rb->lcd_getstringsize("0", &font_width, &font_height);
607 #ifdef HAVE_TOUCHSCREEN
608 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
609 #endif
611 #if LCD_DEPTH > 1
612 rb->lcd_set_backdrop(NULL);
613 rb->lcd_set_foreground(LCD_BLACK);
614 rb->lcd_set_background(LCD_WHITE);
615 #endif
617 /* Avoid compiler warnings */
618 (void)parameter;
620 rb->srand(*rb->current_tick); /* Some AIs use rand() */
621 white_strategy = &strategy_human;
622 black_strategy = &strategy_human;
624 reversi_gui_init();
625 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
626 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
627 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
628 cursor_wrap_mode = WRAP_TORUS;
629 #else
630 cursor_wrap_mode = WRAP_FLAT;
631 #endif
632 /* The main game loop */
633 exit = false;
634 quit_plugin = false;
635 draw_screen = true;
636 while (!exit && !quit_plugin) {
637 const game_strategy_t *cur_strategy = NULL;
638 if (draw_screen) {
639 reversi_gui_display_board();
640 draw_screen = false;
642 switch(cur_player) {
643 case BLACK:
644 cur_strategy = black_strategy;
645 break;
646 case WHITE:
647 cur_strategy = white_strategy;
648 break;
651 if(cur_strategy->is_robot && !game_finished) {
652 move_t m = cur_strategy->move_func(&game, cur_player);
653 reversi_make_move(&game, MOVE_ROW(m), MOVE_COL(m), cur_player);
654 cur_player = reversi_flipped_color(cur_player);
655 draw_screen = true;
656 /* TODO: Add some delay to prevent it from being too fast ? */
657 /* TODO: Don't duplicate end of game check */
658 if (reversi_game_is_finished(&game, cur_player)) {
659 reversi_count_occupied_cells(&game, &w_cnt, &b_cnt);
660 rb->splashf(HZ*2, "Game over. %s won.",
661 (w_cnt>b_cnt?"WHITE":"BLACK"));
662 draw_screen = true; /* Must update screen after splash */
663 game_finished = true;
665 continue;
668 /***********************************************************************
669 * Button handling code happens below here
670 **********************************************************************/
671 button = rb->button_get(true);
673 /* The touchscreen buttons can act as true buttons so OR them in */
674 #ifdef HAVE_TOUCHSCREEN
675 button |= touchbutton_check_button(button, reversi_buttons, TOUCHBUTTON_COUNT);
676 #endif
678 /* All of these button presses wait for the release event */
679 if(button&BUTTON_REL) {
680 #ifdef REVERSI_BUTTON_QUIT
681 if(button&REVERSI_BUTTON_QUIT) {
682 exit = true;
684 #endif
686 #ifdef HAVE_TOUCHSCREEN
687 if(button&BUTTON_TOUCHSCREEN) {
688 button_x = rb->button_get_data();
689 button_y = button_x & 0xffff;
690 button_x >>= 16;
692 /* Check if the click was in the gameboard, if so move cursor.
693 * This has to happen before MAKE_MOVE is processed.
695 if( (CELL_R(button_y)<BOARD_SIZE) &&
696 (CELL_C(button_x)<BOARD_SIZE) )
698 reversi_gui_move_cursor(CELL_R(button_y), CELL_C(button_x));
701 #endif
703 if( (button&REVERSI_BUTTON_MENU)
704 #if defined(REVERSI_BUTTON_MENU_LONGPRESS)
705 && (lastbutton&BUTTON_REPEAT)
706 #endif
708 if (reversi_gui_menu()) {
709 return PLUGIN_USB_CONNECTED;
711 draw_screen = true;
714 if(button&REVERSI_BUTTON_MAKE_MOVE
715 #if defined(REVERSI_BUTTON_MAKE_MOVE_SHORTPRESS)
716 && !(lastbutton&BUTTON_REPEAT)
717 #endif
719 /* If you touch the game board instead of hitting menu after it
720 * has completed the game will exit out.
722 if (game_finished)
723 break;
724 if (reversi_make_move(&game, cur_row, cur_col, cur_player) > 0) {
725 /* Move was made. Global changes on the board are possible */
726 draw_screen = true; /* Redraw the screen next time */
727 cur_player = reversi_flipped_color(cur_player);
728 if (reversi_game_is_finished(&game, cur_player)) {
729 reversi_count_occupied_cells(&game, &w_cnt, &b_cnt);
730 rb->splashf(HZ*2, "Game over. %s won.",
731 (w_cnt>b_cnt?"WHITE":"BLACK"));
732 draw_screen = true; /* Must update screen after splash */
733 game_finished = true;
735 } else {
736 /* An attempt to make an invalid move */
737 rb->splash(HZ/2, "Illegal move!");
738 draw_screen = true;
739 /* Ignore any button presses during the splash */
740 rb->button_clear_queue();
745 /* These button presses will run on a release or a repeat event */
746 if(button&BUTTON_REL || button&BUTTON_REPEAT) {
747 /* Move cursor left */
748 if(button&REVERSI_BUTTON_LEFT) {
749 if (reversi_gui_cursor_pos_hmove(-1, &row, &col)) {
750 reversi_gui_move_cursor(row, col);
753 /* Move cursor right */
754 if(button&REVERSI_BUTTON_RIGHT) {
755 if (reversi_gui_cursor_pos_hmove(1, &row, &col)) {
756 reversi_gui_move_cursor(row, col);
759 /* Move cursor up */
760 if(button&REVERSI_BUTTON_UP) {
761 if (reversi_gui_cursor_pos_vmove(-1, &row, &col)) {
762 reversi_gui_move_cursor(row, col);
765 /* Move cursor down */
766 if(button&REVERSI_BUTTON_DOWN) {
767 if (reversi_gui_cursor_pos_vmove(1, &row, &col)) {
768 reversi_gui_move_cursor(row, col);
773 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
774 /* Quit if USB has been connected */
775 return PLUGIN_USB_CONNECTED;
777 #if defined(REVERSI_BUTTON_MENU_LONGPRESS) || \
778 defined(REVERSI_BUTTON_MAKE_MOVE_SHORTPRESS)
779 if (button != BUTTON_NONE) {
780 lastbutton = button;
782 #endif
785 return PLUGIN_OK;