moving clutter-gtk out of the main tree
[clutter-gtk] / examples / gtk-clutter-test.c
1 #include <gtk/gtk.h>
2 #include <clutter/clutter.h>
3 #include <math.h>
4
5 #include <clutter-gtk/gtk-clutter.h>
6
7 #define TRAILS 0
8 #define NHANDS  2
9 #define WINWIDTH   800
10 #define WINHEIGHT  800
11 #define RADIUS     250
12
13 typedef struct SuperOH
14 {
15   ClutterActor *hand[NHANDS], *bgtex;
16   ClutterGroup   *group;
17   GdkPixbuf      *bgpixb;
18
19 } SuperOH; 
20
21 gboolean fade = FALSE;
22
23 /* input handler */
24 void 
25 input_cb (ClutterStage *stage, 
26           ClutterEvent *event,
27           gpointer      data)
28 {
29   if (event->type == CLUTTER_BUTTON_PRESS)
30     {
31       ClutterButtonEvent *bev = (ClutterButtonEvent *) event;
32       ClutterActor *e;
33
34       g_print ("*** button press event (button:%d) ***\n",
35                bev->button);
36
37       e = clutter_stage_get_actor_at_pos (stage, 
38                                             clutter_button_event_x (bev),
39                                             clutter_button_event_y (bev));
40
41       if (e)
42         clutter_actor_hide(e);
43     }
44   else if (event->type == CLUTTER_KEY_PRESS)
45     {
46       ClutterKeyEvent *kev = (ClutterKeyEvent *) event;
47
48       g_print ("*** key press event (key:%c) ***\n",
49                clutter_key_event_symbol (kev));
50       
51       if (clutter_key_event_symbol (kev) == CLUTTER_q)
52         clutter_main_quit ();
53     }
54 }
55
56
57 /* Timeline handler */
58 void
59 frame_cb (ClutterTimeline *timeline, 
60           gint             frame_num, 
61           gpointer         data)
62 {
63   SuperOH        *oh = (SuperOH *)data;
64   gint            i;
65
66 #if TRAILS
67   oh->bgpixb = clutter_stage_snapshot (CLUTTER_STAGE (stage),
68                                        0, 0,
69                                        WINWIDTH,
70                                        WINHEIGHT);
71   clutter_texture_set_pixbuf (CLUTTER_TEXTURE (oh->bgtex), oh->bgpixb);
72   g_object_unref (G_OBJECT (oh->bgpixb));
73   g_object_unref (stage);
74 #endif
75
76   /* Rotate everything clockwise about stage center*/
77   clutter_actor_rotate_z (CLUTTER_ACTOR(oh->group),
78                             frame_num,
79                             WINWIDTH/2,
80                             WINHEIGHT/2);
81   for (i = 0; i < NHANDS; i++)
82     {
83       /* rotate each hand around there centers */
84       clutter_actor_rotate_z (oh->hand[i],
85                                 - 6.0 * frame_num,
86                                 clutter_actor_get_width (oh->hand[i])/2,
87                                 clutter_actor_get_height (oh->hand[i])/2);
88       if (fade == TRUE) {
89               clutter_actor_set_opacity (oh->hand[i], (255 - (frame_num % 255)));
90       }
91     }
92
93           
94   /*
95   clutter_actor_rotate_x (CLUTTER_ACTOR(oh->group),
96                             75.0,
97                             WINHEIGHT/2, 0);
98   */
99 }
100
101 static void
102 clickity (GtkButton *button,
103           gpointer ud)
104 {
105         fade = !fade;
106 }
107
108 int
109 main (int argc, char *argv[])
110 {
111   ClutterTimeline *timeline;
112   ClutterActor  *stage;
113   ClutterColor     stage_color = { 0x61, 0x64, 0x8c, 0xff };
114   GtkWidget       *window, *clutter;
115   GtkWidget       *label, *button, *vbox;
116   GdkPixbuf       *pixbuf;
117   SuperOH         *oh;
118   gint             i;
119
120   clutter_init (&argc, &argv);
121   gtk_init (&argc, &argv);
122
123
124   pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL);
125
126   if (!pixbuf)
127     g_error("pixbuf load failed");
128
129   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
130   g_signal_connect (window, "destroy",
131                     G_CALLBACK (gtk_main_quit), NULL);
132
133   vbox = gtk_vbox_new (FALSE, 6);
134   gtk_container_add (GTK_CONTAINER (window), vbox);
135
136   clutter = g_object_new (GTK_TYPE_CLUTTER, NULL);
137   stage = gtk_clutter_get_stage (GTK_CLUTTER (clutter));
138   
139   gtk_container_add (GTK_CONTAINER (vbox), clutter);
140
141   /* Set our stage size */
142 /*   clutter_actor_set_size (stage, WINWIDTH, WINHEIGHT); */
143
144   label = gtk_label_new ("This is a label");
145   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
146
147   button = gtk_button_new_with_label ("This is a button...clicky");
148   g_signal_connect (button, "clicked",
149                     G_CALLBACK (clickity), NULL);
150   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
151   
152   /* and its background color */
153
154   clutter_stage_set_color (CLUTTER_STAGE (stage),
155                            &stage_color);
156
157   oh = g_new(SuperOH, 1);
158
159 #if TRAILS
160   oh->bgtex = clutter_texture_new();
161   clutter_actor_set_size (oh->bgtex, WINWIDTH, WINHEIGHT);
162   clutter_actor_set_opacity (oh->bgtex, 0x99);
163   clutter_group_add (CLUTTER_GROUP (stage), oh->bgtex);
164 #endif
165
166   /* create a new group to hold multiple actors in a group */
167   oh->group = CLUTTER_GROUP (clutter_group_new());
168   
169   for (i = 0; i < NHANDS; i++)
170     {
171       gint x, y, w, h;
172 #if 1
173       /* Create a texture from pixbuf, then clone in to same resources */
174       if (i == 0)
175        oh->hand[i] = clutter_texture_new_from_pixbuf (pixbuf);
176      else
177        oh->hand[i] = clutter_clone_texture_new (CLUTTER_TEXTURE(oh->hand[0]));
178 #else
179       ClutterColor colour = { 255, 0, 0, 255 };
180
181       oh->hand[i] = clutter_rectangle_new_with_color (&colour);
182       clutter_actor_set_size (oh->hand[i], 50, 50);
183 #endif
184       /* Place around a circle */
185       w = clutter_actor_get_width (oh->hand[0]);
186       h = clutter_actor_get_height (oh->hand[0]);
187
188       x = WINWIDTH/2  + RADIUS * cos (i * M_PI / (NHANDS/2)) - w/2;
189       y = WINHEIGHT/2 + RADIUS * sin (i * M_PI / (NHANDS/2)) - h/2;
190
191       clutter_actor_set_position (oh->hand[i], x, y);
192
193       /* Add to our group group */
194       clutter_group_add (oh->group, oh->hand[i]);
195     }
196
197   /* Add the group to the stage */
198   clutter_group_add (CLUTTER_GROUP (stage), CLUTTER_ACTOR(oh->group));
199
200   /* Show everying ( and map window ) */
201   clutter_group_show_all (oh->group);
202
203   g_signal_connect (stage, "button-press-event",
204                     G_CALLBACK (input_cb), 
205                     oh);
206   g_signal_connect (stage, "key-release-event",
207                     G_CALLBACK (input_cb),
208                     oh);
209
210   gtk_widget_show_all (window);
211
212   /* Create a timeline to manage animation */
213   timeline = clutter_timeline_new (360, 60); /* num frames, fps */
214   g_object_set(timeline, "loop", TRUE, NULL);   /* have it loop */
215
216   /* fire a callback for frame change */
217   g_signal_connect(timeline, "new-frame",  G_CALLBACK (frame_cb), oh);
218
219   /* and start it */
220   clutter_timeline_start (timeline);
221
222   gtk_main();
223
224   return 0;
225 }