2 * Copyright © 2006 Carl Worth
4 * This program is free software; you can redistribute it and\/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
20 #include <goocanvas.h>
25 #include <gdk/gdkkeysyms.h>
27 #include "word-game.h"
28 #include "demo-item.h"
43 tile_t *tiles[MAX_TILES];
45 char guess[MAX_TILES+1];
50 static rack_t the_rack;
52 #define LETTER_SIZE 60
56 guess_tile_position (int i, int *x, int *y)
58 *x = 20 + i * (LETTER_SIZE + LETTER_PAD);
63 rack_tile_position (int i, int *x, int *y)
65 guess_tile_position (i, x, y);
66 *y += (LETTER_SIZE + LETTER_PAD);
69 typedef struct _dict_paint_cursor
77 } dict_paint_cursor_t;
80 dict_paint_action (void *closure, char *word, dict_entry_t *entry)
82 dict_paint_cursor_t *cursor = closure;
83 cairo_t *cr = cursor->cr;
86 cairo_move_to (cr, cursor->x, cursor->y);
87 cairo_show_text (cr, word);
88 cairo_get_current_point (cr, &new_x, &new_y);
89 if (new_x > cursor->max_column_width)
90 cursor->max_column_width = new_x;
91 cursor->y += cursor->line_height;
92 if (cursor->y > cursor->max_y) {
93 cursor->x = cursor->max_column_width + cursor->line_height / 2;
99 dict_paint (cairo_t *cr, void *closure, double width, double height)
101 dict_t *dict = closure;
102 dict_paint_cursor_t cursor;
105 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
109 cairo_set_font_size (cr, 12);
110 cursor.line_height = 14;
115 cursor.max_column_width = 0;
116 cursor.max_y = height;
118 dict_for_each_by_length (dict,
126 tile_paint (cairo_t *cr, void *closure, double width, double height)
128 tile_t *tile = closure;
130 cairo_pattern_t *gradient;
131 cairo_text_extents_t extents;
132 int rad = (int) MIN (width / 2, height / 2);
136 double spot_angle = M_PI / 4.0;
137 double spot_rad = rad / 2.0;
142 gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
143 cy - spot_rad * sin (spot_angle),
145 cx - spot_rad * cos (spot_angle),
146 cy - spot_rad * sin (spot_angle),
148 cairo_pattern_add_color_stop_rgb (gradient, 0.0, 1.0, 1.0, 1.0);
149 cairo_pattern_add_color_stop_rgb (gradient, 1.0, 0.33, 0.33, 0.33);
151 cairo_set_source (cr, gradient);
159 cairo_select_font_face (cr, "mono",
160 CAIRO_FONT_SLANT_NORMAL,
161 CAIRO_FONT_WEIGHT_BOLD);
162 cairo_set_font_size (cr, 1.8 * rad);
164 string[0] = tile->letter;
166 cairo_text_extents (cr, string, &extents);
167 tx = cx - extents.width / 2 - extents.x_bearing;
168 ty = cy - extents.height / 2 - extents.y_bearing;
170 cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
171 cairo_move_to (cr, tx + 1, ty + 1);
172 cairo_show_text (cr, string);
174 cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
175 cairo_move_to (cr, tx - 1, ty - 1);
176 cairo_show_text (cr, string);
178 cairo_set_source_rgb (cr, 0.2, 0.3, 0.8);
179 cairo_move_to (cr, tx, ty);
180 cairo_show_text (cr, string);
186 tile_glide_to (tile_t *tile, int x, int y)
188 goo_canvas_item_animate (tile->item, x, y,
191 GOO_CANVAS_ANIMATE_FREEZE);
197 tile_create (GooCanvasItem *parent,
198 char letter, int rack_index)
202 tile = g_malloc (sizeof (tile_t));
203 tile->letter = tolower (letter);
204 tile->rack_index = rack_index;
205 rack_tile_position (rack_index, &tile->x, &tile->y);
206 tile->item = goo_demo_item_new (parent,
208 LETTER_SIZE, LETTER_SIZE,
212 tile->guessed = FALSE;
218 on_delete_event (GtkWidget *window,
220 gpointer unused_data)
226 rand_within (int num_values)
228 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
232 shuffle (int *array, int length)
236 for (i = 0; i < length; i++)
238 r = i + rand_within (length - i);
246 rack_init (rack_t *rack, GooCanvasItem *parent, char *word, dict_t *solution)
250 for (i = 0; i < MIN (MAX_TILES, strlen (word)); i++)
251 rack->tiles[i] = tile_create (parent, word[i], i);
253 while (i < MAX_TILES)
254 rack->tiles[i] = NULL;
255 rack->guess_length = 0;
256 rack->solution = solution;
260 rack_shuffle (rack_t *rack)
262 int indices[MAX_TILES];
265 for (i = 0; i < rack->num_tiles; i++)
268 shuffle (indices, rack->num_tiles);
270 for (i = 0; i < rack->num_tiles; i++) {
271 rack->tiles[i]->rack_index = indices[i];
272 rack_tile_position (indices[i], &x, &y);
273 tile_glide_to (rack->tiles[i], x, y);
280 rack_return_tile (rack_t *rack, tile_t *tile)
284 rack_tile_position (tile->rack_index, &x, &y);
285 tile_glide_to (tile, x, y);
286 tile->guessed = FALSE;
287 rack->guess_length--;
288 rack->guess[rack->guess_length] = '\0';
292 rack_return_all (rack_t *rack)
296 for (i = 0; i < rack->num_tiles; i++) {
297 if (rack->tiles[i]->guessed)
298 rack_return_tile (rack, rack->tiles[i]);
300 rack->guess_length = 0;
301 rack->guess[0] = '\0';
305 on_key_press_event (GtkWidget *widget,
311 rack_t *rack = user_data;
313 if (event->keyval == GDK_Return) {
315 entry = dict_lookup (rack->solution, rack->guess);
316 if (DICT_ENTRY_IS_WORD (entry))
317 printf ("%s is good\n", rack->guess);
319 printf ("%s is not a word\n", rack->guess);
320 rack_return_all (rack);
324 if (event->keyval == GDK_space) {
325 rack_return_all (rack);
330 if (event->keyval == GDK_BackSpace) {
331 gboolean found = FALSE;
334 for (i = 0; i < rack->num_tiles; i++) {
335 /* XXX: evil stuff here... please refactor a lot */
336 if (rack->guess[rack->guess_length-1] == rack->tiles[i]->letter &&
337 rack->tiles[i]->guessed &&
338 rack->tiles[i]->x > x)
345 rack_return_tile (rack, rack->tiles[found_index]);
351 /* XXX: event->string is deprecated, but the non-deprecated
352 * input-method stuff (GtkIMContext) is extremely non-obvious to
354 guess_letter = tolower (event->string[0]);
355 for (i = 0; i < rack->num_tiles; i++) {
356 if (guess_letter == rack->tiles[i]->letter &&
357 ! rack->tiles[i]->guessed)
359 guess_tile_position (rack->guess_length, &x, &y);
360 tile_glide_to (rack->tiles[i], x, y);
361 rack->tiles[i]->guessed = TRUE;
362 rack->guess[rack->guess_length++] = guess_letter;
363 rack->guess[rack->guess_length] = '\0';
374 GtkWidget *window, *scrolled_window;
376 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
377 gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
378 gtk_widget_show (window);
379 g_signal_connect (window, "delete_event",
380 (GtkSignalFunc) on_delete_event, NULL);
382 gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
383 g_signal_connect (window, "key_press_event",
384 (GtkSignalFunc) on_key_press_event, &the_rack);
386 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
387 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
389 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
390 GTK_POLICY_AUTOMATIC,
391 GTK_POLICY_AUTOMATIC);
392 gtk_widget_show (scrolled_window);
393 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
395 return scrolled_window;
399 create_canvas (GtkWidget *parent, char *word, dict_t *solution)
404 canvas = goo_canvas_new ();
405 gtk_widget_set_size_request (canvas, 400, 400);
406 goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
407 gtk_widget_show (canvas);
408 gtk_container_add (GTK_CONTAINER (parent), canvas);
410 root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
412 rack_init (&the_rack, root, word, solution);
414 goo_demo_item_new (root,
416 LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
417 400, 400 - (2 * (LETTER_SIZE + 2 * LETTER_PAD)),
423 main (int argc, char *argv[])
425 dict_t dict, solution;
432 gettimeofday (&tv, NULL);
433 srand (tv.tv_sec ^ tv.tv_usec);
438 memcpy (rack, bag.tiles, 7);
441 for (i = 0; i < 7; i++)
442 rack[i] = toupper (rack[i]);
445 dict_add_words_from_file (&dict, "words.txt");
447 dict_init (&solution);
448 subanagram_expand (rack, &dict, &solution);
450 gtk_init (&argc, &argv);
451 window = create_window ();
453 create_canvas (window, rack, &solution);