]> git.cworth.org Git - wordgame/blob - demo-item.c
Abstract tile-specific code out of demo-item.c
[wordgame] / demo-item.c
1 /*
2  * GooCanvas Demo. Copyright (C) 2006 Damon Chaplin.
3  * Released under the GNU LGPL license. See COPYING for details.
4  *
5  * demo-item.c - a simple demo item.
6  */
7 #include <math.h>
8
9 #include "goocanvas.h"
10 #include "demo-item.h"
11
12 /* Use the GLib convenience macro to define the type. GooDemoItem is the
13    class struct, goo_demo_item is the function prefix, and our class is a
14    subclass of GOO_TYPE_CANVAS_ITEM_SIMPLE. */
15 G_DEFINE_TYPE (GooDemoItem, goo_demo_item, GOO_TYPE_CANVAS_ITEM_SIMPLE)
16
17 /* The standard object initialization function. */
18     static void
19 goo_demo_item_init (GooDemoItem *demo_item)
20 {
21     demo_item->size = 0.0;
22 }
23
24 /* The convenience function to create new items. This should start with a 
25    parent argument and end with a variable list of object properties to fit
26    in with the standard canvas items. */
27 GooCanvasItem*
28 goo_demo_item_new (GooCanvasItem        *parent,
29                    gdouble               size,
30                    GooDemoItemPaintFunc  paint,
31                    void                 *closure,
32                    ...)
33 {
34     GooCanvasItem *item;
35     GooDemoItem *demo_item;
36     const char *first_property;
37     va_list var_args;
38
39     item = g_object_new (GOO_TYPE_DEMO_ITEM, NULL);
40
41     demo_item = (GooDemoItem*) item;
42     demo_item->size = size;
43     demo_item->paint = paint;
44     demo_item->closure = closure;
45
46     va_start (var_args, closure);
47     first_property = va_arg (var_args, char*);
48     if (first_property)
49         g_object_set_valist ((GObject*) item, first_property, var_args);
50     va_end (var_args);
51
52     if (parent)
53     {
54         goo_canvas_item_add_child (parent, item, -1);
55         g_object_unref (item);
56     }
57
58     return item;
59 }
60
61 /* The update method. This is called when the canvas is initially shown and
62    also whenever the object is updated and needs to change its size and/or
63    shape. It should calculate its new bounds, storing them in simple->bounds,
64    and it should convert these to device coordinates. */
65 static void
66 goo_demo_item_update  (GooCanvasItemSimple *simple,
67                        cairo_t             *cr)
68 {
69     GooDemoItem *demo_item = (GooDemoItem*) simple;
70
71     /* Compute the new bounds. */
72     simple->bounds.x1 = 0;
73     simple->bounds.y1 = 0;
74     simple->bounds.x2 = demo_item->size;
75     simple->bounds.y2 = demo_item->size;
76
77     /* Convert to device coordinates. */
78     goo_canvas_item_simple_user_bounds_to_device (simple, cr, &simple->bounds);
79 }
80
81 /* The paint method. This should draw the item on the given cairo_t, using
82    the item's own coordinate space. */
83 static void
84 goo_demo_item_paint (GooCanvasItemSimple *simple,
85                      cairo_t             *cr,
86                      GooCanvasBounds     *bounds)
87 {
88     GooDemoItem *item = (GooDemoItem*) simple;
89
90     item->paint (cr, item->closure);
91 }
92
93 /* Hit detection. This should check if the given coordinate (in the item's
94    coordinate space) is within the item. If it is it should return the item,
95    otherwise it should return NULL. */
96 static GooCanvasItem*
97 goo_demo_item_get_item_at (GooCanvasItemSimple *simple,
98                            gdouble              x,
99                            gdouble              y,
100                            cairo_t             *cr,
101                            gboolean             is_pointer_event)
102 {
103     GooDemoItem *demo_item = (GooDemoItem*) simple;
104
105     if (x < 0 || (x > demo_item->size)
106         || y < 0 || (y > demo_item->size))
107         return NULL;
108
109     return (GooCanvasItem*) simple;
110 }
111
112 /* The class initialization function. Here we set the class' update(), paint()
113    and get_item_at() methods. */
114 static void
115 goo_demo_item_class_init (GooDemoItemClass *klass)
116 {
117     GooCanvasItemSimpleClass *simple_class = (GooCanvasItemSimpleClass*) klass;
118
119     simple_class->update        = goo_demo_item_update;
120     simple_class->paint         = goo_demo_item_paint;
121     simple_class->get_item_at   = goo_demo_item_get_item_at;
122 }