From cb94cdd88246219b83ae84b67463ee2714463324 Mon Sep 17 00:00:00 2001 From: Pierre-Luc Beaudoin Date: Thu, 14 Aug 2008 22:31:21 -0400 Subject: [PATCH] Add Maps --- src/Makefile.am | 6 +- src/champlain.h | 2 + src/champlain_map.c | 65 +++++++++++++++++++ src/champlain_map.h | 46 +++++++++++++ src/champlain_map_tile.h | 34 ++++++++++ src/champlain_map_zoom_level.h | 36 +++++++++++ src/champlain_private.h | 26 ++++++++ src/champlain_widget.c | 114 ++++++++++++++++++++------------- src/champlain_widget.h | 5 +- 9 files changed, 286 insertions(+), 48 deletions(-) create mode 100644 src/champlain_map.c create mode 100644 src/champlain_map.h create mode 100644 src/champlain_map_tile.h create mode 100644 src/champlain_map_zoom_level.h create mode 100644 src/champlain_private.h diff --git a/src/Makefile.am b/src/Makefile.am index 0a518bb..27962a3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,7 @@ BUILT_SOURCES = \ champlain-marshal.h \ - champlain-marshal.c + champlain-marshal.c CLEANFILES = $(BUILT_SOURCES) @@ -15,7 +15,9 @@ nodist_champlain_SOURCES = \ champlain_SOURCES = $(CHAMPLAIN_MARSHAL_LIST) \ champlain_widget.c \ - launcher.c + champlain_map.c \ + launcher.c + champlain_LDADD = $(DEPS_LIBS) AM_CPPFLAGS = $(DEPS_CFLAGS) diff --git a/src/champlain.h b/src/champlain.h index 5551c75..248f647 100644 --- a/src/champlain.h +++ b/src/champlain.h @@ -20,6 +20,8 @@ #ifndef CHAMPLAIN_H #define CHAMPLAIN_H +#include "champlain_defines.h" #include "champlain_widget.h" +#include "champlain_map.h" #endif diff --git a/src/champlain_map.c b/src/champlain_map.c new file mode 100644 index 0000000..584846a --- /dev/null +++ b/src/champlain_map.c @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2008 Pierre-Luc Beaudoin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + #include + +ChamplainMap* +champlain_map_new (ChamplainMapSource source) +{ + ChamplainMap* map = g_new0(ChamplainMap, 1); + map->name = "OpenStreetMap"; + map->zoom_levels = 1; + return map; +} + +void +champlain_map_create_tiles(ChamplainMap* map, gint zoom_level) +{ + if (zoom_level == 1) + { + map->current_level = g_new0(ChamplainMapZoomLevel, 1); + map->current_level->level = zoom_level; + map->current_level->row_count = 5; + map->current_level->column_count = 4; + map->current_level->tile_size = 200; + map->current_level->tiles = g_ptr_array_sized_new (20); + map->current_level->group = clutter_group_new (); + + ClutterColor white; + clutter_color_parse ("white", &white); + ClutterColor blue; + clutter_color_parse ("blue", &blue); + + int i; + for (i = 0; i < 20; i++) + { + int x = i % map->current_level->row_count; + int y = i / map->current_level->row_count; + + ClutterColor * color = ( (y + x) % 2 ? &blue : &white); + ClutterActor * tile = clutter_rectangle_new_with_color (color); + clutter_actor_set_position (tile, x * map->current_level->tile_size, y * map->current_level->tile_size); + clutter_actor_set_size (tile, map->current_level->tile_size, map->current_level->tile_size); + clutter_actor_show (tile); + + clutter_container_add (CLUTTER_CONTAINER (map->current_level->group), tile, NULL); + g_ptr_array_add (map->current_level->tiles, tile); + } + } +} diff --git a/src/champlain_map.h b/src/champlain_map.h new file mode 100644 index 0000000..a84ea63 --- /dev/null +++ b/src/champlain_map.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2008 Pierre-Luc Beaudoin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef CHAMPLAIN_MAP_H +#define CHAMPLAIN_MAP_H + +#include "champlain_defines.h" +#include "champlain_map_zoom_level.h" +#include +#include + +typedef enum +{ + CHAMPLAIN_MAP_SOURCE_OPENSTREETMAP, + CHAMPLAIN_MAP_SOURCE_GOOGLE +} ChamplainMapSource; + +typedef struct +{ + int zoom_levels; + const gchar* name; + ChamplainMapZoomLevel* current_level; + +} ChamplainMap; + + +CHAMPLAIN_API ChamplainMap* champlain_map_new (ChamplainMapSource source); + + +#endif diff --git a/src/champlain_map_tile.h b/src/champlain_map_tile.h new file mode 100644 index 0000000..aab6bd9 --- /dev/null +++ b/src/champlain_map_tile.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2008 Pierre-Luc Beaudoin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef CHAMPLAIN_MAP_TILE_H +#define CHAMPLAIN_MAP_TILE_H + +#include +#include + +typedef struct +{ + ClutterActor* actor; + int x; + int y; + +} ChamplainMapTile; + +#endif diff --git a/src/champlain_map_zoom_level.h b/src/champlain_map_zoom_level.h new file mode 100644 index 0000000..9a3138b --- /dev/null +++ b/src/champlain_map_zoom_level.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2008 Pierre-Luc Beaudoin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef CHAMPLAIN_MAP_ZOOM_LEVEL_H +#define CHAMPLAIN_MAP_ZOOM_LEVEL_H + +#include +#include + +typedef struct +{ + int level; + ClutterActor* group; + int row_count; + int column_count; + int tile_size; + GPtrArray *tiles; +} ChamplainMapZoomLevel; + +#endif diff --git a/src/champlain_private.h b/src/champlain_private.h new file mode 100644 index 0000000..a868150 --- /dev/null +++ b/src/champlain_private.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2008 Pierre-Luc Beaudoin + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#ifndef CHAMPLAIN_PRIVATE_H +#define CHAMPLAIN_PRIVATE_H + +void champlain_map_create_tiles(gint zoom_level); + +#endif diff --git a/src/champlain_widget.c b/src/champlain_widget.c index e577ba0..2aaa153 100644 --- a/src/champlain_widget.c +++ b/src/champlain_widget.c @@ -20,10 +20,12 @@ #include "config.h" #include "champlain_defines.h" +#include "champlain_map_tile.h" +#include "champlain_map.h" #include "champlain_widget.h" #include "champlain-marshal.h" -#include +#include #include #include #include @@ -64,6 +66,8 @@ struct _ChamplainWidgetPrivate // Scrolling ScrollMotion position; ScrollMotion hitPoint; + + ChamplainMap* map; }; @@ -189,6 +193,55 @@ champlain_widget_init (ChamplainWidget * champlainWidget) } +static void +champlain_widget_verify_tiles (ChamplainWidget * champlainWidget) +{ + ChamplainWidgetPrivate *priv = CHAMPLAIN_WIDGET_GET_PRIVATE (champlainWidget); + ClutterActor *stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutterEmbed)); + + // Check for tiles that left the viewport + ClutterColor red; + clutter_color_parse ("red", &red); + ClutterColor white; + clutter_color_parse ("white", &white); + ClutterColor blue; + clutter_color_parse ("blue", &blue); + int i; + /*for (i = 0; i < priv->tiles->len; i++) + { + int x = i % ROW_SIZE; + int y = i / ROW_SIZE; + ClutterActor * tile = CLUTTER_ACTOR(g_ptr_array_index (priv->tiles, i)); + ClutterUnit actor_x, actor_y, stage_w, stage_h; + clutter_actor_get_sizeu(stage, &stage_w, &stage_h); + clutter_actor_get_positionu(tile, &actor_x, &actor_y); + if( (actor_x + CLUTTER_UNITS_FROM_INT(TILE_SIZE) + priv->position.x < 0 || actor_x + priv->position.x > stage_w) || + (actor_y + CLUTTER_UNITS_FROM_INT(TILE_SIZE) + priv->position.y < 0 || actor_y + priv->position.y > stage_h)) + { + //clutter_rectangle_set_color(CLUTTER_RECTANGLE(tile), &red); + } + else + { + clutter_rectangle_set_color(CLUTTER_RECTANGLE(tile), ( (y + x) % 2 % 2 ? &blue : &white)); + } + }*/ + + + // Check for missing tiles in the viewport + /*if ( priv->position.x > 0 ) + { + gboolean exist = FALSE; + for (i = 0; i < 20; i++) + { + ClutterActor * tile = CLUTTER_ACTOR(g_ptr_array_index (priv->tiles, i)); + ClutterUnit actor_x, actor_y; + clutter_actor_get_positionu(tile, &actor_x, &actor_y); + //if (actor_x < 0 + } + + }*/ +} + static gboolean viewport_motion_event_cb (ClutterActor * actor, ClutterMotionEvent * event, ChamplainWidget * champlainWidget) { @@ -214,6 +267,7 @@ viewport_motion_event_cb (ClutterActor * actor, ClutterMotionEvent * event, Cham priv->position.y += dy - priv->hitPoint.y; clutter_actor_set_positionu (priv->viewport, priv->position.x, priv->position.y); + champlain_widget_verify_tiles(champlainWidget); } return TRUE; @@ -302,47 +356,11 @@ champlain_widget_load_map (ChamplainWidget * champlainWidget) { ChamplainWidgetPrivate *priv = CHAMPLAIN_WIDGET_GET_PRIVATE (champlainWidget); - ClutterActor *stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutterEmbed)); - - ClutterActor *viewport = clutter_group_new (); - clutter_actor_set_reactive (CLUTTER_ACTOR (viewport), TRUE); - g_signal_connect (CLUTTER_ACTOR (viewport), - "captured-event", G_CALLBACK (viewport_captured_event_cb), champlainWidget); - - ClutterColor white; - clutter_color_parse ("white", &white); - ClutterColor blue; - clutter_color_parse ("blue", &blue); - ClutterActor *group = clutter_group_new (); - - ClutterActor *rect = clutter_rectangle_new_with_color (&blue); - clutter_actor_set_position (rect, 0, 0); - clutter_actor_set_size (rect, 100, 100); - clutter_actor_show (rect); - clutter_container_add (CLUTTER_CONTAINER (group), rect, NULL); - - rect = clutter_rectangle_new_with_color (&white); - clutter_actor_set_position (rect, 0, 100); - clutter_actor_set_size (rect, 100, 100); - clutter_actor_show (rect); - clutter_container_add (CLUTTER_CONTAINER (group), rect, NULL); - - rect = clutter_rectangle_new_with_color (&blue); - clutter_actor_set_position (rect, 100, 100); - clutter_actor_set_size (rect, 100, 100); - clutter_actor_show (rect); - clutter_container_add (CLUTTER_CONTAINER (group), rect, NULL); - - rect = clutter_rectangle_new_with_color (&white); - clutter_actor_set_position (rect, 100, 0); - clutter_actor_set_size (rect, 100, 100); - clutter_actor_show (rect); - clutter_container_add (CLUTTER_CONTAINER (group), rect, NULL); - - priv->viewport = viewport; - clutter_container_add (CLUTTER_CONTAINER (viewport), group, NULL); - - clutter_container_add_actor (CLUTTER_CONTAINER (stage), viewport); + priv->map = champlain_map_new(CHAMPLAIN_MAP_SOURCE_OPENSTREETMAP); + + champlain_map_create_tiles(priv->map, 1); + + clutter_container_add_actor (CLUTTER_CONTAINER (priv->viewport), priv->map->current_level->group); } @@ -353,14 +371,22 @@ champlain_widget_new () ChamplainWidgetPrivate *priv = CHAMPLAIN_WIDGET_GET_PRIVATE (widget); priv->clutterEmbed = gtk_clutter_embed_new (); - ClutterActor *stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutterEmbed)); + /* Setup stage */ + ClutterActor *stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutterEmbed)); ClutterColor black; clutter_color_parse ("black", &black); clutter_stage_set_color (CLUTTER_STAGE (stage), &black); gtk_container_add (GTK_CONTAINER (widget), priv->clutterEmbed); + /* Setup viewport */ + priv->viewport = clutter_group_new (); + clutter_actor_set_reactive (CLUTTER_ACTOR (priv->viewport), TRUE); + g_signal_connect (CLUTTER_ACTOR (priv->viewport), + "captured-event", G_CALLBACK (viewport_captured_event_cb), widget); + clutter_container_add_actor (CLUTTER_CONTAINER (stage), priv->viewport); + champlain_widget_load_map (widget); - + return GTK_WIDGET (widget); } diff --git a/src/champlain_widget.h b/src/champlain_widget.h index 019b522..7eaeebd 100644 --- a/src/champlain_widget.h +++ b/src/champlain_widget.h @@ -25,7 +25,6 @@ #include #include - G_BEGIN_DECLS #define CHAMPLAIN_TYPE_WIDGET (champlain_widget_get_type()) #define CHAMPLAIN_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), CHAMPLAIN_TYPE_WIDGET, ChamplainWidget)) @@ -46,7 +45,6 @@ struct _ChamplainWidgetClass { GtkBinClass parent_class; - ChamplainWidget *(*create_widget) (ChamplainWidget * widget); void (*set_scroll_adjustments) (ChamplainWidget * widget, GtkAdjustment * hadjustment, GtkAdjustment * vadjustment); @@ -57,4 +55,7 @@ CHAMPLAIN_API GType champlain_widget_get_type (void); CHAMPLAIN_API GtkWidget *champlain_widget_new (void); +#define TILE_SIZE 100 +#define ROW_SIZE 5 + #endif -- 2.39.5