| 1 |
#include <clutter/clutter.h> |
| 2 |
|
| 3 |
#include <math.h> |
| 4 |
#include <errno.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <glib.h> |
| 7 |
#include <gmodule.h> |
| 8 |
|
| 9 |
#define NHANDS 6 |
| 10 |
|
| 11 |
typedef struct SuperOH |
| 12 |
{ |
| 13 |
ClutterActor **hand; |
| 14 |
ClutterActor *bgtex; |
| 15 |
ClutterActor *real_hand; |
| 16 |
ClutterActor *group; |
| 17 |
ClutterActor *stage; |
| 18 |
|
| 19 |
gint stage_width; |
| 20 |
gint stage_height; |
| 21 |
gfloat radius; |
| 22 |
|
| 23 |
ClutterBehaviour *scaler_1; |
| 24 |
ClutterBehaviour *scaler_2; |
| 25 |
ClutterTimeline *timeline; |
| 26 |
} SuperOH; |
| 27 |
|
| 28 |
static gint n_hands = NHANDS; |
| 29 |
|
| 30 |
static GOptionEntry super_oh_entries[] = { |
| 31 |
{ |
| 32 |
"num-hands", 'n', |
| 33 |
0, |
| 34 |
G_OPTION_ARG_INT, &n_hands, |
| 35 |
"Number of hands", "HANDS" |
| 36 |
}, |
| 37 |
{ NULL } |
| 38 |
}; |
| 39 |
|
| 40 |
static void |
| 41 |
on_group_destroy (ClutterActor *actor, |
| 42 |
SuperOH *oh) |
| 43 |
{ |
| 44 |
oh->group = NULL; |
| 45 |
} |
| 46 |
|
| 47 |
static void |
| 48 |
on_hand_destroy (ClutterActor *actor, |
| 49 |
SuperOH *oh) |
| 50 |
{ |
| 51 |
int i; |
| 52 |
|
| 53 |
for (i = 0; i < n_hands; i++) |
| 54 |
{ |
| 55 |
if (oh->hand[i] == actor) |
| 56 |
oh->hand[i] = NULL; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static gboolean |
| 61 |
on_button_press_event (ClutterActor *actor, |
| 62 |
ClutterEvent *event, |
| 63 |
SuperOH *oh) |
| 64 |
{ |
| 65 |
gfloat x, y; |
| 66 |
|
| 67 |
clutter_event_get_coords (event, &x, &y); |
| 68 |
|
| 69 |
g_print ("*** button press event (button:%d) at %.2f, %.2f on %s ***\n", |
| 70 |
clutter_event_get_button (event), |
| 71 |
x, y, |
| 72 |
clutter_actor_get_name (actor)); |
| 73 |
|
| 74 |
clutter_actor_hide (actor); |
| 75 |
|
| 76 |
return TRUE; |
| 77 |
} |
| 78 |
|
| 79 |
static gboolean |
| 80 |
input_cb (ClutterActor *stage, |
| 81 |
ClutterEvent *event, |
| 82 |
gpointer data) |
| 83 |
{ |
| 84 |
SuperOH *oh = data; |
| 85 |
|
| 86 |
if (event->type == CLUTTER_KEY_RELEASE) |
| 87 |
{ |
| 88 |
g_print ("*** key press event (key:%c) ***\n", |
| 89 |
clutter_event_get_key_symbol (event)); |
| 90 |
|
| 91 |
if (clutter_event_get_key_symbol (event) == CLUTTER_KEY_q) |
| 92 |
{ |
| 93 |
clutter_main_quit (); |
| 94 |
|
| 95 |
return TRUE; |
| 96 |
} |
| 97 |
else if (clutter_event_get_key_symbol (event) == CLUTTER_KEY_r) |
| 98 |
{ |
| 99 |
gint i; |
| 100 |
|
| 101 |
for (i = 0; i < n_hands; i++) |
| 102 |
{ |
| 103 |
if (oh->hand[i] != NULL) |
| 104 |
clutter_actor_show (oh->hand[i]); |
| 105 |
} |
| 106 |
|
| 107 |
return TRUE; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return FALSE; |
| 112 |
} |
| 113 |
|
| 114 |
/* Timeline handler */ |
| 115 |
static void |
| 116 |
frame_cb (ClutterTimeline *timeline, |
| 117 |
gint msecs, |
| 118 |
gpointer data) |
| 119 |
{ |
| 120 |
SuperOH *oh = data; |
| 121 |
gint i; |
| 122 |
float rotation = clutter_timeline_get_progress (timeline) * 360.0f; |
| 123 |
|
| 124 |
/* Rotate everything clockwise about stage center*/ |
| 125 |
if (oh->group != NULL) |
| 126 |
clutter_actor_set_rotation (oh->group, |
| 127 |
CLUTTER_Z_AXIS, |
| 128 |
rotation, |
| 129 |
oh->stage_width / 2, |
| 130 |
oh->stage_height / 2, |
| 131 |
0); |
| 132 |
|
| 133 |
for (i = 0; i < n_hands; i++) |
| 134 |
{ |
| 135 |
/* Rotate each hand around there centers - to get this we need |
| 136 |
* to take into account any scaling. |
| 137 |
*/ |
| 138 |
if (oh->hand[i] != NULL) |
| 139 |
clutter_actor_set_rotation (oh->hand[i], |
| 140 |
CLUTTER_Z_AXIS, |
| 141 |
-6.0 * rotation, |
| 142 |
0, 0, 0); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
static void |
| 147 |
stop_and_quit (ClutterActor *stage, |
| 148 |
SuperOH *data) |
| 149 |
{ |
| 150 |
clutter_timeline_stop (data->timeline); |
| 151 |
|
| 152 |
clutter_main_quit (); |
| 153 |
} |
| 154 |
|
| 155 |
static gdouble |
| 156 |
my_sine_wave (ClutterAlpha *alpha, |
| 157 |
gpointer dummy G_GNUC_UNUSED) |
| 158 |
{ |
| 159 |
ClutterTimeline *timeline = clutter_alpha_get_timeline (alpha); |
| 160 |
gdouble progress = clutter_timeline_get_progress (timeline); |
| 161 |
|
| 162 |
return sin (progress * G_PI); |
| 163 |
} |
| 164 |
|
| 165 |
G_MODULE_EXPORT int |
| 166 |
test_actors_main (int argc, char *argv[]) |
| 167 |
{ |
| 168 |
ClutterAlpha *alpha; |
| 169 |
SuperOH *oh; |
| 170 |
gint i; |
| 171 |
GError *error; |
| 172 |
ClutterActor *real_hand; |
| 173 |
gchar *file; |
| 174 |
|
| 175 |
error = NULL; |
| 176 |
|
| 177 |
if (clutter_init_with_args (&argc, &argv, |
| 178 |
NULL, |
| 179 |
super_oh_entries, |
| 180 |
NULL, |
| 181 |
&error) != CLUTTER_INIT_SUCCESS) |
| 182 |
{ |
| 183 |
g_warning ("Unable to initialise Clutter:\n%s", |
| 184 |
error->message); |
| 185 |
g_error_free (error); |
| 186 |
|
| 187 |
return EXIT_FAILURE; |
| 188 |
} |
| 189 |
|
| 190 |
oh = g_new (SuperOH, 1); |
| 191 |
|
| 192 |
oh->stage = clutter_stage_new (); |
| 193 |
clutter_actor_set_size (oh->stage, 800, 600); |
| 194 |
clutter_actor_set_name (oh->stage, "Default Stage"); |
| 195 |
clutter_actor_set_background_color (oh->stage, CLUTTER_COLOR_LightSkyBlue); |
| 196 |
g_signal_connect (oh->stage, "destroy", G_CALLBACK (stop_and_quit), oh); |
| 197 |
|
| 198 |
clutter_stage_set_title (CLUTTER_STAGE (oh->stage), "Actors"); |
| 199 |
clutter_stage_set_user_resizable (CLUTTER_STAGE (oh->stage), TRUE); |
| 200 |
|
| 201 |
/* Create a timeline to manage animation */ |
| 202 |
oh->timeline = clutter_timeline_new (6000); |
| 203 |
clutter_timeline_set_repeat_count (oh->timeline, -1); |
| 204 |
|
| 205 |
/* fire a callback for frame change */ |
| 206 |
g_signal_connect (oh->timeline, "new-frame", G_CALLBACK (frame_cb), oh); |
| 207 |
|
| 208 |
/* Set up some behaviours to handle scaling */ |
| 209 |
alpha = clutter_alpha_new_with_func (oh->timeline, my_sine_wave, NULL, NULL); |
| 210 |
|
| 211 |
oh->scaler_1 = clutter_behaviour_scale_new (alpha, 0.5, 0.5, 1.0, 1.0); |
| 212 |
oh->scaler_2 = clutter_behaviour_scale_new (alpha, 1.0, 1.0, 0.5, 0.5); |
| 213 |
|
| 214 |
file = g_build_filename (TESTS_DATADIR, "redhand.png", NULL); |
| 215 |
real_hand = clutter_texture_new_from_file (file, &error); |
| 216 |
if (real_hand == NULL) |
| 217 |
g_error ("image load failed: %s", error->message); |
| 218 |
|
| 219 |
g_free (file); |
| 220 |
|
| 221 |
/* create a new actor to hold other actors */ |
| 222 |
oh->group = clutter_actor_new (); |
| 223 |
clutter_actor_set_layout_manager (oh->group, clutter_fixed_layout_new ()); |
| 224 |
clutter_actor_set_name (oh->group, "Group"); |
| 225 |
g_signal_connect (oh->group, "destroy", G_CALLBACK (on_group_destroy), oh); |
| 226 |
clutter_actor_add_constraint (oh->group, clutter_align_constraint_new (oh->stage, CLUTTER_ALIGN_BOTH, 0.5)); |
| 227 |
clutter_actor_add_constraint (oh->group, clutter_bind_constraint_new (oh->stage, CLUTTER_BIND_SIZE, 0.0f)); |
| 228 |
|
| 229 |
oh->hand = g_new (ClutterActor*, n_hands); |
| 230 |
|
| 231 |
oh->stage_width = clutter_actor_get_width (oh->stage); |
| 232 |
oh->stage_height = clutter_actor_get_height (oh->stage); |
| 233 |
oh->radius = (oh->stage_width + oh->stage_height) |
| 234 |
/ n_hands; |
| 235 |
|
| 236 |
for (i = 0; i < n_hands; i++) |
| 237 |
{ |
| 238 |
gint x, y, w, h; |
| 239 |
|
| 240 |
if (i == 0) |
| 241 |
{ |
| 242 |
oh->hand[i] = real_hand; |
| 243 |
clutter_actor_set_name (oh->hand[i], "Real Hand"); |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
oh->hand[i] = clutter_clone_new (real_hand); |
| 248 |
clutter_actor_set_name (oh->hand[i], "Clone Hand"); |
| 249 |
} |
| 250 |
|
| 251 |
clutter_actor_set_reactive (oh->hand[i], TRUE); |
| 252 |
|
| 253 |
clutter_actor_set_size (oh->hand[i], 200, 213); |
| 254 |
|
| 255 |
/* Place around a circle */ |
| 256 |
w = clutter_actor_get_width (oh->hand[i]); |
| 257 |
h = clutter_actor_get_height (oh->hand[i]); |
| 258 |
|
| 259 |
x = oh->stage_width / 2 |
| 260 |
+ oh->radius |
| 261 |
* cos (i * G_PI / (n_hands / 2)) |
| 262 |
- w / 2; |
| 263 |
|
| 264 |
y = oh->stage_height / 2 |
| 265 |
+ oh->radius |
| 266 |
* sin (i * G_PI / (n_hands / 2)) |
| 267 |
- h / 2; |
| 268 |
|
| 269 |
clutter_actor_set_position (oh->hand[i], x, y); |
| 270 |
|
| 271 |
clutter_actor_move_anchor_point_from_gravity (oh->hand[i], |
| 272 |
CLUTTER_GRAVITY_CENTER); |
| 273 |
|
| 274 |
/* Add to our group group */ |
| 275 |
clutter_container_add_actor (CLUTTER_CONTAINER (oh->group), oh->hand[i]); |
| 276 |
|
| 277 |
g_signal_connect (oh->hand[i], "button-press-event", |
| 278 |
G_CALLBACK (on_button_press_event), |
| 279 |
oh); |
| 280 |
|
| 281 |
g_signal_connect (oh->hand[i], "destroy", |
| 282 |
G_CALLBACK (on_hand_destroy), |
| 283 |
oh); |
| 284 |
|
| 285 |
if (i % 2) |
| 286 |
clutter_behaviour_apply (oh->scaler_1, oh->hand[i]); |
| 287 |
else |
| 288 |
clutter_behaviour_apply (oh->scaler_2, oh->hand[i]); |
| 289 |
} |
| 290 |
|
| 291 |
/* Add the group to the stage */ |
| 292 |
clutter_container_add_actor (CLUTTER_CONTAINER (oh->stage), oh->group); |
| 293 |
|
| 294 |
/* Show everying */ |
| 295 |
clutter_actor_show (oh->stage); |
| 296 |
|
| 297 |
g_signal_connect (oh->stage, "key-release-event", |
| 298 |
G_CALLBACK (input_cb), |
| 299 |
oh); |
| 300 |
|
| 301 |
/* and start it */ |
| 302 |
clutter_timeline_start (oh->timeline); |
| 303 |
|
| 304 |
clutter_main (); |
| 305 |
|
| 306 |
clutter_timeline_stop (oh->timeline); |
| 307 |
|
| 308 |
/* clean up */ |
| 309 |
g_object_unref (oh->scaler_1); |
| 310 |
g_object_unref (oh->scaler_2); |
| 311 |
g_object_unref (oh->timeline); |
| 312 |
g_free (oh->hand); |
| 313 |
g_free (oh); |
| 314 |
|
| 315 |
return EXIT_SUCCESS; |
| 316 |
} |