Add:osd_core:Added new osd eta
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Wed, 27 Feb 2008 19:32:44 +0000 (19:32 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Wed, 27 Feb 2008 19:32:44 +0000 (19:32 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@920 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/src/osd/core/osd_core.c

index 1590fc3..f6865f8 100644 (file)
@@ -1,6 +1,7 @@
 #include <math.h>
 #include <stdio.h>
 #include <glib.h>
+#include <stdlib.h>
 #include <string.h>
 #include "config.h"
 #include "item.h"
 #include "callback.h"
 #include "color.h"
 #include "vehicle.h"
+#include "navigation.h"
+#include "map.h"
 
 struct compass {
        struct point p;
+       int w,h;
        struct graphics *gr;
        struct graphics_gc *bg;
        struct graphics_gc *white;
@@ -82,29 +86,29 @@ osd_compass_draw(struct compass *this, struct navit *nav, struct vehicle *v)
 {
        struct point p;
        struct attr attr_dir, destination_attr, position_attr;
-       double dir;
+       double dir,vdir=0;
        char buffer[16];
        struct coord c1, c2;
        enum projection pro;
 
-       dbg(0,"enter 0x%x 0x%x 0x%x\n", this, nav, v);
        graphics_draw_mode(this->gr, draw_mode_begin);
        p.x=0;
        p.y=0;
-       graphics_draw_rectangle(this->gr, this->bg, &p, 60, 80);
+       graphics_draw_rectangle(this->gr, this->bg, &p, this->w, this->h);
        p.x=30;
        p.y=30;
        graphics_draw_circle(this->gr, this->white, &p, 50);
        if (v && vehicle_position_attr_get(v, attr_position_direction, &attr_dir)) {
-               handle(this->gr, this->white, &p, 20, -*attr_dir.u.numd);
+               vdir=*attr_dir.u.numd;
+               handle(this->gr, this->white, &p, 20, -vdir);
        }
-       dbg(0,"calling navit_get_attr\n");
        if (navit_get_attr(nav, attr_destination, &destination_attr) && v && vehicle_position_attr_get(v, attr_position_coord_geo, &position_attr)) {
                pro=destination_attr.u.pcoord->pro;
                transform_from_geo(pro, position_attr.u.coord_geo, &c1);
                c2.x=destination_attr.u.pcoord->x;
                c2.y=destination_attr.u.pcoord->y;
                dir=atan2(c2.x-c1.x,c2.y-c1.y)*180.0/M_PI;
+               dir-=vdir;
                handle(this->gr, this->green, &p, 20, dir);
                format_distance(buffer, transform_distance(pro, &c1, &c2));
                p.x=8;
@@ -120,7 +124,7 @@ osd_compass_init(struct compass *this, struct navit *nav)
        struct graphics *navit_gr;
        struct color c;
        navit_gr=navit_get_graphics(nav);
-       this->gr=graphics_overlay_new(navit_gr, &this->p, 60, 80);
+       this->gr=graphics_overlay_new(navit_gr, &this->p, this->w, this->h);
 
        this->bg=graphics_gc_new(this->gr);
        c.r=0; c.g=0; c.b=0;
@@ -149,6 +153,8 @@ osd_compass_new(struct navit *nav, struct osd_methods *meth, struct attr **attrs
        struct attr *attr;
        this->p.x=20;
        this->p.y=20;
+       this->w=60;
+       this->h=80;
        attr=attr_search(attrs, NULL, attr_x);
        if (attr)
                this->p.x=attr->u.num;
@@ -159,9 +165,160 @@ osd_compass_new(struct navit *nav, struct osd_methods *meth, struct attr **attrs
        return (struct osd_priv *) this;
 }
 
+struct eta {
+       struct point p;
+       int w,h;
+       struct graphics *gr;
+       struct graphics_gc *bg;
+       struct graphics_gc *white;
+       struct graphics_font *font;
+       struct graphics_image *flag;
+       int active;
+       char last_eta[16];
+       char last_distance[16];
+};
+
+static void
+osd_eta_draw(struct eta *this, struct navit *navit, struct vehicle *v)
+{
+       struct point p;
+       char eta[16];
+       char distance[16];
+       int days=0,do_draw=1;
+       time_t etat;
+        struct tm tm,eta_tm,eta_tm0;
+       struct attr attr;
+       struct navigation *nav=NULL;
+       struct map *map=NULL;
+       struct map_rect *mr=NULL;
+       struct item *item=NULL;
+
+
+       eta[0]='\0';
+       distance[0]='\0';
+
+       if (navit)
+                nav=navit_get_navigation(navit);
+        if (nav)
+                map=navigation_get_map(nav);
+        if (map)
+                mr=map_rect_new(map, NULL);
+        if (mr)
+                item=map_rect_get_item(mr);
+        if (item) {
+                if (item_attr_get(item, attr_destination_length, &attr)) {
+                        format_distance(distance, attr.u.num);
+               }
+                if (item_attr_get(item, attr_destination_time, &attr)) {
+                       etat=time(NULL);
+                       tm=*localtime(&etat);
+                        etat+=attr.u.num/10;
+                        eta_tm=*localtime(&etat);
+                       if (tm.tm_year != eta_tm.tm_year || tm.tm_mon != eta_tm.tm_mon || tm.tm_mday != eta_tm.tm_mday) {
+                               eta_tm0=eta_tm;
+                               eta_tm0.tm_sec=0;
+                               eta_tm0.tm_min=0;
+                               eta_tm0.tm_hour=0;
+                               tm.tm_sec=0;
+                               tm.tm_min=0;
+                               tm.tm_hour=0;
+                               days=(mktime(&eta_tm0)-mktime(&tm)+43200)/86400;
+                       }
+                       if (days) 
+                               sprintf(eta, "%d+%02d:%02d", days, eta_tm.tm_hour, eta_tm.tm_min);
+                       else
+                               sprintf(eta, "  %02d:%02d", eta_tm.tm_hour, eta_tm.tm_min);
+                }
+               if (this->active != 1 || strcmp(this->last_distance, distance) || strcmp(this->last_eta, eta)) {
+                       this->active=1;
+                       strcpy(this->last_distance, distance);
+                       strcpy(this->last_eta, eta);
+                       do_draw=1;
+               }
+        } else {
+               if (this->active != 0) {
+                       this->active=0;
+                       do_draw=1;
+               }
+       }
+        if (mr)
+                map_rect_destroy(mr);
+
+       if (do_draw) {
+               graphics_draw_mode(this->gr, draw_mode_begin);
+               p.x=0;
+               p.y=0;
+               graphics_draw_rectangle(this->gr, this->bg, &p, this->w, this->h);
+               p.x=6;
+               p.y=6;
+               graphics_draw_image(this->gr, this->white, &p, this->flag);
+               if (eta[0]) {
+                       p.x=28;
+                       p.y=28;
+                       graphics_draw_text(this->gr, this->white, NULL, this->font, "ETA", &p, 0x10000, 0);
+                       p.x=6;
+                       p.y=42;
+                       graphics_draw_text(this->gr, this->white, NULL, this->font, eta, &p, 0x10000, 0);
+               }
+               if (distance[0]) {
+                       p.x=6;
+                       p.y=56;
+                       graphics_draw_text(this->gr, this->white, NULL, this->font, distance, &p, 0x10000, 0);
+               }
+               graphics_draw_mode(this->gr, draw_mode_end);
+       }       
+}
+
+static void
+osd_eta_init(struct eta *this, struct navit *nav)
+{
+       struct graphics *navit_gr;
+       struct color c;
+       char *flag=g_strjoin(NULL,getenv("NAVIT_SHAREDIR"), "/xpm/flag_wh_bk.xpm", NULL);
+       navit_gr=navit_get_graphics(nav);
+       this->gr=graphics_overlay_new(navit_gr, &this->p, this->w, this->h);
+
+       this->bg=graphics_gc_new(this->gr);
+       c.r=0; c.g=0; c.b=0;
+       graphics_gc_set_foreground(this->bg, &c);
+
+       this->white=graphics_gc_new(this->gr);
+       c.r=65535; c.g=65535; c.b=65535;
+       graphics_gc_set_foreground(this->white, &c);
+       graphics_gc_set_linewidth(this->white, 2);
+
+       this->font=graphics_font_new(this->gr, 200);
+       this->flag=graphics_image_new(this->gr, flag);
+       navit_add_callback(nav, callback_new_attr_1(callback_cast(osd_eta_draw), attr_position_coord_geo, this));
+
+       osd_eta_draw(this, nav, NULL);
+       g_free(flag);
+}
+
+static struct osd_priv *
+osd_eta_new(struct navit *nav, struct osd_methods *meth, struct attr **attrs)
+{
+       struct eta *this=g_new0(struct eta, 1);
+       struct attr *attr;
+       this->p.x=-80;
+       this->p.y=20;
+       this->w=60;
+       this->h=60;
+       this->active=-1;
+       attr=attr_search(attrs, NULL, attr_x);
+       if (attr)
+               this->p.x=attr->u.num;
+       attr=attr_search(attrs, NULL, attr_y);
+       if (attr)
+               this->p.y=attr->u.num;
+       navit_add_callback(nav, callback_new_attr_1(callback_cast(osd_eta_init), attr_navit, this));
+       return (struct osd_priv *) this;
+}
+
 void
 plugin_init(void)
 {
        plugin_register_osd_type("compass", osd_compass_new);
+       plugin_register_osd_type("eta", osd_eta_new);
 }