Merge branch '1.0-integration'
[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/clutter-gtk.h>
6
7 #define TRAILS 0
8 #define NHANDS  2
9 #define WINWIDTH   400
10 #define WINHEIGHT  400
11 #define RADIUS     150
12
13 typedef struct SuperOH
14 {
15   ClutterActor *hand[NHANDS], *bgtex;
16   ClutterGroup   *group;
17   GdkPixbuf      *bgpixb;
18
19 } SuperOH; 
20
21 static gboolean fade = FALSE;
22 static gboolean fullscreen = FALSE;
23
24 /* input handler */
25 void 
26 input_cb (ClutterStage *stage,
27           ClutterEvent *event,
28           gpointer      data)
29 {
30   if (event->type == CLUTTER_BUTTON_PRESS)
31     {
32       ClutterActor *a;
33       gfloat x, y;
34
35       clutter_event_get_coords (event, &x, &y);
36
37       a = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, x, y);
38       if (a && (CLUTTER_IS_TEXTURE (a) || CLUTTER_IS_CLONE (a)))
39         clutter_actor_hide (a);
40     }
41   else if (event->type == CLUTTER_KEY_PRESS)
42     {
43       ClutterKeyEvent *kev = (ClutterKeyEvent *) event;
44
45       g_print ("*** key press event (key:%c) ***\n",
46                clutter_key_event_symbol (kev));
47       
48       if (clutter_key_event_symbol (kev) == CLUTTER_q)
49         gtk_main_quit ();
50     }
51 }
52
53
54 /* Timeline handler */
55 void
56 frame_cb (ClutterTimeline *timeline, 
57           gint             msecs,
58           gpointer         data)
59 {
60   SuperOH        *oh = (SuperOH *)data;
61   gint            i;
62   guint           rotation = clutter_timeline_get_progress (timeline) * 360.0f;
63
64 #if TRAILS
65   oh->bgpixb = clutter_stage_snapshot (CLUTTER_STAGE (stage),
66                                        0, 0,
67                                        WINWIDTH,
68                                        WINHEIGHT);
69   clutter_texture_set_pixbuf (CLUTTER_TEXTURE (oh->bgtex), oh->bgpixb);
70   g_object_unref (G_OBJECT (oh->bgpixb));
71   g_object_unref (stage);
72 #endif
73
74   /* Rotate everything clockwise about stage center*/
75   clutter_actor_set_rotation (CLUTTER_ACTOR (oh->group),
76                               CLUTTER_Z_AXIS,
77                               rotation,
78                               WINWIDTH / 2, WINHEIGHT / 2, 0);
79
80   for (i = 0; i < NHANDS; i++)
81     {
82       /* rotate each hand around there centers */
83       clutter_actor_set_rotation (oh->hand[i],
84                                   CLUTTER_Z_AXIS,
85                                   - 6.0 * rotation,
86                                   clutter_actor_get_width (oh->hand[i]) / 2,
87                                   clutter_actor_get_height (oh->hand[i]) / 2,
88                                   0);
89       if (fade == TRUE)
90         clutter_actor_set_opacity (oh->hand[i], (255 - (rotation % 255)));
91     }
92
93   /*
94   clutter_actor_rotate_x (CLUTTER_ACTOR(oh->group),
95                             75.0,
96                             WINHEIGHT/2, 0);
97   */
98 }
99
100 static void
101 clickity (GtkButton *button,
102           gpointer ud)
103 {
104         fade = !fade;
105 }
106
107 static void
108 on_fullscreen (GtkButton *button,
109                GtkWindow *window)
110 {
111   if (!fullscreen)
112     {
113       gtk_window_fullscreen (window);
114       fullscreen = TRUE;
115     }
116   else
117     {
118       gtk_window_unfullscreen (window);
119       fullscreen = FALSE;
120     }
121 }
122
123 int
124 main (int argc, char *argv[])
125 {
126   ClutterTimeline *timeline;
127   ClutterActor    *stage;
128   ClutterColor     stage_color = { 0x61, 0x64, 0x8c, 0xff };
129   GtkWidget       *window, *clutter;
130   GtkWidget       *label, *button, *vbox;
131   GdkPixbuf       *pixbuf;
132   SuperOH         *oh;
133   gint             i;
134
135   if (gtk_clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
136     g_error ("Unable to initialize GtkClutter");
137
138   pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL);
139
140   if (!pixbuf)
141     g_error("pixbuf load failed");
142
143   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
144   g_signal_connect (window, "destroy",
145                     G_CALLBACK (gtk_main_quit), NULL);
146
147   vbox = gtk_vbox_new (FALSE, 6);
148   gtk_container_add (GTK_CONTAINER (window), vbox);
149
150   clutter = gtk_clutter_embed_new ();
151   gtk_widget_set_size_request (clutter, WINWIDTH, WINHEIGHT);
152
153   gtk_container_add (GTK_CONTAINER (vbox), clutter);
154
155   stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (clutter));
156
157   label = gtk_label_new ("This is a label");
158   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
159
160   button = gtk_button_new_with_label ("This is a button...clicky");
161   g_signal_connect (button, "clicked",
162                     G_CALLBACK (clickity), NULL);
163   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
164
165   button = gtk_button_new_with_label ("Fullscreen");
166   gtk_button_set_image (GTK_BUTTON (button),
167                         gtk_image_new_from_stock (GTK_STOCK_FULLSCREEN,
168                                                   GTK_ICON_SIZE_BUTTON));
169   g_signal_connect (button, "clicked",
170                     G_CALLBACK (on_fullscreen),
171                     window);
172   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
173
174   button = gtk_button_new_from_stock (GTK_STOCK_QUIT);
175   g_signal_connect_swapped (button, "clicked",
176                             G_CALLBACK (gtk_widget_destroy),
177                             window);
178   gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0);
179   
180   /* and its background color */
181
182   clutter_stage_set_color (CLUTTER_STAGE (stage),
183                            &stage_color);
184
185   oh = g_new(SuperOH, 1);
186
187 #if TRAILS
188   oh->bgtex = clutter_texture_new();
189   clutter_actor_set_size (oh->bgtex, WINWIDTH, WINHEIGHT);
190   clutter_actor_set_opacity (oh->bgtex, 0x99);
191   clutter_group_add (CLUTTER_GROUP (stage), oh->bgtex);
192 #endif
193
194   /* create a new group to hold multiple actors in a group */
195   oh->group = CLUTTER_GROUP (clutter_group_new ());
196   
197   for (i = 0; i < NHANDS; i++)
198     {
199       gint x, y, w, h;
200 #if 1
201       /* Create a texture from pixbuf, then clone in to same resources */
202       if (i == 0)
203        oh->hand[i] = gtk_clutter_texture_new_from_pixbuf (pixbuf);
204      else
205        oh->hand[i] = clutter_clone_new (oh->hand[0]);
206 #else
207       ClutterColor colour = { 255, 0, 0, 255 };
208
209       oh->hand[i] = clutter_rectangle_new_with_color (&colour);
210       clutter_actor_set_size (oh->hand[i], 50, 50);
211 #endif
212       /* Place around a circle */
213       w = clutter_actor_get_width (oh->hand[0]);
214       h = clutter_actor_get_height (oh->hand[0]);
215
216       x = WINWIDTH/2  + RADIUS * cos (i * M_PI / (NHANDS/2)) - w/2;
217       y = WINHEIGHT/2 + RADIUS * sin (i * M_PI / (NHANDS/2)) - h/2;
218
219       clutter_actor_set_position (oh->hand[i], x, y);
220
221       /* Add to our group group */
222       clutter_group_add (oh->group, oh->hand[i]);
223     }
224
225   /* Add the group to the stage */
226   clutter_container_add_actor (CLUTTER_CONTAINER (stage),
227                                CLUTTER_ACTOR (oh->group));
228
229   g_signal_connect (stage, "button-press-event",
230                     G_CALLBACK (input_cb), 
231                     oh);
232   g_signal_connect (stage, "key-release-event",
233                     G_CALLBACK (input_cb),
234                     oh);
235
236   gtk_widget_show_all (window);
237
238   /* Only show the actors after parent show otherwise it will just be
239    * unrealized when the clutter foreign window is set. widget_show
240    * will call show on the stage.
241    */
242   clutter_actor_show_all (CLUTTER_ACTOR (oh->group));
243
244   /* Create a timeline to manage animation */
245   timeline = clutter_timeline_new (6000);
246   g_object_set(timeline, "loop", TRUE, NULL);   /* have it loop */
247
248   /* fire a callback for frame change */
249   g_signal_connect(timeline, "new-frame",  G_CALLBACK (frame_cb), oh);
250
251   /* and start it */
252   clutter_timeline_start (timeline);
253
254   gtk_main();
255
256   return 0;
257 }