From: Pierre-Luc Beaudoin Date: Wed, 11 Feb 2009 18:22:29 +0000 (+0200) Subject: Add an animated marker demo X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1bc7693a2aa02cbcdff1f05980b429e4743b7265;p=libchamplain Add an animated marker demo and various code clean-up to demos --- diff --git a/demos/Makefile.am b/demos/Makefile.am index 5824996..fc18784 100644 --- a/demos/Makefile.am +++ b/demos/Makefile.am @@ -1,10 +1,13 @@ -noinst_PROGRAMS = launcher +noinst_PROGRAMS = launcher animated-marker INCLUDES = -I$(top_srcdir) -AM_CPPFLAGS = $(DEPS_CFLAGS) +AM_CPPFLAGS = $(DEPS_CFLAGS) AM_LDFLAGS = $(DEPS_LIBS) launcher_SOURCES = launcher.c launcher_LDADD = $(DEPS_LIBS) ../champlain/libchamplain-0.3.la + +animated_marker_SOURCES = animated-marker.c +animated_marker_LDADD = $(DEPS_LIBS) ../champlain/libchamplain-0.3.la diff --git a/demos/animated-marker.c b/demos/animated-marker.c new file mode 100644 index 0000000..9559750 --- /dev/null +++ b/demos/animated-marker.c @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2008 Pierre-Luc Beaudoin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include +#include +#include + +#define MARKER_SIZE 10 + +/* The marker is drawn with cairo. It is composed of 1 static filled circle + * and 1 stroked circle animated as an echo. + */ +static ClutterActor* +create_marker () +{ + ClutterActor *marker; + ClutterColor orange = { 0xf3, 0x94, 0x07, 0xbb }; + ClutterColor white = { 0xff, 0xff, 0xff, 0xff }; + ClutterActor *actor, *bg; + ClutterTimeline *timeline; + ClutterBehaviour *behaviour; + ClutterAlpha *alpha; + cairo_t *cr; + + /* Create the marker */ + marker = champlain_marker_new (); + + /* Static filled cercle ----------------------------------------------- */ + bg = clutter_cairo_new (MARKER_SIZE, MARKER_SIZE); + cr = clutter_cairo_create (CLUTTER_CAIRO (bg)); + + /* Draw the circle */ + cairo_set_source_rgb (cr, 0, 0, 0); + cairo_arc (cr, MARKER_SIZE / 2.0, MARKER_SIZE / 2.0, MARKER_SIZE / 2.0, 0, 2 * M_PI); + cairo_close_path (cr); + + /* Fill the circle */ + cairo_set_source_rgba (cr, 0.1, 0.1, 0.9, 1.0); + cairo_fill (cr); + + cairo_destroy (cr); + + /* Add the circle to the marker */ + clutter_container_add_actor (CLUTTER_CONTAINER (marker), bg); + clutter_actor_set_anchor_point_from_gravity (bg, CLUTTER_GRAVITY_CENTER); + clutter_actor_set_position (bg, 0, 0); + + /* Echo cercle -------------------------------------------------------- */ + bg = clutter_cairo_new (2 * MARKER_SIZE, 2 * MARKER_SIZE); + cr = clutter_cairo_create (CLUTTER_CAIRO (bg)); + + /* Draw the circle */ + cairo_set_source_rgb (cr, 0, 0, 0); + cairo_arc (cr, MARKER_SIZE, MARKER_SIZE, 0.9 * MARKER_SIZE, 0, 2 * M_PI); + cairo_close_path (cr); + + /* Stroke the circle */ + cairo_set_line_width (cr, 2.0); + cairo_set_source_rgba (cr, 0.1, 0.1, 0.7, 1.0); + cairo_stroke (cr); + + cairo_destroy (cr); + + /* Add the circle to the marker */ + clutter_container_add_actor (CLUTTER_CONTAINER (marker), bg); + clutter_actor_lower_bottom (bg); /* Ensure it is under the previous circle */ + clutter_actor_set_position (bg, 0, 0); + clutter_actor_set_anchor_point_from_gravity (bg, CLUTTER_GRAVITY_CENTER); + + /* Animate the echo cercle */ + timeline = clutter_timeline_new_for_duration (1000); + clutter_timeline_set_loop (timeline, TRUE); + alpha = clutter_alpha_new_full (timeline, CLUTTER_ALPHA_SINE_INC, NULL, g_object_unref); + + behaviour = clutter_behaviour_scale_new (alpha, 0.5, 0.5, 2.0, 2.0); + clutter_behaviour_apply (behaviour, bg); + + behaviour = clutter_behaviour_opacity_new (alpha, 255, 0); + clutter_behaviour_apply (behaviour, bg); + + clutter_timeline_start (timeline); + + /* Sets marker position on the map */ + champlain_marker_set_position (CHAMPLAIN_MARKER (marker), + 45.528178, -73.563788); + + return marker; +} + +int +main (int argc, char *argv[]) +{ + ClutterActor* actor, *layer, *marker, *stage; + + g_thread_init (NULL); + clutter_init (&argc, &argv); + + stage = clutter_stage_get_default (); + clutter_actor_set_size (stage, 800, 600); + + /* Create the map view */ + actor = champlain_view_new (); + champlain_view_set_size (CHAMPLAIN_VIEW (actor), 800, 600); + clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor); + + /* Create the marker layer */ + layer = champlain_layer_new (); + clutter_actor_show (layer); + champlain_view_add_layer (CHAMPLAIN_VIEW (actor), layer); + + /* Create a marker */ + marker = create_marker (); + clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL); + + /* Finish initialising the map view */ + g_object_set (G_OBJECT (actor), "zoom-level", 5, + "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL); + champlain_view_center_on (CHAMPLAIN_VIEW (actor), 45.466, -73.75); + + clutter_actor_show (stage); + clutter_main (); + + return 0; +} diff --git a/demos/launcher.c b/demos/launcher.c index 0d565bc..37afa6a 100644 --- a/demos/launcher.c +++ b/demos/launcher.c @@ -29,7 +29,8 @@ map_view_button_release_cb (ClutterActor *actor, return; g_print("Map was clicked at "); - if (champlain_view_get_coords_from_event (view, (ClutterEvent*)event, &lat, &lon)) + if (champlain_view_get_coords_from_event (view, (ClutterEvent*)event, &lat, + &lon)) g_print("%f, %f \n", lat, lon); return TRUE; @@ -78,7 +79,6 @@ create_marker_layer (ChamplainView *view) clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL); clutter_actor_show (layer); - return layer; } @@ -94,21 +94,19 @@ main (int argc, stage = clutter_stage_get_default (); clutter_actor_set_size (stage, 800, 600); + /* Create the map view */ actor = champlain_view_new (); - g_object_set (G_OBJECT (actor), "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, - "zoom-level", 12, NULL); - clutter_actor_set_reactive (actor, TRUE); - g_signal_connect_after (actor, "button-release-event", - G_CALLBACK (map_view_button_release_cb), actor); - - champlain_view_set_size (CHAMPLAIN_VIEW (actor), 700, 500); - clutter_actor_set_position (actor, 50, 50); + champlain_view_set_size (CHAMPLAIN_VIEW (actor), 800, 600); + clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor); - layer = create_marker_layer (actor); - champlain_view_add_layer(CHAMPLAIN_VIEW (actor), layer); + /* Create the markers and marker layer */ + layer = create_marker_layer (CHAMPLAIN_VIEW (actor)); + champlain_view_add_layer (CHAMPLAIN_VIEW (actor), layer); - clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor); - champlain_view_center_on (CHAMPLAIN_VIEW(actor), 45.466, -73.75); + /* Finish initialising the map view */ + g_object_set (G_OBJECT (actor), "zoom-level", 12, + "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL); + champlain_view_center_on(CHAMPLAIN_VIEW(actor), 45.466, -73.75); clutter_actor_show (stage); clutter_main ();