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];
47 static rack_t the_rack;
49 static char the_guess[MAX_TILES];
50 static int the_guess_index = 0;
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)
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)
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;
258 rack_shuffle (rack_t *rack)
260 int indices[MAX_TILES];
263 for (i = 0; i < rack->num_tiles; i++)
266 shuffle (indices, rack->num_tiles);
268 for (i = 0; i < rack->num_tiles; i++) {
269 rack->tiles[i]->rack_index = indices[i];
270 rack_tile_position (indices[i], &x, &y);
271 tile_glide_to (rack->tiles[i], x, y);
278 on_key_press_event (GtkWidget *widget,
284 rack_t *rack = user_data;
286 if (event->keyval == GDK_space) {
287 for (i = 0; i < rack->num_tiles; i++) {
288 if (rack->tiles[i]->guessed) {
289 rack_tile_position (rack->tiles[i]->rack_index, &x, &y);
290 tile_glide_to (rack->tiles[i], x, y);
291 rack->tiles[i]->guessed = FALSE;
299 if (event->keyval == GDK_BackSpace) {
300 gboolean found = FALSE;
303 for (i = 0; i < rack->num_tiles; i++) {
304 /* XXX: evil stuff here... please refactor a lot */
305 if (the_guess[the_guess_index-1] == rack->tiles[i]->letter &&
306 rack->tiles[i]->guessed &&
307 rack->tiles[i]->x > x)
314 rack_tile_position (rack->tiles[found_index]->rack_index, &x, &y);
315 tile_glide_to (rack->tiles[found_index], x, y);
316 rack->tiles[found_index]->guessed = FALSE;
323 /* XXX: event->string is deprecated, but the non-deprecated
324 * input-method stuff (GtkIMContext) is extremely non-obvious to
326 guess_letter = tolower (event->string[0]);
327 for (i = 0; i < rack->num_tiles; i++) {
328 if (guess_letter == rack->tiles[i]->letter &&
329 ! rack->tiles[i]->guessed)
331 guess_tile_position (the_guess_index, &x, &y);
332 tile_glide_to (rack->tiles[i], x, y);
333 rack->tiles[i]->guessed = TRUE;
334 the_guess[the_guess_index++] = guess_letter;
345 GtkWidget *window, *scrolled_window;
347 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
348 gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
349 gtk_widget_show (window);
350 g_signal_connect (window, "delete_event",
351 (GtkSignalFunc) on_delete_event, NULL);
353 gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
354 g_signal_connect (window, "key_press_event",
355 (GtkSignalFunc) on_key_press_event, &the_rack);
357 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
358 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
360 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
361 GTK_POLICY_AUTOMATIC,
362 GTK_POLICY_AUTOMATIC);
363 gtk_widget_show (scrolled_window);
364 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
366 return scrolled_window;
370 create_canvas (GtkWidget *parent, char *word, dict_t *solution)
375 canvas = goo_canvas_new ();
376 gtk_widget_set_size_request (canvas, 400, 400);
377 goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
378 gtk_widget_show (canvas);
379 gtk_container_add (GTK_CONTAINER (parent), canvas);
381 root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
383 rack_init (&the_rack, root, word);
385 goo_demo_item_new (root,
387 LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
388 400, 400 - (2 * (LETTER_SIZE + 2 * LETTER_PAD)),
394 main (int argc, char *argv[])
396 dict_t dict, solution;
403 gettimeofday (&tv, NULL);
404 srand (tv.tv_sec ^ tv.tv_usec);
409 memcpy (rack, bag.tiles, 7);
412 for (i = 0; i < 7; i++)
413 rack[i] = toupper (rack[i]);
416 dict_add_words_from_file (&dict, "words.txt");
418 dict_init (&solution);
419 subanagram_expand (rack, &dict, &solution);
421 gtk_init (&argc, &argv);
422 window = create_window ();
424 create_canvas (window, rack, &solution);