From c1e90f8bb0c0a8edb9369a8159176ca6d51fe535 Mon Sep 17 00:00:00 2001 From: Kaj-Michael Lang Date: Tue, 30 Oct 2007 21:53:39 +0200 Subject: [PATCH] Refresh compass every 1 sec. Don't snap directly, animate towards real heading. --- src/gtkcompass.c | 80 ++++++++++++++++++++++++++++++++++++++++-------- src/gtkcompass.h | 2 ++ 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/src/gtkcompass.c b/src/gtkcompass.c index c8c028d..7d67efe 100644 --- a/src/gtkcompass.c +++ b/src/gtkcompass.c @@ -28,6 +28,10 @@ #include #include "gtkcompass.h" +#if 0 +#define DEBUG +#endif + static void gtk_compass_finalize (GObject *object); static void gtk_compass_size_request (GtkWidget *widget, GtkRequisition *requisition); static void gtk_compass_size_allocate (GtkWidget *widget, GtkAllocation *allocate); @@ -36,6 +40,7 @@ static void gtk_compass_realize (GtkWidget *widget); static void gtk_compass_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); static void gtk_compass_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); static void gtk_compass_paint(GtkCompass *compass); +static gboolean gtk_compass_refresh_cb(GtkWidget *widget); G_DEFINE_TYPE(GtkCompass, gtk_compass, GTK_TYPE_WIDGET); @@ -79,20 +84,20 @@ switch (prop_id) { } static void -gtk_compass_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +gtk_compass_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { GtkCompass *compass; g_return_if_fail(GTK_IS_COMPASS(object)); compass=GTK_COMPASS(object); switch (prop_id) { default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); break; } } static void -gtk_compass_init (GtkCompass *compass) +gtk_compass_init(GtkCompass *compass) { compass->gc_h=NULL; compass->gc_d=NULL; @@ -101,15 +106,21 @@ compass->dest_valid=FALSE; compass->way_valid=FALSE; compass->width=300; compass->height=300; +compass->esid=0; } static gboolean -gtk_compass_cb_button_press(GtkWidget * widget, GdkEventButton * event) +gtk_compass_cb_button_press(GtkWidget *widget, GdkEventButton *event) { GtkCompass *compass; compass=GTK_COMPASS(widget); +#ifdef DEBUG +compass->data->heading=0; +compass->heading=180; +#endif + return FALSE; } @@ -122,7 +133,11 @@ GtkWidget *widget; compass=gtk_type_new(gtk_compass_get_type ()); widget=GTK_WIDGET(compass); compass->data=data; +compass->heading=0; g_signal_connect(G_OBJECT(widget), "button_press_event", G_CALLBACK(gtk_compass_cb_button_press), NULL); + +compass->esid=g_timeout_add(1000,(GSourceFunc)gtk_compass_refresh_cb, compass); + return widget; } @@ -133,6 +148,7 @@ GtkCompass *compass; g_return_if_fail(GTK_IS_COMPASS(object)); compass=GTK_COMPASS(object); +g_source_remove(compass->esid); if (GTK_WIDGET(object)->parent && GTK_WIDGET_MAPPED(object)) { gtk_widget_unmap(GTK_WIDGET(object)); @@ -149,7 +165,7 @@ GtkCompass *compass; g_return_if_fail(GTK_IS_COMPASS(widget)); g_return_if_fail(requisition != NULL); -compass = GTK_COMPASS (widget); +compass=GTK_COMPASS(widget); requisition->width=400; requisition->height=300; @@ -298,7 +314,7 @@ BOUND(fs, 1, 16); pango_context_set_matrix (compass->context, NULL); -g_snprintf(htext, 8, "%3.0f°", compass->data->heading); +g_snprintf(htext, 8, "%3.0f°", compass->heading); pango_font_description_set_size(compass->fontdesc, (10+fs) * PANGO_SCALE); pango_layout_set_font_description(compass->layout, compass->fontdesc); pango_layout_set_text(compass->layout, htext, -1); @@ -341,7 +357,7 @@ gdk_draw_line(widget->window, for (i = 0; i < 5; i++) { PangoMatrix matrix = PANGO_MATRIX_INIT; - dir = (gint) (compass->data->heading / 45) * 45 + angle[i]; + dir = (gint) (compass->heading / 45) * 45 + angle[i]; switch (dir) { case 0: @@ -377,19 +393,19 @@ for (i = 0; i < 5; i++) { break; } - tmp = ((dir - compass->data->heading) * (1.f / 180.f)) * G_PI; + tmp = ((dir - compass->heading) * (1.f / 180.f)) * G_PI; gtk_compass_draw_mark(compass, compass->gc_h, tmp, 6); x = fsize[i]; - if (abs((guint) (compass->data->heading / 45) * 45 - compass->data->heading) - > abs((guint) (compass->data->heading / 45) * 45 + 45 - compass->data->heading) && (i > 0)) + if (abs((guint) (compass->heading / 45) * 45 - compass->heading) + > abs((guint) (compass->heading / 45) * 45 + 45 - compass->heading) && (i > 0)) x = fsize[i - 1]; pango_font_description_set_size(compass->fontdesc, (10 + x + fs) * PANGO_SCALE); pango_layout_set_font_description(compass->layout, compass->fontdesc); pango_layout_set_text(compass->layout, text, -1); - pango_matrix_rotate (&matrix, -(dir-compass->data->heading)); + pango_matrix_rotate (&matrix, -(dir-compass->heading)); pango_context_set_matrix (compass->context, &matrix); pango_layout_get_pixel_size(compass->layout, &x, &y); x = compass->xoffset + hsize + ((hsize + 15 + fs) * sinf(tmp)) - x / 2, @@ -398,12 +414,12 @@ for (i = 0; i < 5; i++) { } if (compass->dest_valid) { - tmp=((compass->dest_heading-compass->data->heading) * (1.f / 180.f)) * G_PI; + tmp=((compass->dest_heading-compass->heading) * (1.f / 180.f)) * G_PI; gtk_compass_draw_mark(compass, compass->gc_d, tmp, 10); } if (compass->way_valid) { - tmp=((compass->way_heading-compass->data->heading) * (1.f / 180.f)) * G_PI; + tmp=((compass->way_heading-compass->heading) * (1.f / 180.f)) * G_PI; gtk_compass_draw_mark(compass, compass->gc_w, tmp, 10); } @@ -423,6 +439,44 @@ gtk_compass_paint(compass); return TRUE; } +static gboolean +gtk_compass_refresh_cb(GtkWidget *widget) +{ +GtkCompass *compass; +gfloat tmp; + +g_return_val_if_fail(GTK_IS_COMPASS(widget), FALSE); + +compass=GTK_COMPASS(widget); + +if ((GTK_WIDGET_MAPPED(widget)==FALSE) || (GTK_WIDGET_VISIBLE(widget)==FALSE)) { + compass->heading=compass->data->heading; + return TRUE; +} + +if (compass->heading==compass->data->heading) + return TRUE; + +tmp=fabsf(compass->heading-compass->data->heading); +if (tmp>5) + tmp=tmp/2.2; +else + compass->heading=compass->data->heading; + +if (compass->headingdata->heading) + compass->heading+=tmp; + +if (compass->heading>compass->data->heading) + compass->heading-=tmp; + +#ifdef DEBUG +g_printf("%.2f %.2f\n", compass->heading, compass->data->heading); +#endif + +gtk_widget_queue_draw_area(widget, 0, 0, compass->width, compass->height); +return TRUE; +} + void gtk_compass_refresh(GtkWidget *widget) { diff --git a/src/gtkcompass.h b/src/gtkcompass.h index f4e7919..e760350 100644 --- a/src/gtkcompass.h +++ b/src/gtkcompass.h @@ -47,10 +47,12 @@ struct _GtkCompass { PangoFontDescription *fontdesc; GpsData *data; + gfloat heading; gboolean dest_valid; gboolean way_valid; gfloat dest_heading; gfloat way_heading; + gint esid; }; struct _GtkCompassClass { -- 2.39.5