From: Pierre-Luc Beaudoin Date: Mon, 25 May 2009 03:41:01 +0000 (-0400) Subject: Make the view draw many lines X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bbc090e433995f4befe38a9b1aaf4148b050e7c3;p=libchamplain Make the view draw many lines --- diff --git a/champlain/champlain-view.c b/champlain/champlain-view.c index 5c95b69..0cad84a 100644 --- a/champlain/champlain-view.c +++ b/champlain/champlain-view.c @@ -142,7 +142,6 @@ struct _ChamplainViewPrivate ChamplainRectangle viewport_size; ClutterActor *user_layers; /* Contains the markers */ - ClutterActor *line_layer; /* Contains the lines */ gboolean keep_center_on_resize; @@ -156,7 +155,10 @@ struct _ChamplainViewPrivate /* champlain_view_go_to's context, kept for stop_go_to */ GoToContext *goto_context; - ChamplainLine *line; + /* Lines and shapes */ + GList *lines; + ClutterActor *line_layer; /* Contains the lines */ + }; G_DEFINE_TYPE (ChamplainView, champlain_view, CLUTTER_TYPE_GROUP); @@ -855,6 +857,8 @@ champlain_view_init (ChamplainView *view) priv->user_layers); clutter_actor_raise (priv->user_layers, priv->map_layer); + priv->lines = NULL; + /* Setup line layer */ priv->line_layer = g_object_ref (clutter_cairo_new (800, 600)); clutter_actor_show (priv->line_layer); @@ -2173,35 +2177,13 @@ champlain_view_get_zoom_on_double_click (ChamplainView *view) return priv->zoom_on_double_click; } -void -champlain_view_add_line (ChamplainView *view, - ChamplainLine *line) -{ - g_return_if_fail (CHAMPLAIN_IS_VIEW (view)); - g_return_if_fail (CHAMPLAIN_IS_LINE (line)); - - view->priv->line = g_object_ref (line); -} - static void -view_update_lines (ChamplainView *view) +draw_line (ChamplainView *view, cairo_t *cr, ChamplainLine *line) { ChamplainViewPrivate *priv = view->priv; - if (priv->line == NULL) - return; - - cairo_t *cr; - cr = clutter_cairo_create (CLUTTER_CAIRO (priv->line_layer)); - - /* Clear the drawing area */ - cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); - cairo_rectangle (cr, 0, 0, 800, 600); - cairo_fill (cr); - - /* Draw the line */ cairo_set_operator (cr, CAIRO_OPERATOR_OVER); - GList *list = g_list_first (priv->line->priv->points); + GList *list = g_list_first (line->priv->points); while (list != NULL) { ChamplainPoint *point = (ChamplainPoint*) list->data; @@ -2218,13 +2200,81 @@ view_update_lines (ChamplainView *view) cairo_line_to (cr, x, y); list = list->next; } - gint x, y; + cairo_stroke (cr); +} + +static void +view_update_lines (ChamplainView *view) +{ + ChamplainViewPrivate *priv = view->priv; + GList * lines; + cairo_t *cr; + gint x, y; + + if (priv->lines == NULL) + return; + + cr = clutter_cairo_create (CLUTTER_CAIRO (priv->line_layer)); + + /* Clear the drawing area */ + cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); + cairo_rectangle (cr, 0, 0, 800, 600); + cairo_fill (cr); + + lines = priv->lines; + while (lines != NULL) + { + ChamplainLine *line; + line = CHAMPLAIN_LINE (lines->data); + draw_line (view, cr, line); + lines = lines->next; + } + + /* Position the layer in the viewport */ x = priv->viewport_size.x; y = priv->viewport_size.y; - cairo_stroke (cr); cairo_destroy (cr); clutter_actor_set_position (priv->line_layer, x, y); } + +/** + * champlain_view_add_line: + * @view: a #ChamplainView + * @line: a #ChamplainLine + * + * Adds a #ChamplainLine to the #ChamplainView + * + * Since: 0.4 + */ +void +champlain_view_add_line (ChamplainView *view, + ChamplainLine *line) +{ + g_return_if_fail (CHAMPLAIN_IS_VIEW (view)); + g_return_if_fail (CHAMPLAIN_IS_LINE (line)); + + view->priv->lines = g_list_append (view->priv->lines, g_object_ref (line)); +} + +/** + * champlain_view_remove_line: + * @view: a #ChamplainView + * @line: a #ChamplainLine + * + * Removes a #ChamplainLine to the #ChamplainView + * + * Since: 0.4 + */ +void +champlain_view_remove_line (ChamplainView *view, + ChamplainLine *line) +{ + g_return_if_fail (CHAMPLAIN_IS_VIEW (view)); + g_return_if_fail (CHAMPLAIN_IS_LINE (line)); + + view->priv->lines = g_list_remove (view->priv->lines, line); + g_object_unref (line); +} diff --git a/champlain/champlain-view.h b/champlain/champlain-view.h index 1645cdc..1a11f1b 100644 --- a/champlain/champlain-view.h +++ b/champlain/champlain-view.h @@ -142,6 +142,9 @@ gboolean champlain_view_get_zoom_on_double_click (ChamplainView *view); void champlain_view_add_line (ChamplainView *view, ChamplainLine *line); +void champlain_view_remove_line (ChamplainView *view, + ChamplainLine *line); + G_END_DECLS diff --git a/demos/lines.c b/demos/lines.c index 2bbfd98..e566c7b 100644 --- a/demos/lines.c +++ b/demos/lines.c @@ -113,6 +113,10 @@ main (int argc, champlain_line_add_point (line, 45, -75); champlain_view_add_line (CHAMPLAIN_VIEW (actor), line); + line = champlain_line_new (); + champlain_line_add_point (line, 44, -75); + champlain_line_add_point (line, 45, -74); + champlain_view_add_line (CHAMPLAIN_VIEW (actor), line); /* Finish initialising the map view */ g_object_set (G_OBJECT (actor), "zoom-level", 12,