summaryrefslogtreecommitdiff
path: root/demos/animated-marker.c
blob: 4c2e334aaee8bb15563872cb209f5315a20f9cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (C) 2008 Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
 *
 * 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 <champlain/champlain.h>
#include <math.h>

#define MARKER_SIZE 10


static gboolean
draw_center (ClutterCanvas *canvas,
    cairo_t *cr,
    int width,
    int height)
{
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint(cr);
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);

  /* 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);
  
  return TRUE;
}


static gboolean
draw_circle (ClutterCanvas *canvas,
    cairo_t *cr,
    int width,
    int height)
{
   /* 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);

  return TRUE;
}


/* 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;
  ClutterActor *bg;
  ClutterContent *canvas;
  ClutterTransition *transition;

  /* Create the marker */
  marker = champlain_custom_marker_new ();

  /* Static filled circle ----------------------------------------------- */
  canvas = clutter_canvas_new ();
  clutter_canvas_set_size (CLUTTER_CANVAS (canvas), MARKER_SIZE, MARKER_SIZE);
  g_signal_connect (canvas, "draw", G_CALLBACK (draw_center), NULL);

  bg = clutter_actor_new ();
  clutter_actor_set_size (bg, MARKER_SIZE, MARKER_SIZE);
  clutter_actor_set_content (bg, canvas);
  clutter_content_invalidate (canvas);
  g_object_unref (canvas);

  /* Add the circle to the marker */
  clutter_actor_add_child (marker, bg);
  clutter_actor_set_position (bg, -0.5 * MARKER_SIZE, -0.5 * MARKER_SIZE);

  /* Echo circle -------------------------------------------------------- */
  canvas = clutter_canvas_new ();
  clutter_canvas_set_size (CLUTTER_CANVAS (canvas), 2 * MARKER_SIZE, 2 * MARKER_SIZE);
  g_signal_connect (canvas, "draw", G_CALLBACK (draw_circle), NULL);

  bg = clutter_actor_new ();
  clutter_actor_set_size (bg, 2 * MARKER_SIZE, 2 * MARKER_SIZE);
  clutter_actor_set_content (bg, canvas);
  clutter_content_invalidate (canvas);
  g_object_unref (canvas);

  /* Add the circle to the marker */
  clutter_actor_add_child (marker, bg);
  clutter_actor_set_pivot_point (bg, 0.5, 0.5);
  clutter_actor_set_position (bg, -MARKER_SIZE, -MARKER_SIZE);

  transition = clutter_property_transition_new ("opacity");
  clutter_actor_set_easing_mode (bg, CLUTTER_EASE_OUT_SINE);
  clutter_timeline_set_duration (CLUTTER_TIMELINE (transition), 1000);
  clutter_timeline_set_repeat_count (CLUTTER_TIMELINE (transition), -1);
  clutter_transition_set_from (transition, G_TYPE_UINT, 255);
  clutter_transition_set_to (transition, G_TYPE_UINT, 0);
  clutter_actor_add_transition (bg, "animate-opacity", transition);

  transition = clutter_property_transition_new ("scale-x");
  clutter_actor_set_easing_mode (bg, CLUTTER_EASE_OUT_SINE);
  clutter_timeline_set_duration (CLUTTER_TIMELINE (transition), 1000);
  clutter_timeline_set_repeat_count (CLUTTER_TIMELINE (transition), -1);
  clutter_transition_set_from (transition, G_TYPE_FLOAT, 0.5);
  clutter_transition_set_to (transition, G_TYPE_FLOAT, 2.0);
  clutter_actor_add_transition (bg, "animate-scale-x", transition);

  transition = clutter_property_transition_new ("scale-y");
  clutter_actor_set_easing_mode (bg, CLUTTER_EASE_OUT_SINE);
  clutter_timeline_set_duration (CLUTTER_TIMELINE (transition), 1000);
  clutter_timeline_set_repeat_count (CLUTTER_TIMELINE (transition), -1);
  clutter_transition_set_from (transition, G_TYPE_FLOAT, 0.5);
  clutter_transition_set_to (transition, G_TYPE_FLOAT, 2.0);
  clutter_actor_add_transition (bg, "animate-scale-y", transition);

  return marker;
}


double lat = 45.466;
double lon = -73.75;

typedef struct
{
  ChamplainView *view;
  ChamplainMarker *marker;
} GpsCallbackData;

static gboolean
gps_callback (GpsCallbackData *data)
{
  lat += 0.005;
  lon += 0.005;
  champlain_view_center_on (data->view, lat, lon);
  champlain_location_set_location (CHAMPLAIN_LOCATION (data->marker), lat, lon);
  return TRUE;
}


int
main (int argc, char *argv[])
{
  ClutterActor *actor, *marker, *stage;
  ChamplainMarkerLayer *layer;
  GpsCallbackData callback_data;

  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
    return 1;

  stage = clutter_stage_new ();
  clutter_actor_set_size (stage, 800, 600);
  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);

  /* Create the map view */
  actor = champlain_view_new ();
  clutter_actor_set_size (CLUTTER_ACTOR (actor), 800, 600);
  clutter_actor_add_child (stage, actor);

  /* Create the marker layer */
  layer = champlain_marker_layer_new_full (CHAMPLAIN_SELECTION_SINGLE);
  clutter_actor_show (CLUTTER_ACTOR (layer));
  champlain_view_add_layer (CHAMPLAIN_VIEW (actor), CHAMPLAIN_LAYER (layer));

  /* Create a marker */
  marker = create_marker ();
  champlain_marker_layer_add_marker (layer, CHAMPLAIN_MARKER (marker));

  /* Finish initialising the map view */
  g_object_set (G_OBJECT (actor), "zoom-level", 12,
      "kinetic-mode", TRUE, NULL);
  champlain_view_center_on (CHAMPLAIN_VIEW (actor), lat, lon);

  /* Create callback that updates the map periodically */
  callback_data.view = CHAMPLAIN_VIEW (actor);
  callback_data.marker = CHAMPLAIN_MARKER (marker);

  g_timeout_add (1000, (GSourceFunc) gps_callback, &callback_data);

  clutter_actor_show (stage);
  clutter_main ();

  return 0;
}