From: Pierre-Luc Beaudoin Date: Wed, 19 Nov 2008 16:24:07 +0000 (-0500) Subject: Function to get map coordinates from events X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e703e8df2cbdc591c3171f78658e75521b5c9315;p=libchamplain Function to get map coordinates from events --- diff --git a/champlain/champlainview.c b/champlain/champlainview.c index 4ede337..8253419 100644 --- a/champlain/champlainview.c +++ b/champlain/champlainview.c @@ -771,7 +771,7 @@ champlain_view_center_on (ChamplainView *view, gdouble latitude, gdouble longitu return; } - gdouble x, y; + gint x, y; x = priv->map->longitude_to_x(priv->map, longitude, priv->map->current_level->level); y = priv->map->latitude_to_y(priv->map, latitude, priv->map->current_level->level); ChamplainPoint* anchor = &priv->map->current_level->anchor; @@ -905,3 +905,54 @@ champlain_view_add_layer (ChamplainView *view, ClutterActor *layer) clutter_container_foreach(CLUTTER_CONTAINER(layer), CLUTTER_CALLBACK(connect_marker_notify_cb), view); } + +gboolean +champlain_view_get_coords_from_event (ChamplainView *view, ClutterEvent *event, gdouble *lat, gdouble *lon) +{ + g_return_val_if_fail(CHAMPLAIN_IS_VIEW(view), FALSE); + g_return_val_if_fail(event, FALSE); // Apparently there isn't a more precise test + + ChamplainViewPrivate *priv = CHAMPLAIN_VIEW_GET_PRIVATE (view); + guint x, y; + + switch (clutter_event_type (event)) + { + case CLUTTER_BUTTON_PRESS: + case CLUTTER_BUTTON_RELEASE: + { + ClutterButtonEvent *e = (ClutterButtonEvent*) event; + x = e->x; + y = e->y; + } + break; + case CLUTTER_SCROLL: + { + ClutterScrollEvent *e = (ClutterScrollEvent*) event; + x = e->x; + y = e->y; + } + break; + case CLUTTER_MOTION: + { + ClutterMotionEvent *e = (ClutterMotionEvent*) event; + x = e->x; + y = e->y; + } + break; + case CLUTTER_ENTER: + case CLUTTER_LEAVE: + { + ClutterCrossingEvent *e = (ClutterCrossingEvent*) event; + x = e->x; + y = e->y; + } + break; + default: + return FALSE; + } + if (lat) + *lat = viewport_get_latitude_at(priv, priv->viewport_size.y + y + priv->map->current_level->anchor.y); + if (lon) + *lon = viewport_get_longitude_at(priv, priv->viewport_size.x + x + priv->map->current_level->anchor.x); + return TRUE; +} diff --git a/champlain/champlainview.h b/champlain/champlainview.h index 88810f2..ab627c5 100644 --- a/champlain/champlainview.h +++ b/champlain/champlainview.h @@ -94,4 +94,6 @@ void champlain_view_add_layer (ChamplainView *champlainView, ClutterActor *layer void champlain_view_set_size (ChamplainView *view, guint width, guint height); +gboolean champlain_view_get_coords_from_event (ChamplainView *view, ClutterEvent *event, gdouble *lat, gdouble *lon); + #endif diff --git a/demos/launcher.c b/demos/launcher.c index 958c80b..3f45562 100644 --- a/demos/launcher.c +++ b/demos/launcher.c @@ -22,14 +22,18 @@ static gboolean montreal_click (ClutterActor *actor, - ClutterEvent *event, - gpointer user_data) -{ g_print("Montreal was clicked!\n"); + ClutterButtonEvent *event, + ChamplainView * view) +{ + g_print("Montreal was clicked!\n"); + gdouble lat, lon; + if (champlain_view_get_coords_from_event (view, event, &lat, &lon)) + g_print("%f, %f \n", lat, lon); return TRUE; } static ClutterActor* -create_marker_layer () +create_marker_layer (ChamplainView *view) { ClutterActor *layer, *marker; @@ -44,7 +48,7 @@ create_marker_layer () g_signal_connect_after (marker, "button-release-event", G_CALLBACK (montreal_click), - NULL); + view); marker = champlain_marker_new_with_label("New York", "Sans 25", &white, NULL); champlain_marker_set_position(CHAMPLAIN_MARKER(marker), 40.77, -73.98); @@ -73,11 +77,11 @@ main (int argc, char *argv[]) champlain_view_set_size (CHAMPLAIN_VIEW (actor), 800, 600); - g_object_set(G_OBJECT(actor), "zoom-level", 5, NULL); - layer = create_marker_layer(); + layer = create_marker_layer(actor); champlain_view_add_layer(CHAMPLAIN_VIEW (actor), layer); clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor); + g_object_set (G_OBJECT (actor), "zoom-level", 12, NULL); champlain_view_center_on(CHAMPLAIN_VIEW(actor), 45.466, -73.75); clutter_actor_show (stage);