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

Commit

Permalink
refs #1734: preparations for exposing more annotation style properties
Browse files Browse the repository at this point in the history
  • Loading branch information
incanus committed Jul 2, 2015
1 parent 81d8875 commit 39610dc
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 46 deletions.
6 changes: 3 additions & 3 deletions include/mbgl/annotation/shape_annotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace mbgl {

class ShapeAnnotation {
public:
inline ShapeAnnotation(const AnnotationSegments& segments_, const StyleProperties& styleProperties_)
: segments(segments_), styleProperties(styleProperties_) {
inline ShapeAnnotation(const AnnotationSegments& segments_, const AnnotationStyle& style_)
: segments(segments_), style(style_) {
}

const AnnotationSegments segments;
const StyleProperties styleProperties;
const AnnotationStyle style;
};

}
Expand Down
4 changes: 4 additions & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <mbgl/util/chrono.hpp>
#include <mbgl/map/update.hpp>
#include <mbgl/map/mode.hpp>
#include <mbgl/style/property_key.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/vec.hpp>
Expand All @@ -13,6 +15,7 @@
#include <functional>
#include <vector>
#include <memory>
#include <map>

namespace mbgl {

Expand All @@ -38,6 +41,7 @@ enum class AnnotationType : uint8_t {
using AnnotationIDs = std::vector<uint32_t>;
using AnnotationSegment = std::vector<LatLng>;
using AnnotationSegments = std::vector<AnnotationSegment>;
using AnnotationStyle = std::map<PropertyKey, PropertyValue>;

using EdgeInsets = struct {
double top, left, bottom, right;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 30 additions & 13 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <mbgl/platform/darwin/reachability.h>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/style/function_properties.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/constants.hpp>

Expand Down Expand Up @@ -117,6 +118,16 @@ @implementation MGLMapView
return std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<float, std::chrono::seconds::period>(duration));
}

template <typename T>
mbgl::PropertyValue MGLShapeStyleConstantPropertyValue(T value)
{
mbgl::ConstantFunction<T> constantFunction(value);
mbgl::Function<T> function(constantFunction);
mbgl::PropertyValue result(function);

return result;
}

- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
Expand Down Expand Up @@ -1818,19 +1829,22 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
[strokeColor getRed:&r green:&g blue:&b alpha:&a];
mbgl::Color strokeNativeColor({{ (float)r, (float)g, (float)b, (float)a }});

mbgl::StyleProperties shapeProperties;
mbgl::AnnotationStyle shapeStyle;

if ([annotation isKindOfClass:[MGLPolyline class]])
{
CGFloat lineWidth = (delegateImplementsLineWidthForPolyline ?
CGFloat width = (delegateImplementsLineWidthForPolyline ?
[self.delegate mapView:self lineWidthForPolylineAnnotation:(MGLPolyline *)annotation] :
3.0);

mbgl::LineProperties lineProperties;
lineProperties.opacity = alpha;
lineProperties.color = strokeNativeColor;
lineProperties.width = lineWidth;
shapeProperties.set<mbgl::LineProperties>(lineProperties);
shapeStyle.emplace(mbgl::PropertyKey::LineOpacity,
MGLShapeStyleConstantPropertyValue(float(alpha)));

shapeStyle.emplace(mbgl::PropertyKey::LineColor,
MGLShapeStyleConstantPropertyValue(strokeNativeColor));

shapeStyle.emplace(mbgl::PropertyKey::LineWidth,
MGLShapeStyleConstantPropertyValue(float(width)));

}
else if ([annotation isKindOfClass:[MGLPolygon class]])
Expand All @@ -1844,11 +1858,14 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
[fillColor getRed:&r green:&g blue:&b alpha:&a];
mbgl::Color fillNativeColor({{ (float)r, (float)g, (float)b, (float)a }});

mbgl::FillProperties fillProperties;
fillProperties.opacity = alpha;
fillProperties.stroke_color = strokeNativeColor;
fillProperties.fill_color = fillNativeColor;
shapeProperties.set<mbgl::FillProperties>(fillProperties);
shapeStyle.emplace(mbgl::PropertyKey::FillOpacity,
MGLShapeStyleConstantPropertyValue(float(alpha)));

shapeStyle.emplace(mbgl::PropertyKey::FillOutlineColor,
MGLShapeStyleConstantPropertyValue(strokeNativeColor));

shapeStyle.emplace(mbgl::PropertyKey::FillColor,
MGLShapeStyleConstantPropertyValue(fillNativeColor));
}
else
{
Expand All @@ -1872,7 +1889,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations

free(coordinates);

shapes.emplace_back(mbgl::AnnotationSegments {{ segment }}, shapeProperties);
shapes.emplace_back(mbgl::AnnotationSegments {{ segment }}, shapeStyle);
}
else
{
Expand Down
16 changes: 8 additions & 8 deletions src/mbgl/map/annotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace mbgl {

Annotation::Annotation(AnnotationType type_,
const AnnotationSegments& geometry_,
const StyleProperties& styleProperties_)
: styleProperties(styleProperties_),
const AnnotationStyle& style_)
: style(style_),
type(type_),
geometry(geometry_),
bounds([this] {
Expand Down Expand Up @@ -83,15 +83,15 @@ AnnotationManager::addTileFeature(const uint32_t annotationID,
const AnnotationSegments& segments,
const std::vector<std::vector<vec2<double>>>& projectedFeature,
const AnnotationType& type,
const StyleProperties& styleProperties,
const AnnotationStyle& style,
const std::unordered_map<std::string, std::string>& featureProperties,
const uint8_t maxZoom) {

assert(type != AnnotationType::Any);

// track the annotation global ID and its original geometry
auto anno_it = annotations.emplace(annotationID,
std::make_unique<Annotation>(type, segments, styleProperties));
std::make_unique<Annotation>(type, segments, style));

std::unordered_set<TileID, TileID::Hash> affectedTiles;

Expand All @@ -118,7 +118,7 @@ AnnotationManager::addTileFeature(const uint32_t annotationID,

ProjectedFeatureType featureType;

if (styleProperties.is<FillProperties>()) {
if (style.find(PropertyKey::FillColor) != style.end()) {
featureType = ProjectedFeatureType::Polygon;

if (points.front().lon != points.back().lon || points.front().lat != points.back().lat) {
Expand Down Expand Up @@ -334,7 +334,7 @@ AnnotationManager::addShapeAnnotations(const std::vector<ShapeAnnotation>& shape
shape.segments,
{{ }},
AnnotationType::Shape,
shape.styleProperties,
shape.style,
{{ }},
maxZoom
);
Expand Down Expand Up @@ -415,13 +415,13 @@ std::unordered_set<TileID, TileID::Hash> AnnotationManager::removeAnnotations(co
return affectedTiles;
}

const StyleProperties AnnotationManager::getAnnotationStyleProperties(uint32_t annotationID) const {
const AnnotationStyle& AnnotationManager::getAnnotationStyle(uint32_t annotationID) const {
std::lock_guard<std::mutex> lock(mtx);

auto anno_it = annotations.find(annotationID);
assert(anno_it != annotations.end());

return anno_it->second->styleProperties;
return anno_it->second->style;
}

AnnotationIDs AnnotationManager::getAnnotationsInBounds(const LatLngBounds& queryBounds,
Expand Down
8 changes: 4 additions & 4 deletions src/mbgl/map/annotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ using GeoJSONVT = mapbox::util::geojsonvt::GeoJSONVT;
class Annotation : private util::noncopyable {
friend class AnnotationManager;
public:
Annotation(AnnotationType, const AnnotationSegments&, const StyleProperties&);
Annotation(AnnotationType, const AnnotationSegments&, const AnnotationStyle&);

public:
const StyleProperties styleProperties;
const AnnotationStyle style;

private:
LatLng getPoint() const;
Expand Down Expand Up @@ -67,7 +67,7 @@ class AnnotationManager : private util::noncopyable {

std::unordered_set<TileID, TileID::Hash> removeAnnotations(const AnnotationIDs&, const uint8_t maxZoom);
AnnotationIDs getOrderedShapeAnnotations() const { return orderedShapeAnnotations; }
const StyleProperties getAnnotationStyleProperties(uint32_t) const;
const AnnotationStyle& getAnnotationStyle(uint32_t) const;

AnnotationIDs getAnnotationsInBounds(const LatLngBounds&, const uint8_t maxZoom, const AnnotationType& = AnnotationType::Any) const;
LatLngBounds getBoundsForAnnotations(const AnnotationIDs&) const;
Expand All @@ -85,7 +85,7 @@ class AnnotationManager : private util::noncopyable {
const AnnotationSegments&,
const std::vector<std::vector<vec2<double>>>& projectedFeature,
const AnnotationType&,
const StyleProperties&,
const AnnotationStyle&,
const std::unordered_map<std::string, std::string>& featureProperties,
const uint8_t maxZoom);

Expand Down
33 changes: 15 additions & 18 deletions src/mbgl/map/map_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,43 +164,39 @@ void MapContext::updateAnnotationTiles(const std::unordered_set<TileID, TileID::

if (layer_it == style->layers.end()) {
// query shape styling
auto& shapeStyle = data.annotationManager.getAnnotationStyleProperties(shapeAnnotationID);
auto shapeStyle = data.annotationManager.getAnnotationStyle(shapeAnnotationID);

bool isLine = (shapeStyle.find(PropertyKey::LineColor) != shapeStyle.end());

// apply shape paint properties
ClassProperties paintProperties;

if (shapeStyle.is<LineProperties>()) {
if (isLine) {
// opacity
PropertyValue lineOpacity = ConstantFunction<float>(shapeStyle.get<LineProperties>().opacity);
paintProperties.set(PropertyKey::LineOpacity, lineOpacity);
paintProperties.set(PropertyKey::LineOpacity, shapeStyle.at(PropertyKey::LineOpacity));

// line width
PropertyValue lineWidth = ConstantFunction<float>(shapeStyle.get<LineProperties>().width);
paintProperties.set(PropertyKey::LineWidth, lineWidth);
paintProperties.set(PropertyKey::LineWidth, shapeStyle.at(PropertyKey::LineWidth));

// stroke color
PropertyValue strokeColor = ConstantFunction<Color>(shapeStyle.get<LineProperties>().color);
paintProperties.set(PropertyKey::LineColor, strokeColor);
} else if (shapeStyle.is<FillProperties>()) {
paintProperties.set(PropertyKey::LineColor, shapeStyle.at(PropertyKey::LineColor));
} else {
// opacity
PropertyValue fillOpacity = ConstantFunction<float>(shapeStyle.get<FillProperties>().opacity);
paintProperties.set(PropertyKey::FillOpacity, fillOpacity);
paintProperties.set(PropertyKey::FillOpacity, shapeStyle.at(PropertyKey::FillOpacity));

// fill color
PropertyValue fillColor = ConstantFunction<Color>(shapeStyle.get<FillProperties>().fill_color);
paintProperties.set(PropertyKey::FillColor, fillColor);
paintProperties.set(PropertyKey::FillColor, shapeStyle.at(PropertyKey::FillColor));

// stroke color
PropertyValue strokeColor = ConstantFunction<Color>(shapeStyle.get<FillProperties>().stroke_color);
paintProperties.set(PropertyKey::FillOutlineColor, strokeColor);
paintProperties.set(PropertyKey::FillOutlineColor, shapeStyle.at(PropertyKey::FillOutlineColor));
}

std::map<ClassID, ClassProperties> shapePaints;
shapePaints.emplace(ClassID::Default, std::move(paintProperties));

// create shape layer
util::ptr<StyleLayer> shapeLayer = std::make_shared<StyleLayer>(shapeLayerID, std::move(shapePaints));
shapeLayer->type = (shapeStyle.is<LineProperties>() ? StyleLayerType::Line : StyleLayerType::Fill);
shapeLayer->type = (isLine ? StyleLayerType::Line : StyleLayerType::Fill);

// add to end of other shape layers just before (last) point layer
style->layers.emplace((style->layers.end() - 2), shapeLayer);
Expand All @@ -211,8 +207,9 @@ void MapContext::updateAnnotationTiles(const std::unordered_set<TileID, TileID::
shapeBucket->source = shapeID;
shapeBucket->source_layer = shapeLayer->id;

// apply line layout properties to bucket
if (shapeStyle.is<LineProperties>()) {
// apply shape layout properties
if (isLine) {
// default to round join
shapeBucket->layout.set(PropertyKey::LineJoin, ConstantFunction<JoinType>(JoinType::Round));
}

Expand Down

0 comments on commit 39610dc

Please sign in to comment.