Ticket #609: osd_core.c.speedwarner.diff

File osd_core.c.speedwarner.diff, 6.7 KB (added by tegzed, 11 years ago)

Latest version of osd_core.c for speed warner as of 2010-09-04

  • osd_core.c

     
    10371037struct osd_speed_warner {
    10381038        struct osd_item item;
    10391039        struct graphics_gc *red;
     1040        struct graphics_gc *green;
     1041        struct graphics_gc *grey;
     1042        struct graphics_gc *black;
     1043        struct graphics_gc *white;
    10401044        int width;
    10411045        int active;
    10421046        int d;
     1047        double speed_exceed_limit_offset;
     1048        double speed_exceed_limit_percent;
     1049        int announce_on;
     1050        enum eAnnounceState {eNoWarn=0,eWarningTold=1};
     1051        enum eAnnounceState announce_state;
     1052        int bTextOnly;                 //text of label attribute for this osd
    10431053};
    10441054
    10451055static void
    10461056osd_speed_warner_draw(struct osd_speed_warner *this, struct navit *navit, struct vehicle *v)
    10471057{
    1048         struct point p[4];
    1049         char *text="60";
     1058        struct point p,bbox[4];
     1059        char text[16]="0";
    10501060
    10511061        osd_std_draw(&this->item);
    1052         p[0].x=this->item.w/2-this->d/4;
    1053         p[0].y=this->item.h/2-this->d/4;
    1054         graphics_draw_rectangle(this->item.gr, this->item.graphic_fg_white, p, this->d/2, this->d/2);
    1055         p[0].x=this->item.w/2;
    1056         p[0].y=this->item.h/2;
    1057         graphics_draw_circle(this->item.gr, this->item.graphic_fg_white, p, this->d/2);
    1058         graphics_draw_circle(this->item.gr, this->red, p, this->d-this->width*2);
    1059         graphics_get_text_bbox(this->item.gr, this->item.font, text, 0x10000, 0, p, 0);
    1060         p[0].x=(this->item.w-p[2].x)/2;
    1061         p[0].y=(this->item.h+p[2].y)/2-p[2].y;
    1062         graphics_draw_text(this->item.gr, this->item.graphic_fg_text, NULL, this->item.font, text, p, 0x10000, 0);
     1062        p.x=this->item.w/2-this->d/4;
     1063        p.y=this->item.h/2-this->d/4;
     1064        p.x=this->item.w/2;
     1065        p.y=this->item.h/2;
     1066        //graphics_draw_circle(this->item.gr, this->white, &p, this->d/2);
     1067
     1068    struct tracking *tracking = NULL;
     1069    struct graphics_gc *osd_color=this->grey;
     1070
     1071    if (navit) {
     1072        tracking = navit_get_tracking(navit);
     1073    }
     1074    if (tracking) {
     1075
     1076        struct attr maxspeed_attr,speed_attr;
     1077        struct item *item;
     1078        item=tracking_get_current_item(tracking);
     1079        double routespeed = -1;
     1080        double tracking_speed = -1;
     1081
     1082        int *flags=tracking_get_current_flags(tracking);
     1083        if (flags && (*flags & AF_SPEED_LIMIT) && tracking_get_attr(tracking, attr_maxspeed, &maxspeed_attr, NULL)) {
     1084            routespeed = maxspeed_attr.u.num;
     1085        }
     1086        if (routespeed == -1) {
     1087            struct vehicleprofile *prof=navit_get_vehicleprofile(navit);
     1088            struct roadprofile *rprof=NULL;
     1089            if (prof && item)
     1090                rprof=vehicleprofile_get_roadprofile(prof, item->type);
     1091            if (rprof) {
     1092                if(rprof->maxspeed!=0)
     1093                    routespeed=rprof->maxspeed;
     1094                else
     1095                    routespeed=rprof->speed;
     1096            }
     1097        }
     1098        tracking_get_attr(tracking, attr_position_speed, &speed_attr, NULL);
     1099        tracking_speed = *speed_attr.u.numd;
     1100        if( -1 != tracking_speed && -1 != routespeed ) {
     1101            snprintf(text,16,"%.0lf",routespeed);
     1102            if( this->speed_exceed_limit_offset+routespeed<tracking_speed ||
     1103                (100.0+this->speed_exceed_limit_percent)/100.0*routespeed<tracking_speed ) {
     1104                if(this->announce_state==eNoWarn && this->announce_on) {
     1105                    this->announce_state=eWarningTold; //warning told
     1106                    navit_say(navit,_("Please decrease your speed"));
     1107                }
     1108            }
     1109            if( tracking_speed <= routespeed ) {
     1110                this->announce_state=eNoWarn; //no warning
     1111                osd_color = this->green;
     1112            }
     1113            else {
     1114                osd_color = this->red;
     1115            }
     1116        } else {
     1117            osd_color = this->grey;
     1118        }
     1119    } else {
     1120        //when tracking is not available display grey
     1121        osd_color = this->grey;
     1122    }
     1123    if(0==this->bTextOnly) {
     1124      graphics_draw_circle(this->item.gr, osd_color, &p, this->d-this->width*2 );
     1125    }
     1126
     1127        graphics_get_text_bbox(this->item.gr, this->item.font, text, 0x10000, 0, bbox, 0);
     1128        p.x=(this->item.w-bbox[2].x)/2;
     1129        p.y=(this->item.h+bbox[2].y)/2-bbox[2].y;
     1130        graphics_draw_text(this->item.gr, osd_color, NULL, this->item.font, text, &p, 0x10000, 0);
    10631131        graphics_draw_mode(this->item.gr, draw_mode_end);
    10641132}
    10651133
     
    10681136{
    10691137        osd_set_std_graphic(nav, &this->item, (struct osd_priv *)this);
    10701138        navit_add_callback(nav, callback_new_attr_1(callback_cast(osd_speed_warner_draw), attr_position_coord_geo, this));
     1139
     1140
     1141
     1142        this->white=graphics_gc_new(this->item.gr);
     1143        graphics_gc_set_foreground(this->white, &(struct color ){0xffff,0xffff,0xffff,0x0000});
     1144
     1145        graphics_gc_set_linewidth(this->white, this->d/2-2 /*-this->width*/ );
     1146
    10711147        this->red=graphics_gc_new(this->item.gr);
    10721148        graphics_gc_set_foreground(this->red, &(struct color ){0xffff,0,0,0xffff});
    10731149        graphics_gc_set_linewidth(this->red, this->width);
    1074         graphics_gc_set_linewidth(this->item.graphic_fg_white, this->d/4+2);
     1150
     1151        this->green=graphics_gc_new(this->item.gr);
     1152        graphics_gc_set_foreground(this->green, &(struct color ){0,0xffff,0,0xffff});
     1153        graphics_gc_set_linewidth(this->green, this->width-2);
     1154
     1155        this->grey=graphics_gc_new(this->item.gr);
     1156        graphics_gc_set_foreground(this->grey, &(struct color ){0x8888,0x8888,0x8888,0x8888});
     1157        graphics_gc_set_linewidth(this->grey, this->width);
     1158
     1159        this->black=graphics_gc_new(this->item.gr);
     1160        graphics_gc_set_foreground(this->black, &(struct color ){0x1111,0x1111,0x1111,0x9999});
     1161        graphics_gc_set_linewidth(this->black, this->width);
     1162
    10751163        osd_speed_warner_draw(this, nav, NULL);
    10761164}
    10771165
     
    10791167osd_speed_warner_new(struct navit *nav, struct osd_methods *meth, struct attr **attrs)
    10801168{
    10811169        struct osd_speed_warner *this=g_new0(struct osd_speed_warner, 1);
     1170        struct attr *attr;
    10821171        this->item.p.x=-80;
    10831172        this->item.p.y=20;
    10841173        this->item.w=60;
     
    10861175        this->item.h=60;
    10871176        this->active=-1;
    10881177        this->item.meth.draw = osd_draw_cast(osd_speed_warner_draw);
     1178
     1179        attr = attr_search(attrs, NULL, attr_speed_exceed_limit_offset);
     1180        if (attr) {
     1181                this->speed_exceed_limit_offset = attr->u.num;
     1182        } else
     1183                this->speed_exceed_limit_offset = 15;    //by default 15 km/h
     1184
     1185        attr = attr_search(attrs, NULL, attr_speed_exceed_limit_percent);
     1186        if (attr) {
     1187                this->speed_exceed_limit_percent = attr->u.num;
     1188    } else
     1189                this->speed_exceed_limit_percent = 10;    //by default factor of 1.1
     1190
     1191        this->bTextOnly = 0;    //by default display graphics also
     1192        attr = attr_search(attrs, NULL, attr_label);
     1193        if (attr) {
     1194          if (!strcmp("text_only",attr->u.str)) {
     1195            this->bTextOnly = 1;
     1196          }
     1197        }
     1198
     1199        attr = attr_search(attrs, NULL, attr_announce_on);
     1200        if (attr)
     1201                this->announce_on = attr->u.num;
     1202        else
     1203                this->announce_on = 1;    //announce by default
    10891204        osd_set_std_attr(attrs, &this->item, 2);
    10901205        this->d=this->item.w;
    10911206        if (this->item.h < this->d)