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"
44 tile_t *tiles[MAX_TILES];
48 static rack_t the_rack;
50 static char the_guess[MAX_TILES];
51 static int the_guess_index = 0;
53 #define LETTER_SIZE 60
57 rack_tile_position (int i, int *x, int *y)
59 *x = 20 + i * (LETTER_SIZE + LETTER_PAD);
64 guess_tile_position (int i, int *x, int *y)
66 rack_tile_position (i, x, y);
67 *y -= (LETTER_SIZE + LETTER_PAD);
71 tile_paint (cairo_t *cr, void *closure)
73 tile_t *tile = closure;
75 cairo_pattern_t *gradient;
76 cairo_text_extents_t extents;
77 int rad = (int) (tile->size / 2);
78 int cx = tile->size / 2;
81 double spot_angle = M_PI / 4.0;
82 double spot_rad = rad / 2.0;
87 gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
88 cy - spot_rad * sin (spot_angle),
90 cx - spot_rad * cos (spot_angle),
91 cy - spot_rad * sin (spot_angle),
93 cairo_pattern_add_color_stop_rgb (gradient, 0.0, 1.0, 1.0, 1.0);
94 cairo_pattern_add_color_stop_rgb (gradient, 1.0, 0.33, 0.33, 0.33);
96 cairo_set_source (cr, gradient);
104 cairo_select_font_face (cr, "mono",
105 CAIRO_FONT_SLANT_NORMAL,
106 CAIRO_FONT_WEIGHT_BOLD);
107 cairo_set_font_size (cr, 1.8 * rad);
109 string[0] = tile->letter;
111 cairo_text_extents (cr, string, &extents);
112 tx = cx - extents.width / 2 - extents.x_bearing;
113 ty = cy - extents.height / 2 - extents.y_bearing;
115 cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
116 cairo_move_to (cr, tx + 1, ty + 1);
117 cairo_show_text (cr, string);
119 cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
120 cairo_move_to (cr, tx - 1, ty - 1);
121 cairo_show_text (cr, string);
123 cairo_set_source_rgb (cr, 0.2, 0.3, 0.8);
124 cairo_move_to (cr, tx, ty);
125 cairo_show_text (cr, string);
131 tile_move_to (tile_t *tile, int x, int y)
133 cairo_matrix_t matrix;
135 cairo_matrix_init_translate (&matrix, x, y);
136 goo_canvas_item_set_transform (tile->item, &matrix);
140 tile_glide_to (tile_t *tile, int x, int y)
142 goo_canvas_item_animate (tile->item, x, y,
145 GOO_CANVAS_ANIMATE_FREEZE);
151 tile_create (GooCanvasItem *parent,
152 char letter, int rack_index)
156 tile = g_malloc (sizeof (tile_t));
157 tile->letter = tolower (letter);
158 tile->rack_index = rack_index;
159 rack_tile_position (rack_index, &tile->x, &tile->y);
160 tile->size = LETTER_SIZE;
161 tile->item = goo_demo_item_new (parent,
166 tile_move_to (tile, tile->x, tile->y);
168 tile->guessed = FALSE;
174 on_delete_event (GtkWidget *window,
176 gpointer unused_data)
182 rand_within (int num_values)
184 return (int) ((double) num_values * (rand() / (RAND_MAX + 1.0)));
188 shuffle (int *array, int length)
192 for (i = 0; i < length; i++)
194 r = i + rand_within (length - i);
202 rack_init (rack_t *rack, GooCanvasItem *parent, char *word)
206 for (i = 0; i < MIN (MAX_TILES, strlen (word)); i++)
207 rack->tiles[i] = tile_create (parent, word[i], i);
209 while (i < MAX_TILES)
210 rack->tiles[i] = NULL;
214 rack_shuffle (rack_t *rack)
216 int indices[MAX_TILES];
219 for (i = 0; i < rack->num_tiles; i++)
222 shuffle (indices, rack->num_tiles);
224 for (i = 0; i < rack->num_tiles; i++) {
225 rack->tiles[i]->rack_index = indices[i];
226 rack_tile_position (indices[i], &x, &y);
227 tile_glide_to (rack->tiles[i], x, y);
234 on_key_press_event (GtkWidget *widget,
240 rack_t *rack = user_data;
242 if (event->keyval == GDK_space) {
243 for (i = 0; i < rack->num_tiles; i++) {
244 if (rack->tiles[i]->guessed) {
245 rack_tile_position (rack->tiles[i]->rack_index, &x, &y);
246 tile_glide_to (rack->tiles[i], x, y);
247 rack->tiles[i]->guessed = FALSE;
255 if (event->keyval == GDK_BackSpace) {
256 gboolean found = FALSE;
259 for (i = 0; i < rack->num_tiles; i++) {
260 /* XXX: evil stuff here... please refactor a lot */
261 if (the_guess[the_guess_index-1] == rack->tiles[i]->letter &&
262 rack->tiles[i]->guessed &&
263 rack->tiles[i]->x > x)
270 rack_tile_position (rack->tiles[found_index]->rack_index, &x, &y);
271 tile_glide_to (rack->tiles[found_index], x, y);
272 rack->tiles[found_index]->guessed = FALSE;
279 /* XXX: event->string is deprecated, but the non-deprecated
280 * input-method stuff (GtkIMContext) is extremely non-obvious to
282 guess_letter = tolower (event->string[0]);
283 for (i = 0; i < rack->num_tiles; i++) {
284 if (guess_letter == rack->tiles[i]->letter &&
285 ! rack->tiles[i]->guessed)
287 guess_tile_position (the_guess_index, &x, &y);
288 tile_glide_to (rack->tiles[i], x, y);
289 rack->tiles[i]->guessed = TRUE;
290 the_guess[the_guess_index++] = guess_letter;
301 GtkWidget *window, *scrolled_window;
303 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
304 gtk_window_set_default_size (GTK_WINDOW (window), 500, 500);
305 gtk_widget_show (window);
306 g_signal_connect (window, "delete_event",
307 (GtkSignalFunc) on_delete_event, NULL);
309 gtk_widget_add_events (window, GDK_KEY_PRESS_MASK);
310 g_signal_connect (window, "key_press_event",
311 (GtkSignalFunc) on_key_press_event, &the_rack);
313 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
314 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
316 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
317 GTK_POLICY_AUTOMATIC,
318 GTK_POLICY_AUTOMATIC);
319 gtk_widget_show (scrolled_window);
320 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
322 return scrolled_window;
326 create_canvas (GtkWidget *parent, char *word)
331 canvas = goo_canvas_new ();
332 gtk_widget_set_size_request (canvas, 400, 400);
333 goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 400, 400);
334 gtk_widget_show (canvas);
335 gtk_container_add (GTK_CONTAINER (parent), canvas);
337 root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
339 rack_init (&the_rack, root, word);
343 main (int argc, char *argv[])
351 gettimeofday (&tv, NULL);
352 srand (tv.tv_sec ^ tv.tv_usec);
357 memcpy (rack, bag.tiles, 7);
360 for (i = 0; i < 7; i++)
361 rack[i] = toupper (rack[i]);
363 gtk_init (&argc, &argv);
364 window = create_window ();
366 create_canvas (window, rack);