Skip to content

Instantly share code, notes, and snippets.

@emanueleaina
Created December 14, 2012 13:44
Show Gist options
  • Select an option

  • Save emanueleaina/4285565 to your computer and use it in GitHub Desktop.

Select an option

Save emanueleaina/4285565 to your computer and use it in GitHub Desktop.
Experiment with different techniques to rotate an Actor around a pivot point with the Clutter toolkit.
#include <stdlib.h>
#include <clutter/clutter.h>
static gboolean
on_key_press (ClutterActor *stage G_GNUC_UNUSED,
ClutterEvent *event,
gpointer data)
{
guint key_symbol;
ClutterActor *rect = CLUTTER_ACTOR (data);
key_symbol = clutter_event_get_key_symbol (event);
if (key_symbol == CLUTTER_KEY_Return)
{
clutter_actor_set_transform (rect, NULL);
clutter_actor_set_pivot_point (rect, 0.5, 0.5);
gfloat angle = clutter_actor_get_rotation_angle (rect, CLUTTER_Z_AXIS);
clutter_actor_set_rotation_angle (rect, CLUTTER_Z_AXIS, angle + 30);
}
else if (key_symbol == CLUTTER_KEY_Tab)
{
/* This has been broken for a while due to a bug in ClutterActor.apply_trasform(),
* see https://bugzilla.gnome.org/show_bug.cgi?id=690214 */
static gfloat angle = 0;
ClutterMatrix matrix;
clutter_actor_set_pivot_point (rect, 0.5, 0.5);
clutter_matrix_init_identity(&matrix);
angle += 30;
cogl_matrix_rotate (&matrix, angle, 0, 0, 1.0);
clutter_actor_set_transform (rect, &matrix);
}
else if (key_symbol == CLUTTER_KEY_space)
{
gfloat px, py, pz;
ClutterActorBox allocation;
ClutterMatrix matrix;
clutter_actor_set_pivot_point (rect, 0, 0);
clutter_actor_get_allocation_box (rect, &allocation);
px = 0.5;
py = 0.5;
px *= (allocation.x2 - allocation.x1);
py *= (allocation.y2 - allocation.y1);
pz = clutter_actor_get_pivot_point_z (rect);
clutter_actor_get_transform (rect, &matrix);
cogl_matrix_translate (&matrix, px, py, pz);
cogl_matrix_rotate (&matrix, 30, 0, 0, 1.0);
cogl_matrix_translate (&matrix, -px, -py, -pz);
clutter_actor_set_transform (rect, &matrix);
}
return CLUTTER_EVENT_STOP;
}
int
main (int argc, char *argv[])
{
ClutterActor *stage, *content, *rect, *info;
if (argc < 1)
{
g_error ("Usage: %s", argv[0]);
return EXIT_FAILURE;
}
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
{
g_error ("Failed to initialize clutter\n");
return EXIT_FAILURE;
}
stage = clutter_stage_new ();
clutter_actor_set_name (stage, "stage");
content = clutter_actor_new ();
clutter_actor_set_name (content, "content");
clutter_actor_add_child (stage, content);
info = clutter_text_new_with_text (NULL, "<tab>: set_pivot_point(0.5, 0.5)\n<return>: use set_rotation_angle()\n<space>: explicit translation");
clutter_actor_add_child (stage, info);
clutter_actor_set_position (info, 12, 12);
rect = clutter_actor_new ();
clutter_actor_set_name (rect, "rect");
clutter_actor_set_position (rect, 0, 0);
clutter_actor_set_size (rect, 60, 90);
clutter_actor_set_background_color (rect, CLUTTER_COLOR_Blue);
clutter_actor_add_child (content, rect);
clutter_actor_show (stage);
g_signal_connect (stage, "key-press-event", G_CALLBACK (on_key_press), rect);
clutter_main ();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment