cookbook: Added example for pointer cross recipe

Code example demonstrates detecting pointers entering/leaving
an actor by implementing a (very) simple button with hover
effect.
This commit is contained in:
Elliot Smith 2010-08-19 10:32:28 +01:00
parent 213bd1eab4
commit 72ddd471f0
2 changed files with 115 additions and 0 deletions

View File

@ -11,6 +11,7 @@ noinst_PROGRAMS = \
layouts-stacking \ layouts-stacking \
layouts-stacking-diff-sized-actors \ layouts-stacking-diff-sized-actors \
events-mouse-scroll \ events-mouse-scroll \
events-pointer-cross \
$(NULL) $(NULL)
INCLUDES = \ INCLUDES = \
@ -40,5 +41,6 @@ textures_sub_texture_SOURCES = textures-sub-texture.c
layouts_stacking_SOURCES = layouts-stacking.c layouts_stacking_SOURCES = layouts-stacking.c
layouts_stacking_diff_sized_actors_SOURCES = layouts-stacking-diff-sized-actors.c layouts_stacking_diff_sized_actors_SOURCES = layouts-stacking-diff-sized-actors.c
events_mouse_scroll_SOURCES = events-mouse-scroll.c events_mouse_scroll_SOURCES = events-mouse-scroll.c
events_pointer_cross_SOURCES = events-pointer-cross.c
-include $(top_srcdir)/build/autotools/Makefile.am.gitignore -include $(top_srcdir)/build/autotools/Makefile.am.gitignore

View File

@ -0,0 +1,113 @@
#include <clutter/clutter.h>
static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
static const ClutterColor yellow = { 0xaa, 0x99, 0x00, 0xff };
static const ClutterColor white = { 0xff, 0xff, 0xff, 0xff };
static gboolean
_on_enter_cb (ClutterActor *actor,
ClutterEvent *event,
gpointer user_data)
{
ClutterState *transitions = CLUTTER_STATE (user_data);
clutter_state_set_state (transitions, "fade-in");
return TRUE;
}
static gboolean
_on_leave_cb (ClutterActor *actor,
ClutterEvent *event,
gpointer user_data)
{
ClutterState *transitions = CLUTTER_STATE (user_data);
clutter_state_set_state (transitions, "fade-out");
return TRUE;
}
int
main (int argc, char *argv[])
{
ClutterActor *stage;
ClutterLayoutManager *layout;
ClutterActor *box;
ClutterActor *rect;
ClutterActor *text;
ClutterState *transitions;
clutter_init (&argc, &argv);
stage = clutter_stage_get_default ();
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
clutter_stage_set_title (CLUTTER_STAGE (stage), "btn");
layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_FILL,
CLUTTER_BIN_ALIGNMENT_FILL);
box = clutter_box_new (layout);
clutter_actor_set_position (box, 25, 25);
clutter_actor_set_reactive (box, TRUE);
clutter_actor_set_size (box, 100, 30);
/* background for the button */
rect = clutter_rectangle_new_with_color (&yellow);
clutter_container_add_actor (CLUTTER_CONTAINER (box), rect);
/* text for the button */
text = clutter_text_new_full ("Sans 10pt", "Hover me", &white);
clutter_text_set_line_alignment (CLUTTER_TEXT (text), PANGO_ALIGN_CENTER);
/*
* NB don't set the height, so the actor assumes the height of the text;
* then when added to the bin layout, it gets centred on it;
* also if you don't set the width, the layout goes gets really wide;
* the 10pt text fits inside the 30px height of the rectangle
*/
clutter_actor_set_width (text, 100);
clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (layout),
text,
CLUTTER_BIN_ALIGNMENT_CENTER,
CLUTTER_BIN_ALIGNMENT_CENTER);
/* animations */
transitions = clutter_state_new ();
clutter_state_set (transitions, NULL, "fade-out",
box, "opacity", CLUTTER_LINEAR, 180,
NULL);
/*
* NB you can't use an easing mode where alpha > 1.0 if you're
* animating to a value of 255, as the value you're animating
* to will possibly go > 255
*/
clutter_state_set (transitions, NULL, "fade-in",
box, "opacity", CLUTTER_LINEAR, 255,
NULL);
clutter_state_set_duration (transitions, NULL, NULL, 50);
clutter_state_warp_to_state (transitions, "fade-out");
g_signal_connect (box,
"enter-event",
G_CALLBACK (_on_enter_cb),
transitions);
g_signal_connect (box,
"leave-event",
G_CALLBACK (_on_leave_cb),
transitions);
/* bind the stage size to the box size + 50px in each axis */
clutter_actor_add_constraint (stage, clutter_bind_constraint_new (box, CLUTTER_BIND_HEIGHT, 50.0));
clutter_actor_add_constraint (stage, clutter_bind_constraint_new (box, CLUTTER_BIND_WIDTH, 50.0));
clutter_container_add_actor (CLUTTER_CONTAINER (stage), box);
clutter_actor_show (stage);
clutter_main ();
g_object_unref (transitions);
return 0;
}