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
|
/*
* 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 Library General Public
* License as published by the Free Software Foundation; either
* version 2 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "champlain_defines.h"
#include "champlainmarker.h"
#include "champlain_private.h"
#include "champlain.h"
#include "champlain-marshal.h"
#include "map.h"
#include "tile.h"
#include "zoomlevel.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_ANCHOR_X,
PROP_ANCHOR_Y,
};
static guint champlain_marker_signals[LAST_SIGNAL] = { 0, };
G_DEFINE_TYPE (ChamplainMarker, champlain_marker, CLUTTER_TYPE_GROUP);
static void
champlain_marker_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
ChamplainMarker *marker = CHAMPLAIN_MARKER(object);
ChamplainMarkerPrivate *priv = CHAMPLAIN_MARKER_GET_PRIVATE (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;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void
champlain_marker_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
ChamplainMarker *marker = CHAMPLAIN_MARKER(object);
ChamplainMarkerPrivate *priv = CHAMPLAIN_MARKER_GET_PRIVATE (marker);
switch(prop_id)
{
case PROP_LONGITUDE:
{
gdouble lon = g_value_get_double(value);
champlain_marker_set_position(marker, lon, priv->lat);
break;
}
case PROP_LATITUDE:
{
gdouble lat = g_value_get_double(value);
champlain_marker_set_position(marker, priv->lon, lat);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void
champlain_marker_finalize (GObject *object)
{
ChamplainMarker *marker = CHAMPLAIN_MARKER (object);
ChamplainMarkerPrivate *priv = CHAMPLAIN_MARKER_GET_PRIVATE (marker);
G_OBJECT_CLASS (champlain_marker_parent_class)->finalize (object);
}
static void
champlain_marker_class_init (ChamplainMarkerClass *champlainMarkerClass)
{
g_type_class_add_private (champlainMarkerClass, sizeof (ChamplainMarkerPrivate));
GObjectClass *object_class = G_OBJECT_CLASS (champlainMarkerClass);
object_class->finalize = champlain_marker_finalize;
object_class->get_property = champlain_marker_get_property;
object_class->set_property = champlain_marker_set_property;
/**
* ChamplainMarker:longitude:
*
* The longitude coordonate of the map
*
* Since: 0.2
*/
g_object_class_install_property(object_class, PROP_LONGITUDE,
g_param_spec_double("longitude",
"Longitude",
"The longitude coordonate of the marker",
-180.0f,
180.0f,
0.0f,
CHAMPLAIN_PARAM_READWRITE));
/**
* ChamplainMarker:latitude:
*
* The latitude coordonate of the map
*
* Since: 0.2
*/
g_object_class_install_property(object_class, PROP_LATITUDE,
g_param_spec_double("latitude",
"Latitude",
"The latitude coordonate of the marker",
-90.0f,
90.0f,
0.0f,
CHAMPLAIN_PARAM_READWRITE));
}
static void
champlain_marker_init (ChamplainMarker *champlainMarker)
{
ChamplainMarkerPrivate *priv = CHAMPLAIN_MARKER_GET_PRIVATE (champlainMarker);
priv->anchor.x = 0;
priv->anchor.y = 0;
}
/**
* champlain_marker_new:
*
* Returns a new #ChamplainMarker ready to be used as a #ClutterActor.
*
* Since: 0.2
*/
ClutterActor *
champlain_marker_new ()
{
ChamplainMarker *marker;
marker = CHAMPLAIN_MARKER (g_object_new (CHAMPLAIN_TYPE_MARKER, NULL));
ChamplainMarkerPrivate *priv = CHAMPLAIN_MARKER_GET_PRIVATE (marker);
return CLUTTER_ACTOR (marker);
}
/**
* champlain_marker_set_position:
* @marker: a #ChamplainMarker
* @longitude: the longitude to center the map at
* @latitude: the longitude to center the map at
*
* Positions the marker on the map at the coordinates
*
* Since: 0.2
*/
void
champlain_marker_set_position (ChamplainMarker *champlainMarker, gdouble latitude, gdouble longitude)
{
g_return_if_fail(CHAMPLAIN_IS_MARKER(champlainMarker));
ChamplainMarkerPrivate *priv = CHAMPLAIN_MARKER_GET_PRIVATE (champlainMarker);
priv->lon = longitude;
priv->lat = latitude;
g_object_notify(G_OBJECT(champlainMarker), "latitude");
g_object_notify(G_OBJECT(champlainMarker), "longitude");
}
/**
* champlain_marker_new_with_label:
* @label: the text of the label
* @font: the font to use to draw the text, for example "Courrier Bold 11", can be NULL
* @text_color: a #ClutterColor, the color of the text, can be NULL
* @marker_color: a #ClutterColor, the color of the marker, can be NULL
*
* Returns a new #ChamplainMarker with a drawn marker containing the given text.
*
* Since: 0.2
*/
ClutterActor *
champlain_marker_new_with_label (const gchar *label,
const gchar *font,
ClutterColor *text_color,
ClutterColor *marker_color)
{
ChamplainMarker *champlainMarker = CHAMPLAIN_MARKER(champlain_marker_new ());
ChamplainMarkerPrivate *priv = CHAMPLAIN_MARKER_GET_PRIVATE (champlainMarker);
ClutterColor default_text_color = { 0x22, 022, 0x22, 0xFF },
default_marker_color = { 0x2A, 0xB1, 0x26, 0xEE },
darker_color;
ClutterActor *actor, *bg;
cairo_t *cr;
guint text_width, text_height, point;
const gint padding = 5;
if (!font)
font = "Sans 11";
if (!text_color)
text_color = &default_text_color;
if (!marker_color)
marker_color = &default_marker_color;
actor = clutter_label_new_with_text (font, label);
clutter_actor_set_position (actor, padding, padding / 2.0);
text_width = clutter_actor_get_width (actor) + 2 * padding;
text_height = clutter_actor_get_height (actor)+ padding;
clutter_label_set_color (CLUTTER_LABEL(actor), text_color);
clutter_container_add_actor (CLUTTER_CONTAINER(champlainMarker), actor);
point = (text_height + 2 * padding) / 4.0;
bg = clutter_cairo_new (text_width, text_height + point);
cr = clutter_cairo_create (bg);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_move_to (cr, 0, 0);
cairo_line_to (cr, text_width, 0);
cairo_line_to (cr, text_width, text_height);
cairo_line_to (cr, point, text_height);
cairo_line_to (cr, 0, text_height + point);
cairo_close_path (cr);
cairo_set_line_width (cr, 1.0);
cairo_set_source_rgba (cr,
marker_color->red / 255.0,
marker_color->green / 255.0,
marker_color->blue / 255.0,
marker_color->alpha / 255.0);
cairo_fill_preserve (cr);
clutter_color_darken (marker_color, &darker_color);
cairo_set_source_rgba (cr,
darker_color.red / 255.0,
darker_color.green / 255.0,
darker_color.blue / 255.0,
darker_color.alpha / 255.0);
cairo_stroke (cr);
cairo_destroy (cr);
clutter_container_add_actor (CLUTTER_CONTAINER(champlainMarker), bg);
clutter_actor_raise (actor, bg);
clutter_actor_set_anchor_point (CLUTTER_ACTOR(champlainMarker), 0, text_height + point);
return champlainMarker;
}
|