From: Kevin Worth Date: Fri, 12 Jun 2009 13:54:26 +0000 (-0400) Subject: Allow selection of a tile by including any corner X-Git-Url: https://git.cworth.org/git?p=kub;a=commitdiff_plain;h=3242fcf7fba52a1b73a7511e37ab2865d161bedd Allow selection of a tile by including any corner A group of tiles can now be selected by overlapping the selection_box with any corner of each tile(top-left, bottom-right, etc) Bug fix: previously, the corners of the selection_box were not being updated if the mouse was clicked without being dragged. Now, with each click, x2 is set equal to x1, and y2 to y1. Then, as before, they are updated as the mouse is dragged. --- diff --git a/kub.c b/kub.c index f77dfcb..bae99e7 100644 --- a/kub.c +++ b/kub.c @@ -537,7 +537,9 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game->click_y = event->y; /*...these two lines*/ game->selection_box.x1 = event->x; + game->selection_box.x2 = event->x; game->selection_box.y1 = event->y; + game->selection_box.y2 = event->y; } return TRUE; } @@ -550,26 +552,38 @@ static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *even selection_box_t *box; box = &game->selection_box; - int x = MIN(box->x1, box->x2); - int y = MIN(box->y1, box->y2); + 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, y, width, height); + 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_x, tile_y, 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_x2 = tile_x + TILE_WIDTH; + tile_y2 = tile_y + TILE_HEIGHT; + if (/*If top-left corner*/ + (tile_x >= x_min && tile_x <= x_max && + tile_y >= y_min && tile_y <= 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_x >= x_min && tile_x <= 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_y >= y_min && tile_y <= y_max) ) { group.tiles[group.num_tiles] = game->deck.tiles[i]; group.num_tiles++; @@ -580,7 +594,7 @@ static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *even for (i = 0; i < group.num_tiles; i++) tile_print(group.tiles[i]); } - + game->select_mode = 1; return TRUE;