Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Add LocationComponentLayer
Browse files Browse the repository at this point in the history
New key is "G" in mbgl-glfw, cycling between no puck, centered
in the viewport and positioned in Tokyo.
  • Loading branch information
Gali Nelle committed Mar 27, 2020
1 parent fd1f870 commit 5539c79
Show file tree
Hide file tree
Showing 23 changed files with 2,092 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,16 @@ if(MBGL_WITH_OPENGL)
target_sources(
mbgl-core
PRIVATE
${PROJECT_SOURCE_DIR}/include/mbgl/gl/location_component_layer.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/location_component_layer_factory.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/location_component_layer_factory.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/location_component_layer.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/location_component_layer_impl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/location_component_layer_impl.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/location_component_layer_properties.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/location_component_layer_properties.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/render_location_component_layer.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/render_location_component_layer.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/backend.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/custom_layer.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gl/custom_layer_factory.hpp
Expand Down
111 changes: 111 additions & 0 deletions include/mbgl/gl/location_component_layer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#pragma once

#ifndef GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#endif

#include <mapbox/cheap_ruler.hpp>
#include <mapbox/geometry.hpp>
#include <mbgl/gl/uniform.hpp>
#include <mbgl/platform/gl_functions.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/util/convert.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/mat4.hpp>
#include <mbgl/util/projection.hpp>

#include <array>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/util/color.hpp>

namespace mbgl {
namespace style {

class LocationComponentLayer : public Layer {
public:
LocationComponentLayer(const std::string& id);
~LocationComponentLayer() final;

// Layout properties

static PropertyValue<expression::Image> getDefaultBearingImage();
const PropertyValue<expression::Image>& getBearingImage() const;
void setBearingImage(const PropertyValue<expression::Image>&);

static PropertyValue<expression::Image> getDefaultShadowImage();
const PropertyValue<expression::Image>& getShadowImage() const;
void setShadowImage(const PropertyValue<expression::Image>&);

static PropertyValue<expression::Image> getDefaultTopImage();
const PropertyValue<expression::Image>& getTopImage() const;
void setTopImage(const PropertyValue<expression::Image>&);

static PropertyValue<std::array<double, 3> > getDefaultLocation();
const PropertyValue<std::array<double, 3> >& getLocation() const;
void setLocation(const PropertyValue<std::array<double, 3> >&);
void setLocation(const LatLng& crd); // a sane overload for LatLng

static PropertyValue<float> getDefaultAccuracyRadius();
const PropertyValue<float>& getAccuracyRadius() const;
void setAccuracyRadius(const PropertyValue<float>&);

static PropertyValue<float> getDefaultBearing();
const PropertyValue<float>& getBearing() const;
void setBearing(const PropertyValue<float>&);

static PropertyValue<float> getDefaultBearingImageSize();
const PropertyValue<float>& getBearingImageSize() const;
void setBearingImageSize(const PropertyValue<float>&);

static PropertyValue<float> getDefaultImageTiltDisplacement();
const PropertyValue<float>& getImageTiltDisplacement() const;
void setImageTiltDisplacement(const PropertyValue<float>&);

static PropertyValue<float> getDefaultPerspectiveCompensation();
const PropertyValue<float>& getPerspectiveCompensation() const;
void setPerspectiveCompensation(const PropertyValue<float>&);

static PropertyValue<float> getDefaultShadowImageSize();
const PropertyValue<float>& getShadowImageSize() const;
void setShadowImageSize(const PropertyValue<float>&);

static PropertyValue<float> getDefaultTopImageSize();
const PropertyValue<float>& getTopImageSize() const;
void setTopImageSize(const PropertyValue<float>&);

// Paint properties

static PropertyValue<Color> getDefaultAccuracyRadiusBorderColor();
const PropertyValue<Color>& getAccuracyRadiusBorderColor() const;
void setAccuracyRadiusBorderColor(const PropertyValue<Color>&);
void setAccuracyRadiusBorderColorTransition(const TransitionOptions&);
TransitionOptions getAccuracyRadiusBorderColorTransition() const;

static PropertyValue<Color> getDefaultAccuracyRadiusColor();
const PropertyValue<Color>& getAccuracyRadiusColor() const;
void setAccuracyRadiusColor(const PropertyValue<Color>&);
void setAccuracyRadiusColorTransition(const TransitionOptions&);
TransitionOptions getAccuracyRadiusColorTransition() const;

// Private implementation
class Impl;
const Impl& impl() const;

Mutable<Impl> mutableImpl() const;
LocationComponentLayer(Immutable<Impl>);
std::unique_ptr<Layer> cloneRef(const std::string& id) const final;

// Dynamic properties
optional<conversion::Error> setPropertyInternal(const std::string& name,
const conversion::Convertible& value) final;

StyleProperty getProperty(const std::string& name) const final;
Value serialize() const final;

Mutable<Layer::Impl> mutableBaseImpl() const final;
};
} // namespace style
} // namespace mbgl
15 changes: 15 additions & 0 deletions include/mbgl/gl/location_component_layer_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <mbgl/layermanager/layer_factory.hpp>

namespace mbgl {

class LocationComponentLayerFactory : public LayerFactory {
protected:
const style::LayerTypeInfo* getTypeInfo() const noexcept final;
std::unique_ptr<style::Layer> createLayer(const std::string& id,
const style::conversion::Convertible& value) noexcept final;
std::unique_ptr<RenderLayer> createRenderLayer(Immutable<style::Layer::Impl>) noexcept final;
};

} // namespace mbgl
4 changes: 4 additions & 0 deletions platform/default/src/mbgl/layermanager/layer_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <mbgl/layermanager/circle_layer_factory.hpp>
#ifdef MBGL_RENDER_BACKEND_OPENGL
#include <mbgl/gl/custom_layer_factory.hpp>
#include <mbgl/gl/location_component_layer_factory.hpp>
#endif
#include <mbgl/layermanager/fill_extrusion_layer_factory.hpp>
#include <mbgl/layermanager/fill_layer_factory.hpp>
Expand Down Expand Up @@ -65,6 +66,9 @@ LayerManagerDefault::LayerManagerDefault() {
#if !defined(MBGL_LAYER_CUSTOM_DISABLE_ALL)
addLayerType(std::make_unique<CustomLayerFactory>());
#endif
#if !defined(MBGL_LAYER_LOCATION_COMPONENT_DISABLE_ALL)
addLayerType(std::make_unique<LocationComponentLayerFactory>());
#endif
#endif
}

Expand Down
5 changes: 5 additions & 0 deletions platform/glfw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ add_executable(
${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/map/map_snapshotter.cpp
)

set_property(
SOURCE ${PROJECT_SOURCE_DIR}/platform/glfw/glfw_view.cpp
PROPERTY COMPILE_DEFINITIONS MAPBOX_PUCK_ASSETS_PATH=\"${PROJECT_SOURCE_DIR}/platform/glfw/assets/\"
)

if(MBGL_WITH_OPENGL)
target_sources(
mbgl-glfw
Expand Down
Binary file added platform/glfw/assets/puck.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added platform/glfw/assets/puck_hat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added platform/glfw/assets/puck_shadow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions platform/glfw/glfw_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <mbgl/style/transition_options.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/string.hpp>
Expand All @@ -28,6 +29,9 @@
#include <mapbox/geometry.hpp>
#include <mapbox/geojson.hpp>

// glif this one
#include <mbgl/gl/location_component_layer.hpp>

#if MBGL_USE_GLES2
#define GLFW_INCLUDE_ES2
#endif // MBGL_USE_GLES2
Expand All @@ -41,6 +45,10 @@
#include <iostream>
#include <utility>

namespace {
const std::string mbglPuckAssetsPath{MAPBOX_PUCK_ASSETS_PATH};
}

class SnapshotObserver final : public mbgl::MapSnapshotterObserver {
public:
~SnapshotObserver() override = default;
Expand Down Expand Up @@ -459,6 +467,9 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action,
// Snapshot with overlay
view->makeSnapshot(true);
} break;
case GLFW_KEY_G: {
view->toggleLocationComponentLayer();
} break;
}
}

Expand Down Expand Up @@ -689,6 +700,11 @@ void GLFWView::onScroll(GLFWwindow *window, double /*xOffset*/, double yOffset)
}

view->map->scaleBy(scale, mbgl::ScreenCoordinate { view->lastX, view->lastY });

if (view->puck && view->puckFollowsCameraCenter) {
mbgl::LatLng mapCenter = view->map->getCameraOptions().center.value();
view->puck->setLocation(mapCenter);
}
}

void GLFWView::onWindowResize(GLFWwindow *window, int width, int height) {
Expand Down Expand Up @@ -756,6 +772,11 @@ void GLFWView::onMouseMove(GLFWwindow *window, double x, double y) {
view->lastX = x;
view->lastY = y;

if (view->puck && view->puckFollowsCameraCenter) {
mbgl::LatLng mapCenter = view->map->getCameraOptions().center.value();
view->puck->setLocation(mapCenter);
}

auto &style = view->map->getStyle();
if (style.getLayer("state-fills")) {
auto screenCoordinate = mbgl::ScreenCoordinate{view->lastX, view->lastY};
Expand Down Expand Up @@ -879,6 +900,8 @@ void GLFWView::setWindowTitle(const std::string& title) {
}

void GLFWView::onDidFinishLoadingStyle() {
puck = nullptr;

if (show3DExtrusions) {
toggle3DExtrusions(show3DExtrusions);
}
Expand Down Expand Up @@ -933,3 +956,64 @@ void GLFWView::toggleCustomSource() {
mbgl::style::VisibilityType::None : mbgl::style::VisibilityType::Visible);
}
}

void GLFWView::toggleLocationComponentLayer() {
puck = static_cast<mbgl::style::LocationComponentLayer *>(map->getStyle().getLayer("puck"));
if (puck == nullptr) {
auto puckLayer = std::make_unique<mbgl::style::LocationComponentLayer>("puck");

puckLayer->setBearing(0);
puckLayer->setLocation(mbgl::LatLng{35.683389, 139.76525});
puckLayer->setAccuracyRadius(50);
puckLayer->setAccuracyRadiusColor(mbgl::Color{0.0, 1.0, 0.0, 0.05});
puckLayer->setAccuracyRadiusBorderColor(mbgl::Color{0.0, 1.0, 0.0, 0.25});
puckLayer->setTopImageSize(24);
puckLayer->setBearingImageSize(72);
puckLayer->setShadowImageSize(96);
puckLayer->setImageTiltDisplacement(8.0f); // set to 0 for a "flat" puck
puckLayer->setPerspectiveCompensation(0.9);

map->getStyle().addImage(std::make_unique<mbgl::style::Image>(
"puck.png", mbgl::decodeImage(mbgl::util::read_file(mbglPuckAssetsPath + "puck.png")), 1.0));

map->getStyle().addImage(std::make_unique<mbgl::style::Image>(
"puck_shadow.png", mbgl::decodeImage(mbgl::util::read_file(mbglPuckAssetsPath + "puck_shadow.png")), 1.0));

map->getStyle().addImage(std::make_unique<mbgl::style::Image>(
"puck_hat.png", mbgl::decodeImage(mbgl::util::read_file(mbglPuckAssetsPath + "puck_hat.png")), 1.0));

puckLayer->setBearingImage(mbgl::style::expression::Image("puck.png"));
puckLayer->setShadowImage(mbgl::style::expression::Image("puck_shadow.png"));
puckLayer->setTopImage(mbgl::style::expression::Image("puck_hat.png"));

puck = puckLayer.get();
map->getStyle().addLayer(std::move(puckLayer));
} else {
bool visible = puck->getVisibility() == mbgl::style::VisibilityType::Visible;
if (visible) {
if (!puckFollowsCameraCenter) {
mbgl::LatLng mapCenter = map->getCameraOptions().center.value();
puck->setLocation(mapCenter);
puckFollowsCameraCenter = true;
} else {
puckFollowsCameraCenter = false;
puck->setVisibility(mbgl::style::VisibilityType(mbgl::style::VisibilityType::None));
}
} else {
puck->setLocation(mbgl::LatLng{35.683389, 139.76525});
puck->setVisibility(mbgl::style::VisibilityType(mbgl::style::VisibilityType::Visible));
puckFollowsCameraCenter = false;
}
}
}

using Nanoseconds = std::chrono::nanoseconds;

void GLFWView::onWillStartRenderingFrame() {
puck = static_cast<mbgl::style::LocationComponentLayer *>(map->getStyle().getLayer("puck"));
if (puck) {
uint64_t ns = mbgl::Clock::now().time_since_epoch().count();
const float bearing = float(ns % 2000000000) / 2000000000.0 * 360.0;
puck->setBearing(bearing);
}
}
5 changes: 5 additions & 0 deletions platform/glfw/glfw_view.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <mbgl/gl/location_component_layer.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/map_snapshotter.hpp>
#include <mbgl/util/geometry.hpp>
Expand Down Expand Up @@ -61,6 +62,7 @@ class GLFWView : public mbgl::MapObserver {

// mbgl::MapObserver implementation
void onDidFinishLoadingStyle() override;
void onWillStartRenderingFrame() override;

protected:
// mbgl::Backend implementation
Expand Down Expand Up @@ -91,6 +93,7 @@ class GLFWView : public mbgl::MapObserver {
void addAnimatedAnnotation();
void updateAnimatedAnnotations();
void toggleCustomSource();
void toggleLocationComponentLayer();

void cycleDebugOptions();
void clearAnnotations();
Expand Down Expand Up @@ -148,4 +151,6 @@ class GLFWView : public mbgl::MapObserver {
std::unique_ptr<mbgl::MapSnapshotter> snapshotter;
std::unique_ptr<SnapshotObserver> snapshotterObserver;
mbgl::ResourceOptions mapResourceOptions;
mbgl::style::LocationComponentLayer *puck = nullptr;
bool puckFollowsCameraCenter = false;
};
4 changes: 2 additions & 2 deletions scripts/generate-style-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ global.evaluatedType = function (property) {
case 'boolean':
return 'bool';
case 'number':
return 'float';
return /location$/.test(property.name) ? 'double' : 'float';
case 'resolvedImage':
return 'expression::Image';
case 'formatted':
Expand All @@ -77,7 +77,7 @@ global.evaluatedType = function (property) {
return `Color`;
case 'array':
if (property.length) {
return `std::array<${evaluatedType({type: property.value})}, ${property.length}>`;
return `std::array<${evaluatedType({type: property.value, name: property.name})}, ${property.length}>`;
} else {
return `std::vector<${evaluatedType({type: property.value, name: property.name})}>`;
}
Expand Down
Loading

0 comments on commit 5539c79

Please sign in to comment.