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"
30 #define RACK_DICT_ENTRY_FOUND (1<<1)
44 tile_t *tiles[MAX_TILES];
46 char guess[MAX_TILES+1];
49 GooCanvasItem *solution_item;
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 if (strlen (word) < 3)
89 cairo_move_to (cr, cursor->x, cursor->y);
90 if (*entry & RACK_DICT_ENTRY_FOUND) {
91 cairo_show_text (cr, word);
93 int i, length = strlen (word);
94 for (i = 0; i < length; i++)
95 cairo_show_text (cr, "_");
97 cairo_get_current_point (cr, &new_x, &new_y);
98 if (new_x > cursor->max_column_width)
99 cursor->max_column_width = new_x;
100 cursor->y += cursor->line_height;
101 if (cursor->y > cursor->max_y) {
102 cursor->x = cursor->max_column_width + cursor->line_height / 2;
107 #define SOLUTION_FONT_SIZE 12
108 #define SOLUTION_LINE_HEIGHT (1.5 * SOLUTION_FONT_SIZE)
111 dict_paint (cairo_t *cr, void *closure, double width, double height)
113 dict_t *dict = closure;
114 dict_paint_cursor_t cursor;
117 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
121 cairo_select_font_face (cr, "mono", 0, 0);
122 cairo_set_font_size (cr, SOLUTION_FONT_SIZE);
123 cursor.line_height = SOLUTION_LINE_HEIGHT;
128 cursor.max_column_width = 0;
129 cursor.max_y = height;
131 dict_for_each_by_length (dict,
139 tile_paint (cairo_t *cr, void *closure, double width, double height)
141 tile_t *tile = closure;
143 cairo_pattern_t *gradient;
144 cairo_text_extents_t extents;
145 int rad = (int) MIN (width / 2, height / 2);
149 double spot_angle = M_PI / 4.0;
150 double spot_rad = rad / 2.0;
155 gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
156 cy - spot_rad * sin (spot_angle),
158 cx - spot_rad * cos (spot_angle),
159 cy - spot_rad * sin (spot_angle),
161 cairo_pattern_add_color_stop_rgb (gradient, 0.0, 1.0, 1.0, 1.0);
162 cairo_pattern_add_color_stop_rgb (gradient, 1.0, 0.33, 0.33, 0.33);
164 cairo_set_source (cr, gradient);
172 cairo_select_font_face (cr, "mono",
173 CAIRO_FONT_SLANT_NORMAL,
174 CAIRO_FONT_WEIGHT_BOLD);
175 cairo_set_font_size (cr, 1.8 * rad);
177 string[0] = tile->letter;
179 cairo_text_extents (cr, string, &extents);
180 tx = cx - extents.width / 2 - extents.x_bearing;
181 ty = cy - extents.height / 2 - extents.y_bearing;
183 cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
184 cairo_move_to (cr, tx + 1, ty + 1);
185 cairo_show_text (cr, string);
187 cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
188 cairo_move_to (cr, tx - 1, ty - 1);
189 cairo_show_text (cr, string);
191 cairo_set_source_rgb (cr, 0.2, 0.3, 0.8);
192 cairo_move_to (cr, tx, ty);
193 cairo_show_text (cr, string);
199 tile_glide_to (tile_t *tile, int x, int y)
201 goo_canvas_item_animate (tile->item, x, y,
204 GOO_CANVAS_ANIMATE_FREEZE);
210 tile_create (GooCanvasItem *parent,
211 char letter, int rack_index)
215 tile = g_malloc (sizeof (tile_t));
216 tile->letter = tolower (letter);
217 tile->rack_index = rack_index;
218 rack_tile_position (rack_index, &tile->x, &tile->y);
219 tile->item = goo_demo_item_new (parent,
221 LETTER_SIZE, LETTER_SIZE,
225 tile->guessed = FALSE;
231 on_delete_event (GtkWidget *window,
233 gpointer unused_data)
239 rand_within (int num_values)
241 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
245 shuffle (int *array, int length)
249 for (i = 0; i < length; i++)
251 r = i + rand_within (length - i);
259 rack_init (rack_t *rack,
264 for (i = 0; i < MAX_TILES; i++)
265 rack->tiles[i] = NULL;
268 rack->guess_length = 0;
269 rack->solution = solution;
270 rack->solution_item = NULL;
274 rack_create_tiles (rack_t *rack,
280 GooCanvasItem *root = goo_canvas_get_root_item (canvas);
282 for (i = 0; i < MIN (MAX_TILES, strlen (word)); i++)
283 rack->tiles[i] = tile_create (root, word[i], i);
288 rack_shuffle (rack_t *rack)
290 int indices[MAX_TILES];
293 for (i = 0; i < rack->num_tiles; i++)
296 shuffle (indices, rack->num_tiles);
298 for (i = 0; i < rack->num_tiles; i++) {
299 rack->tiles[i]->rack_index = indices[i];
300 rack_tile_position (indices[i], &x, &y);
301 tile_glide_to (rack->tiles[i], x, y);
308 rack_return_tile (rack_t *rack, tile_t *tile)
312 rack_tile_position (tile->rack_index, &x, &y);
313 tile_glide_to (tile, x, y);
314 tile->guessed = FALSE;
315 rack->guess_length--;
316 rack->guess[rack->guess_length] = '\0';
320 rack_return_all (rack_t *rack)
324 for (i = 0; i < rack->num_tiles; i++) {
325 if (rack->tiles[i]->guessed)
326 rack_return_tile (rack, rack->tiles[i]);
328 rack->guess_length = 0;
329 rack->guess[0] = '\0';
333 on_key_press_event (GtkWidget *widget,
339 rack_t *rack = user_data;
341 if (event->keyval == GDK_Return) {
343 if (rack->guess_length >= 3) {
344 entry = dict_lookup (rack->solution, rack->guess);
345 if (DICT_ENTRY_IS_WORD (entry)) {
346 *entry = *entry | RACK_DICT_ENTRY_FOUND;
347 goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
350 rack_return_all (rack);
354 if (event->keyval == GDK_space) {
355 rack_return_all (rack);
360 if (event->keyval == GDK_BackSpace) {
361 gboolean found = FALSE;
364 for (i = 0; i < rack->num_tiles; i++) {
365 /* XXX: evil stuff here... please refactor a lot */
366 if (rack->guess[rack->guess_length-1] == rack->tiles[i]->letter &&
367 rack->tiles[i]->guessed &&
368 rack->tiles[i]->x > x)
375 rack_return_tile (rack, rack->tiles[found_index]);
381 /* XXX: event->string is deprecated, but the non-deprecated
382 * input-method stuff (GtkIMContext) is extremely non-obvious to
384 guess_letter = tolower (event->string[0]);
385 for (i = 0; i < rack->num_tiles; i++) {
386 if (guess_letter == rack->tiles[i]->letter &&
387 ! rack->tiles[i]->guessed)
389 guess_tile_position (rack->guess_length, &x, &y);
390 tile_glide_to (rack->tiles[i], x, y);
391 rack->tiles[i]->guessed = TRUE;
392 rack->guess[rack->guess_length++] = guess_letter;
393 rack->guess[rack->guess_length] = '\0';
402 create_window (rack_t *rack)
404 GtkWidget *window, *scrolled_window;
406 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
407 gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
408 gtk_widget_show (window);
409 g_signal_connect (window, "delete_event",
410 (GtkSignalFunc) on_delete_event, NULL);
412 gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
413 g_signal_connect (window, "key_press_event",
414 (GtkSignalFunc) on_key_press_event, rack);
416 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
417 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
419 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
420 GTK_POLICY_AUTOMATIC,
421 GTK_POLICY_AUTOMATIC);
422 gtk_widget_show (scrolled_window);
423 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
425 return scrolled_window;
429 create_canvas (GtkWidget *parent, rack_t *rack)
434 canvas = goo_canvas_new ();
435 gtk_widget_set_size_request (canvas, 400, 400);
436 goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
437 gtk_widget_show (canvas);
438 gtk_container_add (GTK_CONTAINER (parent), canvas);
440 root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
442 rack->solution_item = goo_demo_item_new (root,
443 LETTER_PAD + SOLUTION_LINE_HEIGHT,
444 LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
445 400, 400 - (2 * (LETTER_SIZE + 2 * LETTER_PAD)),
446 dict_paint, rack->solution,
449 return GOO_CANVAS (canvas);
453 main (int argc, char *argv[])
455 dict_t dict, solution;
464 gettimeofday (&tv, NULL);
465 srand (tv.tv_sec ^ tv.tv_usec);
470 memcpy (word, bag.tiles, 7);
473 for (i = 0; i < 7; i++)
474 word[i] = toupper (word[i]);
477 dict_add_words_from_file (&dict, "words.txt");
479 dict_init (&solution);
480 subanagram_expand (word, &dict, &solution);
482 gtk_init (&argc, &argv);
484 rack_init (&rack, &solution);
486 window = create_window (&rack);
488 canvas = create_canvas (window, &rack);
490 rack_create_tiles (&rack, canvas, word);