]> git.cworth.org Git - wordgame/commitdiff
Fix randomizing of letter positions to never end up on top of each other
authorCarl Worth <cworth@cworth.org>
Tue, 5 Dec 2006 06:46:53 +0000 (22:46 -0800)
committerCarl Worth <cworth@cworth.org>
Tue, 5 Dec 2006 06:46:53 +0000 (22:46 -0800)
We had some confusion over who was in charge of the letter position,
(whether the matrix as manipulated by the core animate code, or
whether private fields in our demo-item structure). We now do all
positioning based on the matrix. As a regression, animation is now
disabled.

demo-item.c
demo-item.h
rack-fancy.c

index b38e4210dc3258b5f43875aff4fc71cd548f265d..99aaf4afa856a24213c2ea26841e323fe625d6e8 100644 (file)
@@ -18,10 +18,18 @@ G_DEFINE_TYPE (GooDemoItem, goo_demo_item, GOO_TYPE_CANVAS_ITEM_SIMPLE)
     static void
 goo_demo_item_init (GooDemoItem *demo_item)
 {
-    demo_item->x = 0.0;
-    demo_item->y = 0.0;
-    demo_item->width = 0.0;
-    demo_item->height = 0.0;
+    demo_item->size = 0.0;
+}
+
+void
+goo_demo_item_move_to (GooCanvasItem   *item,
+                      gdouble           x,
+                      gdouble           y)
+{
+    cairo_matrix_t matrix;
+
+    cairo_matrix_init_translate (&matrix, x, y);
+    goo_canvas_item_set_transform (item, &matrix);
 }
 
 /* The convenience function to create new items. This should start with a 
@@ -31,8 +39,7 @@ GooCanvasItem*
 goo_demo_item_new (GooCanvasItem      *parent,
                   gdouble             x,
                   gdouble             y,
-                  gdouble             width,
-                  gdouble             height,
+                  gdouble             size,
                   char                letter,
                   ...)
 {
@@ -44,10 +51,7 @@ goo_demo_item_new (GooCanvasItem      *parent,
     item = g_object_new (GOO_TYPE_DEMO_ITEM, NULL);
 
     demo_item = (GooDemoItem*) item;
-    demo_item->x = x;
-    demo_item->y = y;
-    demo_item->width = width;
-    demo_item->height = height;
+    demo_item->size = size;
     demo_item->letter = letter;
 
     va_start (var_args, letter);
@@ -62,6 +66,8 @@ goo_demo_item_new (GooCanvasItem      *parent,
        g_object_unref (item);
     }
 
+    goo_demo_item_move_to (item, x, y);
+
     return item;
 }
 
@@ -76,10 +82,10 @@ goo_demo_item_update  (GooCanvasItemSimple *simple,
     GooDemoItem *demo_item = (GooDemoItem*) simple;
 
     /* Compute the new bounds. */
-    simple->bounds.x1 = demo_item->x;
-    simple->bounds.y1 = demo_item->y;
-    simple->bounds.x2 = demo_item->x + demo_item->width;
-    simple->bounds.y2 = demo_item->y + demo_item->height;
+    simple->bounds.x1 = 0;
+    simple->bounds.y1 = 0;
+    simple->bounds.x2 = demo_item->size;
+    simple->bounds.y2 = demo_item->size;
 
     /* Convert to device coordinates. */
     goo_canvas_item_simple_user_bounds_to_device (simple, cr, &simple->bounds);
@@ -95,9 +101,9 @@ goo_demo_item_paint (GooCanvasItemSimple *simple,
     GooDemoItem *item = (GooDemoItem*) simple;
     cairo_pattern_t *gradient;
     cairo_text_extents_t extents;
-    int rad = (int) MIN (item->width / 2, item->height / 2);
-    int cx = item->x + item->width / 2;
-    int cy = item->y + item->height / 2;
+    int rad = (int) (item->size / 2);
+    int cx = item->size / 2;
+    int cy = cx;
     double spot_angle = M_PI / 4.0;
     double spot_rad = rad / 2.0;
     char string[2];
@@ -135,7 +141,6 @@ goo_demo_item_paint (GooCanvasItemSimple *simple,
 
     cairo_set_source_rgb (cr, 0.2, 0.3, 0.8);
     cairo_show_text (cr, string);
-                  
 
     cairo_restore (cr);
 }
@@ -152,8 +157,8 @@ goo_demo_item_get_item_at (GooCanvasItemSimple *simple,
 {
     GooDemoItem *demo_item = (GooDemoItem*) simple;
 
-    if (x < demo_item->x || (x > demo_item->x + demo_item->width)
-       || y < demo_item->y || (y > demo_item->y + demo_item->height))
+    if (x < 0 || (x > demo_item->size)
+       || y < 0 || (y > demo_item->size))
        return NULL;
 
     return (GooCanvasItem*) simple;
index d7dc396dd3a0a12d9071808d81f5d2c9d6e58834..180b599f98a857b2cdf1e0d2adc5e21870e4c7c1 100644 (file)
@@ -26,7 +26,7 @@ struct _GooDemoItem
 {
     GooCanvasItemSimple parent_object;
 
-    gdouble x, y, width, height;
+    double size;
     char letter;
 };
 
@@ -40,10 +40,15 @@ GType               goo_demo_item_get_type  (void) G_GNUC_CONST;
 GooCanvasItem*      goo_demo_item_new       (GooCanvasItem      *parent,
                                             gdouble             x,
                                             gdouble             y,
-                                            gdouble             width,
-                                            gdouble             height,
+                                            gdouble             size,
                                             char                letter,
                                             ...);
+
+void
+goo_demo_item_move_to (GooCanvasItem   *item,
+                      gdouble           x,
+                      gdouble           y);
+
 G_END_DECLS
 
 #endif /* __GOO_DEMO_ITEM_H__ */
index 9173abb30143579a73febd69a7a86603a5674246..8fc516f41788f87d828264afda97b08e37041b3c 100644 (file)
@@ -21,6 +21,7 @@
 #include <sys/time.h>
 #include <time.h>
 #include <ctype.h>
+#include <math.h>
 
 #include "word-game.h"
 #include "demo-item.h"
@@ -106,12 +107,7 @@ on_button_press (GooCanvasItem  *item,
 
     for (i = 0; i < num_letters; i++) {
        get_letter_position (indices[i], &x, &y);
-       goo_canvas_item_animate (letter_items[i],
-                                x - GOO_DEMO_ITEM (letter_items[i])->x,
-                                y - GOO_DEMO_ITEM (letter_items[i])->y,
-                                1.0, 0,
-                                1000, 40,
-                                GOO_CANVAS_ANIMATE_FREEZE);
+       goo_demo_item_move_to (letter_items[i], x, y);
     }
 
     return TRUE;
@@ -137,7 +133,7 @@ create_canvas (GtkWidget *parent, char *word)
        get_letter_position (i, &x, &y);
        letter_items[i] = goo_demo_item_new (root,
                                             x, y,
-                                            LETTER_SIZE, LETTER_SIZE,
+                                            LETTER_SIZE,
                                             word[i],
                                             NULL);