]> git.cworth.org Git - kub/commitdiff
Highlight and un-highlight tiles as they are selected and unselected with the click...
authorKevin Worth <kworth@kworth-laptop.(none)>
Sat, 4 Jul 2009 04:48:31 +0000 (00:48 -0400)
committerKevin Worth <kworth@kworth-laptop.(none)>
Sat, 4 Jul 2009 04:48:31 +0000 (00:48 -0400)
  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

diff --git a/kub.c b/kub.c
index a029c137b845d6228a4cf9d61ce531ef0b2ba85b..74a37496700835deec7a47db8d802def15c43301 100644 (file)
--- 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