From 8b49dd4f15d5d44dae6a45278f1b202da2911032 Mon Sep 17 00:00:00 2001 From: Kevin Worth Date: Sat, 4 Jul 2009 00:48:31 -0400 Subject: [PATCH] Highlight and un-highlight tiles as they are selected and unselected with the click and drag selection box This code works but could stand a once through to check for redundancies, dead code, and/or places for optimization. Rendering slows down dramatically as the selection box gets bigger and more tiles are selected. --- kub.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/kub.c b/kub.c index a029c13..74a3749 100644 --- a/kub.c +++ b/kub.c @@ -530,6 +530,13 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, for (i = 0; i < game->deck.num_tiles; i++) { + curr_tile = &game->deck.tiles[i]; + if (curr_tile->selected) + { + curr_tile->selected = 0; + gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2); + } + tile_x = game->deck.tiles[i].x; tile_y = game->deck.tiles[i].y; if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) && @@ -537,10 +544,6 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, { game->select_mode = 0; - curr_tile = &game->deck.tiles[game->current_tile]; - game->deck.tiles[game->current_tile].selected = 0; - gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2); - game->current_tile = i; curr_tile = &game->deck.tiles[game->current_tile]; if (!curr_tile->selected) @@ -647,26 +650,69 @@ static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event box = &game->selection_box; box->visible = 1; - 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) ); + 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); + + gtk_widget_queue_draw_area ( widget, x_min, y_min, width, height ); box->x2 = event->x; box->y2 = event->y; 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) ); - int i; + int i, tile_x, tile_y, tile_x2, tile_y2; tile_t *curr_tile; for (i = 0; i < game->deck.num_tiles; i++) - { - if ((event->x >= game->deck.tiles[i].x && - event->x <= game->deck.tiles[i].x + TILE_WIDTH) && - (event->y >= game->deck.tiles[i].y && - event->y <= game->deck.tiles[i].y + TILE_HEIGHT)) + { + curr_tile = &game->deck.tiles[i]; + if (curr_tile->selected) + { + curr_tile->selected = 0; + gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2); + } + + tile_x = game->deck.tiles[i].x; + tile_y = game->deck.tiles[i].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) || + /*or left edge*/ + (y_min >= tile_y && y_min <= tile_y2 && + x_min <= tile_x && x_max >= tile_x) || + /*or top edge*/ + (x_min >= tile_x && x_min <= tile_x2 && + y_min <= tile_y && y_max >= tile_y) || + /*or right edge*/ + (y_min >= tile_y && y_min <= tile_y2 && + x_min >= tile_x && x_min <= tile_x2) || + /*or bottom edge*/ + (x_min >= tile_x && x_min <= tile_x2 && + y_min >= tile_y && y_min <= tile_y) ) { - curr_tile = &game->deck.tiles[i]; curr_tile->selected = 1; gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2); } +/* + else + { + curr_tile->selected = 0; + gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2); + }*/ } } else -- 2.43.0