/[zanavi_public1]/navit/navit/layout.c
ZANavi

Contents of /navit/navit/layout.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (11 years, 1 month ago) by zoff99
File MIME type: text/plain
File size: 14071 byte(s)
new map version, lots of fixes and experimental new features
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2009 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 #include <glib.h>
40 #include <string.h>
41 #include "item.h"
42 #include "attr.h"
43 #include "layout.h"
44 #include "coord.h"
45 #include "debug.h"
46
47 struct layout * layout_new(struct attr *parent, struct attr **attrs)
48 {
49 struct layout *l;
50 struct color def_color = { COLOR_BACKGROUND_ };
51 struct attr *name_attr, *color_attr, *order_delta_attr, *font_attr, *day_attr, *night_attr, *active_attr;
52
53 if (!(name_attr = attr_search(attrs, NULL, attr_name)))
54 {
55 return NULL;
56 } l = g_new0(struct layout, 1);
57 l->name = g_strdup(name_attr->u.str);
58 if ((font_attr = attr_search(attrs, NULL, attr_font)))
59 {
60 l->font = g_strdup(font_attr->u.str);
61 }
62 if ((day_attr = attr_search(attrs, NULL, attr_daylayout)))
63 {
64 l->dayname = g_strdup(day_attr->u.str);
65 }
66 if ((night_attr = attr_search(attrs, NULL, attr_nightlayout)))
67 {
68 l->nightname = g_strdup(night_attr->u.str);
69 }
70 if ((color_attr = attr_search(attrs, NULL, attr_color)))
71 {
72 l->color = *color_attr->u.color;
73 }
74 else
75 {
76 l->color = def_color;
77 }
78 if ((order_delta_attr = attr_search(attrs, NULL, attr_order_delta)))
79 {
80 l->order_delta = order_delta_attr->u.num;
81 }
82 if ((active_attr = attr_search(attrs, NULL, attr_active)))
83 {
84 l->active = active_attr->u.num;
85 }
86 return l;
87 }
88
89 struct attr_iter
90 {
91 GList *last;
92 };
93
94 struct attr_iter *
95 layout_attr_iter_new(void)
96 {
97 return g_new0(struct attr_iter, 1);
98 }
99
100 void layout_attr_iter_destroy(struct attr_iter *iter)
101 {
102 g_free(iter);
103 }
104
105 int layout_get_attr(struct layout *layout, enum attr_type type, struct attr *attr, struct attr_iter *iter)
106 {
107 GList *cursor, *layer;
108 attr->type = type;
109 switch (type)
110 {
111 case attr_cursor:
112 cursor = layout->cursors;
113 while (cursor)
114 {
115 if (!iter || iter->last == g_list_previous(cursor))
116 {
117 attr->u.cursor = cursor->data;
118 if (iter)
119 iter->last = cursor;
120 return 1;
121 }
122 cursor = g_list_next(cursor);
123 }
124 break;
125 case attr_layer:
126 layer = layout->layers;
127 while (layer)
128 {
129 if (!iter || iter->last == g_list_previous(layer))
130 {
131 attr->u.layer = layer->data;
132 if (iter)
133 iter->last = layer;
134 return 1;
135 }
136 layer = g_list_next(layer);
137 }
138 break;
139 case attr_active:
140 attr->u.num = layout->active;
141 break;
142 default:
143 break;
144 }
145 return 0;
146 }
147
148 int layout_add_attr(struct layout *layout, struct attr *attr)
149 {
150 switch (attr->type)
151 {
152 case attr_cursor:
153 layout->cursors = g_list_append(layout->cursors, attr->u.cursor);
154 return 1;
155 case attr_layer:
156 layout->layers = g_list_append(layout->layers, attr->u.layer);
157 return 1;
158 default:
159 return 0;
160 }
161 }
162
163 /**
164 * Searchs the layout for a cursor with the given name.
165 *
166 * @param layout The layout
167 * @param name The name
168 * @returns A pointer to cursor with the given name or the name default or NULL.
169 * @author Ralph Sennhauser (10/2009)
170 */
171 struct cursor *
172 layout_get_cursor(struct layout *this_, char *name)
173 {
174 GList *c;
175 struct cursor *d = NULL;
176
177 c = g_list_first(this_->cursors);
178 while (c)
179 {
180 if (!strcmp(((struct cursor *) c->data)->name, name))
181 return c->data;
182 if (!strcmp(((struct cursor *) c->data)->name, "default"))
183 d = c->data;
184 c = g_list_next(c);
185 }
186 return d;
187 }
188
189 struct cursor *
190 cursor_new(struct attr *parent, struct attr **attrs)
191 {
192 struct attr *w, *h, *name, *interval, *sequence_range;
193 struct cursor *this;
194
195 w = attr_search(attrs, NULL, attr_w);
196 h = attr_search(attrs, NULL, attr_h);
197 if (!w || !h)
198 return NULL;
199
200 this=g_new0(struct cursor,1);
201 this->w = w->u.num;
202 this->h = h->u.num;
203 name = attr_search(attrs, NULL, attr_name);
204 if (name)
205 this->name = g_strdup(name->u.str);
206 else
207 this->name = g_strdup("default");
208 interval = attr_search(attrs, NULL, attr_interval);
209 if (interval)
210 this->interval = interval->u.num;
211 sequence_range = attr_search(attrs, NULL, attr_sequence_range);
212 if (sequence_range)
213 {
214 struct range *r=g_new0(struct range,1);
215 r->min = sequence_range->u.range.min;
216 r->max = sequence_range->u.range.max;
217 this->sequence_range = r;
218 }
219 else
220 {
221 this->sequence_range = NULL;
222 }
223 //dbg(2, "ret=%p\n", this);
224 return this;
225 }
226
227 void cursor_destroy(struct cursor *this_)
228 {
229 if (this_->sequence_range)
230 g_free(this_->sequence_range);
231 if (this_->name)
232 {
233 g_free(this_->name);
234 }
235 g_free(this_);
236 }
237
238 int cursor_add_attr(struct cursor *this_, struct attr *attr)
239 {
240 switch (attr->type)
241 {
242 case attr_itemgra:
243 this_->attrs = attr_generic_add_attr(this_->attrs, attr);
244 return 1;
245 default:
246 break;
247 }
248 return 0;
249 }
250
251 static int layer_set_attr_do(struct layer *l, struct attr *attr, int init)
252 {
253 switch (attr->type)
254 {
255 case attr_active:
256 l->active = attr->u.num;
257 return 1;
258 case attr_details:
259 l->details = attr->u.num;
260 return 1;
261 case attr_name:
262 g_free(l->name);
263 l->name = g_strdup(attr->u.str);
264 return 1;
265 default:
266 return 0;
267 }
268 }
269
270 struct layer * layer_new(struct attr *parent, struct attr **attrs)
271 {
272 struct layer *l;
273
274 l = g_new0(struct layer, 1);
275 l->active = 1;
276 for (; *attrs; attrs++)
277 {
278 layer_set_attr_do(l, *attrs, 1);
279 }
280 return l;
281 }
282
283 int layer_get_attr(struct layer *layer, enum attr_type type, struct attr *attr, struct attr_iter *iter)
284 {
285 attr->type = type;
286 switch (type)
287 {
288 case attr_active:
289 attr->u.num = layer->active;
290 return 1;
291 case attr_details:
292 attr->u.num = layer->details;
293 return 1;
294 case attr_name:
295 if (layer->name)
296 {
297 attr->u.str = layer->name;
298 return 1;
299 }
300 break;
301 default:
302 return 0;
303 }
304 return 0;
305 }
306
307 int layer_add_attr(struct layer *layer, struct attr *attr)
308 {
309 switch (attr->type)
310 {
311 case attr_itemgra:
312 layer->itemgras = g_list_append(layer->itemgras, attr->u.itemgra);
313 return 1;
314 default:
315 return 0;
316 }
317 }
318
319 int layer_set_attr(struct layer *layer, struct attr *attr)
320 {
321 return layer_set_attr_do(layer, attr, 0);
322 }
323
324 struct itemgra * itemgra_new(struct attr *parent, struct attr **attrs)
325 {
326
327 //dbg(0,"EEnter\n");
328
329 struct itemgra *itm;
330 struct attr *order, *item_types, *speed_range, *angle_range, *sequence_range;
331 enum item_type *type;
332 struct range defrange;
333
334 itm = g_new0(struct itemgra, 1);
335 order = attr_search(attrs, NULL, attr_order);
336 item_types = attr_search(attrs, NULL, attr_item_types);
337 speed_range = attr_search(attrs, NULL, attr_speed_range);
338 angle_range = attr_search(attrs, NULL, attr_angle_range);
339 sequence_range = attr_search(attrs, NULL, attr_sequence_range);
340 defrange.min = 0;
341 defrange.max = 32767;
342 if (order)
343 itm->order = order->u.range;
344 else
345 itm->order = defrange;
346 if (speed_range)
347 itm->speed_range = speed_range->u.range;
348 else
349 itm->speed_range = defrange;
350 if (angle_range)
351 itm->angle_range = angle_range->u.range;
352 else
353 itm->angle_range = defrange;
354 if (sequence_range)
355 itm->sequence_range = sequence_range->u.range;
356 else
357 itm->sequence_range = defrange;
358 if (item_types)
359 {
360 type = item_types->u.item_types;
361 while (type && *type != type_none)
362 {
363 itm->type = g_list_append(itm->type, GINT_TO_POINTER(*type));
364 type++;
365 }
366 }
367
368 //dbg(0,"return\n");
369
370 return itm;
371 }
372 int itemgra_add_attr(struct itemgra *itemgra, struct attr *attr)
373 {
374 switch (attr->type)
375 {
376 case attr_polygon:
377 case attr_polyline:
378 case attr_circle:
379 case attr_text:
380 case attr_icon:
381 case attr_image:
382 case attr_arrows:
383 itemgra->elements = g_list_append(itemgra->elements, attr->u.element);
384 return 1;
385 default:
386 dbg(0, "unknown: %s\n", attr_to_name(attr->type));
387 return 0;
388 }
389 }
390
391 static void element_set_color(struct element *e, struct attr **attrs)
392 {
393 struct attr *color;
394 color = attr_search(attrs, NULL, attr_color);
395 if (color)
396 e->color = *color->u.color;
397 }
398
399 static void element_set_background_color(struct color *c, struct attr **attrs)
400 {
401 struct attr *color;
402 color = attr_search(attrs, NULL, attr_background_color);
403 if (color)
404 *c = *color->u.color;
405 }
406
407 static void element_set_text_size(struct element *e, struct attr **attrs)
408 {
409 struct attr *text_size;
410 text_size = attr_search(attrs, NULL, attr_text_size);
411 if (text_size)
412 e->text_size = text_size->u.num;
413 }
414
415 static void element_set_polyline_width(struct element *e, struct attr **attrs)
416 {
417 struct attr *width;
418 width = attr_search(attrs, NULL, attr_width);
419 if (width)
420 e->u.polyline.width = width->u.num;
421 }
422
423 static void element_set_polyline_directed(struct element *e, struct attr **attrs)
424 {
425 struct attr *directed;
426 directed = attr_search(attrs, NULL, attr_directed);
427 if (directed)
428 e->u.polyline.directed = directed->u.num;
429 }
430
431 static void element_set_polyline_dash(struct element *e, struct attr **attrs)
432 {
433 struct attr *dash;
434 int i;
435
436 dash = attr_search(attrs, NULL, attr_dash);
437 if (dash)
438 {
439 /*
440 for (i = 0; i < 4; i++)
441 {
442 if (!dash->u.dash[i])
443 {
444 break;
445 }
446 dbg(0,"1 i=%d d=%d\n", i, dash->u.dash[i]);
447 //e->u.polyline.dash_table[i] = atoi(dash->u.dash[i]);
448 e->u.polyline.dash_table[i] = dash->u.dash[i];
449 }
450 */
451 i = 0;
452 if (!dash->u.dash[i])
453 {
454 }
455 else
456 {
457 e->u.polyline.dash_table[i] = dash->u.dash[i];
458 //dbg(0,"1 i=%d d=%d\n", i, dash->u.dash[i]);
459 e->u.polyline.dash_num = (i + 1);
460 }
461 }
462 }
463
464 static void element_set_polyline_offset(struct element *e, struct attr **attrs)
465 {
466 struct attr *offset;
467 offset = attr_search(attrs, NULL, attr_offset);
468 if (offset)
469 {
470 e->u.polyline.offset = offset->u.num;
471 }
472 }
473
474 static void element_set_circle_width(struct element *e, struct attr **attrs)
475 {
476 struct attr *width;
477 width = attr_search(attrs, NULL, attr_width);
478 if (width)
479 e->u.circle.width = width->u.num;
480 }
481
482 static void element_set_circle_radius(struct element *e, struct attr **attrs)
483 {
484 struct attr *radius;
485 radius = attr_search(attrs, NULL, attr_radius);
486 if (radius)
487 e->u.circle.radius = radius->u.num;
488 }
489
490 struct polygon *
491 polygon_new(struct attr *parent, struct attr **attrs)
492 {
493 struct element *e;
494 e = g_new0(struct element, 1);
495 e->type = element_polygon;
496 element_set_color(e, attrs);
497
498 return (struct polygon *) e;
499 }
500
501 struct polyline *
502 polyline_new(struct attr *parent, struct attr **attrs)
503 {
504 struct element *e;
505
506 e = g_new0(struct element, 1);
507 e->type = element_polyline;
508 element_set_color(e, attrs);
509 element_set_polyline_width(e, attrs);
510 element_set_polyline_directed(e, attrs);
511 element_set_polyline_dash(e, attrs);
512 element_set_polyline_offset(e, attrs);
513 return (struct polyline *) e;
514 }
515
516 struct circle *
517 circle_new(struct attr *parent, struct attr **attrs)
518 {
519 struct element *e;
520 struct color color_black = { COLOR_BLACK_ };
521 struct color color_white = { COLOR_WHITE_ };
522
523 e = g_new0(struct element, 1);
524 e->type = element_circle;
525 e->color = color_black;
526 e->u.circle.background_color = color_white;
527 element_set_color(e, attrs);
528 element_set_background_color(&e->u.circle.background_color, attrs);
529 element_set_text_size(e, attrs);
530 element_set_circle_width(e, attrs);
531 element_set_circle_radius(e, attrs);
532
533 return (struct circle *) e;
534 }
535
536 struct text *
537 text_new(struct attr *parent, struct attr **attrs)
538 {
539 struct element *e;
540 struct color color_black = { COLOR_BLACK_ };
541 struct color color_white = { COLOR_WHITE_ };
542
543 e = g_new0(struct element, 1);
544 e->type = element_text;
545 element_set_text_size(e, attrs);
546 e->color = color_black;
547 e->u.text.background_color = color_white;
548 element_set_color(e, attrs);
549 element_set_background_color(&e->u.text.background_color, attrs);
550
551 return (struct text *) e;
552 }
553
554 struct icon *
555 icon_new(struct attr *parent, struct attr **attrs)
556 {
557 struct element *e;
558 struct attr *src, *w, *h, *rotation;
559 src = attr_search(attrs, NULL, attr_src);
560 if (!src)
561 return NULL;
562
563 e = g_malloc0(sizeof(*e) + strlen(src->u.str) + 1);
564 e->type = element_icon;
565 e->u.icon.src = (char *) (e + 1);
566 if ((w = attr_search(attrs, NULL, attr_w)))
567 e->u.icon.width = w->u.num;
568 else
569 e->u.icon.width = -1;
570 if ((h = attr_search(attrs, NULL, attr_h)))
571 e->u.icon.height = h->u.num;
572 else
573 e->u.icon.height = -1;
574 if ((rotation = attr_search(attrs, NULL, attr_rotation)))
575 e->u.icon.rotation = rotation->u.num;
576 strcpy(e->u.icon.src, src->u.str);
577
578 return (struct icon *) e;
579 }
580
581 struct image *
582 image_new(struct attr *parent, struct attr **attrs)
583 {
584 struct element *e;
585
586 e = g_malloc0(sizeof(*e));
587 e->type = element_image;
588
589 return (struct image *) e;
590 }
591
592 struct arrows *
593 arrows_new(struct attr *parent, struct attr **attrs)
594 {
595 struct element *e;
596 e = g_malloc0(sizeof(*e));
597 e->type = element_arrows;
598 element_set_color(e, attrs);
599 return (struct arrows *) e;
600 }
601
602 int element_add_attr(struct element *e, struct attr *attr)
603 {
604 switch (attr->type)
605 {
606 case attr_coord:
607 e->coord = g_realloc(e->coord, (e->coord_count + 1) * sizeof(struct coord));
608 e->coord[e->coord_count++] = *attr->u.coord;
609 return 1;
610 default:
611 return 0;
612 }
613 }

   
Visit the ZANavi Wiki