2007-06-11 Neil Jagdish Patel <njp@o-hand.com>
[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   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       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_rotate_z (CLUTTER_ACTOR(oh->group),
74                             frame_num,
75                             WINWIDTH/2,
76                             WINHEIGHT/2);
77   for (i = 0; i < NHANDS; i++)
78     {
79       /* rotate each hand around there centers */
80       clutter_actor_rotate_z (oh->hand[i],
81                                 - 6.0 * frame_num,
82                                 clutter_actor_get_width (oh->hand[i])/2,
83                                 clutter_actor_get_height (oh->hand[i])/2);
84       if (fade == TRUE) {
85               clutter_actor_set_opacity (oh->hand[i], (255 - (frame_num % 255)));
86       }
87     }
88
89           
90   /*
91   clutter_actor_rotate_x (CLUTTER_ACTOR(oh->group),
92                             75.0,
93                             WINHEIGHT/2, 0);
94   */
95 }
96
97 static void
98 clickity (GtkButton *button,
99           gpointer ud)
100 {
101         fade = !fade;
102 }
103
104 int
105 main (int argc, char *argv[])
106 {
107   ClutterTimeline *timeline;
108   ClutterActor  *stage;
109   ClutterColor     stage_color = { 0x61, 0x64, 0x8c, 0xff };
110   GtkWidget       *window, *clutter, *socket_box;
111   GtkWidget       *label, *button, *vbox;
112   GdkPixbuf       *pixbuf;
113   SuperOH         *oh;
114   gint             i;
115
116   clutter_init (&argc, &argv);
117   gtk_init (&argc, &argv);
118
119   pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL);
120
121   if (!pixbuf)
122     g_error("pixbuf load failed");
123
124   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
125   g_signal_connect (window, "destroy",
126                     G_CALLBACK (gtk_main_quit), NULL);
127
128   vbox = gtk_vbox_new (FALSE, 6);
129   gtk_container_add (GTK_CONTAINER (window), vbox);
130
131   clutter = gtk_clutter_new ();
132
133   gtk_container_add (GTK_CONTAINER (vbox), clutter);
134
135   stage = gtk_clutter_get_stage (GTK_CLUTTER (clutter));
136
137   label = gtk_label_new ("This is a label");
138   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
139
140   button = gtk_button_new_with_label ("This is a button...clicky");
141   g_signal_connect (button, "clicked",
142                     G_CALLBACK (clickity), NULL);
143   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
144
145   button = gtk_button_new_from_stock (GTK_STOCK_QUIT);
146   g_signal_connect_swapped (button, "clicked",
147                             G_CALLBACK (gtk_widget_destroy),
148                             window);
149   gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0);
150   
151   /* and its background color */
152
153   clutter_stage_set_color (CLUTTER_STAGE (stage),
154                            &stage_color);
155
156   oh = g_new(SuperOH, 1);
157
158 #if TRAILS
159   oh->bgtex = clutter_texture_new();
160   clutter_actor_set_size (oh->bgtex, WINWIDTH, WINHEIGHT);
161   clutter_actor_set_opacity (oh->bgtex, 0x99);
162   clutter_group_add (CLUTTER_GROUP (stage), oh->bgtex);
163 #endif
164
165   /* create a new group to hold multiple actors in a group */
166   oh->group = CLUTTER_GROUP (clutter_group_new());
167   
168   for (i = 0; i < NHANDS; i++)
169     {
170       gint x, y, w, h;
171 #if 1
172       /* Create a texture from pixbuf, then clone in to same resources */
173       if (i == 0)
174        oh->hand[i] = clutter_texture_new_from_pixbuf (pixbuf);
175      else
176        oh->hand[i] = clutter_clone_texture_new (CLUTTER_TEXTURE(oh->hand[0]));
177 #else
178       ClutterColor colour = { 255, 0, 0, 255 };
179
180       oh->hand[i] = clutter_rectangle_new_with_color (&colour);
181       clutter_actor_set_size (oh->hand[i], 50, 50);
182 #endif
183       /* Place around a circle */
184       w = clutter_actor_get_width (oh->hand[0]);
185       h = clutter_actor_get_height (oh->hand[0]);
186
187       x = WINWIDTH/2  + RADIUS * cos (i * M_PI / (NHANDS/2)) - w/2;
188       y = WINHEIGHT/2 + RADIUS * sin (i * M_PI / (NHANDS/2)) - h/2;
189
190       clutter_actor_set_position (oh->hand[i], x, y);
191
192       /* Add to our group group */
193       clutter_group_add (oh->group, oh->hand[i]);
194     }
195
196   /* Add the group to the stage */
197   clutter_group_add (CLUTTER_GROUP (stage), CLUTTER_ACTOR(oh->group));
198
199   /* Show everying ( and map window ) */
200   clutter_actor_show_all (CLUTTER_ACTOR (oh->group));
201
202   g_signal_connect (stage, "button-press-event",
203                     G_CALLBACK (input_cb), 
204                     oh);
205   g_signal_connect (stage, "key-release-event",
206                     G_CALLBACK (input_cb),
207                     oh);
208
209   gtk_widget_show_all (window);
210
211   /* Create a timeline to manage animation */
212   timeline = clutter_timeline_new (360, 60); /* num frames, fps */
213   g_object_set(timeline, "loop", TRUE, NULL);   /* have it loop */
214
215   /* fire a callback for frame change */
216   g_signal_connect(timeline, "new-frame",  G_CALLBACK (frame_cb), oh);
217
218   /* and start it */
219   clutter_timeline_start (timeline);
220
221   gtk_main();
222
223   return 0;
224 }