Ticket #643: osd_core.c.stopwatch.diff

File osd_core.c.stopwatch.diff, 4.4 KB (added by tegzed, 11 years ago)

stopwatch osd , hours fixed

  • osd_core.c

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