int number;
int x;
int y;
+ int selected;
} tile_t;
#define DECK_MAX_TILES 104
tile_group_t hand;
} player_t;
-typedef struct selection_rectangle {
- int x, y, width, height, visible;
-} selection_rectangle_t;
+typedef struct selection_box {
+ int x1, x2, y1, y2, visible;
+} selection_box_t;
#define GAME_MAX_PLAYERS 4
#define GAME_WINDOW_DEFAULT_WIDTH 800
int num_players;
board_t board;
deck_t deck;
- selection_rectangle_t selection_rectangle;
+ selection_box_t selection_box;
RsvgHandle *blanktile;
int current_tile;
+ int select_mode;
int diff_x, diff_y;
int click_x, click_y;
int release_x, release_y; /*Currently unused*/
} game_t;
-static void selection_rectangle_init(selection_rectangle_t *rect)
+static void selection_box_init(selection_box_t *box)
{
- rect->x = 0;
- rect->y = 0;
- rect->width = 0;
- rect->height = 0;
- rect->visible = 0;
+ box->x1 = 0;
+ box->x2 = 0;
+ box->y1 = 0;
+ box->y2 = 0;
+ box->visible = 0;
}
-static void selection_rectangle_draw(selection_rectangle_t *select_rect, cairo_t *cr)
+static void selection_box_draw(selection_box_t *box, cairo_t *cr)
{
- /************Fix this***************/
- cairo_rectangle (cr, select_rect->x, select_rect->y, select_rect->width, select_rect->height);
- cairo_set_source_rgba(cr, 0.0, 0.1, 0.2, 0.0);
+ cairo_rectangle (cr, box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1);
+ cairo_set_source_rgba(cr, 0.0, 0.1, 0.2, 0.5);
cairo_fill (cr);
}
game->num_players += 1;
}
- selection_rectangle_init(&game->selection_rectangle);
+ selection_box_init(&game->selection_box);
board_init(&game->board);
deck_init(&game->deck);
deck_shuffle(&game->deck);
if (error)
FATAL_ERROR (error->message);
- /*This line appears to be useless, has been replaced by line below*/
- //game->current_tile = game->deck.num_tiles - 1;
- game->current_tile = -1;
+ game->current_tile = game->deck.num_tiles - 1;
+ game->select_mode = 1;
game->diff_x = game->diff_y = 0;
}
deck_draw(game, cr, event->region);
- selection_rectangle_draw(&game->selection_rectangle, cr);
+ if (game->selection_box.visible)
+ selection_box_draw(&game->selection_box, cr);
hand_draw(game, 0, cr, event->region);
if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
{
+ game->select_mode = 0;
game->current_tile = i;
game->diff_x = event->x - tile_x;
game->diff_y = event->y - tile_y;
}
}
- if (game->current_tile == -1)
+ if (game->select_mode)
{
+ game->selection_box.visible = 1;
/*These next two lines are likely to be replaced by...*/
game->click_x = event->x;
game->click_y = event->y;
/*...these two lines*/
- game->selection_rectangle.x = event->x;
- game->selection_rectangle.y = event->y;
+ game->selection_box.x1 = event->x;
+ game->selection_box.y1 = event->y;
}
return TRUE;
}
static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
{
- if (game->current_tile == -1)
+ if (game->select_mode)
{
+ game->select_mode = 0;
+ selection_box_t *box;
+ box = &game->selection_box;
+
+ int x_min = MIN(box->x1, box->x2);
+ int x_max = MAX(box->x1, box->x2);
+ int y_min = MIN(box->y1, box->y2);
+ int y_max = MAX(box->y1, box->y2);
+ int width = abs(box->x2 - box->x1);
+ int height = abs(box->y2 - box->y1);
+
+ box->visible = 0;
+ gtk_widget_queue_draw_area (widget, x_min, y_min, width, height);
+
tile_group_t group;
group.num_tiles = 0;
- int i, tile_x, tile_y;
+ int i, tile_x1, tile_y1, tile_x2, tile_y2;
for (i = 0; i < game->deck.num_tiles; i++)
{
- tile_x = game->deck.tiles[i].x;
- tile_y = game->deck.tiles[i].y;
- if ( (event->x >= tile_x && game->click_x <= tile_x &&
- event->y >= tile_y && game->click_y <= tile_y) ||
- (event->x >= tile_x && game->click_x <= tile_x &&
- event->y <= (tile_y + TILE_HEIGHT) && game->click_y >= tile_y) )
+ tile_x1 = game->deck.tiles[i].x;
+ tile_y1 = game->deck.tiles[i].y;
+ tile_x2 = tile_x + TILE_WIDTH;
+ tile_y2 = tile_y + TILE_HEIGHT;
+ if (/*If top-left corner*/
+ (tile_x1 >= x_min && tile_x1 <= x_max &&
+ tile_y1 >= y_min && tile_y1 <= y_max) ||
+ /*or bottom-right corner*/
+ (tile_x2 >= x_min && tile_x2 <= x_max &&
+ tile_y2 >= y_min && tile_y2 <= y_max) ||
+ /*or bottom-left corner*/
+ (tile_x1 >= x_min && tile_x1 <= x_max &&
+ tile_y2 >= y_min && tile_y2 <= y_max) ||
+ /*or top-right corner of tile selected*/
+ (tile_x2 >= x_min && tile_x2 <= x_max &&
+ tile_y1 >= y_min && tile_y1 <= y_max) )
{
group.tiles[group.num_tiles] = game->deck.tiles[i];
group.num_tiles++;
tile_print(group.tiles[i]);
}
- game->current_tile = -1;
+ game->select_mode = 1;
return TRUE;
}
static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
game_t *game, cairo_t *cr)
{
- if (game->current_tile == -1)
+ if (game->select_mode)
{
- selection_rectangle_t *select;
- select = &game->selection_rectangle;
- select->visible = 1;
+ selection_box_t *box;
+ box = &game->selection_box;
+ box->visible = 1;
- gtk_widget_queue_draw_area (widget, select->x - 1, select->y - 1, select->width + 1, select->height + 2);
+ gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2),
+ MIN(box->y1, box->y2), abs(box->x2 - box->x1),
+ abs(box->y2 - box->y1) );
- select->width = event->x - select->x;
- select->height = event->y - select->y;
+ box->x2 = event->x;
+ box->y2 = event->y;
- gtk_widget_queue_draw_area (widget, select->x - 1, select->y - 1, select->width + 1, select->height + 2);
+ gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2),
+ MIN(box->y1, box->y2), abs(box->x2 - box->x1),
+ abs(box->y2 - box->y1) );
}
else
{
tile = &game->deck.tiles[game->current_tile];
/* First, invalidate the region where the tile currently is. */
- gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+ gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH +
+ 1, TILE_HEIGHT + 2);
/* Then, move the tile */
tile->x = event->x - game->diff_x;