File: champlain-base-marker.c

package info (click to toggle)
libchamplain 0.4.6-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,680 kB
  • ctags: 2,295
  • sloc: ansic: 15,212; sh: 10,205; perl: 2,324; makefile: 647; python: 450; xml: 18; cs: 16
file content (398 lines) | stat: -rw-r--r-- 11,694 bytes parent folder | download
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * 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
 */

/**
 * SECTION:champlain-base-marker
 * @short_description: A base marker to identify points of interest on a map
 *
 * Base markers reprensent points of interest on a map. Base markers need to be
 * placed on a layer (a #ChamplainLayer). Layers have to be added to a
 * #champlainview for the base_markers to show on the map.
 *
 * A basemarker is nothing more than a regular #clutteractor. You can draw on
 * it what ever you want. Don't forget to set the anchor position in the base
 * marker using #clutter_actor_set_anchor_point. Set the base_markers position
 * on the map using #champlain_base_marker_set_position.
 *
 * champlain has a more evoluted type of markers with text and image support.
 * See #ChamplainMarker.
 */

#include "config.h"

#include "champlain-base-marker.h"

#include "champlain.h"
#include "champlain-defines.h"
#include "champlain-marshal.h"
#include "champlain-private.h"
#include "champlain-map.h"
#include "champlain-tile.h"
#include "champlain-zoom-level.h"

#include <clutter/clutter.h>
#include <glib.h>
#include <glib-object.h>
#include <cairo.h>
#include <math.h>

enum
{
  /* normal signals */
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_LONGITUDE,
  PROP_LATITUDE,
  PROP_HIGHLIGHTED,
};

//static guint champlain_base_marker_signals[LAST_SIGNAL] = { 0, };

G_DEFINE_TYPE (ChamplainBaseMarker, champlain_base_marker, CLUTTER_TYPE_GROUP);

#define CHAMPLAIN_BASE_MARKER_GET_PRIVATE(obj)    (G_TYPE_INSTANCE_GET_PRIVATE((obj), CHAMPLAIN_TYPE_BASE_MARKER, ChamplainBaseMarkerPrivate))

static void
champlain_base_marker_get_property (GObject *object,
                               guint prop_id,
                               GValue *value,
                               GParamSpec *pspec)
{
    ChamplainBaseMarker *base_marker = CHAMPLAIN_BASE_MARKER (object);
    ChamplainBaseMarkerPrivate *priv = CHAMPLAIN_BASE_MARKER_GET_PRIVATE (base_marker);

    switch (prop_id)
      {
        case PROP_LONGITUDE:
          g_value_set_double (value, priv->lon);
          break;
        case PROP_LATITUDE:
          g_value_set_double (value, priv->lat);
          break;
        case PROP_HIGHLIGHTED:
          g_value_set_boolean (value, priv->highlighted);
          break;
        default:
          G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      }
}

static void
champlain_base_marker_set_property (GObject *object,
                               guint prop_id,
                               const GValue *value,
                               GParamSpec *pspec)
{
    ChamplainBaseMarker *base_marker = CHAMPLAIN_BASE_MARKER (object);
    ChamplainBaseMarkerPrivate *priv = CHAMPLAIN_BASE_MARKER_GET_PRIVATE (base_marker);

    switch (prop_id)
    {
      case PROP_LONGITUDE:
        {
          gdouble lon = g_value_get_double (value);
          champlain_base_marker_set_position (base_marker, priv->lat, lon);
          break;
        }
      case PROP_LATITUDE:
        {
          gdouble lat = g_value_get_double (value);
          champlain_base_marker_set_position (base_marker, lat, priv->lon);
          break;
        }
      case PROP_HIGHLIGHTED:
        {
          gboolean bvalue = g_value_get_boolean (value);
          champlain_base_marker_set_highlighted (base_marker, bvalue);
          break;
        }
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
champlain_base_marker_finalize (GObject *object)
{
  //ChamplainBaseMarker *base_marker = CHAMPLAIN_BASE_MARKER (object);
  //ChamplainBaseMarkerPrivate *priv = CHAMPLAIN_BASE_MARKER_GET_PRIVATE (base_marker);

  G_OBJECT_CLASS (champlain_base_marker_parent_class)->finalize (object);
}

static void
champlain_base_marker_class_init (ChamplainBaseMarkerClass *champlainBaseMarkerClass)
{
  g_type_class_add_private (champlainBaseMarkerClass, sizeof (ChamplainBaseMarkerPrivate));

  GObjectClass *object_class = G_OBJECT_CLASS (champlainBaseMarkerClass);
  object_class->finalize = champlain_base_marker_finalize;
  object_class->get_property = champlain_base_marker_get_property;
  object_class->set_property = champlain_base_marker_set_property;

  /**
  * ChamplainBaseMarker:longitude:
  *
  * The longitude coordonate of the map
  *
  * Since: 0.4
  */
  g_object_class_install_property (object_class, PROP_LONGITUDE,
      g_param_spec_double ("longitude", "Longitude",
          "The longitude coordonate of the base_marker",
          -180.0f, 180.0f, 0.0f, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainBaseMarker:latitude:
  *
  * The latitude coordonate of the map
  *
  * Since: 0.4
  */
  g_object_class_install_property (object_class, PROP_LATITUDE,
      g_param_spec_double ("latitude", "Latitude",
          "The latitude coordonate of the base_marker",
          -90.0f, 90.0f, 0.0f, CHAMPLAIN_PARAM_READWRITE));

  /**
  * ChamplainBaseMarker:highlighted:
  *
  * The highlighted state of the marker
  *
  * Since: 0.4
  */
  g_object_class_install_property (object_class, PROP_HIGHLIGHTED,
      g_param_spec_boolean ("highlighted", "Highlighted",
          "The highlighted stated of the marker",
          FALSE, CHAMPLAIN_PARAM_READWRITE));
}

static void
champlain_base_marker_init (ChamplainBaseMarker *marker)
{
  ChamplainBaseMarkerPrivate *priv = CHAMPLAIN_BASE_MARKER_GET_PRIVATE (marker);
  marker->priv = priv;
  priv->lat = 0;
  priv->lon = 0;
  priv->highlighted = FALSE;
}


/**
 * champlain_base_marker_new:
 *
 * Returns: a new #ChamplainBaseMarker ready to be used as a #ClutterActor.
 *
 * Since: 0.4
 */
ClutterActor *
champlain_base_marker_new (void)
{
  ChamplainBaseMarker *base_marker;

  base_marker = CHAMPLAIN_BASE_MARKER (g_object_new (CHAMPLAIN_TYPE_BASE_MARKER, NULL));
  //ChamplainBaseMarkerPrivate *priv = CHAMPLAIN_BASE_MARKER_GET_PRIVATE (base_marker);

  return CLUTTER_ACTOR (base_marker);
}

/**
 * champlain_base_marker_set_position:
 * @marker: a #ChamplainBaseMarker
 * @latitude: the longitude to center the map at
 * @longitude: the longitude to center the map at
 *
 * Positions the base_marker on the map at the coordinates
 *
 * Since: 0.4
 */
void
champlain_base_marker_set_position (ChamplainBaseMarker *champlainBaseMarker,
    gdouble latitude,
    gdouble longitude)
{
  g_return_if_fail (CHAMPLAIN_IS_BASE_MARKER (champlainBaseMarker));

  ChamplainBaseMarkerPrivate *priv = CHAMPLAIN_BASE_MARKER_GET_PRIVATE (champlainBaseMarker);

  priv->lon = longitude;
  priv->lat = latitude;

  g_object_notify (G_OBJECT (champlainBaseMarker), "latitude");
  g_object_notify (G_OBJECT (champlainBaseMarker), "longitude");
}

/**
 * champlain_base_marker_set_highlighted:
 * @marker: a #ChamplainBaseMarker
 * @value: the highlighted state
 *
 * Sets the marker as highlighted or not. This will affect the "Selected" look
 * of the marker.
 *
 * Since: 0.4
 */
void
champlain_base_marker_set_highlighted (ChamplainBaseMarker *champlainBaseMarker,
    gboolean value)
{
  g_return_if_fail (CHAMPLAIN_IS_BASE_MARKER (champlainBaseMarker));

  ChamplainBaseMarkerPrivate *priv = CHAMPLAIN_BASE_MARKER_GET_PRIVATE (champlainBaseMarker);

  priv->highlighted = value;

  g_object_notify (G_OBJECT (champlainBaseMarker), "highlighted");
}

/**
 * champlain_base_marker_get_highlighted:
 * @marker: a #ChamplainBaseMarker
 *
 * Returns: the highlighted or not state of the marker.
 *
 * Since: 0.4
 */
gboolean
champlain_base_marker_get_highlighted (ChamplainBaseMarker *champlainBaseMarker)
{
  g_return_val_if_fail (CHAMPLAIN_IS_BASE_MARKER (champlainBaseMarker), FALSE);

  ChamplainBaseMarkerPrivate *priv = CHAMPLAIN_BASE_MARKER_GET_PRIVATE (champlainBaseMarker);

  return priv->highlighted;
}

/**
 * champlain_base_marker_animate_in:
 * @marker: The marker
 *
 * Animates the marker as if it were falling from the sky onto the map.
 *
 * Since: 0.4
 */
void
champlain_base_marker_animate_in (ChamplainBaseMarker *marker)
{
  champlain_base_marker_animate_in_with_delay (marker, 0);
}

/**
 * champlain_base_marker_animate_in_with_delay :
 * @marker: The marker
 * @delay: The delay in milliseconds
 *
 * Animates the marker as if it were falling from the sky onto the map after
 * delay.
 *
 * Since: 0.4
 */
void
champlain_base_marker_animate_in_with_delay (ChamplainBaseMarker *marker,
    guint delay)
{
  ClutterAnimation *animation;
  ClutterTimeline *timeline;
  gfloat y;

  g_return_if_fail (CHAMPLAIN_IS_BASE_MARKER (marker));

  clutter_actor_show (CLUTTER_ACTOR (marker));
  clutter_actor_set_opacity (CLUTTER_ACTOR (marker), 0);
  clutter_actor_set_scale (CLUTTER_ACTOR (marker), 1.5, 1.5);
  clutter_actor_get_position (CLUTTER_ACTOR (marker), NULL, &y);
  clutter_actor_move_by (CLUTTER_ACTOR (marker), 0, -100);

  timeline = clutter_timeline_new (1000);
  clutter_timeline_set_delay (timeline, delay);
  animation = clutter_actor_animate_with_timeline (CLUTTER_ACTOR (marker),
      CLUTTER_EASE_OUT_BOUNCE, timeline, "opacity", 255, "y", y,
      "scale-x", 1.0, "scale-y", 1.0, NULL);
  timeline = clutter_animation_get_timeline (animation);
}

/**
 * champlain_base_marker_animate_out:
 * @marker: The marker
 *
 * Animates the marker as if it were drawn through the sky.
 *
 * Since: 0.4
 */
void
champlain_base_marker_animate_out (ChamplainBaseMarker *marker)
{
  champlain_base_marker_animate_out_with_delay (marker, 0);
}

static gboolean
on_idle (ChamplainBaseMarker* marker)
{
  /* Notify the view that the position changed so that the marker's
   * position is reset, it has to happen on idle as Clutter seems to
   * set actors position after calling animation_completed */
  clutter_actor_hide (CLUTTER_ACTOR (marker));

  g_object_notify (G_OBJECT (marker), "latitude");
  g_object_notify (G_OBJECT (marker), "longitude");
  return FALSE;
}

static void
on_animation_completed (ClutterAnimation* animation,
    ChamplainBaseMarker *marker)
{
  g_idle_add ((GSourceFunc) on_idle, marker);
}

/**
 * champlain_base_marker_animate_out_with_delay :
 * @marker: The marker
 * @delay: The delay in milliseconds
 *
 * Animates the marker as if it were drawn through the sky after
 * delay.
 *
 * Since: 0.4
 */
void
champlain_base_marker_animate_out_with_delay (ChamplainBaseMarker *marker,
    guint delay)
{
  ClutterAnimation *animation;
  ClutterTimeline *timeline;
  gfloat y;

  g_return_if_fail (CHAMPLAIN_IS_BASE_MARKER (marker));

  clutter_actor_get_position (CLUTTER_ACTOR (marker), NULL, &y);
  clutter_actor_set_opacity (CLUTTER_ACTOR (marker), 200);

  timeline = clutter_timeline_new (750);
  clutter_timeline_set_delay (timeline, delay);
  animation = clutter_actor_animate_with_timeline (CLUTTER_ACTOR (marker),
      CLUTTER_EASE_IN_BACK, timeline, "opacity", 0, "y", y - 100,
      "scale-x", 2.0, "scale-y", 2.0, NULL);
  timeline = clutter_animation_get_timeline (animation);
  g_signal_connect (animation, "completed",
      G_CALLBACK (on_animation_completed), marker);
}