X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=kub.c;h=74a37496700835deec7a47db8d802def15c43301;hb=8b49dd4f15d5d44dae6a45278f1b202da2911032;hp=f77dfcbd4bdd093b556332e79f616e72c6df41f7;hpb=343a6b435b5a47fc30afb603c3259b96f5e384e8;p=kub diff --git a/kub.c b/kub.c index f77dfcb..74a3749 100644 --- a/kub.c +++ b/kub.c @@ -53,6 +53,7 @@ typedef struct tile { int number; int x; int y; + int selected; } tile_t; #define DECK_MAX_TILES 104 @@ -95,6 +96,7 @@ typedef struct game { deck_t deck; selection_box_t selection_box; RsvgHandle *blanktile; + RsvgHandle *selectedtile; int current_tile; int select_mode; @@ -125,6 +127,7 @@ static void tile_init (tile_t *tile, color_t color, int number) tile->number = number; tile->x = 0; tile->y = 0; + tile->selected = 0; } static void tile_set_x_y (tile_t *tile, int x, int y) @@ -157,7 +160,11 @@ static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region cairo_save(cr); cairo_translate(cr, tile->x, tile->y); - rsvg_handle_render_cairo (game->blanktile, cr); + + if (tile->selected) + rsvg_handle_render_cairo (game->selectedtile, cr); + else + rsvg_handle_render_cairo (game->blanktile, cr); if (tile->color == BLACK) cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); @@ -477,6 +484,10 @@ static void game_init(game_t *game) deck_init(&game->deck); deck_shuffle(&game->deck); + game->selectedtile = rsvg_handle_new_from_file ("tiles/selectedtile.svg", &error); + if (error) + FATAL_ERROR (error->message); + game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error); if (error) FATAL_ERROR (error->message); @@ -515,29 +526,50 @@ static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game) { int i, tile_x, tile_y; + tile_t *curr_tile = &game->deck.tiles[game->current_tile]; 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) && event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) ) { game->select_mode = 0; + game->current_tile = i; + curr_tile = &game->deck.tiles[game->current_tile]; + if (!curr_tile->selected) + curr_tile->selected = 1; + 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); + game->diff_x = event->x - tile_x; game->diff_y = event->y - tile_y; } } if (game->select_mode) { + 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->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.x2 = event->x; game->selection_box.y1 = event->y; + game->selection_box.y2 = event->y; } return TRUE; } @@ -550,26 +582,50 @@ 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) || + /*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) ) { group.tiles[group.num_tiles] = game->deck.tiles[i]; group.num_tiles++; @@ -579,8 +635,7 @@ static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *even printf("is set %d\n", tile_group_is_set(&group) ); for (i = 0; i < group.num_tiles; i++) tile_print(group.tiles[i]); - } - + } game->select_mode = 1; return TRUE; @@ -595,12 +650,70 @@ 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, tile_x, tile_y, tile_x2, tile_y2; + tile_t *curr_tile; + 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; + 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->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 {