diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp index 094974b59..9a7e29fe2 100644 --- a/include/mbgl/platform/default/glfw_view.hpp +++ b/include/mbgl/platform/default/glfw_view.hpp @@ -37,7 +37,15 @@ public: void fps(); private: + mbgl::LatLng makeRandomPoint() const; + void addRandomPointAnnotations(int count); + void addRandomShapeAnnotations(int count); + + void clearAnnotations(); + void popAnnotation(); + + mbgl::AnnotationIDs annotationIDs; private: bool fullscreen = false; diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 0f063e592..61d553ae3 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -114,37 +114,96 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, if (!mods) view->map->resetNorth(); break; + case GLFW_KEY_Q: + view->clearAnnotations(); + break; } } if (action == GLFW_RELEASE || action == GLFW_REPEAT) { switch (key) { + case GLFW_KEY_W: view->popAnnotation(); break; case GLFW_KEY_1: view->addRandomPointAnnotations(1); break; case GLFW_KEY_2: view->addRandomPointAnnotations(10); break; case GLFW_KEY_3: view->addRandomPointAnnotations(100); break; case GLFW_KEY_4: view->addRandomPointAnnotations(1000); break; case GLFW_KEY_5: view->addRandomPointAnnotations(10000); break; case GLFW_KEY_6: view->addRandomPointAnnotations(100000); break; + case GLFW_KEY_7: view->addRandomShapeAnnotations(1); break; + case GLFW_KEY_8: view->addRandomShapeAnnotations(10); break; + case GLFW_KEY_9: view->addRandomShapeAnnotations(100); break; + case GLFW_KEY_0: view->addRandomShapeAnnotations(1000); break; } } } +mbgl::LatLng GLFWView::makeRandomPoint() const { + const auto sw = map->latLngForPixel({ 0, 0 }); + const auto ne = map->latLngForPixel({ double(width), double(height) }); + + const double lon = sw.longitude + (ne.longitude - sw.longitude) * (double(std::rand()) / RAND_MAX); + const double lat = sw.latitude + (ne.latitude - sw.latitude) * (double(std::rand()) / RAND_MAX); + + return { lat, lon }; +} + + void GLFWView::addRandomPointAnnotations(int count) { std::vector points; std::vector markers; - const auto sw = map->latLngForPixel({ 0, 0 }); - const auto ne = map->latLngForPixel({ double(width), double(height) }); - for (int i = 0; i < count; i++) { - const double lon = sw.longitude + (ne.longitude - sw.longitude) * (double(std::rand()) / RAND_MAX); - const double lat = sw.latitude + (ne.latitude - sw.latitude) * (double(std::rand()) / RAND_MAX); - - points.push_back({ lat, lon }); + points.push_back(makeRandomPoint()); markers.push_back("default_marker"); } - map->addPointAnnotations(points, markers); + auto newIDs = map->addPointAnnotations(points, markers); + annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end()); +} + +void GLFWView::addRandomShapeAnnotations(int count) { + std::vector shapes; + std::vector shapesProperties; + + mbgl::FillProperties fillProperties; + fillProperties.opacity = .1; + + mbgl::StyleProperties properties; + properties.set(fillProperties); + + for (int i = 0; i < count; i++) { + mbgl::AnnotationSegment triangle; + triangle.push_back(makeRandomPoint()); + triangle.push_back(makeRandomPoint()); + triangle.push_back(makeRandomPoint()); + + mbgl::AnnotationSegments segments; + segments.push_back(triangle); + + shapes.push_back(segments); + shapesProperties.push_back(properties); + } + + auto newIDs = map->addShapeAnnotations(shapes, shapesProperties); + annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end()); +} + +void GLFWView::clearAnnotations() { + if (annotationIDs.empty()) { + return; + } + + map->removeAnnotations(annotationIDs); + annotationIDs.clear(); +} + +void GLFWView::popAnnotation() { + if (annotationIDs.empty()) { + return; + } + + map->removeAnnotation(annotationIDs.back()); + annotationIDs.pop_back(); } void GLFWView::onScroll(GLFWwindow *window, double /*xOffset*/, double yOffset) {