diff options
author | Juha Alanen <juha.alanen@mapbox.com> | 2019-08-29 13:40:01 +0300 |
---|---|---|
committer | Juha Alanen <19551460+jmalanen@users.noreply.github.com> | 2019-09-18 14:29:15 +0300 |
commit | 0042f0b9f5de7151f14300f02a559b6ecb48a507 (patch) | |
tree | 23bf9e1b6f1d0885665817172e4b806db78d4196 /platform/glfw | |
parent | 7c4c091a2bc1f382a79a7015155eeee6e7fcefcf (diff) | |
download | qtlocation-mapboxgl-0042f0b9f5de7151f14300f02a559b6ecb48a507.tar.gz |
[glfw] Add feature state support
Diffstat (limited to 'platform/glfw')
-rw-r--r-- | platform/glfw/glfw_view.cpp | 80 | ||||
-rw-r--r-- | platform/glfw/glfw_view.hpp | 2 |
2 files changed, 82 insertions, 0 deletions
diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 43c4de9759..7d05bab43a 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -6,10 +6,12 @@ #include <mbgl/annotation/annotation.hpp> #include <mbgl/style/style.hpp> #include <mbgl/style/sources/custom_geometry_source.hpp> +#include <mbgl/style/sources/geojson_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/layers/fill_layer.hpp> #include <mbgl/style/expression/dsl.hpp> #include <mbgl/util/logging.hpp> #include <mbgl/util/platform.hpp> @@ -323,6 +325,48 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, case GLFW_KEY_T: view->toggleCustomSource(); break; + case GLFW_KEY_F: { + using namespace mbgl; + using namespace mbgl::style; + using namespace mbgl::style::expression::dsl; + + auto& style = view->map->getStyle(); + if (!style.getSource("states")) { + std::string url = "https://docs.mapbox.com/mapbox-gl-js/assets/us_states.geojson"; + auto source = std::make_unique<GeoJSONSource>("states"); + source->setURL(url); + style.addSource(std::move(source)); + + mbgl::CameraOptions cameraOptions; + cameraOptions.center = mbgl::LatLng { 42.619626, -103.523181 }; + cameraOptions.zoom = 3; + cameraOptions.pitch = 0; + cameraOptions.bearing = 0; + view->map->jumpTo(cameraOptions); + } + + auto layer = style.getLayer("state-fills"); + if (!layer) { + auto fillLayer = std::make_unique<FillLayer>("state-fills", "states"); + fillLayer->setFillColor(mbgl::Color{ 0.0, 0.0, 1.0, 0.5 }); + fillLayer->setFillOpacity(PropertyExpression<float>(createExpression(R"(["case", ["boolean", ["feature-state", "hover"], false], 1, 0.5])"))); + style.addLayer(std::move(fillLayer)); + } else { + layer->setVisibility(layer->getVisibility() == mbgl::style::VisibilityType::Visible ? + mbgl::style::VisibilityType::None : mbgl::style::VisibilityType::Visible); + } + + layer = style.getLayer("state-borders"); + if (!layer) { + auto borderLayer = std::make_unique<LineLayer>("state-borders", "states"); + borderLayer->setLineColor(mbgl::Color{ 0.0, 0.0, 1.0, 1.0 }); + borderLayer->setLineWidth(PropertyExpression<float>(createExpression(R"(["case", ["boolean", ["feature-state", "hover"], false], 2, 1])"))); + style.addLayer(std::move(borderLayer)); + } else { + layer->setVisibility(layer->getVisibility() == mbgl::style::VisibilityType::Visible ? + mbgl::style::VisibilityType::None : mbgl::style::VisibilityType::Visible); + } + } break; } } @@ -537,6 +581,7 @@ void GLFWView::onMouseClick(GLFWwindow *window, int button, int action, int modi } view->lastClick = now; } + } } @@ -558,6 +603,41 @@ void GLFWView::onMouseMove(GLFWwindow *window, double x, double y) { } view->lastX = x; view->lastY = y; + + auto& style = view->map->getStyle(); + if (style.getLayer("state-fills")) { + auto screenCoordinate = mbgl::ScreenCoordinate { view->lastX, view->lastY }; + const mbgl::RenderedQueryOptions queryOptions({{{ "state-fills" }}, {}}); + auto result = view->rendererFrontend->getRenderer()->queryRenderedFeatures(screenCoordinate, queryOptions); + using namespace mbgl; + FeatureState newState; + + if (result.size() > 0) { + FeatureIdentifier id = result[0].id; + optional<std::string> idStr = featureIDtoString(id); + + if (idStr) { + if (view->featureID && (*view->featureID != *idStr)) { + newState["hover"] = false; + view->rendererFrontend->getRenderer()->setFeatureState("states", { }, *view->featureID, newState); + view->featureID = nullopt; + } + + if (!view->featureID) { + newState["hover"] = true; + view->featureID = featureIDtoString(id); + view->rendererFrontend->getRenderer()->setFeatureState("states", { }, *view->featureID, newState); + } + } + } else { + if (view->featureID) { + newState["hover"] = false; + view->rendererFrontend->getRenderer()->setFeatureState("states", { }, *view->featureID, newState); + view->featureID = nullopt; + } + } + view->invalidate(); + } } void GLFWView::onWindowFocus(GLFWwindow *window, int focused) { diff --git a/platform/glfw/glfw_view.hpp b/platform/glfw/glfw_view.hpp index 54b89ba2d9..cc7c821810 100644 --- a/platform/glfw/glfw_view.hpp +++ b/platform/glfw/glfw_view.hpp @@ -4,6 +4,7 @@ #include <mbgl/util/run_loop.hpp> #include <mbgl/util/timer.hpp> #include <mbgl/util/geometry.hpp> +#include <mbgl/util/optional.hpp> struct GLFWwindow; class GLFWBackend; @@ -134,4 +135,5 @@ private: GLFWwindow *window = nullptr; bool dirty = false; + mbgl::optional<std::string> featureID; }; |