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];
49 static rack_t the_rack;
51 #define LETTER_SIZE 60
55 guess_tile_position (int i, int *x, int *y)
57 *x = 20 + i * (LETTER_SIZE + LETTER_PAD);
62 rack_tile_position (int i, int *x, int *y)
64 guess_tile_position (i, x, y);
65 *y += (LETTER_SIZE + LETTER_PAD);
68 typedef struct _dict_paint_cursor
76 } dict_paint_cursor_t;
79 dict_paint_action (void *closure, char *word)
81 dict_paint_cursor_t *cursor = closure;
82 cairo_t *cr = cursor->cr;
85 cairo_move_to (cr, cursor->x, cursor->y);
86 cairo_show_text (cr, word);
87 cairo_get_current_point (cr, &new_x, &new_y);
88 if (new_x > cursor->max_column_width)
89 cursor->max_column_width = new_x;
90 cursor->y += cursor->line_height;
91 if (cursor->y > cursor->max_y) {
92 cursor->x = cursor->max_column_width + cursor->line_height / 2;
98 dict_paint (cairo_t *cr, void *closure, double width, double height)
100 dict_t *dict = closure;
101 dict_paint_cursor_t cursor;
104 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
108 cairo_set_font_size (cr, 12);
109 cursor.line_height = 14;
114 cursor.max_column_width = 0;
115 cursor.max_y = height;
117 dict_for_each_by_length (dict,
125 tile_paint (cairo_t *cr, void *closure, double width, double height)
127 tile_t *tile = closure;
129 cairo_pattern_t *gradient;
130 cairo_text_extents_t extents;
131 int rad = (int) MIN (width / 2, height / 2);
135 double spot_angle = M_PI / 4.0;
136 double spot_rad = rad / 2.0;
141 gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
142 cy - spot_rad * sin (spot_angle),
144 cx - spot_rad * cos (spot_angle),
145 cy - spot_rad * sin (spot_angle),
147 cairo_pattern_add_color_stop_rgb (gradient, 0.0, 1.0, 1.0, 1.0);
148 cairo_pattern_add_color_stop_rgb (gradient, 1.0, 0.33, 0.33, 0.33);
150 cairo_set_source (cr, gradient);
158 cairo_select_font_face (cr, "mono",
159 CAIRO_FONT_SLANT_NORMAL,
160 CAIRO_FONT_WEIGHT_BOLD);
161 cairo_set_font_size (cr, 1.8 * rad);
163 string[0] = tile->letter;
165 cairo_text_extents (cr, string, &extents);
166 tx = cx - extents.width / 2 - extents.x_bearing;
167 ty = cy - extents.height / 2 - extents.y_bearing;
169 cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
170 cairo_move_to (cr, tx + 1, ty + 1);
171 cairo_show_text (cr, string);
173 cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
174 cairo_move_to (cr, tx - 1, ty - 1);
175 cairo_show_text (cr, string);
177 cairo_set_source_rgb (cr, 0.2, 0.3, 0.8);
178 cairo_move_to (cr, tx, ty);
179 cairo_show_text (cr, string);
185 tile_glide_to (tile_t *tile, int x, int y)
187 goo_canvas_item_animate (tile->item, x, y,
190 GOO_CANVAS_ANIMATE_FREEZE);
196 tile_create (GooCanvasItem *parent,
197 char letter, int rack_index)
201 tile = g_malloc (sizeof (tile_t));
202 tile->letter = tolower (letter);
203 tile->rack_index = rack_index;
204 rack_tile_position (rack_index, &tile->x, &tile->y);
205 tile->item = goo_demo_item_new (parent,
207 LETTER_SIZE, LETTER_SIZE,
211 tile->guessed = FALSE;
217 on_delete_event (GtkWidget *window,
219 gpointer unused_data)
225 rand_within (int num_values)
227 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
231 shuffle (int *array, int length)
235 for (i = 0; i < length; i++)
237 r = i + rand_within (length - i);
245 rack_init (rack_t *rack, GooCanvasItem *parent, char *word)
249 for (i = 0; i < MIN (MAX_TILES, strlen (word)); i++)
250 rack->tiles[i] = tile_create (parent, word[i], i);
252 while (i < MAX_TILES)
253 rack->tiles[i] = NULL;
254 rack->guess_length = 0;
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 rack_return_tile (rack_t *rack, tile_t *tile)
282 rack_tile_position (tile->rack_index, &x, &y);
283 tile_glide_to (tile, x, y);
284 tile->guessed = FALSE;
285 rack->guess_length--;
289 rack_return_all (rack_t *rack)
293 for (i = 0; i < rack->num_tiles; i++) {
294 if (rack->tiles[i]->guessed)
295 rack_return_tile (rack, rack->tiles[i]);
297 rack->guess_length = 0;
301 on_key_press_event (GtkWidget *widget,
307 rack_t *rack = user_data;
309 if (event->keyval == GDK_Return) {
310 rack_return_all (rack);
314 if (event->keyval == GDK_space) {
315 rack_return_all (rack);
320 if (event->keyval == GDK_BackSpace) {
321 gboolean found = FALSE;
324 for (i = 0; i < rack->num_tiles; i++) {
325 /* XXX: evil stuff here... please refactor a lot */
326 if (rack->guess[rack->guess_length-1] == rack->tiles[i]->letter &&
327 rack->tiles[i]->guessed &&
328 rack->tiles[i]->x > x)
335 rack_return_tile (rack, rack->tiles[found_index]);
341 /* XXX: event->string is deprecated, but the non-deprecated
342 * input-method stuff (GtkIMContext) is extremely non-obvious to
344 guess_letter = tolower (event->string[0]);
345 for (i = 0; i < rack->num_tiles; i++) {
346 if (guess_letter == rack->tiles[i]->letter &&
347 ! rack->tiles[i]->guessed)
349 guess_tile_position (rack->guess_length, &x, &y);
350 tile_glide_to (rack->tiles[i], x, y);
351 rack->tiles[i]->guessed = TRUE;
352 rack->guess[rack->guess_length++] = guess_letter;
363 GtkWidget *window, *scrolled_window;
365 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
366 gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
367 gtk_widget_show (window);
368 g_signal_connect (window, "delete_event",
369 (GtkSignalFunc) on_delete_event, NULL);
371 gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
372 g_signal_connect (window, "key_press_event",
373 (GtkSignalFunc) on_key_press_event, &the_rack);
375 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
376 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
378 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
379 GTK_POLICY_AUTOMATIC,
380 GTK_POLICY_AUTOMATIC);
381 gtk_widget_show (scrolled_window);
382 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
384 return scrolled_window;
388 create_canvas (GtkWidget *parent, char *word, dict_t *solution)
393 canvas = goo_canvas_new ();
394 gtk_widget_set_size_request (canvas, 400, 400);
395 goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
396 gtk_widget_show (canvas);
397 gtk_container_add (GTK_CONTAINER (parent), canvas);
399 root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
401 rack_init (&the_rack, root, word);
403 goo_demo_item_new (root,
405 LETTER_PAD + 2 * (LETTER_SIZE + 2 * LETTER_PAD),
406 400, 400 - (2 * (LETTER_SIZE + 2 * LETTER_PAD)),
412 main (int argc, char *argv[])
414 dict_t dict, solution;
421 gettimeofday (&tv, NULL);
422 srand (tv.tv_sec ^ tv.tv_usec);
427 memcpy (rack, bag.tiles, 7);
430 for (i = 0; i < 7; i++)
431 rack[i] = toupper (rack[i]);
434 dict_add_words_from_file (&dict, "words.txt");
436 dict_init (&solution);
437 subanagram_expand (rack, &dict, &solution);
439 gtk_init (&argc, &argv);
440 window = create_window ();
442 create_canvas (window, rack, &solution);