summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2019-05-02 11:49:17 +0200
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2019-05-02 11:43:26 +0300
commit2db496e04bf6a7f7b6eddf70fefe4fba4d9868c4 (patch)
treeea91e67cd0ca6af6753b6168576f04efcb236731
parentb7b25fde783f088bf1f23bdf943a360b24fdbd78 (diff)
downloadqtlocation-mapboxgl-upstream/alexshalamov_custom_geometry_source_fixes.tar.gz
[core] Add custom source to glfw-appupstream/alexshalamov_custom_geometry_source_fixes
-rw-r--r--platform/glfw/glfw_view.cpp51
-rw-r--r--platform/glfw/glfw_view.hpp1
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();