X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=kub.c;h=6a2a3a5903e7ca4a51590430747e1bd5a325ca16;hb=e03d6fa328956896006b17f49d885923a20db8e1;hp=bb29e6f05cd7b72939fd0f6bfedb7de8d7f5125c;hpb=25d37bea83f759fe07dca78790b69b76c3c6f773;p=kub diff --git a/kub.c b/kub.c index bb29e6f..6a2a3a5 100644 --- a/kub.c +++ b/kub.c @@ -92,6 +92,7 @@ typedef struct game { RsvgHandle *blanktile; int current_tile; + int diff_x, diff_y; } game_t; static void @@ -390,6 +391,11 @@ static void hand_print(game_t *game) } } +/* static void hand_draw(game_t *game) */ +/* { */ + +/* } */ + static void game_init(game_t *game) { int i; @@ -412,6 +418,7 @@ static void game_init(game_t *game) FATAL_ERROR (error->message); game->current_tile = game->deck.num_tiles - 1; + game->diff_x = game->diff_y = 0; } static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game) @@ -444,10 +451,11 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, tile_y = game->deck.tiles[i].y; if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) && event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) ) - + { game->current_tile = i; - else - game->current_tile = -1; + game->diff_x = event->x - tile_x; + game->diff_y = event->y - tile_y; + } } return TRUE; @@ -455,6 +463,7 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game) { + game->current_tile = -1; printf ("You released button %d\n", event->button); return TRUE; @@ -464,20 +473,19 @@ static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event game_t *game, cairo_t *cr) { tile_t *tile; - if (game->current_tile >= 0) - { - tile = &game->deck.tiles[game->current_tile]; - - /* First, invalidate the region where the tile currently is. */ - gtk_widget_queue_draw_area (widget, tile->x, tile->y, TILE_WIDTH, TILE_HEIGHT); - - /* Then, move the tile */ - tile->x = event->x; - tile->y = event->y; - - /* Finally, invalidate the region where the tile is now. */ - gtk_widget_queue_draw_area (widget, tile->x, tile->y, TILE_WIDTH, TILE_HEIGHT); - } + + 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); + + /* Then, move the tile */ + 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); + return TRUE; }