mutter/examples/pan-action.c
Emanuele Aina 9ca06d2895 pan-action: add PanAction, to handle panning in scrollable actors
PanAction is a GestureAction-subclass that implements the panning
concept for scrollable actors, with the ability to emit interpolated
signals to emulate the kinetic inertia of the panning. PanAction provides:

• pan signal, notifying users of the panning gesture status;

• pan-stopped signal, emitted at the end of the interpolated phase
  of the panning gesture, if enabled;

• pan-axis property, to allow constraining the dragging to a specific
  axis;

• interpolated property, to enable or disable the inertial behaviour;

• deceleration property, to customize the rate at which the momentum
  of the panning will be slowed down;

• acceleration-factor property, applied to the inertial momentum when
  starting the interpolated sequence.

An interactive test is also provided.

https://bugzilla.gnome.org/show_bug.cgi?id=681648
2012-08-28 10:11:16 -03:00

128 lines
3.8 KiB
C

#include <stdlib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <clutter/clutter.h>
static ClutterActor *
create_content_actor (void)
{
ClutterActor *content;
ClutterContent *image;
GdkPixbuf *pixbuf;
content = clutter_actor_new ();
clutter_actor_set_size (content, 720, 720);
pixbuf = gdk_pixbuf_new_from_file (TESTS_DATADIR G_DIR_SEPARATOR_S "redhand.png", NULL);
image = clutter_image_new ();
clutter_image_set_data (CLUTTER_IMAGE (image),
gdk_pixbuf_get_pixels (pixbuf),
gdk_pixbuf_get_has_alpha (pixbuf)
? COGL_PIXEL_FORMAT_RGBA_8888
: COGL_PIXEL_FORMAT_RGB_888,
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf),
gdk_pixbuf_get_rowstride (pixbuf),
NULL);
g_object_unref (pixbuf);
clutter_actor_set_content_scaling_filters (content,
CLUTTER_SCALING_FILTER_TRILINEAR,
CLUTTER_SCALING_FILTER_LINEAR);
clutter_actor_set_content_gravity (content, CLUTTER_CONTENT_GRAVITY_RESIZE_ASPECT);
clutter_actor_set_content (content, image);
g_object_unref (image);
return content;
}
static gboolean
on_pan (ClutterPanAction *action,
ClutterActor *scroll,
gboolean is_interpolated,
gpointer *user_data)
{
gfloat delta_x, delta_y;
if (is_interpolated)
clutter_pan_action_get_interpolated_delta (action, &delta_x, &delta_y);
else
clutter_gesture_action_get_motion_delta (CLUTTER_GESTURE_ACTION (action), 0, &delta_x, &delta_y);
g_print ("panning dx:%.2f dy:%.2f\n", delta_x, delta_y);
return TRUE;
}
static ClutterActor *
create_scroll_actor (ClutterActor *stage)
{
ClutterActor *scroll;
ClutterAction *pan_action;
/* our scrollable viewport */
scroll = clutter_actor_new ();
clutter_actor_set_name (scroll, "scroll");
clutter_actor_add_constraint (scroll, clutter_align_constraint_new (stage, CLUTTER_ALIGN_X_AXIS, 0));
clutter_actor_add_constraint (scroll, clutter_bind_constraint_new (stage, CLUTTER_BIND_SIZE, 0));
clutter_actor_add_child (scroll, create_content_actor ());
pan_action = clutter_pan_action_new ();
clutter_pan_action_set_interpolate (CLUTTER_PAN_ACTION (pan_action), TRUE);
g_signal_connect (pan_action, "pan", G_CALLBACK (on_pan), NULL);
clutter_actor_add_action (scroll, pan_action);
clutter_actor_set_reactive (scroll, TRUE);
return scroll;
}
static gboolean
on_key_press (ClutterActor *stage,
ClutterEvent *event,
gpointer unused)
{
ClutterActor *scroll;
guint key_symbol;
scroll = clutter_actor_get_first_child (stage);
key_symbol = clutter_event_get_key_symbol (event);
if (key_symbol == CLUTTER_KEY_space)
clutter_actor_set_child_transform (scroll, NULL);
return CLUTTER_EVENT_STOP;
}
int
main (int argc, char *argv[])
{
ClutterActor *stage, *scroll, *info;
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
return EXIT_FAILURE;
/* create a new stage */
stage = clutter_stage_new ();
clutter_stage_set_title (CLUTTER_STAGE (stage), "Pan Action");
clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
scroll = create_scroll_actor (stage);
clutter_actor_add_child (stage, scroll);
info = clutter_text_new_with_text (NULL, "Press <space> to reset the image position.");
clutter_actor_add_child (stage, info);
clutter_actor_set_position (info, 12, 12);
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
g_signal_connect (stage, "key-press-event", G_CALLBACK (on_key_press), scroll);
clutter_actor_show (stage);
clutter_main ();
return EXIT_SUCCESS;
}