[glfw] Add feature state support

This commit is contained in:
Juha Alanen
2019-08-29 13:40:01 +03:00
committed by Juha Alanen
parent 7c4c091a2b
commit 0042f0b9f5
2 changed files with 82 additions and 0 deletions

View File

@@ -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) {

View File

@@ -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;
};