Ticket #643: osd_core.c.643.diff

File osd_core.c.643.diff, 5.3 KB (added by number6, 11 years ago)
  • osd_core.c

     
    4848#include "roadprofile.h"
    4949#include "osd.h"
    5050#include "speech.h"
     51#include "event.h"
    5152
    5253struct compass {
    5354    struct osd_item osd_item;
     
    161162    return g_strdup_printf("%.0f", num);
    162163}
    163164
     165 
     166struct stopwatch {
     167    struct osd_item osd_item;
     168    int width;
     169    struct graphics_gc *orange,*white;
     170    struct callback *click_cb;
     171 
     172    int bActive;                //counting or not
     173    time_t current_base_time;   //base time of currently measured time interval
     174    time_t sum_time;            //sum of previous time intervals (except current intervals)
     175    time_t last_click_time;     //time of last click (for double click handling)
     176};
     177 
     178static void 
     179osd_stopwatch_draw(struct stopwatch *this, struct navit *nav,
     180        struct vehicle *v)
     181{
     182    osd_std_draw(&this->osd_item);
     183 
     184    char buffer[32]="00:00:00";
     185    struct point p, bbox[4];
     186    time_t total_sec,total_min,total_hours,total_days;
     187    total_sec = this->sum_time;
     188 
     189    if(this->bActive) {
     190        total_sec += time(0)-this->current_base_time;
     191        }
     192 
     193    total_min = total_sec/60;
     194    total_hours = total_min/60;
     195    total_days = total_hours/24;
     196 
     197    if (total_days==0) {
     198        snprintf(buffer,32,"%02d:%02d:%02d", (int)total_hours%24, (int)total_min%60, (int)total_sec%60);
     199    } else {
     200        snprintf(buffer,32,"%02dd %02d:%02d:%02d",
     201        (int)total_days, (int)total_hours%24, (int)total_min%60, (int)total_sec%60); 
     202    }
     203 
     204    graphics_get_text_bbox(this->osd_item.gr, this->osd_item.font, buffer, 0x10000, 0, bbox, 0);
     205    p.x=(this->osd_item.w-bbox[2].x)/2;
     206    p.y = this->osd_item.h-this->osd_item.h/10;
     207 
     208    struct graphics_gc *curr_color = this->bActive?this->white:this->orange;
     209    graphics_draw_text(this->osd_item.gr, curr_color, NULL, this->osd_item.font, buffer, &p, 0x10000, 0);
     210    graphics_draw_mode(this->osd_item.gr, draw_mode_end);
     211}
     212 
     213 
    164214static void
     215osd_stopwatch_click(struct stopwatch *this, struct navit *nav, int pressed, int button, struct point *p)
     216{
     217    struct point bp = this->osd_item.p;
     218    osd_wrap_point(&bp, nav);
     219    if ((p->x < bp.x || p->y < bp.y || p->x > bp.x + this->osd_item.w || p->y > bp.y + this->osd_item.h) && !this->osd_item.pressed) 
     220    return;
     221 
     222    navit_ignore_button(nav);
     223    if (pressed) { //single click handling
     224 
     225        if(this->bActive) {
     226        this->sum_time += time(0)-this->current_base_time;
     227        this->current_base_time = 0;
     228        } else {
     229            this->current_base_time = time(0);
     230        }
     231 
     232        this->bActive ^= 1;  //toggle active flag
     233 
     234        if (this->last_click_time == time(0)) { //double click handling
     235        this->bActive = 0;
     236        this->current_base_time = 0;
     237        this->sum_time = 0;
     238        }
     239 
     240    this->last_click_time = time(0);
     241    }
     242 
     243    osd_stopwatch_draw(this, nav,NULL);
     244}
     245 
     246 
     247static void
     248osd_stopwatch_init(struct stopwatch *this, struct navit *nav)
     249{
     250 
     251    struct color c;
     252 
     253    osd_set_std_graphic(nav, &this->osd_item, (struct osd_priv *)this);
     254 
     255    this->orange = graphics_gc_new(this->osd_item.gr);
     256    c.r = 0xFFFF;
     257    c.g = 0xA5A5;
     258    c.b = 0x0000;
     259    c.a = 65535;
     260    graphics_gc_set_foreground(this->orange, &c);
     261    graphics_gc_set_linewidth(this->orange, this->width);
     262 
     263    this->white = graphics_gc_new(this->osd_item.gr);
     264    c.r = 65535;
     265    c.g = 65535;
     266    c.b = 65535;
     267    c.a = 65535;
     268    graphics_gc_set_foreground(this->white, &c);
     269    graphics_gc_set_linewidth(this->white, this->width);
     270 
     271 
     272    graphics_gc_set_linewidth(this->osd_item.graphic_fg_white, this->width);
     273 
     274    event_add_timeout(500, 1, callback_new_1(callback_cast(osd_stopwatch_draw), this));
     275 
     276    navit_add_callback(nav, this->click_cb = callback_new_attr_1(callback_cast (osd_stopwatch_click), attr_button, this));
     277 
     278    osd_stopwatch_draw(this, nav, NULL);
     279}
     280 
     281static struct osd_priv *
     282osd_stopwatch_new(struct navit *nav, struct osd_methods *meth,
     283        struct attr **attrs)
     284{
     285    struct stopwatch *this = g_new0(struct stopwatch, 1);
     286    struct attr *attr;
     287    this->osd_item.p.x = 120;
     288    this->osd_item.p.y = 20;
     289    this->osd_item.w = 60;
     290    this->osd_item.h = 80;
     291    this->osd_item.navit = nav;
     292    this->osd_item.font_size = 200;
     293    this->osd_item.meth.draw = osd_draw_cast(osd_stopwatch_draw);
     294 
     295    this->bActive = 0; //do not count on init
     296    this->current_base_time = 0;
     297    this->sum_time = 0;
     298    this->last_click_time = 0;
     299 
     300    osd_set_std_attr(attrs, &this->osd_item, 2);
     301    attr = attr_search(attrs, NULL, attr_width);
     302    this->width=attr ? attr->u.num : 2;
     303    navit_add_callback(nav, callback_new_attr_1(callback_cast(osd_stopwatch_init), attr_graphics_ready, this));
     304    return (struct osd_priv *) this;
     305}
     306 
     307 
     308static void
    165309osd_compass_draw(struct compass *this, struct navit *nav,
    166310         struct vehicle *v)
    167311{
     
    17081852        plugin_register_osd_type("volume", osd_volume_new);
    17091853        plugin_register_osd_type("scale", osd_scale_new);
    17101854        plugin_register_osd_type("image", osd_image_new);
     1855        plugin_register_osd_type("stopwatch", osd_stopwatch_new);
    17111856}