]> git.cworth.org Git - kub/commitdiff
Draw a rectangle to clearly show which tiles are being selected.
authorKevin Worth <kworth@kworth-laptop.(none)>
Sun, 7 Jun 2009 16:36:58 +0000 (12:36 -0400)
committerKevin Worth <kworth@kworth-laptop.(none)>
Sun, 7 Jun 2009 16:36:58 +0000 (12:36 -0400)
     New "selection_box_t" struct has been added.
     "selection_box.visible" determines when the rectangle is(isn't) shown.

kub.c

diff --git a/kub.c b/kub.c
index 8ad1bd992fe5739e2259cac03820ed89bf76aeb0..489effb4cd77945d570c5b58bf81c2307f4a5896 100644 (file)
--- a/kub.c
+++ b/kub.c
@@ -80,6 +80,10 @@ typedef struct player {
     tile_group_t hand;
 } player_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
 #define GAME_WINDOW_DEFAULT_HEIGHT 600
@@ -89,14 +93,31 @@ typedef struct game {
     int num_players;
     board_t board;
     deck_t deck;
+    selection_box_t selection_box;
     RsvgHandle *blanktile;
 
     int current_tile;
     int diff_x, diff_y;
     int click_x, click_y;
-    int release_x, release_y;
+    int release_x, release_y;    /*Currently unused*/
 } game_t;
 
+static void selection_box_init(selection_box_t *box)
+{
+    box->x1 = 0;
+    box->x2 = 0;
+    box->y1 = 0;
+    box->y2 = 0;
+    box->visible = 0;
+}
+
+static void selection_box_draw(selection_box_t *box, cairo_t *cr)
+{
+    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);
+}
+
 static void tile_init (tile_t *tile, color_t color, int number)
 {
     tile->color = color;
@@ -449,7 +470,8 @@ static void game_init(game_t *game)
        player_init(&game->players[i]);
        game->num_players += 1;
     }
-
+    
+    selection_box_init(&game->selection_box);
     board_init(&game->board);
     deck_init(&game->deck);
     deck_shuffle(&game->deck);
@@ -473,6 +495,9 @@ static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_
 
     deck_draw(game, cr, event->region);
 
+    if (game->selection_box.visible)
+       selection_box_draw(&game->selection_box, cr);
+
     hand_draw(game, 0, cr, event->region);
 
     cairo_destroy (cr);
@@ -505,8 +530,13 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event,
     }
     if (game->current_tile == -1)
     {
+       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_box.x1 = event->x;
+       game->selection_box.y1 = event->y;
     }
     return TRUE;
 }
@@ -515,6 +545,17 @@ static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *even
 {
     if (game->current_tile == -1)
     {
+       selection_box_t *box;
+       box = &game->selection_box;
+
+       int x = MIN(box->x1, box->x2);
+       int y = MIN(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);
+
        tile_group_t group;
        group.num_tiles = 0;
 
@@ -546,20 +587,34 @@ static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *even
 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
                                        game_t *game, cairo_t *cr)
 {
-    tile_t *tile;
+    if (game->current_tile == -1)
+    {
+       selection_box_t *box;
+       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) );
+
+       box->x2 = event->x;
+       box->y2 = event->y;
 
-    tile = &game->deck.tiles[game->current_tile];
+       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_t *tile;
+       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);
+       /* 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);
 
     /* Then, move the tile */
-    tile->x = event->x - game->diff_x;
-    tile->y = event->y - game->diff_y;
+       tile->x = event->x - game->diff_x;
+       tile->y = event->y - game->diff_y;
 
     /* Finally, invalidate the region where the tile is now. */
-    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);
+    }
     return TRUE;
 }