X-Git-Url: https://git.cworth.org/git?p=wordgame;a=blobdiff_plain;f=demo-item.c;h=198857ed5206c50412abadbe4e585207c6a5fba9;hp=2eaf830c2235faee43ec53633f3c7d2262e90ec7;hb=HEAD;hpb=f8d083966703a2b0efb6db1782a14a1a0b5d56f4 diff --git a/demo-item.c b/demo-item.c index 2eaf830..198857e 100644 --- a/demo-item.c +++ b/demo-item.c @@ -4,6 +4,8 @@ * * demo-item.c - a simple demo item. */ +#include + #include "goocanvas.h" #include "demo-item.h" @@ -16,8 +18,6 @@ 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; } @@ -26,27 +26,30 @@ goo_demo_item_init (GooDemoItem *demo_item) parent argument and end with a variable list of object properties to fit in with the standard canvas items. */ GooCanvasItem* -goo_demo_item_new (GooCanvasItem *parent, - gdouble x, - gdouble y, - gdouble width, - gdouble height, +goo_demo_item_new (GooCanvasItem *parent, + gdouble x, + gdouble y, + gdouble width, + gdouble height, + GooDemoItemPaintFunc paint, + void *closure, ...) { GooCanvasItem *item; GooDemoItem *demo_item; const char *first_property; va_list var_args; + cairo_matrix_t matrix; 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->paint = paint; + demo_item->closure = closure; - va_start (var_args, height); + va_start (var_args, closure); first_property = va_arg (var_args, char*); if (first_property) g_object_set_valist ((GObject*) item, first_property, var_args); @@ -58,6 +61,9 @@ goo_demo_item_new (GooCanvasItem *parent, g_object_unref (item); } + cairo_matrix_init_translate (&matrix, x, y); + goo_canvas_item_set_transform (item, &matrix); + return item; } @@ -72,10 +78,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->width; + simple->bounds.y2 = demo_item->height; /* Convert to device coordinates. */ goo_canvas_item_simple_user_bounds_to_device (simple, cr, &simple->bounds); @@ -88,16 +94,9 @@ goo_demo_item_paint (GooCanvasItemSimple *simple, cairo_t *cr, GooCanvasBounds *bounds) { - GooDemoItem *demo_item = (GooDemoItem*) simple; + GooDemoItem *item = (GooDemoItem*) simple; - 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); + item->paint (cr, item->closure, item->width, item->height); } /* Hit detection. This should check if the given coordinate (in the item's @@ -112,8 +111,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->width) + || y < 0 || (y > demo_item->height)) return NULL; return (GooCanvasItem*) simple;