]> git.cworth.org Git - wordgame/blobdiff - demo-item.c
Fix randomizing of letter positions to never end up on top of each other
[wordgame] / demo-item.c
index 2eaf830c2235faee43ec53633f3c7d2262e90ec7..99aaf4afa856a24213c2ea26841e323fe625d6e8 100644 (file)
@@ -4,6 +4,8 @@
  *
  * demo-item.c - a simple demo item.
  */
+#include <math.h>
+
 #include "goocanvas.h"
 #include "demo-item.h"
 
@@ -16,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 
@@ -29,8 +39,8 @@ GooCanvasItem*
 goo_demo_item_new (GooCanvasItem      *parent,
                   gdouble             x,
                   gdouble             y,
-                  gdouble             width,
-                  gdouble             height,
+                  gdouble             size,
+                  char                letter,
                   ...)
 {
     GooCanvasItem *item;
@@ -41,12 +51,10 @@ 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, height);
+    va_start (var_args, letter);
     first_property = va_arg (var_args, char*);
     if (first_property)
        g_object_set_valist ((GObject*) item, first_property, var_args);
@@ -58,6 +66,8 @@ goo_demo_item_new (GooCanvasItem      *parent,
        g_object_unref (item);
     }
 
+    goo_demo_item_move_to (item, x, y);
+
     return item;
 }
 
@@ -72,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);
@@ -88,16 +98,51 @@ goo_demo_item_paint (GooCanvasItemSimple *simple,
                     cairo_t             *cr,
                     GooCanvasBounds     *bounds)
 {
-    GooDemoItem *demo_item = (GooDemoItem*) simple;
+    GooDemoItem *item = (GooDemoItem*) simple;
+    cairo_pattern_t *gradient;
+    cairo_text_extents_t extents;
+    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];
+
+    cairo_save (cr);
+
+    gradient = cairo_pattern_create_radial (cx - spot_rad * cos (spot_angle),
+                                           cy - spot_rad * sin (spot_angle),
+                                           0.0,
+                                           cx - spot_rad * cos (spot_angle),
+                                           cy - spot_rad * sin (spot_angle),
+                                           rad + spot_rad);
+    cairo_pattern_add_color_stop_rgb (gradient, 0.0, 1.0, 1.0, 1.0);
+    cairo_pattern_add_color_stop_rgb (gradient, 1.0, 0.33, 0.33, 0.33);
+
+    cairo_set_source (cr, gradient);
+
+    cairo_arc (cr,
+              cx, cy,
+              rad, 0, 2 * M_PI);
 
-    cairo_move_to (cr, demo_item->x, demo_item->y);
-    cairo_line_to (cr, demo_item->x, demo_item->y + demo_item->height);
-    cairo_line_to (cr, demo_item->x + demo_item->width,
-                  demo_item->y + demo_item->height);
-    cairo_line_to (cr, demo_item->x + demo_item->width, demo_item->y);
-    cairo_close_path (cr);
-    goo_canvas_style_set_fill_options (simple->simple_data->style, cr);
     cairo_fill (cr);
+
+    cairo_select_font_face (cr, "mono",
+                           CAIRO_FONT_SLANT_NORMAL,
+                           CAIRO_FONT_WEIGHT_BOLD);
+    cairo_set_font_size (cr, 1.8 * rad);
+
+    string[0] = item->letter;
+    string[1] = '\0';
+    cairo_text_extents (cr, string, &extents);
+    cairo_move_to (cr,
+                  cx - extents.width / 2 - extents.x_bearing,
+                  cy - extents.height / 2 - extents.y_bearing);
+
+    cairo_set_source_rgb (cr, 0.2, 0.3, 0.8);
+    cairo_show_text (cr, string);
+
+    cairo_restore (cr);
 }
 
 /* Hit detection. This should check if the given coordinate (in the item's
@@ -112,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;