cookbook: Simplified full scroll example

Removed the layout and box to simplify the scrollable
actor example.
This commit is contained in:
Elliot Smith 2010-08-11 14:39:22 +01:00
parent e216771a86
commit 5e0bc919c3

View File

@ -1,20 +1,19 @@
#include <clutter/clutter.h> #include <clutter/clutter.h>
#define STAGE_HEIGHT 400 #define STAGE_HEIGHT 400
#define STAGE_WIDTH STAGE_HEIGHT * 2 #define STAGE_WIDTH STAGE_HEIGHT
#define SCROLL_AMOUNT STAGE_HEIGHT * 0.2 #define SCROLL_AMOUNT STAGE_HEIGHT * 0.125
static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff }; static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
static const ClutterColor box_color = { 0xaa, 0xaa, 0x55, 0xff };
static gboolean static gboolean
_scroll_event_cb (ClutterActor *scroll, _scroll_event_cb (ClutterActor *viewport,
ClutterEvent *event, ClutterEvent *event,
gpointer user_data) gpointer user_data)
{ {
ClutterActor *viewport = CLUTTER_ACTOR (user_data); ClutterActor *scrollable = CLUTTER_ACTOR (user_data);
gfloat y = clutter_actor_get_y (viewport); gfloat y = clutter_actor_get_y (scrollable);
ClutterScrollDirection direction; ClutterScrollDirection direction;
direction = clutter_event_get_scroll_direction (event); direction = clutter_event_get_scroll_direction (event);
@ -33,12 +32,11 @@ _scroll_event_cb (ClutterActor *scroll,
} }
y = CLAMP (y, y = CLAMP (y,
clutter_actor_get_height (scroll) clutter_actor_get_height (viewport)
+ clutter_actor_get_y (scroll) - clutter_actor_get_height (scrollable),
- clutter_actor_get_height (viewport),
0.0); 0.0);
clutter_actor_animate (viewport, clutter_actor_animate (scrollable,
CLUTTER_EASE_OUT_CUBIC, CLUTTER_EASE_OUT_CUBIC,
300, 300,
"y", y, "y", y,
@ -51,7 +49,6 @@ int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
ClutterActor *stage; ClutterActor *stage;
ClutterActor *scroll;
ClutterActor *viewport; ClutterActor *viewport;
ClutterActor *texture; ClutterActor *texture;
@ -62,40 +59,44 @@ main (int argc, char *argv[])
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
/* the "letterbox" which the viewport is scrolled within */ /* the scrollable actor */
scroll = clutter_group_new ();
clutter_actor_set_size (scroll, STAGE_WIDTH, STAGE_HEIGHT * 0.75);
clutter_actor_add_constraint (scroll, clutter_align_constraint_new (stage, CLUTTER_BIND_Y, 0.5));
clutter_actor_set_reactive (scroll, TRUE);
/* this clips all actors inside the scroll group to that group's allocation */
clutter_actor_set_clip_to_allocation (scroll, TRUE);
viewport = clutter_box_new (clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
CLUTTER_BIN_ALIGNMENT_CENTER));
clutter_box_set_color (CLUTTER_BOX (viewport), &box_color);
/* the actor to scroll */
texture = clutter_texture_new (); texture = clutter_texture_new ();
clutter_actor_set_request_mode (texture, CLUTTER_REQUEST_HEIGHT_FOR_WIDTH);
clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (texture), clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (texture),
TRUE); TRUE);
/* the box resizes itself to fit this texture */ /* set the texture's height so it's as tall as the stage */
clutter_actor_set_width (texture, STAGE_WIDTH); clutter_actor_set_request_mode (texture, CLUTTER_REQUEST_WIDTH_FOR_HEIGHT);
clutter_actor_set_height (texture, STAGE_HEIGHT);
clutter_texture_set_from_file (CLUTTER_TEXTURE (texture), clutter_texture_set_from_file (CLUTTER_TEXTURE (texture),
TESTS_DATA_DIR "/redhand.png", TESTS_DATA_DIR "/redhand.png",
NULL); NULL);
g_signal_connect (scroll, /* 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));
/* 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 */
clutter_container_add_actor (CLUTTER_CONTAINER (viewport), texture);
/* add the viewport to the stage */
clutter_container_add_actor (CLUTTER_CONTAINER (stage), viewport);
g_signal_connect (viewport,
"scroll-event", "scroll-event",
G_CALLBACK (_scroll_event_cb), G_CALLBACK (_scroll_event_cb),
viewport); texture);
clutter_container_add_actor (CLUTTER_CONTAINER (viewport), texture);
clutter_container_add_actor (CLUTTER_CONTAINER (scroll), viewport);
clutter_container_add_actor (CLUTTER_CONTAINER (stage), scroll);
clutter_actor_show (stage); clutter_actor_show (stage);