From 1f12bcee900732064e6956ac2c23c9066ab5779f Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Wed, 17 Jun 2015 15:02:29 +0300 Subject: Support shape annotations on GLFWView Add different random triangles count by using the keys 7, 8, 9 or 0. Q will clear all the annotations. W will remove one annotation. --- platform/default/glfw_view.cpp | 73 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 7 deletions(-) (limited to 'platform/default') diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 0f063e5925..61d553ae3d 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++) { + points.push_back(makeRandomPoint()); + markers.push_back("default_marker"); + } + + 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++) { - 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); + mbgl::AnnotationSegment triangle; + triangle.push_back(makeRandomPoint()); + triangle.push_back(makeRandomPoint()); + triangle.push_back(makeRandomPoint()); - points.push_back({ lat, lon }); - markers.push_back("default_marker"); + 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->addPointAnnotations(points, markers); + map->removeAnnotation(annotationIDs.back()); + annotationIDs.pop_back(); } void GLFWView::onScroll(GLFWwindow *window, double /*xOffset*/, double yOffset) { -- cgit v1.2.1