From 2379e94a6780633c5100d4b0982d7b806b2afa1c Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Sun, 17 Dec 2006 21:40:13 -0800 Subject: [PATCH] Add display of solution to the canvas This is currently displaying all possible words, (which is kind of pointless for actually playing the game), but is useful progress worth recording. --- demo-item.c | 25 ++++++++---- demo-item.h | 10 +++-- rack-fancy.c | 108 +++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 107 insertions(+), 36 deletions(-) diff --git a/demo-item.c b/demo-item.c index 99c45fb..198857e 100644 --- a/demo-item.c +++ b/demo-item.c @@ -18,7 +18,8 @@ G_DEFINE_TYPE (GooDemoItem, goo_demo_item, GOO_TYPE_CANVAS_ITEM_SIMPLE) static void goo_demo_item_init (GooDemoItem *demo_item) { - demo_item->size = 0.0; + demo_item->width = 0.0; + demo_item->height = 0.0; } /* The convenience function to create new items. This should start with a @@ -26,7 +27,10 @@ goo_demo_item_init (GooDemoItem *demo_item) in with the standard canvas items. */ GooCanvasItem* goo_demo_item_new (GooCanvasItem *parent, - gdouble size, + gdouble x, + gdouble y, + gdouble width, + gdouble height, GooDemoItemPaintFunc paint, void *closure, ...) @@ -35,11 +39,13 @@ goo_demo_item_new (GooCanvasItem *parent, GooDemoItem *demo_item; const char *first_property; va_list var_args; + cairo_matrix_t matrix; item = g_object_new (GOO_TYPE_DEMO_ITEM, NULL); demo_item = (GooDemoItem*) item; - demo_item->size = size; + demo_item->width = width; + demo_item->height = height; demo_item->paint = paint; demo_item->closure = closure; @@ -55,6 +61,9 @@ goo_demo_item_new (GooCanvasItem *parent, g_object_unref (item); } + cairo_matrix_init_translate (&matrix, x, y); + goo_canvas_item_set_transform (item, &matrix); + return item; } @@ -71,8 +80,8 @@ goo_demo_item_update (GooCanvasItemSimple *simple, /* Compute the new bounds. */ simple->bounds.x1 = 0; simple->bounds.y1 = 0; - simple->bounds.x2 = demo_item->size; - simple->bounds.y2 = demo_item->size; + simple->bounds.x2 = demo_item->width; + simple->bounds.y2 = demo_item->height; /* Convert to device coordinates. */ goo_canvas_item_simple_user_bounds_to_device (simple, cr, &simple->bounds); @@ -87,7 +96,7 @@ goo_demo_item_paint (GooCanvasItemSimple *simple, { GooDemoItem *item = (GooDemoItem*) simple; - item->paint (cr, item->closure); + item->paint (cr, item->closure, item->width, item->height); } /* Hit detection. This should check if the given coordinate (in the item's @@ -102,8 +111,8 @@ goo_demo_item_get_item_at (GooCanvasItemSimple *simple, { GooDemoItem *demo_item = (GooDemoItem*) simple; - if (x < 0 || (x > demo_item->size) - || y < 0 || (y > demo_item->size)) + if (x < 0 || (x > demo_item->width) + || y < 0 || (y > demo_item->height)) return NULL; return (GooCanvasItem*) simple; diff --git a/demo-item.h b/demo-item.h index 7918858..e53221d 100644 --- a/demo-item.h +++ b/demo-item.h @@ -22,12 +22,13 @@ G_BEGIN_DECLS typedef struct _GooDemoItem GooDemoItem; typedef struct _GooDemoItemClass GooDemoItemClass; -typedef void (*GooDemoItemPaintFunc) (cairo_t *cr, void *); +typedef void (*GooDemoItemPaintFunc) (cairo_t *cr, void *, double width, double height); struct _GooDemoItem { GooCanvasItemSimple parent_object; - double size; + double width; + double height; GooDemoItemPaintFunc paint; void *closure; }; @@ -40,7 +41,10 @@ struct _GooDemoItemClass GType goo_demo_item_get_type (void) G_GNUC_CONST; GooCanvasItem* goo_demo_item_new (GooCanvasItem *parent, - gdouble size, + gdouble x, + gdouble y, + gdouble width, + gdouble height, GooDemoItemPaintFunc paint, void *closure, ...); diff --git a/rack-fancy.c b/rack-fancy.c index abd2228..393c7be 100644 --- a/rack-fancy.c +++ b/rack-fancy.c @@ -34,7 +34,6 @@ typedef struct _tile char letter; int rack_index; int x, y; - int size; GooCanvasItem *item; gboolean guessed; } tile_t; @@ -54,28 +53,84 @@ static int the_guess_index = 0; #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) +{ + 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) { - rack_tile_position (i, x, y); - *y -= (LETTER_SIZE + LETTER_PAD); + 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) +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) (tile->size / 2); - int cx = tile->size / 2; + 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; @@ -127,15 +182,6 @@ tile_paint (cairo_t *cr, void *closure) cairo_restore (cr); } -static void -tile_move_to (tile_t *tile, int x, int y) -{ - cairo_matrix_t matrix; - - cairo_matrix_init_translate (&matrix, x, y); - goo_canvas_item_set_transform (tile->item, &matrix); -} - static void tile_glide_to (tile_t *tile, int x, int y) { @@ -157,13 +203,11 @@ tile_create (GooCanvasItem *parent, tile->letter = tolower (letter); tile->rack_index = rack_index; rack_tile_position (rack_index, &tile->x, &tile->y); - tile->size = LETTER_SIZE; tile->item = goo_demo_item_new (parent, - tile->size, + tile->x, tile->y, + LETTER_SIZE, LETTER_SIZE, tile_paint, - tile); - - tile_move_to (tile, tile->x, tile->y); + tile, NULL); tile->guessed = FALSE; @@ -323,7 +367,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; @@ -337,11 +381,19 @@ create_canvas (GtkWidget *parent, char *word) root = goo_canvas_get_root_item (GOO_CANVAS (canvas)); rack_init (&the_rack, root, word); + + 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]; @@ -360,10 +412,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 (); -- 2.43.0