Contents of /trunk/src/osm-gps-map-osd-classic.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 75 - (show annotations)
Mon Aug 24 12:57:15 2009 UTC (14 years, 8 months ago) by harbaum
File MIME type: text/plain
File size: 13208 byte(s)
Double buffering for OSD
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */
2 /* vim:set et sw=4 ts=4 cino=t0,(0: */
3 /*
4 * Copyright (C) Till Harbaum 2009 <till@harbaum.org>
5 *
6 * osm-gps-map is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * osm-gps-map is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include <stdlib.h> // abs
22 #include <math.h> // M_PI
23
24 /* parameters that can be overwritten from the config file: */
25 /* OSM_GPS_MAP_OSD_DIAMETER */
26
27 #ifndef USE_CAIRO
28 #error "OSD control display lacks a non-cairo implementation!"
29 #endif
30
31 #include <cairo.h>
32
33 #include "osm-gps-map.h"
34 #include "osm-gps-map-osd-classic.h"
35
36 //the osd controls
37 typedef struct {
38 /* the offscreen representation of the OSD */
39 cairo_surface_t *overlay;
40
41 GdkColor bg, fg, da;
42
43 } osd_priv_t;
44
45 /* position and extent of bounding box */
46 #define OSD_X (10)
47 #define OSD_Y (10)
48
49 /* parameters of the direction shape */
50 #ifndef OSM_GPS_MAP_OSD_DIAMETER
51 #define D_RAD (30) // diameter of dpad
52 #else
53 #define D_RAD (OSM_GPS_MAP_OSD_DIAMETER)
54 #endif
55 #define D_TIP (4*D_RAD/5) // distance of arrow tip from dpad center
56 #define D_LEN (D_RAD/4) // length of arrow
57 #define D_WID (D_LEN) // width of arrow
58
59 /* parameters of the "zoom" pad */
60 #define Z_STEP (D_RAD/4) // distance between dpad and zoom
61 #define Z_RAD (D_RAD/2) // radius of "caps" of zoom bar
62
63 #ifdef OSD_SHADOW_ENABLE
64 /* shadow also depends on control size */
65 #define OSD_SHADOW (D_RAD/6)
66 #else
67 #define OSD_SHADOW (0)
68 #endif
69
70 /* total width and height of controls incl. shadow */
71 #define OSD_W (2*D_RAD + OSD_SHADOW)
72 #define OSD_H (2*D_RAD + Z_STEP + 2*Z_RAD + OSD_SHADOW)
73
74 #ifdef OSD_SHADOW_ENABLE
75 #define OSD_LBL_SHADOW (OSD_SHADOW/2)
76 #endif
77
78 #define Z_TOP (2 * D_RAD + Z_STEP)
79 #define Z_MID (Z_TOP + Z_RAD)
80 #define Z_BOT (Z_MID + Z_RAD)
81 #define Z_LEFT (Z_RAD)
82 #define Z_RIGHT (2 * D_RAD - Z_RAD)
83
84
85 /* create the cairo shape used for the zoom buttons */
86 static void
87 osm_gps_map_osd_zoom_shape(cairo_t *cr, gint x, gint y)
88 {
89 cairo_move_to (cr, x+Z_LEFT, y+Z_TOP);
90 cairo_line_to (cr, x+Z_RIGHT, y+Z_TOP);
91 cairo_arc (cr, x+Z_RIGHT, y+Z_MID, Z_RAD, -M_PI/2, M_PI/2);
92 cairo_line_to (cr, x+Z_LEFT, y+Z_BOT);
93 cairo_arc (cr, x+Z_LEFT, y+Z_MID, Z_RAD, M_PI/2, -M_PI/2);
94 }
95
96 /* create the cairo shape used for the dpad */
97 static void
98 osm_gps_map_osd_dpad_shape(cairo_t *cr, gint x, gint y)
99 {
100 cairo_arc (cr, x+D_RAD, y+D_RAD, D_RAD, 0, 2 * M_PI);
101 }
102
103 static gboolean
104 osm_gps_map_in_circle(gint x, gint y, gint cx, gint cy, gint rad)
105 {
106 return( pow(cx - x, 2) + pow(cy - y, 2) < rad * rad);
107 }
108
109 /* check whether x/y is within the dpad */
110 static osd_button_t
111 osm_gps_map_osd_check_dpad(gint x, gint y)
112 {
113 /* within entire dpad circle */
114 if( osm_gps_map_in_circle(x, y, OSD_X + D_RAD, OSD_Y + D_RAD, D_RAD))
115 {
116 /* convert into position relative to dpads centre */
117 x -= (OSD_X + D_RAD);
118 y -= (OSD_Y + D_RAD);
119
120 /* check for dpad center goes here! */
121 if( osm_gps_map_in_circle(x, y, 0, 0, D_RAD/3))
122 return OSD_GPS;
123
124 if( y < 0 && abs(x) < abs(y))
125 return OSD_UP;
126
127 if( y > 0 && abs(x) < abs(y))
128 return OSD_DOWN;
129
130 if( x < 0 && abs(y) < abs(x))
131 return OSD_LEFT;
132
133 if( x > 0 && abs(y) < abs(x))
134 return OSD_RIGHT;
135
136 return OSD_BG;
137 }
138 return OSD_NONE;
139 }
140
141 /* check whether x/y is within the zoom pads */
142 static osd_button_t
143 osm_gps_map_osd_check_zoom(gint x, gint y) {
144 if( x > OSD_X && x < (OSD_X + OSD_W) && y > Z_TOP && y < (OSD_Y+Z_BOT)) {
145
146 /* within circle around (-) label */
147 if( osm_gps_map_in_circle(x, y, OSD_X + Z_LEFT, OSD_Y + Z_MID, Z_RAD))
148 return OSD_OUT;
149
150 /* between center of (-) button and center of entire zoom control area */
151 if(x > OSD_LEFT && x < OSD_X + D_RAD)
152 return OSD_OUT;
153
154 /* within circle around (+) label */
155 if( osm_gps_map_in_circle(x, y, OSD_X + Z_RIGHT, OSD_Y + Z_MID, Z_RAD))
156 return OSD_IN;
157
158 /* between center of (+) button and center of entire zoom control area */
159 if(x < OSD_RIGHT && x > OSD_X + D_RAD)
160 return OSD_IN;
161 }
162
163 return OSD_NONE;
164 }
165
166 static osd_button_t
167 osm_gps_map_osd_check(gint x, gint y) {
168 osd_button_t but = OSD_NONE;
169
170 /* first do a rough test for the OSD area. */
171 /* this is just to avoid an unnecessary detailed test */
172 if(x > OSD_X && x < OSD_X + OSD_W &&
173 y > OSD_Y && y < OSD_Y + OSD_H) {
174 but = osm_gps_map_osd_check_dpad(x, y);
175
176 if(but == OSD_NONE)
177 but = osm_gps_map_osd_check_zoom(x, y);
178 }
179
180 return but;
181 }
182
183 #ifdef OSD_SHADOW_ENABLE
184 static void
185 osm_gps_map_osd_shape_shadow(cairo_t *cr) {
186 cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
187 cairo_fill (cr);
188 cairo_stroke (cr);
189 }
190 #endif
191
192 #ifndef OSD_COLOR
193 /* if no color has been specified we just use the gdks default colors */
194 static void
195 osm_gps_map_osd_shape(cairo_t *cr, GdkColor *bg, GdkColor *fg) {
196 gdk_cairo_set_source_color(cr, bg);
197 cairo_fill_preserve (cr);
198 gdk_cairo_set_source_color(cr, fg);
199 cairo_set_line_width (cr, 1);
200 cairo_stroke (cr);
201 }
202 #else
203 static void
204 osm_gps_map_osd_shape(cairo_t *cr) {
205 cairo_set_source_rgb (cr, OSD_COLOR_BG);
206 cairo_fill_preserve (cr);
207 cairo_set_source_rgb (cr, OSD_COLOR);
208 cairo_set_line_width (cr, 1);
209 cairo_stroke (cr);
210 }
211 #endif
212
213 static void
214 osm_gps_map_osd_dpad_labels(cairo_t *cr, gint x, gint y) {
215 /* move reference to dpad center */
216 x += D_RAD;
217 y += D_RAD;
218
219 const static gint offset[][3][2] = {
220 /* left arrow/triangle */
221 { { -D_TIP+D_LEN, -D_WID }, { -D_LEN, D_WID }, { +D_LEN, D_WID } },
222 /* right arrow/triangle */
223 { { +D_TIP-D_LEN, -D_WID }, { +D_LEN, D_WID }, { -D_LEN, D_WID } },
224 /* top arrow/triangle */
225 { { -D_WID, -D_TIP+D_LEN }, { D_WID, -D_LEN }, { D_WID, +D_LEN } },
226 /* bottom arrow/triangle */
227 { { -D_WID, +D_TIP-D_LEN }, { D_WID, +D_LEN }, { D_WID, -D_LEN } }
228 };
229
230 int i;
231 for(i=0;i<4;i++) {
232 cairo_move_to (cr, x + offset[i][0][0], y + offset[i][0][1]);
233 cairo_rel_line_to (cr, offset[i][1][0], offset[i][1][1]);
234 cairo_rel_line_to (cr, offset[i][2][0], offset[i][2][1]);
235 }
236 }
237
238 /* draw the sattelite dish icon in the center of the dpad */
239 #define GPS_V0 (D_RAD/8)
240 #define GPS_V1 (D_RAD/10)
241 #define GPS_V2 (D_RAD/5)
242
243 /* draw a satellite receiver dish */
244 static void
245 osm_gps_map_osd_dpad_gps(cairo_t *cr, gint x, gint y) {
246 /* move reference to dpad center */
247 x += D_RAD;
248 y += D_RAD + GPS_V0;
249
250 cairo_move_to (cr, x-GPS_V0, y+GPS_V0);
251 cairo_rel_line_to (cr, +GPS_V0, -GPS_V0);
252 cairo_rel_line_to (cr, +GPS_V0, +GPS_V0);
253 cairo_close_path (cr);
254
255 cairo_move_to (cr, x+GPS_V1-GPS_V2, y-2*GPS_V2);
256 cairo_curve_to (cr, x-GPS_V2, y, x+GPS_V1, y+GPS_V1, x+GPS_V1+GPS_V2, y);
257 cairo_close_path (cr);
258
259 x += GPS_V1;
260 cairo_move_to (cr, x, y-GPS_V2);
261 cairo_rel_line_to (cr, +GPS_V1, -GPS_V1);
262 }
263
264 #define Z_LEN (2*Z_RAD/3)
265
266 static void
267 osm_gps_map_osd_zoom_labels(cairo_t *cr, gint x, gint y) {
268 cairo_move_to (cr, x + Z_LEFT - Z_LEN, y + Z_MID);
269 cairo_line_to (cr, x + Z_LEFT + Z_LEN, y + Z_MID);
270
271 cairo_move_to (cr, x + Z_RIGHT, y + Z_MID - Z_LEN);
272 cairo_line_to (cr, x + Z_RIGHT, y + Z_MID + Z_LEN);
273 cairo_move_to (cr, x + Z_RIGHT - Z_LEN, y + Z_MID);
274 cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
275 }
276
277 #ifndef OSD_COLOR
278 /* if no color has been specified we just use the gdks default colors */
279 static void
280 osm_gps_map_osd_labels(cairo_t *cr, gint width, gboolean enabled,
281 GdkColor *fg, GdkColor *disabled) {
282 if(enabled) gdk_cairo_set_source_color(cr, fg);
283 else gdk_cairo_set_source_color(cr, disabled);
284 cairo_set_line_width (cr, width);
285 cairo_stroke (cr);
286 }
287 #else
288 static void
289 osm_gps_map_osd_labels(cairo_t *cr, gint width, gboolean enabled) {
290 if(enabled) cairo_set_source_rgb (cr, OSD_COLOR);
291 else cairo_set_source_rgb (cr, OSD_COLOR_DISABLED);
292 cairo_set_line_width (cr, width);
293 cairo_stroke (cr);
294 }
295 #endif
296
297 #ifdef OSD_SHADOW_ENABLE
298 static void
299 osm_gps_map_osd_labels_shadow(cairo_t *cr, gint width, gboolean enabled) {
300 cairo_set_source_rgba (cr, 0, 0, 0, enabled?0.3:0.15);
301 cairo_set_line_width (cr, width);
302 cairo_stroke (cr);
303 }
304 #endif
305
306 static void
307 osm_gps_map_osd_render(osm_gps_map_osd_t *osd) {
308 osd_priv_t *priv = (osd_priv_t*)osd->priv;
309
310 /* first fill with transparency */
311 cairo_t *cr = cairo_create(priv->overlay);
312 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
313 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
314 cairo_paint(cr);
315
316 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
317
318 /* --------- draw zoom and dpad shape shadow ----------- */
319 gint x = 0, y = 0;
320
321 #ifdef OSD_SHADOW_ENABLE
322 osm_gps_map_osd_zoom_shape(cr, x + OSD_SHADOW, y + OSD_SHADOW);
323 osm_gps_map_osd_shape_shadow(cr);
324 osm_gps_map_osd_dpad_shape(cr, x + OSD_SHADOW, y + OSD_SHADOW);
325 osm_gps_map_osd_shape_shadow(cr);
326 #endif
327
328 /* --------- draw zoom and dpad shape ----------- */
329
330 osm_gps_map_osd_zoom_shape(cr, x, y);
331 #ifndef OSD_COLOR
332 osm_gps_map_osd_shape(cr, &priv->bg, &priv->fg);
333 #else
334 osm_gps_map_osd_shape(cr);
335 #endif
336 osm_gps_map_osd_dpad_shape(cr, x, y);
337 #ifndef OSD_COLOR
338 osm_gps_map_osd_shape(cr, &priv->bg, &priv->fg);
339 #else
340 osm_gps_map_osd_shape(cr);
341 #endif
342
343 /* --------- draw zoom and dpad labels --------- */
344
345 #ifdef OSD_SHADOW_ENABLE
346 osm_gps_map_osd_zoom_labels(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);
347 osm_gps_map_osd_dpad_labels(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);
348 osm_gps_map_osd_labels_shadow(cr, Z_RAD/3, TRUE);
349 osm_gps_map_osd_dpad_gps(cr, x + OSD_LBL_SHADOW, y + OSD_LBL_SHADOW);
350 osm_gps_map_osd_labels_shadow(cr, Z_RAD/6, osd->cb != NULL);
351 #endif
352
353 osm_gps_map_osd_zoom_labels(cr, x, y);
354 osm_gps_map_osd_dpad_labels(cr, x, y);
355 #ifndef OSD_COLOR
356 osm_gps_map_osd_labels(cr, Z_RAD/3, TRUE, &priv->fg, &priv->da);
357 #else
358 osm_gps_map_osd_labels(cr, Z_RAD/3, TRUE);
359 #endif
360 osm_gps_map_osd_dpad_gps(cr, x, y);
361 #ifndef OSD_COLOR
362 osm_gps_map_osd_labels(cr, Z_RAD/6, osd->cb != NULL, &priv->fg, &priv->da);
363 #else
364 osm_gps_map_osd_labels(cr, Z_RAD/6, osd->cb != NULL);
365 #endif
366
367 cairo_destroy(cr);
368 }
369
370 static void
371 osm_gps_map_osd_draw(osm_gps_map_osd_t *osd, GdkDrawable *drawable)
372 {
373 osd_priv_t *priv = (osd_priv_t*)osd->priv;
374
375 /* OSD itself uses some off-screen rendering, so check if the */
376 /* offscreen buffer is present and create it if not */
377 if(!priv->overlay) {
378 /* create overlay ... */
379 priv->overlay =
380 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, OSD_W, OSD_H);
381 /* ... and render it */
382 osm_gps_map_osd_render(osd);
383 }
384
385 // now draw this onto the original context
386 cairo_t *cr = gdk_cairo_create(drawable);
387 cairo_set_source_surface(cr, priv->overlay, OSD_X, OSD_Y);
388 cairo_paint(cr);
389 cairo_destroy(cr);
390 }
391
392 static void
393 osm_gps_map_osd_free(osm_gps_map_osd_t *osd)
394 {
395 osd_priv_t *priv = (osd_priv_t *)(osd->priv);
396
397 if (priv->overlay)
398 cairo_surface_destroy(priv->overlay);
399
400 g_free(priv);
401 }
402
403 static osm_gps_map_osd_t osd_classic = {
404 .draw = osm_gps_map_osd_draw,
405 .check = osm_gps_map_osd_check,
406 .render = osm_gps_map_osd_render,
407 .free = osm_gps_map_osd_free,
408
409 .cb = NULL,
410 .data = NULL,
411
412 .priv = NULL
413 };
414
415 /* this is the only function that's externally visible */
416 void
417 osm_gps_map_osd_classic_init(OsmGpsMap *map)
418 {
419 osd_priv_t *priv = osd_classic.priv = g_new0(osd_priv_t, 1);
420
421 osd_classic.priv = priv;
422
423 /* extract style info from the widget */
424 priv->bg = GTK_WIDGET(map)->style->bg[GTK_STATE_NORMAL];
425 priv->fg = GTK_WIDGET(map)->style->fg[GTK_STATE_NORMAL];
426 priv->da = GTK_WIDGET(map)->style->fg[GTK_STATE_INSENSITIVE];
427
428 osm_gps_map_register_osd(map, &osd_classic);
429 }
430
431
432 /* below are osd specific functions which aren't used by osm-gps-map */
433 /* but instead are to be used by the main application */
434 void osm_gps_map_osd_enable_gps (OsmGpsMap *map, OsmGpsMapOsdCallback cb, gpointer data) {
435 osm_gps_map_osd_t *osd = osm_gps_map_osd_get(map);
436
437 g_return_if_fail (osd);
438
439 osd->cb = cb;
440 osd->data = data;
441
442 /* this may have changed the state of the gps button */
443 /* we thus re-render the overlay */
444 osd->render(osd);
445
446 osm_gps_map_redraw(map);
447 }