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 static rack_t the_rack;
54 #define LETTER_SIZE 60
58 guess_tile_position (int i, int *x, int *y)
60 *x = 20 + i * (LETTER_SIZE + LETTER_PAD);
65 rack_tile_position (int i, int *x, int *y)
67 guess_tile_position (i, x, y);
68 *y += (LETTER_SIZE + LETTER_PAD);
71 typedef struct _dict_paint_cursor
79 } dict_paint_cursor_t;
82 dict_paint_action (void *closure, char *word, dict_entry_t *entry)
84 dict_paint_cursor_t *cursor = closure;
85 cairo_t *cr = cursor->cr;
88 cairo_move_to (cr, cursor->x, cursor->y);
89 if (*entry & RACK_DICT_ENTRY_FOUND) {
90 cairo_show_text (cr, word);
92 int i, length = strlen (word);
93 for (i = 0; i < length; i++)
94 cairo_show_text (cr, "_");
96 cairo_get_current_point (cr, &new_x, &new_y);
97 if (new_x > cursor->max_column_width)
98 cursor->max_column_width = new_x;
99 cursor->y += cursor->line_height;
100 if (cursor->y > cursor->max_y) {
101 cursor->x = cursor->max_column_width + cursor->line_height / 2;
107 dict_paint (cairo_t *cr, void *closure, double width, double height)
109 dict_t *dict = closure;
110 dict_paint_cursor_t cursor;
113 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
117 cairo_set_font_size (cr, 12);
118 cursor.line_height = 14;
123 cursor.max_column_width = 0;
124 cursor.max_y = height;
126 dict_for_each_by_length (dict,
134 tile_paint (cairo_t *cr, void *closure, double width, double height)
136 tile_t *tile = closure;
138 cairo_pattern_t *gradient;
139 cairo_text_extents_t extents;
140 int rad = (int) MIN (width / 2, height / 2);
144 double spot_angle = M_PI / 4.0;
145 double spot_rad = rad / 2.0;
150 gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
151 cy - spot_rad * sin (spot_angle),
153 cx - spot_rad * cos (spot_angle),
154 cy - spot_rad * sin (spot_angle),
156 cairo_pattern_add_color_stop_rgb (gradient, 0.0, 1.0, 1.0, 1.0);
157 cairo_pattern_add_color_stop_rgb (gradient, 1.0, 0.33, 0.33, 0.33);
159 cairo_set_source (cr, gradient);
167 cairo_select_font_face (cr, "mono",
168 CAIRO_FONT_SLANT_NORMAL,
169 CAIRO_FONT_WEIGHT_BOLD);
170 cairo_set_font_size (cr, 1.8 * rad);
172 string[0] = tile->letter;
174 cairo_text_extents (cr, string, &extents);
175 tx = cx - extents.width / 2 - extents.x_bearing;
176 ty = cy - extents.height / 2 - extents.y_bearing;
178 cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
179 cairo_move_to (cr, tx + 1, ty + 1);
180 cairo_show_text (cr, string);
182 cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
183 cairo_move_to (cr, tx - 1, ty - 1);
184 cairo_show_text (cr, string);
186 cairo_set_source_rgb (cr, 0.2, 0.3, 0.8);
187 cairo_move_to (cr, tx, ty);
188 cairo_show_text (cr, string);
194 tile_glide_to (tile_t *tile, int x, int y)
196 goo_canvas_item_animate (tile->item, x, y,
199 GOO_CANVAS_ANIMATE_FREEZE);
205 tile_create (GooCanvasItem *parent,
206 char letter, int rack_index)
210 tile = g_malloc (sizeof (tile_t));
211 tile->letter = tolower (letter);
212 tile->rack_index = rack_index;
213 rack_tile_position (rack_index, &tile->x, &tile->y);
214 tile->item = goo_demo_item_new (parent,
216 LETTER_SIZE, LETTER_SIZE,
220 tile->guessed = FALSE;
226 on_delete_event (GtkWidget *window,
228 gpointer unused_data)
234 rand_within (int num_values)
236 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
240 shuffle (int *array, int length)
244 for (i = 0; i < length; i++)
246 r = i + rand_within (length - i);
254 rack_init (rack_t *rack, GooCanvasItem *parent, char *word, dict_t *solution)
258 for (i = 0; i < MIN (MAX_TILES, strlen (word)); i++)
259 rack->tiles[i] = tile_create (parent, word[i], i);
261 while (i < MAX_TILES)
262 rack->tiles[i] = NULL;
263 rack->guess_length = 0;
264 rack->solution = solution;
265 rack->solution_item = NULL;
269 rack_shuffle (rack_t *rack)
271 int indices[MAX_TILES];
274 for (i = 0; i < rack->num_tiles; i++)
277 shuffle (indices, rack->num_tiles);
279 for (i = 0; i < rack->num_tiles; i++) {
280 rack->tiles[i]->rack_index = indices[i];
281 rack_tile_position (indices[i], &x, &y);
282 tile_glide_to (rack->tiles[i], x, y);
289 rack_return_tile (rack_t *rack, tile_t *tile)
293 rack_tile_position (tile->rack_index, &x, &y);
294 tile_glide_to (tile, x, y);
295 tile->guessed = FALSE;
296 rack->guess_length--;
297 rack->guess[rack->guess_length] = '\0';
301 rack_return_all (rack_t *rack)
305 for (i = 0; i < rack->num_tiles; i++) {
306 if (rack->tiles[i]->guessed)
307 rack_return_tile (rack, rack->tiles[i]);
309 rack->guess_length = 0;
310 rack->guess[0] = '\0';
314 on_key_press_event (GtkWidget *widget,
320 rack_t *rack = user_data;
322 if (event->keyval == GDK_Return) {
324 entry = dict_lookup (rack->solution, rack->guess);
325 if (DICT_ENTRY_IS_WORD (entry)) {
326 *entry = *entry | RACK_DICT_ENTRY_FOUND;
327 goo_canvas_item_simple_changed (GOO_CANVAS_ITEM_SIMPLE (rack->solution_item), FALSE);
329 rack_return_all (rack);
333 if (event->keyval == GDK_space) {
334 rack_return_all (rack);
339 if (event->keyval == GDK_BackSpace) {
340 gboolean found = FALSE;
343 for (i = 0; i < rack->num_tiles; i++) {
344 /* XXX: evil stuff here... please refactor a lot */
345 if (rack->guess[rack->guess_length-1] == rack->tiles[i]->letter &&
346 rack->tiles[i]->guessed &&
347 rack->tiles[i]->x > x)
354 rack_return_tile (rack, rack->tiles[found_index]);
360 /* XXX: event->string is deprecated, but the non-deprecated
361 * input-method stuff (GtkIMContext) is extremely non-obvious to
363 guess_letter = tolower (event->string[0]);
364 for (i = 0; i < rack->num_tiles; i++) {
365 if (guess_letter == rack->tiles[i]->letter &&
366 ! rack->tiles[i]->guessed)
368 guess_tile_position (rack->guess_length, &x, &y);
369 tile_glide_to (rack->tiles[i], x, y);
370 rack->tiles[i]->guessed = TRUE;
371 rack->guess[rack->guess_length++] = guess_letter;
372 rack->guess[rack->guess_length] = '\0';
383 GtkWidget *window, *scrolled_window;
385 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
386 gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
387 gtk_widget_show (window);
388 g_signal_connect (window, "delete_event",
389 (GtkSignalFunc) on_delete_event, NULL);
391 gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
392 g_signal_connect (window, "key_press_event",
393 (GtkSignalFunc) on_key_press_event, &the_rack);
395 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
396 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
398 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
399 GTK_POLICY_AUTOMATIC,
400 GTK_POLICY_AUTOMATIC);
401 gtk_widget_show (scrolled_window);
402 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
404 return scrolled_window;
408 create_canvas (GtkWidget *parent, char *word, dict_t *solution)
413 canvas = goo_canvas_new ();
414 gtk_widget_set_size_request (canvas, 400, 400);
415 goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
416 gtk_widget_show (canvas);
417 gtk_container_add (GTK_CONTAINER (parent), canvas);
419 root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
421 rack_init (&the_rack, root, word, solution);
423 the_rack.solution_item = goo_demo_item_new (root,
425 LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
426 400, 400 - (2 * (LETTER_SIZE + 2 * LETTER_PAD)),
432 main (int argc, char *argv[])
434 dict_t dict, solution;
441 gettimeofday (&tv, NULL);
442 srand (tv.tv_sec ^ tv.tv_usec);
447 memcpy (rack, bag.tiles, 7);
450 for (i = 0; i < 7; i++)
451 rack[i] = toupper (rack[i]);
454 dict_add_words_from_file (&dict, "words.txt");
456 dict_init (&solution);
457 subanagram_expand (rack, &dict, &solution);
459 gtk_init (&argc, &argv);
460 window = create_window ();
462 create_canvas (window, rack, &solution);