[libshumate] demos: Add ShumateTestTileSource
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libshumate] demos: Add ShumateTestTileSource
- Date: Sat, 13 Mar 2021 21:54:27 +0000 (UTC)
commit 923faf48c7351377bde720e41eb2cd1e44ccfbc9
Author: James Westman <james jwestman net>
Date: Sat Feb 27 21:44:52 2021 -0600
demos: Add ShumateTestTileSource
Add a test pattern option to the demo. This tile source generates a test
pattern designed to help debug tile placement. It also provides an
example of custom map sources and how to switch between map styles.
demos/meson.build | 1 +
demos/shumate-demo-window.c | 82 +++++++++++++++++++++++-----
demos/shumate-demo-window.ui | 17 ++++++
demos/shumate-test-tile-source.c | 115 +++++++++++++++++++++++++++++++++++++++
demos/shumate-test-tile-source.h | 32 +++++++++++
5 files changed, 234 insertions(+), 13 deletions(-)
---
diff --git a/demos/meson.build b/demos/meson.build
index 7085e40..b1c4851 100644
--- a/demos/meson.build
+++ b/demos/meson.build
@@ -9,6 +9,7 @@ shumate_demo_sources = [
'shumate-demo.c',
'shumate-demo-window.c',
+ 'shumate-test-tile-source.c',
]
shumate_demo = executable(
diff --git a/demos/shumate-demo-window.c b/demos/shumate-demo-window.c
index 640e965..d34a686 100644
--- a/demos/shumate-demo-window.c
+++ b/demos/shumate-demo-window.c
@@ -17,6 +17,8 @@
#include "shumate-demo-window.h"
+#include "shumate-test-tile-source.h"
+
struct _ShumateDemoWindow
{
@@ -25,9 +27,13 @@ struct _ShumateDemoWindow
ShumateView *view;
GtkOverlay *overlay;
ShumateLicense *license;
+ GtkDropDown *layers_dropdown;
+ ShumateMapLayer *tile_layer;
ShumateMarkerLayer *marker_layer;
ShumatePathLayer *path_layer;
+
+ ShumateMapSource *current_source;
};
G_DEFINE_TYPE (ShumateDemoWindow, shumate_demo_window, GTK_TYPE_APPLICATION_WINDOW)
@@ -56,40 +62,86 @@ create_marker (ShumateDemoWindow *self, double lat, double lng)
}
+static void
+set_map_source (ShumateDemoWindow *self, ShumateMapSource *new_source)
+{
+ ShumateViewport *viewport = shumate_view_get_viewport (self->view);
+ ShumateMapLayer *tile_layer;
+
+ if (self->current_source) {
+ shumate_license_remove_map_source (self->license, self->current_source);
+ }
+
+ g_clear_object (&self->current_source);
+ self->current_source = new_source;
+
+ shumate_viewport_set_reference_map_source (viewport, new_source);
+ shumate_view_set_map_source (self->view, new_source);
+
+ tile_layer = shumate_map_layer_new (new_source, viewport);
+ shumate_view_insert_layer_behind (self->view, SHUMATE_LAYER (tile_layer), SHUMATE_LAYER
(self->tile_layer));
+ if (self->tile_layer) {
+ shumate_view_remove_layer (self->view, SHUMATE_LAYER (self->tile_layer));
+ }
+ self->tile_layer = tile_layer;
+
+ shumate_license_append_map_source (self->license, new_source);
+}
+
+static void
+on_layers_dropdown_notify_selected (ShumateDemoWindow *self, GParamSpec *pspec, GtkDropDown *dropdown)
+{
+ g_autoptr(ShumateMapSourceFactory) factory = NULL;
+
+ switch (gtk_drop_down_get_selected (dropdown)) {
+ case 0:
+ factory = shumate_map_source_factory_dup_default ();
+ set_map_source (self, shumate_map_source_factory_create_cached_source (factory,
SHUMATE_MAP_SOURCE_OSM_MAPNIK));
+ break;
+ case 1:
+ set_map_source (self, SHUMATE_MAP_SOURCE (shumate_test_tile_source_new ()));
+ break;
+ }
+}
+
+
+static void
+shumate_demo_window_finalize (GObject *object)
+{
+ ShumateDemoWindow *self = SHUMATE_DEMO_WINDOW (object);
+
+ g_clear_object (&self->current_source);
+
+ G_OBJECT_CLASS (shumate_demo_window_parent_class)->finalize (object);
+}
+
+
static void
shumate_demo_window_class_init (ShumateDemoWindowClass *klass)
{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+ object_class->finalize = shumate_demo_window_finalize;
+
gtk_widget_class_set_template_from_resource (widget_class,
"/org/gnome/Shumate/Demo/ui/shumate-demo-window.ui");
gtk_widget_class_bind_template_child (widget_class, ShumateDemoWindow, view);
gtk_widget_class_bind_template_child (widget_class, ShumateDemoWindow, overlay);
gtk_widget_class_bind_template_child (widget_class, ShumateDemoWindow, license);
+ gtk_widget_class_bind_template_child (widget_class, ShumateDemoWindow, layers_dropdown);
+ gtk_widget_class_bind_template_callback (widget_class, on_layers_dropdown_notify_selected);
}
static void
shumate_demo_window_init (ShumateDemoWindow *self)
{
- g_autoptr(ShumateMapSourceFactory) factory = NULL;
- g_autoptr(ShumateMapSource) map_source = NULL;
- ShumateMapLayer *layer;
ShumateScale *scale;
ShumateViewport *viewport;
gtk_widget_init_template (GTK_WIDGET (self));
- factory = shumate_map_source_factory_dup_default ();
- map_source = shumate_map_source_factory_create_cached_source (factory, SHUMATE_MAP_SOURCE_OSM_MAPNIK);
- shumate_view_set_map_source (self->view, map_source);
-
viewport = shumate_view_get_viewport (self->view);
- shumate_viewport_set_reference_map_source (viewport, map_source);
-
- layer = shumate_map_layer_new (map_source, viewport);
- shumate_view_add_layer (self->view, SHUMATE_LAYER (layer));
-
- shumate_license_append_map_source (self->license, map_source);
scale = shumate_scale_new (viewport);
g_object_set (scale,
@@ -98,6 +150,10 @@ shumate_demo_window_init (ShumateDemoWindow *self)
NULL);
gtk_overlay_add_overlay (self->overlay, GTK_WIDGET (scale));
+ /* Set the map source */
+ on_layers_dropdown_notify_selected (self, NULL, self->layers_dropdown);
+
+ /* Add the marker layers */
self->marker_layer = shumate_marker_layer_new (viewport);
shumate_view_add_layer (self->view, SHUMATE_LAYER (self->marker_layer));
self->path_layer = shumate_path_layer_new (viewport);
diff --git a/demos/shumate-demo-window.ui b/demos/shumate-demo-window.ui
index eb2edc9..6bb2a59 100644
--- a/demos/shumate-demo-window.ui
+++ b/demos/shumate-demo-window.ui
@@ -6,8 +6,19 @@
<property name="title" translatable="yes">Shumate Demo</property>
<property name="default_width">800</property>
<property name="default_height">600</property>
+ <child type="titlebar">
+ <object class="GtkHeaderBar">
+ <child>
+ <object class="GtkDropDown" id="layers_dropdown">
+ <property name="model">layers</property>
+ <signal name="notify::selected" handler="on_layers_dropdown_notify_selected" swapped="true" />
+ </object>
+ </child>
+ </object>
+ </child>
<child>
<object class="GtkOverlay" id="overlay">
+ <property name="vexpand">True</property>
<child>
<object class="ShumateView" id="view">
</object>
@@ -21,4 +32,10 @@
</object>
</child>
</template>
+ <object class="GtkStringList" id="layers">
+ <items>
+ <item translatable="yes">Mapnik (OSM)</item>
+ <item translatable="yes">Test Pattern</item>
+ </items>
+ </object>
</interface>
diff --git a/demos/shumate-test-tile-source.c b/demos/shumate-test-tile-source.c
new file mode 100644
index 0000000..0ff847c
--- /dev/null
+++ b/demos/shumate-test-tile-source.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2021 James Westman <james jwestman net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+
+#include "shumate-test-tile-source.h"
+
+struct _ShumateTestTileSource
+{
+ ShumateTileSource parent_instance;
+};
+
+G_DEFINE_TYPE (ShumateTestTileSource, shumate_test_tile_source, SHUMATE_TYPE_TILE_SOURCE)
+
+
+ShumateTestTileSource *
+shumate_test_tile_source_new (void)
+{
+ return g_object_new (SHUMATE_TYPE_TEST_TILE_SOURCE, NULL);
+}
+
+
+static GdkTexture *
+texture_new_for_surface (cairo_surface_t *surface)
+{
+ g_autoptr(GBytes) bytes = NULL;
+ GdkTexture *texture;
+
+ g_return_val_if_fail (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE, NULL);
+ g_return_val_if_fail (cairo_image_surface_get_width (surface) > 0, NULL);
+ g_return_val_if_fail (cairo_image_surface_get_height (surface) > 0, NULL);
+
+ bytes = g_bytes_new_with_free_func (cairo_image_surface_get_data (surface),
+ cairo_image_surface_get_height (surface)
+ * cairo_image_surface_get_stride (surface),
+ (GDestroyNotify) cairo_surface_destroy,
+ cairo_surface_reference (surface));
+
+ texture = gdk_memory_texture_new (cairo_image_surface_get_width (surface),
+ cairo_image_surface_get_height (surface),
+ GDK_MEMORY_B8G8R8A8_PREMULTIPLIED,
+ bytes,
+ cairo_image_surface_get_stride (surface));
+
+ return texture;
+}
+
+
+static void
+shumate_test_tile_source_fill_tile (ShumateMapSource *map_source,
+ ShumateTile *tile,
+ GCancellable *cancellable)
+{
+ g_autoptr(GdkTexture) texture = NULL;
+ guint tile_size = shumate_tile_get_size (tile);
+ int x = shumate_tile_get_x (tile), y = shumate_tile_get_y (tile);
+ int zoom = shumate_tile_get_zoom_level (tile);
+ int max_zoom = shumate_map_source_get_max_zoom_level (map_source);
+ cairo_surface_t *surface;
+ cairo_t *cr;
+ float r, g, b;
+ g_autofree char *text = NULL;
+
+ g_return_if_fail (SHUMATE_IS_TEST_TILE_SOURCE (map_source));
+ g_return_if_fail (SHUMATE_IS_TILE (tile));
+
+ surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, tile_size, tile_size);
+ cr = cairo_create (surface);
+
+ gtk_hsv_to_rgb (zoom / (double) max_zoom, 1, ((x + y) % 2 == 0) ? 1 : 0.5, &r, &g, &b);
+ cairo_set_source_rgb (cr, r, g, b);
+ cairo_paint (cr);
+
+ cairo_set_source_rgb (cr, 0, 0, 0);
+ cairo_move_to (cr, 8, tile_size - 8);
+ cairo_scale (cr, 2, 2);
+ text = g_strdup_printf ("%d, %d (z%d)", x, y, zoom);
+ cairo_show_text (cr, text);
+
+ texture = texture_new_for_surface (surface);
+ cairo_surface_destroy (surface);
+ cairo_destroy (cr);
+
+ shumate_tile_set_texture (tile, texture);
+ shumate_tile_set_fade_in (tile, TRUE);
+ shumate_tile_set_state (tile, SHUMATE_STATE_DONE);
+}
+
+
+static void
+shumate_test_tile_source_class_init (ShumateTestTileSourceClass *klass)
+{
+ ShumateMapSourceClass *map_source_class = SHUMATE_MAP_SOURCE_CLASS (klass);
+
+ map_source_class->fill_tile = shumate_test_tile_source_fill_tile;
+}
+
+
+static void
+shumate_test_tile_source_init (ShumateTestTileSource *self)
+{
+}
diff --git a/demos/shumate-test-tile-source.h b/demos/shumate-test-tile-source.h
new file mode 100644
index 0000000..8a17d44
--- /dev/null
+++ b/demos/shumate-test-tile-source.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2021 James Westman <james jwestman net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+
+#pragma once
+
+#include <glib-object.h>
+#include <shumate/shumate.h>
+
+G_BEGIN_DECLS
+
+#define SHUMATE_TYPE_TEST_TILE_SOURCE (shumate_test_tile_source_get_type())
+
+G_DECLARE_FINAL_TYPE (ShumateTestTileSource, shumate_test_tile_source, SHUMATE, TEST_TILE_SOURCE,
ShumateTileSource)
+
+ShumateTestTileSource *shumate_test_tile_source_new (void);
+
+G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]