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