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