2009-09-11 10:34:13 -04:00
|
|
|
#include <stdlib.h>
|
2011-06-10 12:48:54 -04:00
|
|
|
#include <cairo.h>
|
2012-03-29 11:35:22 -04:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2009-09-11 10:34:13 -04:00
|
|
|
#include <clutter/clutter.h>
|
|
|
|
|
2010-06-16 08:58:41 -04:00
|
|
|
static const ClutterColor bg_color = { 0xcc, 0xcc, 0xcc, 0x99 };
|
|
|
|
|
2011-06-09 09:42:31 -04:00
|
|
|
static gboolean is_expanded = FALSE;
|
|
|
|
|
2011-07-26 07:53:22 -04:00
|
|
|
static gboolean
|
2012-03-29 11:35:22 -04:00
|
|
|
on_canvas_draw (ClutterCanvas *canvas,
|
|
|
|
cairo_t *cr,
|
|
|
|
gint width,
|
|
|
|
gint height)
|
2009-09-14 16:51:49 -04:00
|
|
|
{
|
|
|
|
cairo_pattern_t *pat;
|
|
|
|
gfloat x, y;
|
2011-07-26 07:53:22 -04:00
|
|
|
|
2012-03-29 11:35:22 -04:00
|
|
|
g_print (G_STRLOC ": Painting at %d x %d\n", width, height);
|
|
|
|
|
|
|
|
cairo_save (cr);
|
|
|
|
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
|
|
|
|
cairo_paint (cr);
|
|
|
|
cairo_restore (cr);
|
2009-09-14 16:51:49 -04:00
|
|
|
|
|
|
|
#define BG_ROUND_RADIUS 12
|
|
|
|
|
|
|
|
x = y = 0;
|
|
|
|
|
|
|
|
cairo_move_to (cr, BG_ROUND_RADIUS, y);
|
|
|
|
cairo_line_to (cr, width - BG_ROUND_RADIUS, y);
|
|
|
|
cairo_curve_to (cr, width, y, width, y, width, BG_ROUND_RADIUS);
|
|
|
|
cairo_line_to (cr, width, height - BG_ROUND_RADIUS);
|
|
|
|
cairo_curve_to (cr, width, height, width, height, width - BG_ROUND_RADIUS, height);
|
|
|
|
cairo_line_to (cr, BG_ROUND_RADIUS, height);
|
|
|
|
cairo_curve_to (cr, x, height, x, height, x, height - BG_ROUND_RADIUS);
|
|
|
|
cairo_line_to (cr, x, BG_ROUND_RADIUS);
|
|
|
|
cairo_curve_to (cr, x, y, x, y, BG_ROUND_RADIUS, y);
|
|
|
|
|
|
|
|
cairo_close_path (cr);
|
|
|
|
|
2011-07-26 07:53:22 -04:00
|
|
|
clutter_cairo_set_source_color (cr, &bg_color);
|
2009-09-14 16:51:49 -04:00
|
|
|
cairo_stroke (cr);
|
|
|
|
|
|
|
|
x += 4;
|
|
|
|
y += 4;
|
|
|
|
width -= 4;
|
|
|
|
height -= 4;
|
|
|
|
|
|
|
|
cairo_move_to (cr, BG_ROUND_RADIUS, y);
|
|
|
|
cairo_line_to (cr, width - BG_ROUND_RADIUS, y);
|
|
|
|
cairo_curve_to (cr, width, y, width, y, width, BG_ROUND_RADIUS);
|
|
|
|
cairo_line_to (cr, width, height - BG_ROUND_RADIUS);
|
|
|
|
cairo_curve_to (cr, width, height, width, height, width - BG_ROUND_RADIUS, height);
|
|
|
|
cairo_line_to (cr, BG_ROUND_RADIUS, height);
|
|
|
|
cairo_curve_to (cr, x, height, x, height, x, height - BG_ROUND_RADIUS);
|
|
|
|
cairo_line_to (cr, x, BG_ROUND_RADIUS);
|
|
|
|
cairo_curve_to (cr, x, y, x, y, BG_ROUND_RADIUS, y);
|
|
|
|
|
|
|
|
cairo_close_path (cr);
|
|
|
|
|
|
|
|
pat = cairo_pattern_create_linear (0, 0, 0, height);
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, 1, .85, .85, .85, 1);
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, .95, 1, 1, 1, 1);
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, .05, 1, 1, 1, 1);
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, 0, .85, .85, .85, 1);
|
|
|
|
|
|
|
|
cairo_set_source (cr, pat);
|
|
|
|
cairo_fill (cr);
|
|
|
|
|
|
|
|
cairo_pattern_destroy (pat);
|
|
|
|
|
|
|
|
#undef BG_ROUND_RADIUS
|
2011-07-26 07:53:22 -04:00
|
|
|
|
|
|
|
return TRUE;
|
2009-09-14 16:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
on_box_enter (ClutterActor *box,
|
|
|
|
ClutterEvent *event,
|
|
|
|
ClutterActor *emblem)
|
|
|
|
{
|
2012-03-29 11:35:22 -04:00
|
|
|
/* we ease the opacity linearly */
|
2012-03-29 10:12:09 -04:00
|
|
|
clutter_actor_save_easing_state (emblem);
|
|
|
|
clutter_actor_set_easing_mode (emblem, CLUTTER_LINEAR);
|
2012-03-15 07:25:52 -04:00
|
|
|
clutter_actor_set_opacity (emblem, 255);
|
2012-03-29 10:12:09 -04:00
|
|
|
clutter_actor_restore_easing_state (emblem);
|
2009-09-14 16:51:49 -04:00
|
|
|
|
2012-03-15 07:25:52 -04:00
|
|
|
return CLUTTER_EVENT_STOP;
|
2009-09-14 16:51:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
on_box_leave (ClutterActor *box,
|
|
|
|
ClutterEvent *event,
|
|
|
|
ClutterActor *emblem)
|
|
|
|
{
|
2012-03-29 10:12:09 -04:00
|
|
|
clutter_actor_save_easing_state (emblem);
|
|
|
|
clutter_actor_set_easing_mode (emblem, CLUTTER_LINEAR);
|
2012-03-15 07:25:52 -04:00
|
|
|
clutter_actor_set_opacity (emblem, 0);
|
2012-03-29 10:12:09 -04:00
|
|
|
clutter_actor_restore_easing_state (emblem);
|
2009-09-14 16:51:49 -04:00
|
|
|
|
2012-03-15 07:25:52 -04:00
|
|
|
return CLUTTER_EVENT_STOP;
|
2009-09-14 16:51:49 -04:00
|
|
|
}
|
|
|
|
|
2010-06-16 08:58:41 -04:00
|
|
|
static void
|
2012-03-29 11:35:22 -04:00
|
|
|
on_emblem_clicked (ClutterClickAction *action,
|
|
|
|
ClutterActor *emblem,
|
|
|
|
ClutterActor *box)
|
2010-06-16 08:58:41 -04:00
|
|
|
{
|
2012-03-29 11:35:22 -04:00
|
|
|
/* we add a little bounce to the resizing of the box */
|
2012-03-29 10:12:09 -04:00
|
|
|
clutter_actor_save_easing_state (box);
|
|
|
|
clutter_actor_set_easing_mode (box, CLUTTER_EASE_OUT_BOUNCE);
|
|
|
|
clutter_actor_set_easing_duration (box, 500);
|
|
|
|
|
2010-06-16 08:58:41 -04:00
|
|
|
if (!is_expanded)
|
2012-03-15 07:25:52 -04:00
|
|
|
clutter_actor_set_size (box, 400, 400);
|
2010-06-16 08:58:41 -04:00
|
|
|
else
|
2012-03-15 07:25:52 -04:00
|
|
|
clutter_actor_set_size (box, 200, 200);
|
2010-06-16 08:58:41 -04:00
|
|
|
|
2012-03-29 10:12:09 -04:00
|
|
|
clutter_actor_restore_easing_state (box);
|
|
|
|
|
2010-06-16 08:58:41 -04:00
|
|
|
is_expanded = !is_expanded;
|
|
|
|
}
|
|
|
|
|
2011-06-09 09:42:31 -04:00
|
|
|
static gboolean
|
2012-03-29 11:35:22 -04:00
|
|
|
on_emblem_long_press (ClutterClickAction *action,
|
|
|
|
ClutterActor *emblem,
|
|
|
|
ClutterLongPressState state,
|
|
|
|
ClutterActor *box)
|
2011-06-09 09:42:31 -04:00
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case CLUTTER_LONG_PRESS_QUERY:
|
|
|
|
g_print ("*** long press: query ***\n");
|
|
|
|
return is_expanded;
|
|
|
|
|
|
|
|
case CLUTTER_LONG_PRESS_CANCEL:
|
|
|
|
g_print ("*** long press: cancel ***\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLUTTER_LONG_PRESS_ACTIVATE:
|
|
|
|
g_print ("*** long press: activate ***\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-16 08:58:41 -04:00
|
|
|
static void
|
2012-03-29 11:35:22 -04:00
|
|
|
redraw_canvas (ClutterActor *actor,
|
|
|
|
ClutterCanvas *canvas)
|
2010-06-16 08:58:41 -04:00
|
|
|
{
|
2012-03-29 11:35:22 -04:00
|
|
|
/* we want to invalidate the canvas and redraw its contents
|
|
|
|
* only when the size changes at the end of the animation,
|
|
|
|
* to avoid drawing all the states inbetween
|
|
|
|
*/
|
|
|
|
clutter_canvas_set_size (canvas,
|
|
|
|
clutter_actor_get_width (actor),
|
|
|
|
clutter_actor_get_height (actor));
|
2010-06-16 08:58:41 -04:00
|
|
|
}
|
|
|
|
|
2012-05-01 13:30:10 -04:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2009-09-11 10:34:13 -04:00
|
|
|
{
|
2012-03-29 11:35:22 -04:00
|
|
|
ClutterActor *stage, *box, *bg, *icon, *emblem, *label;
|
2009-09-11 10:34:13 -04:00
|
|
|
ClutterLayoutManager *layout;
|
2012-03-29 11:35:22 -04:00
|
|
|
ClutterContent *canvas, *image;
|
2009-09-11 10:34:13 -04:00
|
|
|
ClutterColor *color;
|
2010-06-16 08:58:41 -04:00
|
|
|
ClutterAction *action;
|
2012-03-29 11:35:22 -04:00
|
|
|
GdkPixbuf *pixbuf;
|
2009-09-11 10:34:13 -04:00
|
|
|
|
2011-02-21 19:19:35 -05:00
|
|
|
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
|
|
|
|
return 1;
|
2009-09-11 10:34:13 -04:00
|
|
|
|
2012-03-29 11:35:22 -04:00
|
|
|
/* prepare the stage */
|
2011-11-09 08:41:15 -05:00
|
|
|
stage = clutter_stage_new ();
|
|
|
|
clutter_stage_set_title (CLUTTER_STAGE (stage), "BinLayout");
|
2012-03-29 11:35:22 -04:00
|
|
|
clutter_actor_set_background_color (stage, CLUTTER_COLOR_Aluminium2);
|
2009-09-14 16:51:49 -04:00
|
|
|
clutter_actor_set_size (stage, 640, 480);
|
2012-03-29 11:35:22 -04:00
|
|
|
clutter_actor_show (stage);
|
2011-11-09 08:41:15 -05:00
|
|
|
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
|
2009-09-11 10:34:13 -04:00
|
|
|
|
2012-08-24 05:42:26 -04:00
|
|
|
/* this is our BinLayout */
|
|
|
|
layout = clutter_bin_layout_new ();
|
2009-09-11 10:34:13 -04:00
|
|
|
|
2012-03-29 11:35:22 -04:00
|
|
|
/* the main container; this actor will use the BinLayout to lay
|
|
|
|
* out its children; we use the anchor point to keep it centered
|
|
|
|
* on the same position even when we change its size
|
|
|
|
*/
|
|
|
|
box = clutter_actor_new ();
|
|
|
|
clutter_actor_set_layout_manager (box, layout);
|
2012-07-06 13:06:33 -04:00
|
|
|
clutter_actor_add_constraint (box, clutter_align_constraint_new (stage, CLUTTER_ALIGN_BOTH, 0.5));
|
2009-09-14 16:51:49 -04:00
|
|
|
clutter_actor_set_position (box, 320, 240);
|
|
|
|
clutter_actor_set_reactive (box, TRUE);
|
|
|
|
clutter_actor_set_name (box, "box");
|
2012-03-29 11:35:22 -04:00
|
|
|
clutter_actor_add_child (stage, box);
|
|
|
|
|
|
|
|
/* the background is drawn using a canvas content */
|
|
|
|
canvas = clutter_canvas_new ();
|
|
|
|
g_signal_connect (canvas, "draw", G_CALLBACK (on_canvas_draw), NULL);
|
|
|
|
clutter_canvas_set_size (CLUTTER_CANVAS (canvas), 200, 200);
|
2009-09-11 10:34:13 -04:00
|
|
|
|
2012-03-29 11:35:22 -04:00
|
|
|
/* this is the background actor; we want it to fill the whole
|
|
|
|
* of the allocation given to it by its parent
|
2010-06-16 08:58:41 -04:00
|
|
|
*/
|
2012-03-29 11:35:22 -04:00
|
|
|
bg = clutter_actor_new ();
|
|
|
|
clutter_actor_set_name (bg, "background");
|
|
|
|
clutter_actor_set_size (bg, 200, 200);
|
|
|
|
clutter_actor_set_content (bg, canvas);
|
|
|
|
clutter_actor_set_x_expand (bg, TRUE);
|
|
|
|
clutter_actor_set_y_expand (bg, TRUE);
|
|
|
|
clutter_actor_set_x_align (bg, CLUTTER_ACTOR_ALIGN_FILL);
|
|
|
|
clutter_actor_set_y_align (bg, CLUTTER_ACTOR_ALIGN_FILL);
|
|
|
|
clutter_actor_add_child (box, bg);
|
|
|
|
/* we use the ::transitions-completed signal to get notification
|
|
|
|
* of the end of the sizing animation; this allows us to redraw
|
|
|
|
* the canvas only once the animation has stopped
|
|
|
|
*/
|
|
|
|
g_signal_connect (box, "transitions-completed",
|
|
|
|
G_CALLBACK (redraw_canvas),
|
|
|
|
canvas);
|
|
|
|
|
|
|
|
/* we use GdkPixbuf to load an image from our data directory */
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* this is the icon; it's going to be centered inside the box actor.
|
|
|
|
* we use the content gravity to keep the aspect ratio of the image,
|
|
|
|
* and the scaling filters to get a better result when scaling the
|
|
|
|
* image down.
|
|
|
|
*/
|
|
|
|
icon = clutter_actor_new ();
|
|
|
|
clutter_actor_set_name (icon, "icon");
|
|
|
|
clutter_actor_set_size (icon, 196, 196);
|
|
|
|
clutter_actor_set_x_expand (icon, TRUE);
|
|
|
|
clutter_actor_set_y_expand (icon, TRUE);
|
|
|
|
clutter_actor_set_x_align (icon, CLUTTER_ACTOR_ALIGN_CENTER);
|
|
|
|
clutter_actor_set_y_align (icon, CLUTTER_ACTOR_ALIGN_CENTER);
|
|
|
|
clutter_actor_set_content_gravity (icon, CLUTTER_CONTENT_GRAVITY_RESIZE_ASPECT);
|
|
|
|
clutter_actor_set_content_scaling_filters (icon,
|
|
|
|
CLUTTER_SCALING_FILTER_TRILINEAR,
|
|
|
|
CLUTTER_SCALING_FILTER_LINEAR);
|
|
|
|
clutter_actor_set_content (icon, image);
|
|
|
|
clutter_actor_add_child (box, icon);
|
2009-09-11 10:34:13 -04:00
|
|
|
|
|
|
|
color = clutter_color_new (g_random_int_range (0, 255),
|
|
|
|
g_random_int_range (0, 255),
|
|
|
|
g_random_int_range (0, 255),
|
2009-09-14 16:51:49 -04:00
|
|
|
224);
|
2009-09-11 10:34:13 -04:00
|
|
|
|
2012-03-29 11:35:22 -04:00
|
|
|
/* this is the emblem: a small rectangle with a random color, that we
|
|
|
|
* want to put in the bottom right corner
|
|
|
|
*/
|
|
|
|
emblem = clutter_actor_new ();
|
|
|
|
clutter_actor_set_name (emblem, "emblem");
|
|
|
|
clutter_actor_set_size (emblem, 48, 48);
|
|
|
|
clutter_actor_set_background_color (emblem, color);
|
|
|
|
clutter_actor_set_x_expand (emblem, TRUE);
|
|
|
|
clutter_actor_set_y_expand (emblem, TRUE);
|
|
|
|
clutter_actor_set_x_align (emblem, CLUTTER_ACTOR_ALIGN_END);
|
|
|
|
clutter_actor_set_y_align (emblem, CLUTTER_ACTOR_ALIGN_END);
|
|
|
|
clutter_actor_set_reactive (emblem, TRUE);
|
|
|
|
clutter_actor_set_opacity (emblem, 0);
|
|
|
|
clutter_actor_add_child (box, emblem);
|
|
|
|
clutter_color_free (color);
|
2009-09-14 16:51:49 -04:00
|
|
|
|
2012-03-29 11:35:22 -04:00
|
|
|
/* when clicking on the emblem, we want to perform an action */
|
2010-06-16 08:58:41 -04:00
|
|
|
action = clutter_click_action_new ();
|
2012-03-29 11:35:22 -04:00
|
|
|
clutter_actor_add_action (emblem, action);
|
|
|
|
g_signal_connect (action, "clicked", G_CALLBACK (on_emblem_clicked), box);
|
|
|
|
g_signal_connect (action, "long-press", G_CALLBACK (on_emblem_long_press), box);
|
|
|
|
|
|
|
|
/* whenever the pointer enters the box, we show the emblem; we hide
|
|
|
|
* the emblem when the pointer leaves the box
|
|
|
|
*/
|
2009-09-14 16:51:49 -04:00
|
|
|
g_signal_connect (box,
|
|
|
|
"enter-event", G_CALLBACK (on_box_enter),
|
2012-03-29 11:35:22 -04:00
|
|
|
emblem);
|
2009-09-14 16:51:49 -04:00
|
|
|
g_signal_connect (box,
|
|
|
|
"leave-event", G_CALLBACK (on_box_leave),
|
2012-03-29 11:35:22 -04:00
|
|
|
emblem);
|
|
|
|
|
|
|
|
/* a label, that we want to position at the top and center of the box */
|
|
|
|
label = clutter_text_new ();
|
|
|
|
clutter_actor_set_name (label, "text");
|
|
|
|
clutter_text_set_text (CLUTTER_TEXT (label), "A simple test");
|
|
|
|
clutter_actor_set_x_expand (label, TRUE);
|
|
|
|
clutter_actor_set_x_align (label, CLUTTER_ACTOR_ALIGN_CENTER);
|
|
|
|
clutter_actor_set_y_expand (label, TRUE);
|
|
|
|
clutter_actor_set_y_align (label, CLUTTER_ACTOR_ALIGN_START);
|
|
|
|
clutter_actor_add_child (box, label);
|
2009-09-11 10:34:13 -04:00
|
|
|
|
|
|
|
clutter_main ();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|