diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-05-02 11:49:17 +0200 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-05-03 10:01:19 +0300 |
commit | 8a3d07fcd83d01415b672dd5f45a5e08af7d5594 (patch) | |
tree | 711f2e8343fb67b8ac76e13fb2a2e848c963ff78 | |
parent | ad30ba2cd145846b1c8a6c8bd26b427d48dde60f (diff) | |
download | qtlocation-mapboxgl-8a3d07fcd83d01415b672dd5f45a5e08af7d5594.tar.gz |
[core] Add custom source to glfw-app
-rw-r--r-- | platform/glfw/glfw_view.cpp | 51 | ||||
-rw-r--r-- | platform/glfw/glfw_view.hpp | 1 |
2 files changed, 52 insertions, 0 deletions
diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index fe956d8a68..cd979822c4 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -5,9 +5,11 @@ #include <mbgl/annotation/annotation.hpp> #include <mbgl/style/style.hpp> +#include <mbgl/style/sources/custom_geometry_source.hpp> #include <mbgl/style/image.hpp> #include <mbgl/style/transition_options.hpp> #include <mbgl/style/layers/fill_extrusion_layer.hpp> +#include <mbgl/style/layers/line_layer.hpp> #include <mbgl/style/expression/dsl.hpp> #include <mbgl/util/logging.hpp> #include <mbgl/util/platform.hpp> @@ -128,6 +130,7 @@ GLFWView::GLFWView(bool fullscreen_, bool benchmark_) printf("- Press `A` to cycle through Mapbox offices in the world + dateline monument\n"); printf("- Press `B` to cycle through the color, stencil, and depth buffer\n"); printf("- Press `D` to cycle through camera bounds: inside, crossing IDL at left, crossing IDL at right, and disabled\n"); + printf("- Press `T` to add custom geometry source\n"); printf("\n"); printf("- Press `1` through `6` to add increasing numbers of point annotations for testing\n"); printf("- Press `7` through `0` to add increasing numbers of shape annotations for testing\n"); @@ -320,6 +323,9 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, } } } break; + case GLFW_KEY_T: + view->toggleCustomSource(); + break; } } @@ -676,6 +682,51 @@ void GLFWView::toggle3DExtrusions(bool visible) { map->getStyle().addLayer(std::move(extrusionLayer)); } +void GLFWView::toggleCustomSource() { + if (!map->getStyle().getSource("custom")) { + mbgl::style::CustomGeometrySource::Options options; + options.cancelTileFunction = [](const mbgl::CanonicalTileID&) {}; + options.fetchTileFunction = [&](const mbgl::CanonicalTileID& tileID) { + double gridSpacing = 0.1; + mbgl::FeatureCollection features; + const mbgl::LatLngBounds bounds(tileID); + for (double y = ceil(bounds.north() / gridSpacing) * gridSpacing; y >= floor(bounds.south() / gridSpacing) * gridSpacing; y -= gridSpacing) { + + mapbox::geojson::line_string gridLine; + gridLine.emplace_back(bounds.west(), y); + gridLine.emplace_back(bounds.east(), y); + + features.emplace_back(gridLine); + } + + for (double x = floor(bounds.west() / gridSpacing) * gridSpacing; x <= ceil(bounds.east() / gridSpacing) * gridSpacing; x += gridSpacing) { + mapbox::geojson::line_string gridLine; + gridLine.emplace_back(x, bounds.south()); + gridLine.emplace_back(x, bounds.north()); + + features.emplace_back(gridLine); + } + auto source = static_cast<mbgl::style::CustomGeometrySource *>(map->getStyle().getSource("custom")); + if (source) { + source->setTileData(tileID, features); + source->invalidateTile(tileID); + } + }; + map->getStyle().addSource(std::make_unique<mbgl::style::CustomGeometrySource>("custom", options)); + } + + auto* layer = map->getStyle().getLayer("grid"); + + if (!layer) { + auto lineLayer = std::make_unique<mbgl::style::LineLayer>("grid", "custom"); + lineLayer->setLineColor(mbgl::Color{ 1.0, 0.0, 0.0, 1.0 }); + map->getStyle().addLayer(std::move(lineLayer)); + } else { + layer->setVisibility(layer->getVisibility() == mbgl::style::VisibilityType::Visible ? + mbgl::style::VisibilityType::None : mbgl::style::VisibilityType::Visible); + } +} + namespace mbgl { namespace platform { diff --git a/platform/glfw/glfw_view.hpp b/platform/glfw/glfw_view.hpp index 1957970b85..1f27bb4421 100644 --- a/platform/glfw/glfw_view.hpp +++ b/platform/glfw/glfw_view.hpp @@ -84,6 +84,7 @@ private: void addRandomCustomPointAnnotations(int count); void addAnimatedAnnotation(); void updateAnimatedAnnotations(); + void toggleCustomSource(); void clearAnnotations(); void popAnnotation(); |