X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=rack-fancy.c;h=63677da3d52594a37b995256854ccfca8ea5a2ca;hb=4a18152043dd8fa417e5b8db7047de2e3998da7b;hp=99d0aa0fa9b305ffd8e8255cd781ac9d8a6f2102;hpb=ec58ce1cd1f8effe49f12485f65c345d75767dd5;p=wordgame diff --git a/rack-fancy.c b/rack-fancy.c index 99d0aa0..63677da 100644 --- a/rack-fancy.c +++ b/rack-fancy.c @@ -42,28 +42,155 @@ typedef struct _rack { tile_t *tiles[MAX_TILES]; int num_tiles; + char guess[MAX_TILES+1]; + int guess_length; + dict_t *solution; } rack_t; static rack_t the_rack; -static char the_guess[MAX_TILES]; -static int the_guess_index = 0; - #define LETTER_SIZE 60 #define LETTER_PAD 5 static void -rack_tile_position (int i, int *x, int *y) +guess_tile_position (int i, int *x, int *y) { *x = 20 + i * (LETTER_SIZE + LETTER_PAD); - *y = 85; + *y = LETTER_PAD; } static void -guess_tile_position (int i, int *x, int *y) +rack_tile_position (int i, int *x, int *y) { - rack_tile_position (i, x, y); - *y -= (LETTER_SIZE + LETTER_PAD); + guess_tile_position (i, x, y); + *y += (LETTER_SIZE + LETTER_PAD); +} + +typedef struct _dict_paint_cursor +{ + cairo_t *cr; + int line_height; + int x; + int y; + int max_column_width; + int max_y; +} dict_paint_cursor_t; + +static void +dict_paint_action (void *closure, char *word) +{ + dict_paint_cursor_t *cursor = closure; + cairo_t *cr = cursor->cr; + double new_x, new_y; + + cairo_move_to (cr, cursor->x, cursor->y); + cairo_show_text (cr, word); + cairo_get_current_point (cr, &new_x, &new_y); + if (new_x > cursor->max_column_width) + cursor->max_column_width = new_x; + cursor->y += cursor->line_height; + if (cursor->y > cursor->max_y) { + cursor->x = cursor->max_column_width + cursor->line_height / 2; + cursor->y = 0; + } +} + +static void +dict_paint (cairo_t *cr, void *closure, double width, double height) +{ + dict_t *dict = closure; + dict_paint_cursor_t cursor; + + cairo_save (cr); + cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */ + + cursor.cr = cr; + + cairo_set_font_size (cr, 12); + cursor.line_height = 14; + + cursor.x = 0; + cursor.y = 0; + + cursor.max_column_width = 0; + cursor.max_y = height; + + dict_for_each_by_length (dict, + dict_paint_action, + &cursor); + + cairo_restore (cr); +} + +static void +tile_paint (cairo_t *cr, void *closure, double width, double height) +{ + tile_t *tile = closure; + + cairo_pattern_t *gradient; + cairo_text_extents_t extents; + int rad = (int) MIN (width / 2, height / 2); + int cx = width / 2; + int cy = cx; + int tx, ty; + double spot_angle = M_PI / 4.0; + double spot_rad = rad / 2.0; + char string[2]; + + cairo_save (cr); + + gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle), + cy - spot_rad * sin (spot_angle), + 0.0, + cx - spot_rad * cos (spot_angle), + cy - spot_rad * sin (spot_angle), + rad + spot_rad); + cairo_pattern_add_color_stop_rgb (gradient, 0.0, 1.0, 1.0, 1.0); + cairo_pattern_add_color_stop_rgb (gradient, 1.0, 0.33, 0.33, 0.33); + + cairo_set_source (cr, gradient); + + cairo_arc (cr, + cx, cy, + rad, 0, 2 * M_PI); + + cairo_fill (cr); + + cairo_select_font_face (cr, "mono", + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_BOLD); + cairo_set_font_size (cr, 1.8 * rad); + + string[0] = tile->letter; + string[1] = '\0'; + cairo_text_extents (cr, string, &extents); + tx = cx - extents.width / 2 - extents.x_bearing; + ty = cy - extents.height / 2 - extents.y_bearing; + + cairo_set_source_rgb (cr, 0.7, 0.7, 0.7); + cairo_move_to (cr, tx + 1, ty + 1); + cairo_show_text (cr, string); + + cairo_set_source_rgb (cr, 0.33, 0.33, 0.33); + cairo_move_to (cr, tx - 1, ty - 1); + cairo_show_text (cr, string); + + cairo_set_source_rgb (cr, 0.2, 0.3, 0.8); + cairo_move_to (cr, tx, ty); + cairo_show_text (cr, string); + + cairo_restore (cr); +} + +static void +tile_glide_to (tile_t *tile, int x, int y) +{ + goo_canvas_item_animate (tile->item, x, y, + 1.0, 0, + 500, 40, + GOO_CANVAS_ANIMATE_FREEZE); + tile->x = x; + tile->y = y; } static tile_t * @@ -78,22 +205,15 @@ tile_create (GooCanvasItem *parent, rack_tile_position (rack_index, &tile->x, &tile->y); tile->item = goo_demo_item_new (parent, tile->x, tile->y, - LETTER_SIZE, - toupper (letter), - NULL); + LETTER_SIZE, LETTER_SIZE, + tile_paint, + tile, NULL); + tile->guessed = FALSE; return tile; } -static void -tile_glide_to (tile_t *tile, int x, int y) -{ - goo_demo_item_glide_to (tile->item, x, y); - tile->x = x; - tile->y = y; -} - static gboolean on_delete_event (GtkWidget *window, GdkEvent *event, @@ -123,7 +243,7 @@ shuffle (int *array, int length) } static void -rack_init (rack_t *rack, GooCanvasItem *parent, char *word) +rack_init (rack_t *rack, GooCanvasItem *parent, char *word, dict_t *solution) { int i; @@ -132,6 +252,8 @@ rack_init (rack_t *rack, GooCanvasItem *parent, char *word) rack->num_tiles = i; while (i < MAX_TILES) rack->tiles[i] = NULL; + rack->guess_length = 0; + rack->solution = solution; } static gboolean @@ -154,6 +276,31 @@ rack_shuffle (rack_t *rack) return TRUE; } +static void +rack_return_tile (rack_t *rack, tile_t *tile) +{ + int x, y; + + rack_tile_position (tile->rack_index, &x, &y); + tile_glide_to (tile, x, y); + tile->guessed = FALSE; + rack->guess_length--; + rack->guess[rack->guess_length] = '\0'; +} + +static void +rack_return_all (rack_t *rack) +{ + int i; + + for (i = 0; i < rack->num_tiles; i++) { + if (rack->tiles[i]->guessed) + rack_return_tile (rack, rack->tiles[i]); + } + rack->guess_length = 0; + rack->guess[0] = '\0'; +} + static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, @@ -163,15 +310,19 @@ on_key_press_event (GtkWidget *widget, char guess_letter; rack_t *rack = user_data; + if (event->keyval == GDK_Return) { + dict_entry_t *entry; + entry = dict_lookup (rack->solution, rack->guess); + if (DICT_ENTRY_IS_WORD (entry)) + printf ("%s is good\n", rack->guess); + else + printf ("%s is not a word\n", rack->guess); + rack_return_all (rack); + return TRUE; + } + if (event->keyval == GDK_space) { - for (i = 0; i < rack->num_tiles; i++) { - if (rack->tiles[i]->guessed) { - rack_tile_position (rack->tiles[i]->rack_index, &x, &y); - tile_glide_to (rack->tiles[i], x, y); - rack->tiles[i]->guessed = FALSE; - } - } - the_guess_index = 0; + rack_return_all (rack); rack_shuffle (rack); return TRUE; } @@ -182,7 +333,7 @@ on_key_press_event (GtkWidget *widget, x = 0; for (i = 0; i < rack->num_tiles; i++) { /* XXX: evil stuff here... please refactor a lot */ - if (the_guess[the_guess_index-1] == rack->tiles[i]->letter && + if (rack->guess[rack->guess_length-1] == rack->tiles[i]->letter && rack->tiles[i]->guessed && rack->tiles[i]->x > x) { @@ -191,10 +342,7 @@ on_key_press_event (GtkWidget *widget, } } if (found) { - rack_tile_position (rack->tiles[found_index]->rack_index, &x, &y); - tile_glide_to (rack->tiles[found_index], x, y); - rack->tiles[found_index]->guessed = FALSE; - the_guess_index--; + rack_return_tile (rack, rack->tiles[found_index]); return TRUE; } return FALSE; @@ -208,10 +356,11 @@ on_key_press_event (GtkWidget *widget, if (guess_letter == rack->tiles[i]->letter && ! rack->tiles[i]->guessed) { - guess_tile_position (the_guess_index, &x, &y); + guess_tile_position (rack->guess_length, &x, &y); tile_glide_to (rack->tiles[i], x, y); rack->tiles[i]->guessed = TRUE; - the_guess[the_guess_index++] = guess_letter; + rack->guess[rack->guess_length++] = guess_letter; + rack->guess[rack->guess_length] = '\0'; return TRUE; } } @@ -247,7 +396,7 @@ create_window (void) } static void -create_canvas (GtkWidget *parent, char *word) +create_canvas (GtkWidget *parent, char *word, dict_t *solution) { GtkWidget *canvas; GooCanvasItem *root; @@ -260,12 +409,20 @@ create_canvas (GtkWidget *parent, char *word) root = goo_canvas_get_root_item (GOO_CANVAS (canvas)); - rack_init (&the_rack, root, word); + rack_init (&the_rack, root, word, solution); + + goo_demo_item_new (root, + LETTER_PAD, + LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD), + 400, 400 - (2 * (LETTER_SIZE + 2 * LETTER_PAD)), + dict_paint, + solution, NULL); } int main (int argc, char *argv[]) { + dict_t dict, solution; struct timeval tv; bag_t bag; char rack[8]; @@ -284,10 +441,16 @@ main (int argc, char *argv[]) for (i = 0; i < 7; i++) rack[i] = toupper (rack[i]); + dict_init (&dict); + dict_add_words_from_file (&dict, "words.txt"); + + dict_init (&solution); + subanagram_expand (rack, &dict, &solution); + gtk_init (&argc, &argv); window = create_window (); - create_canvas (window, rack); + create_canvas (window, rack, &solution); gtk_main ();