2010-08-11 09:34:49 +00:00
|
|
|
#include <clutter/clutter.h>
|
|
|
|
|
2010-08-11 16:11:29 +00:00
|
|
|
#define STAGE_HEIGHT 300
|
2010-08-11 13:39:22 +00:00
|
|
|
#define STAGE_WIDTH STAGE_HEIGHT
|
|
|
|
#define SCROLL_AMOUNT STAGE_HEIGHT * 0.125
|
2010-08-11 09:34:49 +00:00
|
|
|
|
|
|
|
static gboolean
|
2010-08-11 13:39:22 +00:00
|
|
|
_scroll_event_cb (ClutterActor *viewport,
|
2010-08-11 09:34:49 +00:00
|
|
|
ClutterEvent *event,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2010-08-11 13:39:22 +00:00
|
|
|
ClutterActor *scrollable = CLUTTER_ACTOR (user_data);
|
2010-08-11 09:34:49 +00:00
|
|
|
|
2010-08-11 15:54:28 +00:00
|
|
|
gfloat viewport_height = clutter_actor_get_height (viewport);
|
|
|
|
gfloat scrollable_height = clutter_actor_get_height (scrollable);
|
2010-11-12 09:32:04 +00:00
|
|
|
gfloat y;
|
|
|
|
ClutterScrollDirection direction;
|
2010-08-11 15:54:28 +00:00
|
|
|
|
|
|
|
/* no need to scroll if the scrollable is shorter than the viewport */
|
|
|
|
if (scrollable_height < viewport_height)
|
|
|
|
return TRUE;
|
|
|
|
|
2010-11-12 09:32:04 +00:00
|
|
|
y = clutter_actor_get_y (scrollable);
|
2010-08-11 09:34:49 +00:00
|
|
|
|
|
|
|
direction = clutter_event_get_scroll_direction (event);
|
|
|
|
|
|
|
|
switch (direction)
|
|
|
|
{
|
|
|
|
case CLUTTER_SCROLL_UP:
|
|
|
|
y -= SCROLL_AMOUNT;
|
|
|
|
break;
|
|
|
|
case CLUTTER_SCROLL_DOWN:
|
|
|
|
y += SCROLL_AMOUNT;
|
|
|
|
break;
|
2010-08-11 15:54:28 +00:00
|
|
|
|
|
|
|
/* we're only interested in up and down */
|
2010-08-11 10:10:53 +00:00
|
|
|
case CLUTTER_SCROLL_LEFT:
|
|
|
|
case CLUTTER_SCROLL_RIGHT:
|
|
|
|
break;
|
2010-08-11 09:34:49 +00:00
|
|
|
}
|
|
|
|
|
2010-08-11 15:54:28 +00:00
|
|
|
/*
|
|
|
|
* the CLAMP macro returns a value for the first argument
|
|
|
|
* that falls within the range specified by the second and
|
|
|
|
* third arguments
|
|
|
|
*
|
|
|
|
* we allow the scrollable's y position to be decremented to the point
|
|
|
|
* where its base is aligned with the base of the viewport
|
|
|
|
*/
|
2010-08-11 09:34:49 +00:00
|
|
|
y = CLAMP (y,
|
2010-08-11 15:54:28 +00:00
|
|
|
viewport_height - scrollable_height,
|
2010-08-11 09:34:49 +00:00
|
|
|
0.0);
|
|
|
|
|
2010-08-11 16:14:09 +00:00
|
|
|
/* animate the change to the scrollable's y coordinate */
|
2010-08-11 13:39:22 +00:00
|
|
|
clutter_actor_animate (scrollable,
|
2010-08-11 09:34:49 +00:00
|
|
|
CLUTTER_EASE_OUT_CUBIC,
|
|
|
|
300,
|
|
|
|
"y", y,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2010-11-12 09:32:04 +00:00
|
|
|
ClutterActor *stage;
|
|
|
|
ClutterActor *viewport;
|
|
|
|
ClutterActor *texture;
|
|
|
|
|
2010-08-11 15:54:28 +00:00
|
|
|
gchar *image_file_path = TESTS_DATA_DIR "/redhand.png";
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
image_file_path = argv[1];
|
|
|
|
}
|
|
|
|
|
2011-02-22 00:44:55 +00:00
|
|
|
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
|
|
|
|
return 1;
|
2010-08-11 09:34:49 +00:00
|
|
|
|
|
|
|
stage = clutter_stage_get_default ();
|
|
|
|
clutter_actor_set_size (stage, STAGE_WIDTH, STAGE_HEIGHT);
|
|
|
|
|
2010-08-11 13:39:22 +00:00
|
|
|
/* the scrollable actor */
|
2010-08-11 09:34:49 +00:00
|
|
|
texture = clutter_texture_new ();
|
|
|
|
clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (texture),
|
|
|
|
TRUE);
|
|
|
|
|
2010-08-11 13:39:22 +00:00
|
|
|
/* set the texture's height so it's as tall as the stage */
|
|
|
|
clutter_actor_set_request_mode (texture, CLUTTER_REQUEST_WIDTH_FOR_HEIGHT);
|
|
|
|
clutter_actor_set_height (texture, STAGE_HEIGHT);
|
2010-08-11 09:34:49 +00:00
|
|
|
|
|
|
|
clutter_texture_set_from_file (CLUTTER_TEXTURE (texture),
|
2010-08-11 15:54:28 +00:00
|
|
|
image_file_path,
|
2010-08-11 09:34:49 +00:00
|
|
|
NULL);
|
|
|
|
|
2010-08-11 13:39:22 +00:00
|
|
|
/* the viewport which the box is scrolled within */
|
|
|
|
viewport = clutter_group_new ();
|
|
|
|
|
|
|
|
/* viewport is shorter than the stage */
|
|
|
|
clutter_actor_set_size (viewport, STAGE_WIDTH, STAGE_HEIGHT * 0.5);
|
|
|
|
|
|
|
|
/* align the viewport to the center of the stage's y axis */
|
|
|
|
clutter_actor_add_constraint (viewport, clutter_align_constraint_new (stage, CLUTTER_BIND_Y, 0.5));
|
2010-08-11 09:34:49 +00:00
|
|
|
|
2010-08-11 13:39:22 +00:00
|
|
|
/* viewport needs to respond to scroll events */
|
|
|
|
clutter_actor_set_reactive (viewport, TRUE);
|
|
|
|
|
|
|
|
/* clip all actors inside the viewport to that group's allocation */
|
|
|
|
clutter_actor_set_clip_to_allocation (viewport, TRUE);
|
|
|
|
|
|
|
|
/* put the texture inside the viewport */
|
2010-08-11 09:34:49 +00:00
|
|
|
clutter_container_add_actor (CLUTTER_CONTAINER (viewport), texture);
|
2010-08-11 13:39:22 +00:00
|
|
|
|
|
|
|
/* add the viewport to the stage */
|
|
|
|
clutter_container_add_actor (CLUTTER_CONTAINER (stage), viewport);
|
|
|
|
|
|
|
|
g_signal_connect (viewport,
|
|
|
|
"scroll-event",
|
|
|
|
G_CALLBACK (_scroll_event_cb),
|
|
|
|
texture);
|
2010-08-11 09:34:49 +00:00
|
|
|
|
|
|
|
clutter_actor_show (stage);
|
|
|
|
|
|
|
|
clutter_main ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|