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"
49 //##############################################################################################################
52 //# Authors: Martin Schaller (04/2008)
53 //##############################################################################################################
56 struct graphics_priv *priv;
57 struct graphics_methods meth;
58 struct graphics_font *font[16];
59 struct graphics_gc *gc[3];
63 //##############################################################################################################
66 //# Authors: Martin Schaller (04/2008)
67 //##############################################################################################################
71 //##############################################################################################################
72 //# Description: Creates a new graphics object
73 //# Comment: attr type required
74 //# Authors: Martin Schaller (04/2008)
75 //##############################################################################################################
76 struct graphics * graphics_new(struct attr *parent, struct attr **attrs)
78 struct graphics *this_;
79 struct attr *type_attr;
80 struct graphics_priv * (*graphicstype_new)(struct graphics_methods *meth, struct attr **attrs);
82 if (! (type_attr=attr_search(attrs, NULL, attr_type))) {
86 graphicstype_new=plugin_get_graphics_type(type_attr->u.str);
87 if (! graphicstype_new)
89 this_=g_new0(struct graphics, 1);
90 this_->priv=(*graphicstype_new)(&this_->meth, attrs);
91 this_->attrs=attr_list_dup(attrs);
95 //##############################################################################################################
98 //# Authors: Martin Schaller (04/2008)
99 //##############################################################################################################
100 int graphics_get_attr(struct graphics *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
102 return attr_generic_get_attr(this_->attrs, type, attr, iter);
105 //##############################################################################################################
108 //# Authors: Martin Schaller (04/2008)
109 //##############################################################################################################
110 struct graphics * graphics_overlay_new(struct graphics *parent, struct point *p, int w, int h)
112 struct graphics *this_;
113 this_=g_new0(struct graphics, 1);
114 this_->priv=parent->meth.overlay_new(parent->priv, &this_->meth, p, w, h);
118 //##############################################################################################################
121 //# Authors: Martin Schaller (04/2008)
122 //##############################################################################################################
123 void graphics_init(struct graphics *this_)
125 this_->gc[0]=graphics_gc_new(this_);
126 graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7 });
127 graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7 });
128 this_->gc[1]=graphics_gc_new(this_);
129 graphics_gc_set_background(this_->gc[1], &(struct color) { 0x0000, 0x0000, 0x0000 });
130 graphics_gc_set_foreground(this_->gc[1], &(struct color) { 0xffff, 0xffff, 0xffff });
131 this_->gc[2]=graphics_gc_new(this_);
132 graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff });
133 graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff });
134 this_->meth.background_gc(this_->priv, this_->gc[0]->priv);
137 //##############################################################################################################
140 //# Authors: Martin Schaller (04/2008)
141 //##############################################################################################################
142 void * graphics_get_data(struct graphics *this_, char *type)
144 return (this_->meth.get_data(this_->priv, type));
147 //##############################################################################################################
150 //# Authors: Martin Schaller (04/2008)
151 //##############################################################################################################
152 void graphics_register_resize_callback(struct graphics *this_, void (*callback)(void *data, int w, int h), void *data)
154 this_->meth.register_resize_callback(this_->priv, callback, data);
157 //##############################################################################################################
159 //# Comment: Called in navit.c
160 //# Authors: Martin Schaller (04/2008)
161 //##############################################################################################################
162 void graphics_register_button_callback(struct graphics *this_, void (*callback)(void *data, int pressed, int button, struct point *p), void *data)
164 this_->meth.register_button_callback(this_->priv, callback, data);
167 //##############################################################################################################
170 //# Authors: Martin Schaller (04/2008)
171 //##############################################################################################################
172 void graphics_register_motion_callback(struct graphics *this_, void (*callback)(void *data, struct point *p), void *data)
174 this_->meth.register_motion_callback(this_->priv, callback, data);
177 //##############################################################################################################
180 //# Authors: Martin Schaller (04/2008)
181 //##############################################################################################################
182 struct graphics_font * graphics_font_new(struct graphics *gra, int size, int flags)
184 struct graphics_font *this_;
186 this_=g_new0(struct graphics_font,1);
187 this_->priv=gra->meth.font_new(gra->priv, &this_->meth, size, flags);
191 //##############################################################################################################
194 //# Authors: Martin Schaller (04/2008)
195 //##############################################################################################################
196 struct graphics_gc * graphics_gc_new(struct graphics *gra)
198 struct graphics_gc *this_;
200 this_=g_new0(struct graphics_gc,1);
201 this_->priv=gra->meth.gc_new(gra->priv, &this_->meth);
205 //##############################################################################################################
208 //# Authors: Martin Schaller (04/2008)
209 //##############################################################################################################
210 void graphics_gc_destroy(struct graphics_gc *gc)
212 gc->meth.gc_destroy(gc->priv);
216 //##############################################################################################################
219 //# Authors: Martin Schaller (04/2008)
220 //##############################################################################################################
221 void graphics_gc_set_foreground(struct graphics_gc *gc, struct color *c)
223 gc->meth.gc_set_foreground(gc->priv, c);
226 //##############################################################################################################
229 //# Authors: Martin Schaller (04/2008)
230 //##############################################################################################################
231 void graphics_gc_set_background(struct graphics_gc *gc, struct color *c)
233 gc->meth.gc_set_background(gc->priv, c);
236 //##############################################################################################################
239 //# Authors: Martin Schaller (04/2008)
240 //##############################################################################################################
241 void graphics_gc_set_linewidth(struct graphics_gc *gc, int width)
243 gc->meth.gc_set_linewidth(gc->priv, width);
246 //##############################################################################################################
249 //# Authors: Martin Schaller (04/2008)
250 //##############################################################################################################
251 void graphics_gc_set_dashes(struct graphics_gc *gc, int width, int offset, unsigned char dash_list[], int n)
253 if (gc->meth.gc_set_dashes)
254 gc->meth.gc_set_dashes(gc->priv, width, offset, dash_list, n);
257 //##############################################################################################################
258 //# Description: Create a new image from file path scaled to w and h pixels
260 //# Authors: Martin Schaller (04/2008)
261 //##############################################################################################################
262 struct graphics_image * graphics_image_new_scaled(struct graphics *gra, char *path, int w, int h)
264 struct graphics_image *this_;
266 this_=g_new0(struct graphics_image,1);
269 this_->priv=gra->meth.image_new(gra->priv, &this_->meth, path, &this_->width, &this_->height, &this_->hot);
277 //##############################################################################################################
278 //# Description: Create a new image from file path
280 //# Authors: Martin Schaller (04/2008)
281 //##############################################################################################################
282 struct graphics_image * graphics_image_new(struct graphics *gra, char *path)
284 return graphics_image_new_scaled(gra, path, -1, -1);
287 //##############################################################################################################
290 //# Authors: Martin Schaller (04/2008)
291 //##############################################################################################################
292 void graphics_image_free(struct graphics *gra, struct graphics_image *img)
294 if (gra->meth.image_free)
295 gra->meth.image_free(gra->priv, img->priv);
299 //##############################################################################################################
302 //# Authors: Martin Schaller (04/2008)
303 //##############################################################################################################
304 void graphics_draw_restore(struct graphics *this_, struct point *p, int w, int h)
306 this_->meth.draw_restore(this_->priv, p, w, h);
309 //##############################################################################################################
312 //# Authors: Martin Schaller (04/2008)
313 //##############################################################################################################
314 void graphics_draw_mode(struct graphics *this_, enum draw_mode_num mode)
316 this_->meth.draw_mode(this_->priv, mode);
319 //##############################################################################################################
322 //# Authors: Martin Schaller (04/2008)
323 //##############################################################################################################
324 void graphics_draw_lines(struct graphics *this_, struct graphics_gc *gc, struct point *p, int count)
326 this_->meth.draw_lines(this_->priv, gc->priv, p, count);
329 //##############################################################################################################
332 //# Authors: Martin Schaller (04/2008)
333 //##############################################################################################################
334 void graphics_draw_circle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int r)
336 this_->meth.draw_circle(this_->priv, gc->priv, p, r);
339 //##############################################################################################################
342 //# Authors: Martin Schaller (04/2008)
343 //##############################################################################################################
344 void graphics_draw_rectangle(struct graphics *this_, struct graphics_gc *gc, struct point *p, int w, int h)
346 this_->meth.draw_rectangle(this_->priv, gc->priv, p, w, h);
349 //##############################################################################################################
352 //# Authors: Martin Schaller (04/2008)
353 //##############################################################################################################
354 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)
356 this_->meth.draw_text(this_->priv, gc1->priv, gc2 ? gc2->priv : NULL, font->priv, text, p, dx, dy);
359 //##############################################################################################################
362 //# Authors: Martin Schaller (04/2008)
363 //##############################################################################################################
364 void graphics_get_text_bbox(struct graphics *this_, struct graphics_font *font, char *text, int dx, int dy, struct point *ret)
366 this_->meth.get_text_bbox(this_->priv, font->priv, text, dx, dy, ret);
369 //##############################################################################################################
372 //# Authors: Martin Schaller (04/2008)
373 //##############################################################################################################
374 void graphics_overlay_disable(struct graphics *this_, int disable)
376 if (this_->meth.overlay_disable)
377 this_->meth.overlay_disable(this_->priv, disable);
380 //##############################################################################################################
383 //# Authors: Martin Schaller (04/2008)
384 //##############################################################################################################
385 void graphics_draw_image(struct graphics *this_, struct graphics_gc *gc, struct point *p, struct graphics_image *img)
387 this_->meth.draw_image(this_->priv, gc->priv, p, img->priv);
396 //##############################################################################################################
399 //# Authors: Martin Schaller (04/2008)
400 //##############################################################################################################
401 static void popup_view_html(struct popup_item *item, char *file)
404 sprintf(command,"firefox %s", file);
408 //##############################################################################################################
411 //# Authors: Martin Schaller (04/2008)
412 //##############################################################################################################
413 static void graphics_popup(struct display_list *list, struct popup_item **popup)
419 struct popup_item *curr_item,*last=NULL;
421 mr=map_rect_new(item->map, NULL, NULL, 0);
422 printf("id hi=0x%x lo=0x%x\n", item->id_hi, item->id_lo);
423 item=map_rect_get_item_byid(mr, item->id_hi, item->id_lo);
425 if (item_attr_get(item, attr_name, &attr)) {
426 curr_item=popup_item_new_text(popup,attr.u.str,1);
427 if (item_attr_get(item, attr_info_html, &attr)) {
428 popup_item_new_func(&last,"HTML Info",1, popup_view_html, g_strdup(attr.u.str));
430 if (item_attr_get(item, attr_price_html, &attr)) {
431 popup_item_new_func(&last,"HTML Preis",2, popup_view_html, g_strdup(attr.u.str));
433 curr_item->submenu=last;
436 map_rect_destroy(mr);
440 //##############################################################################################################
443 //# Authors: Martin Schaller (04/2008)
444 //##############################################################################################################
453 //##############################################################################################################
456 //# Authors: Martin Schaller (04/2008)
457 //##############################################################################################################
458 static int xdisplay_free_list(gpointer key, gpointer value, gpointer user_data)
464 struct displayitem *di=l->data;
465 if (! di->displayed && di->item.type < type_line)
466 dbg(1,"warning: item '%s' not displayed\n", item_to_name(di->item.type));
474 //##############################################################################################################
477 //# Authors: Martin Schaller (04/2008)
478 //##############################################################################################################
479 static void xdisplay_free(GHashTable *display_list)
481 g_hash_table_foreach_remove(display_list, xdisplay_free_list, NULL);
484 //##############################################################################################################
487 //# Authors: Martin Schaller (04/2008)
488 //##############################################################################################################
489 void display_add(struct displaylist *displaylist, struct item *item, int count, struct point *pnt, char *label)
491 struct displayitem *di;
496 len=sizeof(*di)+count*sizeof(*pnt);
498 len+=strlen(label)+1;
502 di=(struct displayitem *)p;
504 p+=sizeof(*di)+count*sizeof(*pnt);
508 strcpy(di->label, label);
512 memcpy(di->pnt, pnt, count*sizeof(*pnt));
514 l=g_hash_table_lookup(displaylist->dl, GINT_TO_POINTER(item->type));
515 l=g_list_prepend(l, di);
516 g_hash_table_insert(displaylist->dl, GINT_TO_POINTER(item->type), l);
520 //##############################################################################################################
523 //# Authors: Martin Schaller (04/2008)
524 //##############################################################################################################
525 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)
531 tl=strlen(label)*400;
532 for (i = 0 ; i < count-1 ; i++) {
537 l=(int)sqrt((float)(dx*dx+dy*dy));
554 printf("display_text: '%s', %d, %d, %d, %d %d\n", label, x, y, dx*0x10000/l, dy*0x10000/l, l);
556 gra->meth.draw_text(gra->priv, fg->priv, bg->priv, font->priv, label, &p_t, dx*0x10000/l, dy*0x10000/l);
561 //##############################################################################################################
564 //# Authors: Martin Schaller (04/2008)
565 //##############################################################################################################
566 static void xdisplay_draw_elements(struct graphics *gra, GHashTable *display_list, struct itemtype *itm)
569 GList *l,*ls,*es,*types;
571 struct graphics_gc *gc = NULL;
572 struct graphics_image *img;
580 type=GPOINTER_TO_INT(types->data);
581 ls=g_hash_table_lookup(display_list, GINT_TO_POINTER(type));
584 graphics_gc_destroy(gc);
588 struct displayitem *di;
592 gc=graphics_gc_new(gra);
593 gc->meth.gc_set_foreground(gc->priv, &e->color);
596 case element_polygon:
597 gra->meth.draw_polygon(gra->priv, gc->priv, di->pnt, di->count);
599 case element_polyline:
600 if (e->u.polyline.width > 1)
601 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
602 if (e->u.polyline.width > 0 && e->u.polyline.dash_num > 0)
603 graphics_gc_set_dashes(gc, e->u.polyline.width, 0,
604 e->u.polyline.dash_table,
605 e->u.polyline.dash_num);
606 gra->meth.draw_lines(gra->priv, gc->priv, di->pnt, di->count);
609 if (e->u.circle.width > 1)
610 gc->meth.gc_set_linewidth(gc->priv, e->u.polyline.width);
611 gra->meth.draw_circle(gra->priv, gc->priv, &di->pnt[0], e->u.circle.radius);
612 if (di->label && e->label_size) {
615 if (! gra->font[e->label_size])
616 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20, 0);
617 gra->meth.draw_text(gra->priv, gra->gc[2]->priv, gra->gc[1]->priv, gra->font[e->label_size]->priv, di->label, &p, 0x10000, 0);
622 if (! gra->font[e->label_size])
623 gra->font[e->label_size]=graphics_font_new(gra, e->label_size*20, 0);
624 label_line(gra, gra->gc[2], gra->gc[1], gra->font[e->label_size], di->pnt, di->count, di->label);
629 char *icon=g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/xpm/", e->u.icon.src, NULL);
630 img=graphics_image_new(gra, icon);
633 g_warning("failed to load icon '%s'\n", e->u.icon.src);
636 p.x=di->pnt[0].x - img->hot.x;
637 p.y=di->pnt[0].y - img->hot.y;
638 gra->meth.draw_image(gra->priv, gra->gc[0]->priv, &p, img->priv);
639 graphics_image_free(gra, img);
644 dbg(1,"image: '%s'\n", di->label);
645 if (gra->meth.draw_image_warp)
646 gra->meth.draw_image_warp(gra->priv, gra->gc[0]->priv, di->pnt, di->count, di->label);
648 dbg(0,"draw_image_warp not supported by graphics driver drawing '%s'\n", di->label);
651 printf("Unhandled element type %d\n", e->type);
656 types=g_list_next(types);
661 graphics_gc_destroy(gc);
664 //##############################################################################################################
667 //# Authors: Martin Schaller (04/2008)
668 //##############################################################################################################
669 static void xdisplay_draw_layer(GHashTable *display_list, struct graphics *gra, struct layer *lay, int order)
672 struct itemtype *itm;
677 if (order >= itm->order_min && order <= itm->order_max)
678 xdisplay_draw_elements(gra, display_list, itm);
679 itms=g_list_next(itms);
683 //##############################################################################################################
686 //# Authors: Martin Schaller (04/2008)
687 //##############################################################################################################
688 static void xdisplay_draw(GHashTable *display_list, struct graphics *gra, struct layout *l, int order)
696 xdisplay_draw_layer(display_list, gra, lay, order);
697 lays=g_list_next(lays);
701 //##############################################################################################################
704 //# Authors: Martin Schaller (04/2008)
705 //##############################################################################################################
706 extern void *route_selection;
708 //##############################################################################################################
711 //# Authors: Martin Schaller (04/2008)
712 //##############################################################################################################
713 static void do_draw_map(struct displaylist *displaylist, struct transformation *t, struct map *m, int order)
718 int conv,count,max=16384;
719 struct point pnt[max];
720 struct coord ca[max];
722 struct map_selection *sel;
724 pro=map_projection(m);
725 conv=map_requires_conversion(m);
726 sel=transform_get_selection(t, pro, order);
728 mr=map_rect_new(m, route_selection);
730 mr=map_rect_new(m, sel);
732 map_selection_destroy(sel);
735 while ((item=map_rect_get_item(mr))) {
736 count=item_coord_get(item, ca, item->type < type_line ? 1: max);
737 if (item->type >= type_line && count < 2) {
738 dbg(1,"poly from map has only %d points\n", count);
741 if (item->type < type_line) {
742 if (! map_selection_contains_point(sel, &ca[0])) {
743 dbg(1,"point not visible\n");
746 } else if (item->type < type_area) {
747 if (! map_selection_contains_polyline(sel, ca, count)) {
748 dbg(1,"polyline not visible\n");
752 if (! map_selection_contains_polygon(sel, ca, count)) {
753 dbg(1,"polygon not visible\n");
758 dbg(0,"point count overflow\n", count);
759 count=transform(t, pro, ca, pnt, count, 1);
760 if (item->type >= type_line && count < 2) {
761 dbg(1,"poly from transform has only %d points\n", count);
764 if (!item_attr_get(item, attr_label, &attr))
766 if (conv && attr.u.str && attr.u.str[0]) {
767 char *str=map_convert_string(m, attr.u.str);
768 display_add(displaylist, item, count, pnt, str);
769 map_convert_free(str);
771 display_add(displaylist, item, count, pnt, attr.u.str);
773 map_rect_destroy(mr);
774 map_selection_destroy(sel);
777 //##############################################################################################################
780 //# Authors: Martin Schaller (04/2008)
781 //##############################################################################################################
782 static void do_draw(struct displaylist *displaylist, struct transformation *t, GList *mapsets, int order)
786 struct mapset_handle *h;
792 while ((m=mapset_next(h, 1))) {
793 do_draw_map(displaylist, t, m, order);
798 //##############################################################################################################
801 //# Authors: Martin Schaller (04/2008)
802 //##############################################################################################################
803 int graphics_ready(struct graphics *this_)
808 //##############################################################################################################
811 //# Authors: Martin Schaller (04/2008)
812 //##############################################################################################################
813 void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l)
815 int order=transform_get_order(trans);
819 // FIXME find a better place to set the background color
820 graphics_gc_set_background(gra->gc[0], l->color);
821 graphics_gc_set_foreground(gra->gc[0], l->color);
822 gra->meth.background_gc(gra->priv, gra->gc[0]->priv);
823 gra->meth.draw_mode(gra->priv, draw_mode_begin);
824 gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &p, 32767, 32767);
825 xdisplay_draw(displaylist->dl, gra, l, order);
826 gra->meth.draw_mode(gra->priv, draw_mode_end);
829 //##############################################################################################################
832 //# Authors: Martin Schaller (04/2008)
833 //##############################################################################################################
834 void graphics_displaylist_move(struct displaylist *displaylist, int dx, int dy)
836 struct displaylist_handle *dlh;
837 struct displayitem *di;
840 dlh=graphics_displaylist_open(displaylist);
841 while ((di=graphics_displaylist_next(dlh))) {
842 for (i = 0 ; i < di->count ; i++) {
847 graphics_displaylist_close(dlh);
850 //##############################################################################################################
853 //# Authors: Martin Schaller (04/2008)
854 //##############################################################################################################
855 void graphics_draw(struct graphics *gra, struct displaylist *displaylist, GList *mapsets, struct transformation *trans, struct layout *l)
857 int order=transform_get_order(trans);
862 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));
865 xdisplay_free(displaylist->dl);
866 dbg(1,"order=%d\n", order);
870 for (i = 0 ; i < data_window_type_end; i++) {
871 data_window_begin(co->data_window[i]);
875 do_draw(displaylist, trans, mapsets, order);
876 // profile(1,"do_draw");
877 graphics_displaylist_draw(gra, displaylist, trans, l);
878 profile(1,"xdisplay_draw");
882 for (i = 0 ; i < data_window_type_end; i++) {
883 data_window_end(co->data_window[i]);
889 //##############################################################################################################
892 //# Authors: Martin Schaller (04/2008)
893 //##############################################################################################################
894 struct displaylist_handle {
895 GList *hl_head,*hl,*l;
898 //##############################################################################################################
901 //# Authors: Martin Schaller (04/2008)
902 //##############################################################################################################
903 struct displaylist_handle * graphics_displaylist_open(struct displaylist *displaylist)
905 struct displaylist_handle *ret;
907 ret=g_new0(struct displaylist_handle, 1);
908 ret->hl_head=ret->hl=g_hash_to_list(displaylist->dl);
913 //##############################################################################################################
916 //# Authors: Martin Schaller (04/2008)
917 //##############################################################################################################
918 struct displayitem * graphics_displaylist_next(struct displaylist_handle *dlh)
920 struct displayitem *ret;
924 dlh->l=dlh->hl->data;
925 dlh->hl=g_list_next(dlh->hl);
928 dlh->l=g_list_next(dlh->l);
932 //##############################################################################################################
935 //# Authors: Martin Schaller (04/2008)
936 //##############################################################################################################
937 void graphics_displaylist_close(struct displaylist_handle *dlh)
939 g_list_free(dlh->hl_head);
943 //##############################################################################################################
946 //# Authors: Martin Schaller (04/2008)
947 //##############################################################################################################
948 struct displaylist * graphics_displaylist_new(void)
950 struct displaylist *ret=g_new(struct displaylist, 1);
952 ret->dl=g_hash_table_new(NULL,NULL);
957 //##############################################################################################################
960 //# Authors: Martin Schaller (04/2008)
961 //##############################################################################################################
962 struct item * graphics_displayitem_get_item(struct displayitem *di)
967 //##############################################################################################################
970 //# Authors: Martin Schaller (04/2008)
971 //##############################################################################################################
972 char * graphics_displayitem_get_label(struct displayitem *di)
977 //##############################################################################################################
980 //# Authors: Martin Schaller (04/2008)
981 //##############################################################################################################
982 static int within_dist_point(struct point *p0, struct point *p1, int dist)
984 if (p0->x == 32767 || p0->y == 32767 || p1->x == 32767 || p1->y == 32767)
986 if (p0->x == -32768 || p0->y == -32768 || p1->x == -32768 || p1->y == -32768)
988 if ((p0->x-p1->x)*(p0->x-p1->x) + (p0->y-p1->y)*(p0->y-p1->y) <= dist*dist) {
994 //##############################################################################################################
997 //# Authors: Martin Schaller (04/2008)
998 //##############################################################################################################
999 static int within_dist_line(struct point *p, struct point *line_p0, struct point *line_p1, int dist)
1003 struct point line_p;
1005 vx=line_p1->x-line_p0->x;
1006 vy=line_p1->y-line_p0->y;
1012 return within_dist_point(p, line_p0, dist);
1015 return within_dist_point(p, line_p1, dist);
1017 line_p.x=line_p0->x+vx*c1/c2;
1018 line_p.y=line_p0->y+vy*c1/c2;
1019 return within_dist_point(p, &line_p, dist);
1022 //##############################################################################################################
1025 //# Authors: Martin Schaller (04/2008)
1026 //##############################################################################################################
1027 static int within_dist_polyline(struct point *p, struct point *line_pnt, int count, int dist, int close)
1030 for (i = 0 ; i < count-1 ; i++) {
1031 if (within_dist_line(p,line_pnt+i,line_pnt+i+1,dist)) {
1036 return (within_dist_line(p,line_pnt,line_pnt+count-1,dist));
1040 //##############################################################################################################
1043 //# Authors: Martin Schaller (04/2008)
1044 //##############################################################################################################
1045 static int within_dist_polygon(struct point *p, struct point *poly_pnt, int count, int dist)
1048 for (i = 0, j = count-1; i < count; j = i++) {
1049 if ((((poly_pnt[i].y <= p->y) && ( p->y < poly_pnt[j].y )) ||
1050 ((poly_pnt[j].y <= p->y) && ( p->y < poly_pnt[i].y))) &&
1051 (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))
1055 return within_dist_polyline(p, poly_pnt, count, dist, 1);
1059 //##############################################################################################################
1062 //# Authors: Martin Schaller (04/2008)
1063 //##############################################################################################################
1064 int graphics_displayitem_within_dist(struct displayitem *di, struct point *p, int dist)
1066 if (di->item.type < type_line) {
1067 return within_dist_point(p, &di->pnt[0], dist);
1069 if (di->item.type < type_area) {
1070 return within_dist_polyline(p, di->pnt, di->count, dist, 0);
1072 return within_dist_polygon(p, di->pnt, di->count, dist);