| 1 |
#include <stdlib.h> |
| 2 |
#include <string.h> |
| 3 |
|
| 4 |
#define CLUTTER_DISABLE_DEPRECATION_WARNINGS |
| 5 |
#include <clutter/clutter.h> |
| 6 |
|
| 7 |
#define NOTIFY_ANCHOR_X (1 << 0) |
| 8 |
#define NOTIFY_ANCHOR_Y (1 << 1) |
| 9 |
#define NOTIFY_ANCHOR_GRAVITY (1 << 2) |
| 10 |
#define NOTIFY_SCALE_X (1 << 3) |
| 11 |
#define NOTIFY_SCALE_Y (1 << 4) |
| 12 |
#define NOTIFY_SCALE_CENTER_X (1 << 5) |
| 13 |
#define NOTIFY_SCALE_CENTER_Y (1 << 6) |
| 14 |
#define NOTIFY_SCALE_GRAVITY (1 << 7) |
| 15 |
#define NOTIFY_ROTATION_ANGLE_X (1 << 8) |
| 16 |
#define NOTIFY_ROTATION_ANGLE_Y (1 << 9) |
| 17 |
#define NOTIFY_ROTATION_ANGLE_Z (1 << 10) |
| 18 |
#define NOTIFY_ROTATION_CENTER_X (1 << 11) |
| 19 |
#define NOTIFY_ROTATION_CENTER_Y (1 << 12) |
| 20 |
#define NOTIFY_ROTATION_CENTER_Z (1 << 13) |
| 21 |
#define NOTIFY_ROTATION_CENTER_Z_GRAVITY (1 << 14) |
| 22 |
|
| 23 |
#define RECT_WIDTH 100.0 |
| 24 |
#define RECT_HEIGHT 80.0 |
| 25 |
|
| 26 |
/* Allow the transformed position by off by a certain number of |
| 27 |
pixels */ |
| 28 |
#define POSITION_TOLERANCE 2 |
| 29 |
|
| 30 |
typedef struct _TestState |
| 31 |
{ |
| 32 |
gulong notifications; |
| 33 |
ClutterActor *rect; |
| 34 |
} TestState; |
| 35 |
|
| 36 |
static const struct |
| 37 |
{ |
| 38 |
ClutterGravity gravity; |
| 39 |
gfloat x_pos; |
| 40 |
gfloat y_pos; |
| 41 |
} gravities[] = |
| 42 |
{ |
| 43 |
{ CLUTTER_GRAVITY_NORTH, RECT_WIDTH / 2, 0 }, |
| 44 |
{ CLUTTER_GRAVITY_NORTH_EAST, RECT_WIDTH, 0 }, |
| 45 |
{ CLUTTER_GRAVITY_EAST, RECT_WIDTH, RECT_HEIGHT / 2 }, |
| 46 |
{ CLUTTER_GRAVITY_SOUTH_EAST, RECT_WIDTH, RECT_HEIGHT }, |
| 47 |
{ CLUTTER_GRAVITY_SOUTH, RECT_WIDTH / 2, RECT_HEIGHT }, |
| 48 |
{ CLUTTER_GRAVITY_SOUTH_WEST, 0, RECT_HEIGHT }, |
| 49 |
{ CLUTTER_GRAVITY_WEST, 0, RECT_HEIGHT / 2 }, |
| 50 |
{ CLUTTER_GRAVITY_NORTH_WEST, 0, 0 }, |
| 51 |
{ CLUTTER_GRAVITY_CENTER, RECT_WIDTH / 2, RECT_HEIGHT / 2 } |
| 52 |
}; |
| 53 |
|
| 54 |
static const char * const properties[] = { |
| 55 |
"anchor-x", |
| 56 |
"anchor-y", |
| 57 |
"anchor-gravity", |
| 58 |
"scale-x", |
| 59 |
"scale-y", |
| 60 |
"scale-center-x", |
| 61 |
"scale-center-y", |
| 62 |
"scale-gravity", |
| 63 |
"rotation-angle-x", |
| 64 |
"rotation-angle-y", |
| 65 |
"rotation-angle-z", |
| 66 |
"rotation-center-x", |
| 67 |
"rotation-center-y", |
| 68 |
"rotation-center-z", |
| 69 |
"rotation-center-z-gravity" |
| 70 |
}; |
| 71 |
|
| 72 |
static const int n_properties = G_N_ELEMENTS (properties); |
| 73 |
|
| 74 |
static void |
| 75 |
notify_cb (GObject *object, GParamSpec *pspec, TestState *state) |
| 76 |
{ |
| 77 |
int i; |
| 78 |
int new_flags = 0; |
| 79 |
int flag = 1; |
| 80 |
|
| 81 |
for (i = 0; i < n_properties; i++) |
| 82 |
{ |
| 83 |
if (!strcmp (properties[i], pspec->name)) |
| 84 |
new_flags |= flag; |
| 85 |
|
| 86 |
flag <<= 1; |
| 87 |
} |
| 88 |
|
| 89 |
g_assert ((new_flags & state->notifications) == 0); |
| 90 |
|
| 91 |
state->notifications |= new_flags; |
| 92 |
} |
| 93 |
|
| 94 |
#define assert_notifications(flags) G_STMT_START { \ |
| 95 |
g_assert (state->notifications == (flags)); \ |
| 96 |
state->notifications = 0; } G_STMT_END |
| 97 |
|
| 98 |
/* Helper macro to assert the transformed position. This needs to be a |
| 99 |
macro so that the assertion failure will report the right line |
| 100 |
number */ |
| 101 |
#define assert_coords(state, x_1, y_1, x_2, y_2) G_STMT_START { \ |
| 102 |
ClutterVertex verts[4]; \ |
| 103 |
clutter_actor_get_abs_allocation_vertices ((state)->rect, verts); \ |
| 104 |
check_coords ((state), (x_1), (y_1), (x_2), (y_2), verts); \ |
| 105 |
g_assert (approx_equal ((x_1), verts[0].x)); \ |
| 106 |
g_assert (approx_equal ((y_1), verts[0].y)); \ |
| 107 |
g_assert (approx_equal ((x_2), verts[3].x)); \ |
| 108 |
g_assert (approx_equal ((y_2), verts[3].y)); } G_STMT_END |
| 109 |
|
| 110 |
#define assert_position(state, x, y) \ |
| 111 |
assert_coords((state), (x), (y), (x) + RECT_WIDTH, (y) + RECT_HEIGHT) |
| 112 |
|
| 113 |
#define assert_vertex_and_free(v, xc, yc, zc) G_STMT_START { \ |
| 114 |
g_assert (approx_equal (v->x, xc) && \ |
| 115 |
approx_equal (v->y, yc) && \ |
| 116 |
approx_equal (v->z, zc)); \ |
| 117 |
g_boxed_free (CLUTTER_TYPE_VERTEX, v); } G_STMT_END |
| 118 |
|
| 119 |
static inline gboolean |
| 120 |
approx_equal (int a, int b) |
| 121 |
{ |
| 122 |
return abs (a - b) <= POSITION_TOLERANCE; |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
check_coords (TestState *state, |
| 127 |
gint x_1, |
| 128 |
gint y_1, |
| 129 |
gint x_2, |
| 130 |
gint y_2, |
| 131 |
const ClutterVertex *verts) |
| 132 |
{ |
| 133 |
if (g_test_verbose ()) |
| 134 |
g_print ("checking that (%i,%i,%i,%i) \xe2\x89\x88 (%i,%i,%i,%i): %s\n", |
| 135 |
x_1, y_1, x_2, y_2, |
| 136 |
(int) (verts[0].x), |
| 137 |
(int) (verts[0].y), |
| 138 |
(int) (verts[3].x), |
| 139 |
(int) (verts[3].y), |
| 140 |
approx_equal (x_1, verts[0].x) && |
| 141 |
approx_equal (y_1, verts[0].y) && |
| 142 |
approx_equal (x_2, verts[3].x) && |
| 143 |
approx_equal (y_2, verts[3].y) ? "yes" |
| 144 |
: "NO"); |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
test_anchor_point (TestState *state) |
| 149 |
{ |
| 150 |
ClutterActor *rect = state->rect; |
| 151 |
gfloat anchor_x, anchor_y; |
| 152 |
ClutterGravity anchor_gravity; |
| 153 |
int i; |
| 154 |
|
| 155 |
/* Assert the default settings */ |
| 156 |
g_assert (clutter_actor_get_x (rect) == 100); |
| 157 |
g_assert (clutter_actor_get_y (rect) == 200); |
| 158 |
g_assert (clutter_actor_get_width (rect) == RECT_WIDTH); |
| 159 |
g_assert (clutter_actor_get_height (rect) == RECT_HEIGHT); |
| 160 |
g_object_get (rect, |
| 161 |
"anchor-x", &anchor_x, |
| 162 |
"anchor-y", &anchor_y, |
| 163 |
"anchor-gravity", &anchor_gravity, |
| 164 |
NULL); |
| 165 |
g_assert (anchor_x == 0); |
| 166 |
g_assert (anchor_y == 0); |
| 167 |
g_assert (anchor_gravity == CLUTTER_GRAVITY_NONE); |
| 168 |
|
| 169 |
/* Change the anchor point */ |
| 170 |
clutter_actor_set_anchor_point (rect, 20, 30); |
| 171 |
g_object_get (rect, |
| 172 |
"anchor-x", &anchor_x, |
| 173 |
"anchor-y", &anchor_y, |
| 174 |
"anchor-gravity", &anchor_gravity, |
| 175 |
NULL); |
| 176 |
g_assert (anchor_x == 20); |
| 177 |
g_assert (anchor_y == 30); |
| 178 |
g_assert (anchor_gravity == CLUTTER_GRAVITY_NONE); |
| 179 |
assert_position (state, 80, 170); |
| 180 |
assert_notifications (NOTIFY_ANCHOR_X | NOTIFY_ANCHOR_Y); |
| 181 |
|
| 182 |
/* Move the anchor point */ |
| 183 |
clutter_actor_move_anchor_point (rect, 40, 50); |
| 184 |
g_object_get (rect, |
| 185 |
"anchor-x", &anchor_x, |
| 186 |
"anchor-y", &anchor_y, |
| 187 |
"anchor-gravity", &anchor_gravity, |
| 188 |
NULL); |
| 189 |
g_assert (anchor_x == 40); |
| 190 |
g_assert (anchor_y == 50); |
| 191 |
g_assert (anchor_gravity == CLUTTER_GRAVITY_NONE); |
| 192 |
assert_position (state, 80, 170); |
| 193 |
assert_notifications (NOTIFY_ANCHOR_X | NOTIFY_ANCHOR_Y); |
| 194 |
|
| 195 |
/* Put the actor back to its default position */ |
| 196 |
clutter_actor_set_position (rect, 100, 200); |
| 197 |
|
| 198 |
/* Change the anchor point with each of the gravities */ |
| 199 |
for (i = 0; i < G_N_ELEMENTS (gravities); i++) |
| 200 |
{ |
| 201 |
if (g_test_verbose ()) |
| 202 |
{ |
| 203 |
GEnumClass *gravity_class = g_type_class_ref (CLUTTER_TYPE_GRAVITY); |
| 204 |
GEnumValue *value = g_enum_get_value (gravity_class, |
| 205 |
gravities[i].gravity); |
| 206 |
g_print ("Setting gravity to %s\n", |
| 207 |
value ? value->value_name : "?"); |
| 208 |
g_type_class_unref (gravity_class); |
| 209 |
} |
| 210 |
|
| 211 |
g_object_set (rect, "anchor-gravity", gravities[i].gravity, NULL); |
| 212 |
|
| 213 |
g_object_get (rect, |
| 214 |
"anchor-x", &anchor_x, |
| 215 |
"anchor-y", &anchor_y, |
| 216 |
"anchor-gravity", &anchor_gravity, |
| 217 |
NULL); |
| 218 |
g_assert (anchor_x == gravities[i].x_pos); |
| 219 |
g_assert (anchor_y == gravities[i].y_pos); |
| 220 |
g_assert (anchor_gravity == gravities[i].gravity); |
| 221 |
assert_position (state, |
| 222 |
100 - gravities[i].x_pos, |
| 223 |
200 - gravities[i].y_pos); |
| 224 |
|
| 225 |
assert_notifications (NOTIFY_ANCHOR_X | NOTIFY_ANCHOR_Y |
| 226 |
| NOTIFY_ANCHOR_GRAVITY); |
| 227 |
} |
| 228 |
|
| 229 |
/* Verify that the anchor point moves if the actor changes size when |
| 230 |
it is set from the gravity */ |
| 231 |
clutter_actor_set_size (rect, RECT_WIDTH * 2, RECT_HEIGHT * 2); |
| 232 |
g_object_get (rect, |
| 233 |
"anchor-x", &anchor_x, |
| 234 |
"anchor-y", &anchor_y, |
| 235 |
"anchor-gravity", &anchor_gravity, |
| 236 |
NULL); |
| 237 |
g_assert (anchor_x == RECT_WIDTH); |
| 238 |
g_assert (anchor_y == RECT_HEIGHT); |
| 239 |
g_assert (anchor_gravity == CLUTTER_GRAVITY_CENTER); |
| 240 |
assert_coords (state, 100 - RECT_WIDTH, 200 - RECT_HEIGHT, |
| 241 |
100 + RECT_WIDTH, 200 + RECT_HEIGHT); |
| 242 |
assert_notifications (0); |
| 243 |
clutter_actor_set_size (rect, RECT_WIDTH, RECT_HEIGHT); |
| 244 |
|
| 245 |
/* Change the anchor point using units again to assert that the |
| 246 |
gravity property changes */ |
| 247 |
clutter_actor_set_anchor_point (rect, 20, 30); |
| 248 |
g_object_get (rect, |
| 249 |
"anchor-x", &anchor_x, "anchor-y", &anchor_y, |
| 250 |
"anchor-gravity", &anchor_gravity, |
| 251 |
NULL); |
| 252 |
g_assert (anchor_x == 20); |
| 253 |
g_assert (anchor_y == 30); |
| 254 |
g_assert (anchor_gravity == CLUTTER_GRAVITY_NONE); |
| 255 |
assert_position (state, 80, 170); |
| 256 |
assert_notifications (NOTIFY_ANCHOR_X | NOTIFY_ANCHOR_Y |
| 257 |
| NOTIFY_ANCHOR_GRAVITY); |
| 258 |
|
| 259 |
/* Verify that the anchor point doesn't move if the actor changes |
| 260 |
size when it is set from units */ |
| 261 |
clutter_actor_set_size (rect, RECT_WIDTH * 2, RECT_HEIGHT * 2); |
| 262 |
g_object_get (rect, |
| 263 |
"anchor-x", &anchor_x, |
| 264 |
"anchor-y", &anchor_y, |
| 265 |
"anchor-gravity", &anchor_gravity, |
| 266 |
NULL); |
| 267 |
g_assert (anchor_x == 20); |
| 268 |
g_assert (anchor_y == 30); |
| 269 |
g_assert (anchor_gravity == CLUTTER_GRAVITY_NONE); |
| 270 |
assert_coords (state, 80, 170, 80 + RECT_WIDTH * 2, 170 + RECT_HEIGHT * 2); |
| 271 |
assert_notifications (0); |
| 272 |
clutter_actor_set_size (rect, RECT_WIDTH, RECT_HEIGHT); |
| 273 |
|
| 274 |
/* Put the anchor back */ |
| 275 |
clutter_actor_set_anchor_point_from_gravity (rect, CLUTTER_GRAVITY_NONE); |
| 276 |
assert_notifications (NOTIFY_ANCHOR_X | NOTIFY_ANCHOR_Y); |
| 277 |
} |
| 278 |
|
| 279 |
static void |
| 280 |
test_scale_center (TestState *state) |
| 281 |
{ |
| 282 |
ClutterActor *rect = state->rect; |
| 283 |
gdouble scale_x, scale_y; |
| 284 |
gfloat center_x, center_y; |
| 285 |
ClutterGravity gravity; |
| 286 |
int i; |
| 287 |
|
| 288 |
/* Assert the default settings */ |
| 289 |
g_assert (clutter_actor_get_x (rect) == 100); |
| 290 |
g_assert (clutter_actor_get_y (rect) == 200); |
| 291 |
g_assert (clutter_actor_get_width (rect) == RECT_WIDTH); |
| 292 |
g_assert (clutter_actor_get_height (rect) == RECT_HEIGHT); |
| 293 |
g_object_get (rect, |
| 294 |
"scale-center-x", ¢er_x, |
| 295 |
"scale-center-y", ¢er_y, |
| 296 |
"scale-x", &scale_x, |
| 297 |
"scale-y", &scale_y, |
| 298 |
"scale-gravity", &gravity, |
| 299 |
NULL); |
| 300 |
g_assert (center_x == 0); |
| 301 |
g_assert (center_y == 0); |
| 302 |
g_assert (scale_x == 1.0); |
| 303 |
g_assert (scale_y == 1.0); |
| 304 |
g_assert (gravity == CLUTTER_GRAVITY_NONE); |
| 305 |
|
| 306 |
/* Try changing the scale without affecting the center */ |
| 307 |
g_object_set (rect, "scale-x", 2.0, "scale-y", 3.0, NULL); |
| 308 |
g_assert (clutter_actor_get_x (rect) == 100); |
| 309 |
g_assert (clutter_actor_get_y (rect) == 200); |
| 310 |
g_assert (clutter_actor_get_width (rect) == RECT_WIDTH); |
| 311 |
g_assert (clutter_actor_get_height (rect) == RECT_HEIGHT); |
| 312 |
g_object_get (rect, |
| 313 |
"scale-center-x", ¢er_x, |
| 314 |
"scale-center-y", ¢er_y, |
| 315 |
"scale-x", &scale_x, |
| 316 |
"scale-y", &scale_y, |
| 317 |
"scale-gravity", &gravity, |
| 318 |
NULL); |
| 319 |
g_assert (center_x == 0); |
| 320 |
g_assert (center_y == 0); |
| 321 |
g_assert (scale_x == 2.0); |
| 322 |
g_assert (scale_y == 3.0); |
| 323 |
g_assert (gravity == CLUTTER_GRAVITY_NONE); |
| 324 |
assert_notifications (NOTIFY_SCALE_X | NOTIFY_SCALE_Y); |
| 325 |
assert_coords (state, 100, 200, 100 + RECT_WIDTH * 2, 200 + RECT_HEIGHT * 3); |
| 326 |
|
| 327 |
/* Change the scale and center */ |
| 328 |
g_object_set (rect, |
| 329 |
"scale-x", 4.0, |
| 330 |
"scale-y", 2.0, |
| 331 |
"scale-center-x", 10.0, |
| 332 |
"scale-center-y", 20.0, |
| 333 |
NULL); |
| 334 |
g_assert (clutter_actor_get_x (rect) == 100); |
| 335 |
g_assert (clutter_actor_get_y (rect) == 200); |
| 336 |
g_assert (clutter_actor_get_width (rect) == RECT_WIDTH); |
| 337 |
g_assert (clutter_actor_get_height (rect) == RECT_HEIGHT); |
| 338 |
g_object_get (rect, |
| 339 |
"scale-center-x", ¢er_x, "scale-center-y", ¢er_y, |
| 340 |
"scale-x", &scale_x, "scale-y", &scale_y, |
| 341 |
"scale-gravity", &gravity, |
| 342 |
NULL); |
| 343 |
g_assert (center_x == 10); |
| 344 |
g_assert (center_y == 20); |
| 345 |
g_assert (scale_x == 4.0); |
| 346 |
g_assert (scale_y == 2.0); |
| 347 |
g_assert (gravity == CLUTTER_GRAVITY_NONE); |
| 348 |
assert_notifications (NOTIFY_SCALE_X | NOTIFY_SCALE_Y |
| 349 |
| NOTIFY_SCALE_CENTER_X | NOTIFY_SCALE_CENTER_Y); |
| 350 |
assert_coords (state, 100 + 10 - 10 * 4, 200 + 20 - 20 * 2, |
| 351 |
100 + 10 + (RECT_WIDTH - 10) * 4, |
| 352 |
200 + 20 + (RECT_HEIGHT - 20) * 2); |
| 353 |
|
| 354 |
/* Change the anchor point with each of the gravities */ |
| 355 |
for (i = 0; i < G_N_ELEMENTS (gravities); i++) |
| 356 |
{ |
| 357 |
if (g_test_verbose ()) |
| 358 |
{ |
| 359 |
GEnumClass *gravity_class = g_type_class_ref (CLUTTER_TYPE_GRAVITY); |
| 360 |
GEnumValue *value = g_enum_get_value (gravity_class, |
| 361 |
gravities[i].gravity); |
| 362 |
g_print ("Setting scale center to %s\n", |
| 363 |
value ? value->value_name : "?"); |
| 364 |
g_type_class_unref (gravity_class); |
| 365 |
} |
| 366 |
|
| 367 |
g_object_set (rect, "scale-gravity", gravities[i].gravity, NULL); |
| 368 |
|
| 369 |
g_assert (clutter_actor_get_x (rect) == 100); |
| 370 |
g_assert (clutter_actor_get_y (rect) == 200); |
| 371 |
g_assert (clutter_actor_get_width (rect) == RECT_WIDTH); |
| 372 |
g_assert (clutter_actor_get_height (rect) == RECT_HEIGHT); |
| 373 |
g_object_get (rect, |
| 374 |
"scale-center-x", ¢er_x, "scale-center-y", ¢er_y, |
| 375 |
"scale-x", &scale_x, "scale-y", &scale_y, |
| 376 |
"scale-gravity", &gravity, |
| 377 |
NULL); |
| 378 |
g_assert (center_x == gravities[i].x_pos); |
| 379 |
g_assert (center_y == gravities[i].y_pos); |
| 380 |
g_assert (scale_x == 4.0); |
| 381 |
g_assert (scale_y == 2.0); |
| 382 |
g_assert (gravity == gravities[i].gravity); |
| 383 |
assert_notifications (NOTIFY_SCALE_CENTER_X | |
| 384 |
NOTIFY_SCALE_CENTER_Y | |
| 385 |
NOTIFY_SCALE_GRAVITY); |
| 386 |
assert_coords (state, |
| 387 |
100 - gravities[i].x_pos * 3, |
| 388 |
200 - gravities[i].y_pos, |
| 389 |
100 + (gravities[i].x_pos |
| 390 |
+ (RECT_WIDTH - gravities[i].x_pos) * 4), |
| 391 |
200 + (gravities[i].y_pos |
| 392 |
+ (RECT_HEIGHT - gravities[i].y_pos) * 2)); |
| 393 |
} |
| 394 |
|
| 395 |
/* Change the scale center using units again to assert that the |
| 396 |
gravity property changes */ |
| 397 |
clutter_actor_set_scale_full (rect, 4, 2, 10, 20); |
| 398 |
g_object_get (rect, |
| 399 |
"scale-center-x", ¢er_x, |
| 400 |
"scale-center-y", ¢er_y, |
| 401 |
"scale-x", &scale_x, |
| 402 |
"scale-y", &scale_y, |
| 403 |
"scale-gravity", &gravity, |
| 404 |
NULL); |
| 405 |
g_assert (center_x == 10); |
| 406 |
g_assert (center_y == 20); |
| 407 |
g_assert (scale_x == 4.0); |
| 408 |
g_assert (scale_y == 2.0); |
| 409 |
g_assert (gravity == CLUTTER_GRAVITY_NONE); |
| 410 |
assert_notifications (NOTIFY_SCALE_X | NOTIFY_SCALE_Y |
| 411 |
| NOTIFY_SCALE_CENTER_X | NOTIFY_SCALE_CENTER_Y |
| 412 |
| NOTIFY_SCALE_GRAVITY); |
| 413 |
assert_coords (state, 100 + 10 - 10 * 4, 200 + 20 - 20 * 2, |
| 414 |
100 + 10 + (RECT_WIDTH - 10) * 4, |
| 415 |
200 + 20 + (RECT_HEIGHT - 20) * 2); |
| 416 |
|
| 417 |
/* Put the scale back to normal */ |
| 418 |
clutter_actor_set_scale_full (rect, 1, 1, 0, 0); |
| 419 |
assert_notifications (NOTIFY_SCALE_X | NOTIFY_SCALE_Y |
| 420 |
| NOTIFY_SCALE_CENTER_X | NOTIFY_SCALE_CENTER_Y); |
| 421 |
} |
| 422 |
|
| 423 |
static void |
| 424 |
test_rotate_center (TestState *state) |
| 425 |
{ |
| 426 |
ClutterActor *rect = state->rect; |
| 427 |
gdouble angle_x, angle_y, angle_z; |
| 428 |
ClutterVertex *center_x, *center_y, *center_z; |
| 429 |
ClutterGravity z_center_gravity; |
| 430 |
gfloat stage_width, stage_height; |
| 431 |
gfloat rect_x, rect_y; |
| 432 |
int i; |
| 433 |
|
| 434 |
/* Position the rectangle at the center of the stage so that |
| 435 |
rotations by 90° along the X or Y axis will cause the actor to be |
| 436 |
appear as a flat line. This makes verifying the transformations |
| 437 |
easier */ |
| 438 |
clutter_actor_get_size (clutter_actor_get_stage (rect), |
| 439 |
&stage_width, |
| 440 |
&stage_height); |
| 441 |
rect_x = stage_width / 2; |
| 442 |
rect_y = stage_height / 2; |
| 443 |
clutter_actor_set_position (rect, rect_x, rect_y); |
| 444 |
|
| 445 |
/* Assert the default settings */ |
| 446 |
g_assert_cmpfloat (clutter_actor_get_x (rect), ==, rect_x); |
| 447 |
g_assert_cmpfloat (clutter_actor_get_y (rect), ==, rect_y); |
| 448 |
g_assert_cmpfloat (clutter_actor_get_width (rect), ==, RECT_WIDTH); |
| 449 |
g_assert_cmpfloat (clutter_actor_get_height (rect), ==, RECT_HEIGHT); |
| 450 |
g_object_get (rect, |
| 451 |
"rotation-angle-x", &angle_x, |
| 452 |
"rotation-angle-y", &angle_y, |
| 453 |
"rotation-angle-z", &angle_z, |
| 454 |
"rotation-center-x", ¢er_x, |
| 455 |
"rotation-center-y", ¢er_y, |
| 456 |
"rotation-center-z", ¢er_z, |
| 457 |
"rotation-center-z-gravity", &z_center_gravity, |
| 458 |
NULL); |
| 459 |
g_assert (angle_x == 0.0); |
| 460 |
g_assert (angle_y == 0.0); |
| 461 |
g_assert (angle_z == 0.0); |
| 462 |
assert_vertex_and_free (center_x, 0, 0, 0); |
| 463 |
assert_vertex_and_free (center_y, 0, 0, 0); |
| 464 |
assert_vertex_and_free (center_z, 0, 0, 0); |
| 465 |
g_assert (z_center_gravity == CLUTTER_GRAVITY_NONE); |
| 466 |
|
| 467 |
/* Change each of the rotation angles without affecting the center |
| 468 |
point */ |
| 469 |
for (i = CLUTTER_X_AXIS; i <= CLUTTER_Z_AXIS; i++) |
| 470 |
{ |
| 471 |
char prop_name[] = "rotation-angle- "; |
| 472 |
prop_name[sizeof (prop_name) - 2] = i - CLUTTER_X_AXIS + 'x'; |
| 473 |
|
| 474 |
if (g_test_verbose ()) |
| 475 |
g_print ("Setting %s to 90 degrees\n", prop_name); |
| 476 |
|
| 477 |
g_object_set (rect, prop_name, 90.0, NULL); |
| 478 |
assert_notifications (NOTIFY_ROTATION_ANGLE_X << (i - CLUTTER_X_AXIS)); |
| 479 |
|
| 480 |
g_assert (clutter_actor_get_x (rect) == rect_x); |
| 481 |
g_assert (clutter_actor_get_y (rect) == rect_y); |
| 482 |
g_assert (clutter_actor_get_width (rect) == RECT_WIDTH); |
| 483 |
g_assert (clutter_actor_get_height (rect) == RECT_HEIGHT); |
| 484 |
g_object_get (rect, |
| 485 |
"rotation-angle-x", &angle_x, |
| 486 |
"rotation-angle-y", &angle_y, |
| 487 |
"rotation-angle-z", &angle_z, |
| 488 |
"rotation-center-x", ¢er_x, |
| 489 |
"rotation-center-y", ¢er_y, |
| 490 |
"rotation-center-z", ¢er_z, |
| 491 |
"rotation-center-z-gravity", &z_center_gravity, |
| 492 |
NULL); |
| 493 |
if (i == CLUTTER_X_AXIS) |
| 494 |
{ |
| 495 |
g_assert (angle_x == 90.0); |
| 496 |
assert_coords (state, rect_x, rect_y, verts[3].x, rect_y); |
| 497 |
} |
| 498 |
else |
| 499 |
g_assert (angle_x == 0.0); |
| 500 |
if (i == CLUTTER_Y_AXIS) |
| 501 |
{ |
| 502 |
g_assert (angle_y == 90.0); |
| 503 |
assert_coords (state, rect_x, rect_y, rect_x, verts[3].y); |
| 504 |
} |
| 505 |
else |
| 506 |
g_assert (angle_y == 0.0); |
| 507 |
if (i == CLUTTER_Z_AXIS) |
| 508 |
{ |
| 509 |
g_assert (angle_z == 90.0); |
| 510 |
assert_coords (state, rect_x, rect_y, |
| 511 |
rect_x - RECT_HEIGHT, |
| 512 |
rect_y + RECT_WIDTH); |
| 513 |
} |
| 514 |
else |
| 515 |
g_assert (angle_z == 0.0); |
| 516 |
assert_vertex_and_free (center_x, 0, 0, 0); |
| 517 |
assert_vertex_and_free (center_y, 0, 0, 0); |
| 518 |
assert_vertex_and_free (center_z, 0, 0, 0); |
| 519 |
g_assert (z_center_gravity == CLUTTER_GRAVITY_NONE); |
| 520 |
|
| 521 |
g_object_set (rect, prop_name, 0.0, NULL); |
| 522 |
assert_notifications (NOTIFY_ROTATION_ANGLE_X << (i - CLUTTER_X_AXIS)); |
| 523 |
} |
| 524 |
|
| 525 |
clutter_actor_set_position (rect, rect_x -= 10, rect_y -= 20); |
| 526 |
|
| 527 |
/* Same test but also change the center position */ |
| 528 |
for (i = CLUTTER_X_AXIS; i <= CLUTTER_Z_AXIS; i++) |
| 529 |
{ |
| 530 |
char prop_name[] = "rotation-angle- "; |
| 531 |
prop_name[sizeof (prop_name) - 2] = i - CLUTTER_X_AXIS + 'x'; |
| 532 |
|
| 533 |
if (g_test_verbose ()) |
| 534 |
g_print ("Setting %s to 90 degrees with center 10,20,0\n", prop_name); |
| 535 |
|
| 536 |
clutter_actor_set_rotation (rect, i, 90.0, 10, 20, 0); |
| 537 |
assert_notifications ((NOTIFY_ROTATION_ANGLE_X << (i - CLUTTER_X_AXIS)) |
| 538 |
| (NOTIFY_ROTATION_CENTER_X |
| 539 |
<< (i - CLUTTER_X_AXIS))); |
| 540 |
|
| 541 |
g_assert (clutter_actor_get_x (rect) == rect_x); |
| 542 |
g_assert (clutter_actor_get_y (rect) == rect_y); |
| 543 |
g_assert (clutter_actor_get_width (rect) == RECT_WIDTH); |
| 544 |
g_assert (clutter_actor_get_height (rect) == RECT_HEIGHT); |
| 545 |
g_object_get (rect, |
| 546 |
"rotation-angle-x", &angle_x, |
| 547 |
"rotation-angle-y", &angle_y, |
| 548 |
"rotation-angle-z", &angle_z, |
| 549 |
"rotation-center-x", ¢er_x, |
| 550 |
"rotation-center-y", ¢er_y, |
| 551 |
"rotation-center-z", ¢er_z, |
| 552 |
"rotation-center-z-gravity", &z_center_gravity, |
| 553 |
NULL); |
| 554 |
if (i == CLUTTER_X_AXIS) |
| 555 |
{ |
| 556 |
g_assert (angle_x == 90.0); |
| 557 |
assert_coords (state, |
| 558 |
verts[0].x, rect_y + 20, |
| 559 |
verts[3].x, rect_y + 20); |
| 560 |
assert_vertex_and_free (center_x, 10, 20, 0); |
| 561 |
} |
| 562 |
else |
| 563 |
{ |
| 564 |
g_assert (angle_x == 0.0); |
| 565 |
assert_vertex_and_free (center_x, 0, 0, 0); |
| 566 |
} |
| 567 |
if (i == CLUTTER_Y_AXIS) |
| 568 |
{ |
| 569 |
g_assert (angle_y == 90.0); |
| 570 |
assert_coords (state, |
| 571 |
rect_x + 10, verts[0].y, |
| 572 |
rect_x + 10, verts[3].y); |
| 573 |
assert_vertex_and_free (center_y, 10, 20, 0); |
| 574 |
} |
| 575 |
else |
| 576 |
{ |
| 577 |
g_assert (angle_y == 0.0); |
| 578 |
assert_vertex_and_free (center_y, 0, 0, 0); |
| 579 |
} |
| 580 |
if (i == CLUTTER_Z_AXIS) |
| 581 |
{ |
| 582 |
g_assert (angle_z == 90.0); |
| 583 |
assert_coords (state, |
| 584 |
rect_x + 10 + 20, |
| 585 |
rect_y + 20 - 10, |
| 586 |
rect_x + 10 + 20 - RECT_HEIGHT, |
| 587 |
rect_y + 20 + RECT_WIDTH - 10); |
| 588 |
assert_vertex_and_free (center_z, 10, 20, 0); |
| 589 |
} |
| 590 |
else |
| 591 |
{ |
| 592 |
g_assert (angle_z == 0.0); |
| 593 |
assert_vertex_and_free (center_z, 0, 0, 0); |
| 594 |
} |
| 595 |
g_assert (z_center_gravity == CLUTTER_GRAVITY_NONE); |
| 596 |
|
| 597 |
clutter_actor_set_rotation (rect, i, 0, 0, 0, 0); |
| 598 |
assert_notifications ((NOTIFY_ROTATION_ANGLE_X << (i - CLUTTER_X_AXIS)) |
| 599 |
| (NOTIFY_ROTATION_CENTER_X |
| 600 |
<< (i - CLUTTER_X_AXIS))); |
| 601 |
|
| 602 |
} |
| 603 |
|
| 604 |
/* Try rotating the z with all of the gravities */ |
| 605 |
for (i = 0; i < G_N_ELEMENTS (gravities); i++) |
| 606 |
{ |
| 607 |
if (g_test_verbose ()) |
| 608 |
{ |
| 609 |
GEnumClass *gravity_class = g_type_class_ref (CLUTTER_TYPE_GRAVITY); |
| 610 |
GEnumValue *value = g_enum_get_value (gravity_class, |
| 611 |
gravities[i].gravity); |
| 612 |
g_print ("Setting z rotation to 90 degrees with center at %s\n", |
| 613 |
value ? value->value_name : "?"); |
| 614 |
g_type_class_unref (gravity_class); |
| 615 |
} |
| 616 |
|
| 617 |
clutter_actor_set_z_rotation_from_gravity (rect, 90, |
| 618 |
gravities[i].gravity); |
| 619 |
assert_notifications (NOTIFY_ROTATION_ANGLE_Z |
| 620 |
| NOTIFY_ROTATION_CENTER_Z |
| 621 |
| NOTIFY_ROTATION_CENTER_Z_GRAVITY); |
| 622 |
|
| 623 |
g_assert (clutter_actor_get_x (rect) == rect_x); |
| 624 |
g_assert (clutter_actor_get_y (rect) == rect_y); |
| 625 |
g_assert (clutter_actor_get_width (rect) == RECT_WIDTH); |
| 626 |
g_assert (clutter_actor_get_height (rect) == RECT_HEIGHT); |
| 627 |
g_object_get (rect, |
| 628 |
"rotation-angle-x", &angle_x, |
| 629 |
"rotation-angle-y", &angle_y, |
| 630 |
"rotation-angle-z", &angle_z, |
| 631 |
"rotation-center-x", ¢er_x, |
| 632 |
"rotation-center-y", ¢er_y, |
| 633 |
"rotation-center-z", ¢er_z, |
| 634 |
"rotation-center-z-gravity", &z_center_gravity, |
| 635 |
NULL); |
| 636 |
g_assert (angle_x == 0.0); |
| 637 |
g_assert (angle_y == 0.0); |
| 638 |
g_assert (angle_z == 90.0); |
| 639 |
assert_vertex_and_free (center_x, 0, 0, 0); |
| 640 |
assert_vertex_and_free (center_y, 0, 0, 0); |
| 641 |
assert_vertex_and_free (center_z, |
| 642 |
gravities[i].x_pos, gravities[i].y_pos, 0); |
| 643 |
assert_coords (state, |
| 644 |
rect_x + gravities[i].x_pos + gravities[i].y_pos, |
| 645 |
rect_y + gravities[i].y_pos - gravities[i].x_pos, |
| 646 |
rect_x + gravities[i].x_pos + gravities[i].y_pos |
| 647 |
- RECT_HEIGHT, |
| 648 |
rect_y + gravities[i].y_pos + RECT_WIDTH |
| 649 |
- gravities[i].x_pos); |
| 650 |
g_assert (z_center_gravity == gravities[i].gravity); |
| 651 |
g_assert (clutter_actor_get_z_rotation_gravity (rect) |
| 652 |
== gravities[i].gravity); |
| 653 |
|
| 654 |
/* Put the rotation back */ |
| 655 |
clutter_actor_set_z_rotation_from_gravity (rect, 0, CLUTTER_GRAVITY_NONE); |
| 656 |
assert_notifications (NOTIFY_ROTATION_ANGLE_Z |
| 657 |
| NOTIFY_ROTATION_CENTER_Z |
| 658 |
| NOTIFY_ROTATION_CENTER_Z_GRAVITY); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
static gboolean |
| 663 |
idle_cb (gpointer data) |
| 664 |
{ |
| 665 |
test_anchor_point (data); |
| 666 |
test_scale_center (data); |
| 667 |
test_rotate_center (data); |
| 668 |
|
| 669 |
clutter_main_quit (); |
| 670 |
|
| 671 |
return G_SOURCE_REMOVE; |
| 672 |
} |
| 673 |
|
| 674 |
static void |
| 675 |
actor_anchors (void) |
| 676 |
{ |
| 677 |
TestState state; |
| 678 |
ClutterActor *stage; |
| 679 |
|
| 680 |
stage = clutter_test_get_stage (); |
| 681 |
|
| 682 |
state.rect = clutter_actor_new (); |
| 683 |
clutter_actor_add_child (stage, state.rect); |
| 684 |
clutter_actor_set_position (state.rect, 100, 200); |
| 685 |
clutter_actor_set_size (state.rect, RECT_WIDTH, RECT_HEIGHT); |
| 686 |
|
| 687 |
/* Record notifications on the actor properties */ |
| 688 |
state.notifications = 0; |
| 689 |
g_signal_connect (state.rect, "notify", |
| 690 |
G_CALLBACK (notify_cb), &state); |
| 691 |
|
| 692 |
/* Run the tests in a low priority idle function so that we can be |
| 693 |
sure the stage is correctly setup */ |
| 694 |
clutter_threads_add_idle_full (G_PRIORITY_LOW, idle_cb, &state, NULL); |
| 695 |
|
| 696 |
clutter_actor_show (stage); |
| 697 |
|
| 698 |
clutter_main (); |
| 699 |
} |
| 700 |
|
| 701 |
static void |
| 702 |
actor_pivot (void) |
| 703 |
{ |
| 704 |
ClutterActor *stage, *actor_implicit, *actor_explicit; |
| 705 |
ClutterMatrix transform, result_implicit, result_explicit; |
| 706 |
ClutterActorBox allocation = CLUTTER_ACTOR_BOX_INIT (0, 0, 90, 30); |
| 707 |
gfloat angle = 30; |
| 708 |
|
| 709 |
stage = clutter_test_get_stage (); |
| 710 |
|
| 711 |
actor_implicit = clutter_actor_new (); |
| 712 |
actor_explicit = clutter_actor_new (); |
| 713 |
|
| 714 |
clutter_actor_add_child (stage, actor_implicit); |
| 715 |
clutter_actor_add_child (stage, actor_explicit); |
| 716 |
|
| 717 |
/* Fake allocation or pivot-point will not have any effect */ |
| 718 |
clutter_actor_allocate (actor_implicit, &allocation, CLUTTER_ALLOCATION_NONE); |
| 719 |
clutter_actor_allocate (actor_explicit, &allocation, CLUTTER_ALLOCATION_NONE); |
| 720 |
|
| 721 |
clutter_actor_set_pivot_point (actor_implicit, 0.5, 0.5); |
| 722 |
clutter_actor_set_pivot_point (actor_explicit, 0.5, 0.5); |
| 723 |
|
| 724 |
/* Implict transformation */ |
| 725 |
clutter_actor_set_rotation_angle (actor_implicit, CLUTTER_Z_AXIS, angle); |
| 726 |
|
| 727 |
/* Explict transformation */ |
| 728 |
clutter_matrix_init_identity(&transform); |
| 729 |
cogl_matrix_rotate (&transform, angle, 0, 0, 1.0); |
| 730 |
clutter_actor_set_transform (actor_explicit, &transform); |
| 731 |
|
| 732 |
clutter_actor_get_transform (actor_implicit, &result_implicit); |
| 733 |
clutter_actor_get_transform (actor_explicit, &result_explicit); |
| 734 |
|
| 735 |
g_assert (cogl_matrix_equal (&result_implicit, &result_explicit)); |
| 736 |
} |
| 737 |
|
| 738 |
CLUTTER_TEST_SUITE ( |
| 739 |
CLUTTER_TEST_UNIT ("/actor/transforms/anchor-point", actor_anchors) |
| 740 |
CLUTTER_TEST_UNIT ("/actor/transforms/pivot-point", actor_pivot) |
| 741 |
) |