[core] Add custom source to glfw-app
This commit is contained in:
@@ -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 {
|
||||
|
||||
|
||||
@@ -84,6 +84,7 @@ private:
|
||||
void addRandomCustomPointAnnotations(int count);
|
||||
void addAnimatedAnnotation();
|
||||
void updateAnimatedAnnotations();
|
||||
void toggleCustomSource();
|
||||
|
||||
void clearAnnotations();
|
||||
void popAnnotation();
|
||||
|
||||
Reference in New Issue
Block a user