Modularized vehicle
[navit-package] / src / cursor.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <math.h>
7 #include <glib.h>
8 #include "debug.h"
9 #include "coord.h"
10 #include "transform.h"
11 #include "projection.h"
12 #include "point.h"
13 #include "graphics.h"
14 #include "statusbar.h"
15 #include "menu.h"
16 #include "vehicle.h"
17 #include "navit.h"
18 #include "callback.h"
19 #include "color.h"
20 #include "cursor.h"
21 #include "compass.h"
22 /* #include "track.h" */
23
24
25
26 struct cursor {
27         struct graphics *gra;
28         struct graphics_gc *cursor_gc;
29         struct point cursor_pnt;
30 };
31
32
33 void
34 cursor_draw(struct cursor *this_, struct point *pnt, int dir, int draw_dir, int force)
35 {
36         int x=this_->cursor_pnt.x;
37         int y=this_->cursor_pnt.y;
38         int r=12,lw=2;
39         double dx,dy;
40         double fac1,fac2;
41         struct point cpnt[3];
42         struct graphics *gra=this_->gra;
43
44         if (pnt && x == pnt->x && y == pnt->y && !force)
45                 return;
46         if (!graphics_ready(gra))
47                 return;
48         cpnt[0]=this_->cursor_pnt;
49         cpnt[0].x-=r+lw;
50         cpnt[0].y-=r+lw;
51         graphics_draw_restore(gra, &cpnt[0], (r+lw)*2, (r+lw)*2);
52         if (pnt) {
53                 graphics_draw_mode(gra, draw_mode_cursor);
54                 this_->cursor_pnt=*pnt;
55                 x=pnt->x;
56                 y=pnt->y;
57                 cpnt[0].x=x;
58                 cpnt[0].y=y;
59                 graphics_draw_circle(gra, this_->cursor_gc, &cpnt[0], r*2);
60                 if (draw_dir) {
61                         dx=sin(M_PI*dir/180.0);
62                         dy=-cos(M_PI*dir/180.0);
63
64                         fac1=0.7*r;
65                         fac2=0.4*r;     
66                         cpnt[0].x=x-dx*fac1+dy*fac2;
67                         cpnt[0].y=y-dy*fac1-dx*fac2;
68                         cpnt[1].x=x+dx*r;
69                         cpnt[1].y=y+dy*r;
70                         cpnt[2].x=x-dx*fac1-dy*fac2;
71                         cpnt[2].y=y-dy*fac1+dx*fac2;
72                         graphics_draw_lines(gra, this_->cursor_gc, cpnt, 3);
73                 } else {
74                         cpnt[1]=cpnt[0];
75                         graphics_draw_lines(gra, this_->cursor_gc, cpnt, 2);
76                 }
77                 graphics_draw_mode(gra, draw_mode_end);
78         }
79 }
80
81 struct cursor *
82 cursor_new(struct graphics *gra, struct color *c)
83 {
84         dbg(2,"enter gra=%p c=%p\n", gra, c);
85         struct cursor *this=g_new(struct cursor,1);
86         this->gra=gra;
87         this->cursor_gc=graphics_gc_new(gra);
88         graphics_gc_set_foreground(this->cursor_gc, c);
89         graphics_gc_set_linewidth(this->cursor_gc, 2);
90         dbg(2,"ret=%p\n", this);
91         return this;
92 }
93
94 void
95 cursor_destroy(struct cursor *this_)
96 {
97         graphics_gc_destroy(this_->cursor_gc);
98         g_free(this_);
99 }