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
  • Loading branch information
Gali Nelle committed Mar 26, 2020
1 parent fd1f870 commit 4b9e1b7
Show file tree
Hide file tree
Showing 21 changed files with 2,027 additions and 7 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,17 @@ 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
100 changes: 100 additions & 0 deletions include/mbgl/gl/location_component_layer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#pragma once

#ifndef GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#endif
#include <GLFW/glfw3.h>

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

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

using namespace mbgl;
using namespace mbgl::style;

namespace mbgl {
namespace style {

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

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

class Impl;
const Impl& impl() const;

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

LocationComponentLayer(const LocationComponentLayer&) = delete;

Value serialize() const final;
Mutable<Layer::Impl> mutableBaseImpl() const final;

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

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

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

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

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

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

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

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

static PropertyValue<Position> getDefaultLocation();
const PropertyValue<Position> getLocation() const;
void setLocation(const PropertyValue<Position>&);
void setCoordinate(const LatLng &crd);

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

static PropertyValue<float> getDefaultShadowImageSize();
const PropertyValue<float> getShadowImageSize() const;
void setShadowImageSize(const PropertyValue<float>&);
};
} // namespace style
} // namespace mbgl
14 changes: 14 additions & 0 deletions include/mbgl/gl/location_component_layer_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#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
12 changes: 6 additions & 6 deletions include/mbgl/style/position.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class Position {
}

private:
float radial;
float azimuthal;
float polar;
float x;
float y;
float z;
float radial = 0;
float azimuthal = 0;
float polar = 0;
float x = 0;
float y = 0;
float z = 0;

void calculateCartesian() {
// We abstract "north"/"up" (compass-wise) to be 0° when really this is 90° (π/2): we
Expand Down
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
7 changes: 7 additions & 0 deletions platform/glfw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ 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.
57 changes: 57 additions & 0 deletions platform/glfw/glfw_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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 +44,8 @@
#include <iostream>
#include <utility>

static const std::string mbglPuckAssetsPath{MAPBOX_PUCK_ASSETS_PATH};

class SnapshotObserver final : public mbgl::MapSnapshotterObserver {
public:
~SnapshotObserver() override = default;
Expand Down Expand Up @@ -459,6 +464,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 +697,11 @@ void GLFWView::onScroll(GLFWwindow *window, double /*xOffset*/, double yOffset)
}

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

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

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

if (view->puck) {
mbgl::LatLng mapCenter = view->map->getCameraOptions().center.value();
view->puck->setCoordinate(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 +897,8 @@ void GLFWView::setWindowTitle(const std::string& title) {
}

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

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

//static const int numPucks = 1; //00;
void GLFWView::toggleLocationComponentLayer()
{
if (map->getStyle().getLayer("puck") == nullptr) {
auto puckLayer = std::make_unique<mbgl::style::LocationComponentLayer>("puck");
puckLayer->setBearingImage(expression::Image(mbglPuckAssetsPath+"puck.png"));
puckLayer->setBearingImageSize(72);
puckLayer->setBearing(0);
puckLayer->setCoordinate(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->setTopImage(expression::Image(mbglPuckAssetsPath+"puck_hat.png"));
puckLayer->setTopImageSize(24);
puckLayer->setShadowImage(expression::Image(mbglPuckAssetsPath+"puck_shadow.png"));
puckLayer->setShadowImageSize(96);

puck = puckLayer.get();
map->getStyle().addLayer(std::move(puckLayer));
} else {
bool visible = bool(int(puck->getVisibility()));
puck->setVisibility(mbgl::style::VisibilityType(!visible));
}
}

using Nanoseconds = std::chrono::nanoseconds;

void GLFWView::onWillStartRenderingFrame()
{
uint64_t ns = mbgl::Clock::now().time_since_epoch().count();
const float bearing = float(ns % 2000000000) / 2000000000.0 * 360.0; // testing only

if (puck) {
puck->setBearing(bearing);
}
}
4 changes: 4 additions & 0 deletions platform/glfw/glfw_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mbgl/util/optional.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/timer.hpp>
#include <mbgl/gl/location_component_layer.hpp>

struct GLFWwindow;
class GLFWBackend;
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,5 @@ class GLFWView : public mbgl::MapObserver {
std::unique_ptr<mbgl::MapSnapshotter> snapshotter;
std::unique_ptr<SnapshotObserver> snapshotterObserver;
mbgl::ResourceOptions mapResourceOptions;
mbgl::style::LocationComponentLayer *puck = nullptr;
};
Loading

0 comments on commit 4b9e1b7

Please sign in to comment.