2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
20 //##############################################################################################################
25 //# Authors: Martin Schaller (04/2008)
27 //##############################################################################################################
35 #include "draw_info.h"
38 #include "projection.h"
41 #include "transform.h"
51 static char *navit_sharedir;
53 //##############################################################################################################
56 //# Authors: Martin Schaller (04/2008)
57 //##############################################################################################################
60 struct graphics_priv *priv;
61 struct graphics_methods meth;
63 struct graphics_font *font[16];
64 struct graphics_gc *gc[3];
66 struct callback_list *cbl;
76 * Creates a new graphics object
80 * @author Martin Schaller (04/2008)
82 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
84 struct graphics *this_;
85 struct attr *type_attr;
86 struct graphics_priv * (*graphicstype_new)(struct navit *nav, struct graphics_methods *meth, struct attr **attrs, struct callback_list *cbl);
88 if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
92 graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
93 if (! graphicstype_new)
95 this_=g_new0(struct graphics, 1);
96 this_->cbl=callback_list_new();
97 this_->priv=(*graphicstype_new)(parent->u.navit, &this_->meth, attrs, this_->cbl);
98 this_->attrs=attr_list_dup(attrs);
106 * @author Martin Schaller (04/2008)
108 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
110 return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
117 * @author Martin Schaller (04/2008)
119 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h, int alpha)
121 struct graphics *this_;
122 this_=g_new0(struct graphics, 1);
123 this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h, alpha);
131 * @author Martin Schaller (04/2008)
133 void graphics_init(struct graphics *this_)
135 this_->gc[0]=graphics_gc_new(this_);
136 graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff});
137 graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff });
138 this_->gc[1]=graphics_gc_new(this_);
139 graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
140 graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
141 this_->gc[2]=graphics_gc_new(this_);
142 graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
143 graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
144 graphics_background_gc(this_, this_->gc[0]);
145 navit_sharedir = getenv("NAVIT_SHAREDIR");
152 * @author Martin Schaller (04/2008)
154 void * graphics_get_data(struct graphics *this_, char *type)
156 return (this_->meth.get_data(this_->priv, type));
159 void graphics_add_callback(struct graphics *this_, struct callback *cb)
161 callback_list_add(this_->cbl, cb);
168 * @author Martin Schaller (04/2008)
170 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
172 struct graphics_font *this_;
174 this_=g_new0(struct graphics_font,1);
175 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, gra->default_font, size, flags);
180 * Free all loaded fonts.
181 * Used when switching layouts.
182 * @param gra The graphics instance
184 * @author Sarah Nordstrom (05/2008)
186 void graphics_font_destroy_all(struct graphics *gra)
189 for(i = 0 ; i < sizeof(gra->font) / sizeof(gra->font[0]); i++) {
190 if(!gra->font[i]) continue;
191 gra->font[i]->meth.font_destroy(gra->font[i]->priv);
200 * @author Martin Schaller (04/2008)
202 struct graphics_gc * graphics_gc_new(struct graphics *gra)
204 struct graphics_gc *this_;
206 this_=g_new0(struct graphics_gc,1);
207 this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
215 * @author Martin Schaller (04/2008)
217 void graphics_gc_destroy(struct graphics_gc *gc)
219 gc->meth.gc_destroy(gc->priv);
227 * @author Martin Schaller (04/2008)
229 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
231 gc->meth.gc_set_foreground(gc->priv, c);
238 * @author Martin Schaller (04/2008)
240 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
242 gc->meth.gc_set_background(gc->priv, c);
249 * @author Martin Schaller (04/2008)
251 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
253 gc->meth.gc_set_linewidth(gc->priv, width);
260 * @author Martin Schaller (04/2008)
262 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
264 if (gc->meth.gc_set_dashes)
265 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
269 * Create a new image from file path scaled to w and h pixels
270 * @param gra the graphics instance
271 * @param path path of the image to load
272 * @param w width to rescale to
273 * @param h height to rescale to
275 * @author Martin Schaller (04/2008)
277 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
279 struct graphics_image *this_;
281 this_=g_new0(struct graphics_image,1);
284 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot);
293 * Create a new image from file path
294 * @param gra the graphics instance
295 * @param path path of the image to load
297 * @author Martin Schaller (04/2008)
299 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
301 return graphics_image_new_scaled(gra, path, -1, -1);
308 * @author Martin Schaller (04/2008)
310 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
312 if (gra->meth.image_free)
313 gra->meth.image_free(gra->priv, img->priv);
321 * @author Martin Schaller (04/2008)
323 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
325 this_->meth.draw_restore(this_->priv, p, w, h);
332 * @author Martin Schaller (04/2008)
334 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
336 this_->meth.draw_mode(this_->priv, mode);
343 * @author Martin Schaller (04/2008)
345 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
347 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
354 * @author Martin Schaller (04/2008)
356 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
358 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
365 * @author Martin Schaller (04/2008)
367 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
369 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
376 * @author Martin Schaller (04/2008)
378 void graphics_draw_text(struct graphics *this_, struct graphics_gc *gc1, struct graphics_gc *gc2, struct graphics_font *font, char *text, struct point *p, int dx, int dy)
380 this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
387 * @author Martin Schaller (04/2008)
389 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret)
391 this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret);
398 * @author Martin Schaller (04/2008)
400 void graphics_overlay_disable(struct graphics *this_, int disable)
402 if (this_->meth.overlay_disable)
403 this_->meth.overlay_disable(this_->priv, disable);
410 * @author Martin Schaller (04/2008)
412 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
414 this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
418 //##############################################################################################################
421 //# Authors: Martin Schaller (04/2008)
422 //##############################################################################################################
424 graphics_draw_drag(struct graphics *this_, struct point *p)
426 if (!this_->meth.draw_drag)
428 this_->meth.draw_drag(this_->priv, p);
433 graphics_background_gc(struct graphics *this_, struct graphics_gc *gc)
435 this_->meth.background_gc(this_->priv, gc ? gc->priv : NULL);
444 //##############################################################################################################
447 //# Authors: Martin Schaller (04/2008)
448 //##############################################################################################################
449 static void popup_view_html(struct popup_item *item, char *file)
452 sprintf(command,"firefox %s", file);
456 //##############################################################################################################
459 //# Authors: Martin Schaller (04/2008)
460 //##############################################################################################################
461 static void graphics_popup(struct display_list *list, struct popup_item **popup)
467 struct popup_item *curr_item,*last=NULL;
469 mr=map_rect_new(item->map, NULL, NULL, 0);
470 printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
471 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
473 if (item_attr_get(item, attr_name, &attr)) {
474 curr_item=popup_item_new_text(popup,attr.u.str,1);
475 if (item_attr_get(item, attr_info_html, &attr)) {
476 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
478 if (item_attr_get(item, attr_price_html, &attr)) {
479 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
481 curr_item->submenu=last;
484 map_rect_destroy(mr);
492 * @author Martin Schaller (04/2008)
506 * @author Martin Schaller (04/2008)
508 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
514 struct displayitem *di=l->data;
515 if (! di->displayed && di->item.type < type_line)
516 dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
528 * @author Martin Schaller (04/2008)
530 static void xdisplay_free(GHashTable *display_list)
532 g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
539 * @author Martin Schaller (04/2008)
541 void display_add(struct displaylist *displaylist, struct item *item, int count, struct point *pnt, char *label)
543 struct displayitem *di;
548 len=sizeof(*di)+count*sizeof(*pnt);
550 len+=strlen(label)+1;
554 di=(struct displayitem *)p;
556 p+=sizeof(*di)+count*sizeof(*pnt);
560 strcpy(di->label, label);
564 memcpy(di->pnt, pnt, count*sizeof(*pnt));
566 l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
567 l=g_list_prepend(l, di);
568 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
576 * @author Martin Schaller (04/2008)
578 static void label_line(struct graphics *gra, struct graphics_gc *fg, struct graphics_gc *bg, struct graphics_font *font, struct point *p, int count, char *label)
584 tl=strlen(label)*400;
585 for (i = 0 ; i < count-1 ; i++) {
590 l=(int)sqrt((float)(dx*dx+dy*dy));
607 printf("display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
609 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
614 static void display_draw_arrow(struct point *p, int dx, int dy, int l, struct graphics_gc *gc, struct graphics *gra)
617 pnt[0]=pnt[1]=pnt[2]=*p;
618 pnt[0].x+=-dx*l/65536+dy*l/65536;
619 pnt[0].y+=-dy*l/65536-dx*l/65536;
620 pnt[2].x+=-dx*l/65536-dy*l/65536;
621 pnt[2].y+=-dy*l/65536+dx*l/65536;
622 gra->meth.draw_lines(gra->priv, gc->priv, pnt, 3);
625 static void display_draw_arrows(struct displayitem *di, struct graphics_gc *gc, struct graphics *gra)
629 for (i = 0 ; i < di->count-1 ; i++) {
630 dx=di->pnt[i+1].x-di->pnt[i].x;
631 dy=di->pnt[i+1].y-di->pnt[i].y;
639 display_draw_arrow(&p, dx, dy, 10, gc, gra);
643 display_draw_arrow(&p, dx, dy, 10, gc, gra);
652 * @author Martin Schaller (04/2008)
654 static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemgra *itm)
657 GList *l,*ls,*es,*types;
659 struct graphics_gc *gc = NULL;
660 struct graphics_image *img;
669 type=GPOINTER_TO_INT(types->data);
670 ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
673 graphics_gc_destroy(gc);
677 struct displayitem *di;
681 gc=graphics_gc_new(gra);
682 gc->meth.gc_set_foreground(gc->priv, &e->color);
685 case element_polygon:
686 gra->meth.draw_polygon(gra->priv, gc->priv, di->pnt, di->count);
688 case element_polyline:
689 if (e->u.polyline.width > 1)
690 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
691 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
692 graphics_gc_set_dashes(gc, e->u.polyline.width,
693 e->u.polyline.offset,
694 e->u.polyline.dash_table,
695 e->u.polyline.dash_num);
696 gra->meth.draw_lines(gra->priv, gc->priv, di->pnt, di->count);
699 if (e->u.circle.width > 1)
700 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
701 gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius);
702 if (di->label && e->text_size) {
705 if (! gra->font[e->text_size])
706 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
707 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, di->label, &p, 0x10000, 0);
712 if (! gra->font[e->text_size])
713 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
714 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->text_size], di->pnt, di->count, di->label);
719 sprintf(path,"%s/xpm/%s", navit_sharedir, e->u.icon.src);
720 img=graphics_image_new_scaled(gra, path, e->u.icon.width, e->u.icon.height);
722 dbg(0,"failed to load icon '%s'\n", e->u.icon.src);
725 p.x=di->pnt[0].x - img->hot.x;
726 p.y=di->pnt[0].y - img->hot.y;
727 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
728 graphics_image_free(gra, img);
733 dbg(1,"image: '%s'\n", di->label);
734 if (gra->meth.draw_image_warp)
735 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, di->pnt, di->count, di->label);
737 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
740 display_draw_arrows(di,gc,gra);
743 printf("Unhandled element type %d\n", e->type);
748 types=g_list_next(types);
753 graphics_gc_destroy(gc);
757 graphics_draw_itemgra(struct graphics *gra, struct itemgra *itm, struct transformation *t)
762 struct graphics_gc *gc = NULL;
765 struct element *e=es->data;
766 int count=e->coord_count;
767 struct point pnt[count];
768 transform(t, projection_screen, e->coord, pnt, count, 0);
769 gc=graphics_gc_new(gra);
770 gc->meth.gc_set_foreground(gc->priv, &e->color);
772 case element_polyline:
773 if (e->u.polyline.width > 1)
774 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
775 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
776 graphics_gc_set_dashes(gc, e->u.polyline.width,
777 e->u.polyline.offset,
778 e->u.polyline.dash_table,
779 e->u.polyline.dash_num);
780 gra->meth.draw_lines(gra->priv, gc->priv, pnt, count);
783 if (e->u.circle.width > 1)
784 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
785 gra->meth.draw_circle(gra->priv, gc->priv, &pnt[0], e->u.circle.radius);
786 if (label && e->text_size) {
789 if (! gra->font[e->text_size])
790 gra->font[e->text_size]=graphics_font_new(gra, e->text_size*20, 0);
791 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->text_size]->priv, label, &p, 0x10000, 0);
795 dbg(0,"dont know how to draw %d\n", e->type);
797 graphics_gc_destroy(gc);
806 * @author Martin Schaller (04/2008)
808 static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
816 if (order >= itm->order.min && order <= itm->order.max)
817 xdisplay_draw_elements(gra, display_list, itm);
818 itms=g_list_next(itms);
826 * @author Martin Schaller (04/2008)
828 static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
836 xdisplay_draw_layer(display_list, gra, lay, order);
837 lays=g_list_next(lays);
845 * @author Martin Schaller (04/2008)
847 extern void *route_selection;
853 * @author Martin Schaller (04/2008)
855 static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
860 int conv,count,max=16384;
861 struct point pnt[max];
862 struct coord ca[max];
864 struct map_selection *sel;
866 pro=map_projection(m);
867 conv=map_requires_conversion(m);
868 sel=transform_get_selection(t, pro, order);
870 mr=map_rect_new(m, route_selection);
872 mr=map_rect_new(m, sel);
874 map_selection_destroy(sel);
877 while ((item=map_rect_get_item(mr))) {
878 count=item_coord_get(item, ca, item->type < type_line ? 1: max);
879 if (item->type >= type_line && count < 2) {
880 dbg(1,"poly from map has only %d points\n", count);
883 if (item->type < type_line) {
884 if (! map_selection_contains_point(sel, &ca[0])) {
885 dbg(1,"point not visible\n");
888 } else if (item->type < type_area) {
889 if (! map_selection_contains_polyline(sel, ca, count)) {
890 dbg(1,"polyline not visible\n");
894 if (! map_selection_contains_polygon(sel, ca, count)) {
895 dbg(1,"polygon not visible\n");
900 dbg(0,"point count overflow\n", count);
901 count=transform(t, pro, ca, pnt, count, 1);
902 if (item->type >= type_line && count < 2) {
903 dbg(1,"poly from transform has only %d points\n", count);
906 if (!item_attr_get(item, attr_label, &attr))
908 if (conv && attr.u.str && attr.u.str[0]) {
909 char *str=map_convert_string(m, attr.u.str);
910 display_add(displaylist, item, count, pnt, str);
911 map_convert_free(str);
913 display_add(displaylist, item, count, pnt, attr.u.str);
915 map_rect_destroy(mr);
916 map_selection_destroy(sel);
923 * @author Martin Schaller (04/2008)
925 static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
929 struct mapset_handle *h;
935 while ((m=mapset_next(h, 1))) {
936 do_draw_map(displaylist, t, m, order);
945 * @author Martin Schaller (04/2008)
947 int graphics_ready(struct graphics *this_)
956 * @author Martin Schaller (04/2008)
958 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
960 int order=transform_get_order(trans);
964 // FIXME find a better place to set the background color
965 graphics_gc_set_background(gra->gc[0], &l->color);
966 graphics_gc_set_foreground(gra->gc[0], &l->color);
967 gra->default_font = g_strdup(l->font);
968 graphics_background_gc(gra, gra->gc[0]);
969 gra->meth.draw_mode(gra->priv, draw_mode_begin);
970 gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
971 xdisplay_draw(displaylist->dl, gra, l, order+l->order_delta);
973 callback_list_call_attr_0(gra->cbl, attr_postdraw);
974 gra->meth.draw_mode(gra->priv, draw_mode_end);
981 * @author Martin Schaller (04/2008)
983 void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
985 struct displaylist_handle *dlh;
986 struct displayitem *di;
989 dlh=graphics_displaylist_open(displaylist);
990 while ((di=graphics_displaylist_next(dlh))) {
991 for (i = 0 ; i < di->count ; i++) {
996 graphics_displaylist_close(dlh);
1003 * @author Martin Schaller (04/2008)
1005 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l)
1007 int order=transform_get_order(trans);
1012 printf("scale=%d center=0x%x,0x%x mercator scale=%f\n", scale, co->trans->center.x, co->trans->center.y, transform_scale(co->trans->center.y));
1015 xdisplay_free(displaylist->dl);
1016 dbg(1,"order=%d\n", order);
1020 for (i = 0 ; i < data_window_type_end; i++) {
1021 data_window_begin(co->data_window[i]);
1025 order+=l->order_delta;
1026 do_draw(displaylist, trans, mapsets, order);
1027 // profile(1,"do_draw");
1028 graphics_displaylist_draw(gra, displaylist, trans, l, 1);
1029 profile(1,"xdisplay_draw");
1033 for (i = 0 ; i < data_window_type_end; i++) {
1034 data_window_end(co->data_window[i]);
1044 * @author Martin Schaller (04/2008)
1046 struct displaylist_handle {
1047 GList *hl_head,*hl,*l;
1054 * @author Martin Schaller (04/2008)
1056 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
1058 struct displaylist_handle *ret;
1060 ret=g_new0(struct displaylist_handle, 1);
1061 ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
1070 * @author Martin Schaller (04/2008)
1072 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
1074 struct displayitem *ret;
1078 dlh->l=dlh->hl->data;
1079 dlh->hl=g_list_next(dlh->hl);
1082 dlh->l=g_list_next(dlh->l);
1090 * @author Martin Schaller (04/2008)
1092 void graphics_displaylist_close(struct displaylist_handle *dlh)
1094 g_list_free(dlh->hl_head);
1102 * @author Martin Schaller (04/2008)
1104 struct displaylist * graphics_displaylist_new(void)
1106 struct displaylist *ret=g_new(struct displaylist, 1);
1108 ret->dl=g_hash_table_new(NULL,NULL);
1117 * @author Martin Schaller (04/2008)
1119 struct item * graphics_displayitem_get_item(struct displayitem *di)
1128 * @author Martin Schaller (04/2008)
1130 char * graphics_displayitem_get_label(struct displayitem *di)
1139 * @author Martin Schaller (04/2008)
1141 static int within_dist_point(struct point *p0, struct point *p1, int dist)
1143 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
1145 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
1147 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
1157 * @author Martin Schaller (04/2008)
1159 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1163 struct point line_p;
1165 vx=line_p1->x-line_p0->x;
1166 vy=line_p1->y-line_p0->y;
1172 return within_dist_point(p, line_p0, dist);
1175 return within_dist_point(p, line_p1, dist);
1177 line_p.x=line_p0->x+vx*c1/c2;
1178 line_p.y=line_p0->y+vy*c1/c2;
1179 return within_dist_point(p, &line_p, dist);
1186 * @author Martin Schaller (04/2008)
1188 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1191 for (i = 0 ; i < count-1 ; i++) {
1192 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1197 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1205 * @author Martin Schaller (04/2008)
1207 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1210 for (i = 0, j = count-1; i < count; j = i++) {
1211 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1212 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1213 (p->x < (poly_pnt[j].x - poly_pnt[i].x) * (p->y - poly_pnt[i].y) / (poly_pnt[j].y - poly_pnt[i].y) + poly_pnt[i].x))
1217 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1225 * @author Martin Schaller (04/2008)
1227 int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
1229 if (di->item.type < type_line) {
1230 return within_dist_point(p, &di->pnt[0], dist);
1232 if (di->item.type < type_area) {
1233 return within_dist_polyline(p, di->pnt, di->count, dist, 0);
1235 return within_dist_polygon(p, di->pnt, di->count, dist);