-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Map Darwin style enumerations to mbgl equivalents #7061
Map Darwin style enumerations to mbgl equivalents #7061
Conversation
@boundsj, thanks for your PR! By analyzing the history of the files in this pull request, we identified @1ec5, @incanus and @friedbunny to be potential reviewers. |
@@ -71,8 +71,8 @@ - (void)setLineJoin:(MGLStyleValue<NSValue *> *)lineJoin { | |||
} | |||
|
|||
- (MGLStyleValue<NSValue *> *)lineJoin { | |||
auto propertyValue = self.rawLayer->getLineJoin() ?: self.rawLayer->getDefaultLineJoin(); | |||
return MGLStyleValueTransformer<mbgl::style::LineJoinType, NSValue *>().toStyleValue(propertyValue); | |||
auto propertyValue = self.rawLayer->getLineJoin() ?: self.rawLayer->getDefaultLineJoin(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: trailing whitespace.
|
||
MGLStyleValue<ObjCType> *toLineJoinStyleValue(const mbgl::style::PropertyValue<MBGLType> &mbglValue) { | ||
auto str = mbgl::Enum<MBGLType>::toString(mbglValue.asConstant()); | ||
NSString *enumString = [NSString stringWithCString:str encoding:NSUTF8StringEncoding]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this can be expressed as str.UTF8String
.
auto str = mbgl::Enum<MBGLType>::toString(mbglValue.asConstant()); | ||
NSString *enumString = [NSString stringWithCString:str encoding:NSUTF8StringEncoding]; | ||
NSUInteger enumValue; | ||
if ([enumString isEqualToString:@"miter"]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Using mbgl::Enum
is a lot cleaner than what I thought we’d have to resort to (the _names
macros).
I think we could take further advantage of mbgl::Enum
, using MBGL_DEFINE_ENUM()
to map e.g. { MGLLineJoinMiter, "miter" }
and calling toEnum()
on str
. (Bogus strings that are used internally to mbgl, such as fakeround
, would trip an assertion.) That way, we can avoid the NSString conversion and these if statements, and we can generate everything using the style specification (in a separate file).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In terms of organization, these MBGL_DEFINE_ENUM()
s can go in MGLLineStyleLayer.mm, for instance. We can make this toLineJoinStyleValue()
method into a more generic toEnumStyleValue()
by adding an ObjCEnumType
template argument that we set to MGLLineJoin
, for instance.
19af5d1
to
72f009d
Compare
@1ec5 thanks for the advice in #7061 (comment). Most of that is implemented now although I did not DRY it up yet with anything like The lookup table in https://github.com/mapbox/mapbox-gl-native/pull/7061/files#diff-01213d8ba02156a3771961755cf10994R12 is sort of unfortunate. But, since the MBGL enums are not generated and because several of them are coalesced into things like Anyway, this is working! |
@@ -9,6 +9,30 @@ const spec = _.merge(require('mapbox-gl-style-spec').latest, require('./style-sp | |||
const prefix = 'MGL'; | |||
const suffix = 'StyleLayer'; | |||
|
|||
global.mbglTypeName = function(str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this can be replaced with global.mbglType()
This will also fix #5970 (again) |
c964aa9
to
e330840
Compare
@1ec5 @frederoni @incanus please review |
@@ -10,6 +10,13 @@ | |||
|
|||
#include <mbgl/style/layers/background_layer.hpp> | |||
|
|||
namespace mbgl { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get rid of this block if there’s are no symbols to define in this translation unit?
|
||
using namespace style; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: extra blank line.
@@ -10,6 +10,21 @@ | |||
|
|||
#include <mbgl/style/layers/circle_layer.hpp> | |||
|
|||
namespace mbgl { | |||
|
|||
using namespace style; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing in this block is using the mbgl::style
namespace, so this statement can go away.
} | ||
id value = [(MGLStyleConstantValue<NSValue *> *)circleTranslateAnchor rawValue]; | ||
MGLCircleTranslateAnchor circleTranslateAnchorValue; | ||
[value getValue:&circleTranslateAnchorValue]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the previous code, if circleTranslateAnchor
was nil
, we’d reset the value in mbgl to an implicit default value. In this code, if circleTranslateAnchor
is nil
, circleTranslateAnchorValue
will be MGLCircleTranslateAnchorMap
, even if the implicit default would’ve been mbgl::style::TranslateAnchorType::Viewport
.
return MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toStyleValue(propertyValue); | ||
} | ||
|
||
- (void)setCircleTranslateAnchor:(MGLStyleValue<NSValue *> *)circleTranslateAnchor { | ||
auto mbglValue = MGLStyleValueTransformer<mbgl::style::TranslateAnchorType, NSValue *>().toPropertyValue(circleTranslateAnchor); | ||
self.rawLayer->setCircleTranslateAnchor(mbglValue); | ||
if ([circleTranslateAnchor isKindOfClass:[MGLStyleFunction class]]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code should be brought back into MGLStyleValueTransformer
and made generic. Bring back the enumeration specialization of toMGLRawStyleValue()
, but add an additional, optional ObjCEnum
template parameter to MGLStyleValueTransformer
that you can plug into mbgl::Enum
. For additional safety, I think you can also make the specialization conditional on ObjCEnum
satisfying std::is_enum
just like it required MBGLEnum
to satisfy std::is_enum
.
*/ | ||
+ (instancetype)valueWithMGLCirclePitchScale:(MGLCirclePitchScale)type; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each factory method should come with a readonly property that converts back into the raw type (example). This way you (and the developer) won’t ever have to use -getValue:
.
|
||
@interface NSValue (MGLStyleEnumAttributeAdditions) | ||
|
||
#pragma mark Runtime Styling enumerations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A documentation section name (that is, amark
in a public header) should describe a task, in title case for consistency:
#pragma mark Working with Style Layer Attribute Values
- (void)styleFilteredLines | ||
{ | ||
|
||
#warning remove this test stub |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: We currently lack a demo in iosapp that draws a route line. We could add one that sets the line join and line cap to something that looks better than the default.
@@ -127,6 +127,10 @@ | |||
4018B1C91CDC288A00F666AF /* MGLAnnotationView_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4018B1C31CDC277F00F666AF /* MGLAnnotationView_Private.h */; }; | |||
4018B1CA1CDC288E00F666AF /* MGLAnnotationView.h in Headers */ = {isa = PBXBuildFile; fileRef = 4018B1C51CDC277F00F666AF /* MGLAnnotationView.h */; settings = {ATTRIBUTES = (Public, ); }; }; | |||
4018B1CB1CDC288E00F666AF /* MGLAnnotationView.h in Headers */ = {isa = PBXBuildFile; fileRef = 4018B1C51CDC277F00F666AF /* MGLAnnotationView.h */; settings = {ATTRIBUTES = (Public, ); }; }; | |||
4032C5BF1DE1FC780062E8BD /* NSValue+MGLStyleEnumAttributeAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 4032C5B81DE1EE7D0062E8BD /* NSValue+MGLStyleEnumAttributeAdditions.h */; }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These headers should be public.
@@ -5,6 +5,7 @@ | |||
#import "MBXOfflinePacksTableViewController.h" | |||
#import "MBXAnnotationView.h" | |||
#import "MBXUserLocationAnnotationView.h" | |||
#import "NSValue+MGLStyleEnumAttributeAdditions.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import this header in both SDKs’ umbrella headers instead of here, since only iosapp and macosapp have this file in their header search paths.
auto str = mbgl::Enum<MBGLType>::toString(mbglStop.second); | ||
MGLType mglType; | ||
mglType = mbgl::Enum<MGLType>::toEnum(str).value_or(mglType); | ||
stops[@(mbglStop.first)] = [MGLStyleValue valueWithRawValue:[NSValue value:&mglType withObjCType:@encode(NSUInteger)]]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Encode MGLType
instead of NSUInteger
, if possible.
@@ -13,6 +15,35 @@ | |||
|
|||
#include <array> | |||
|
|||
template <typename MBGLType, typename MGLType> | |||
class MGLStyleEnumerationValueTransformer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this method live inside MGLStyleValueTransformer
? Use optional template parameters on the class to allow both NSValue *
and MGLLineCap
to be specific, and use conditional template parameters on the enumeration-specific methods to enable the methods only for enumerations.
Correctly map SDK runtime styling enumerations to mbgl equivalents. Also, add category methods to NSValue so enums can be wrapped up with less of the details of how they are layed out in memory in Objective-C.
10ee43b
to
b18b2bc
Compare
This will also fix #7153 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good other than a couple nits about whitespace and indentation.
@@ -10,6 +10,7 @@ | |||
|
|||
#include <mbgl/style/layers/background_layer.hpp> | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: extra whitespace.
auto mbglValue = MGLStyleValueTransformer<mbgl::style::TranslateAnchorType, NSValue *>().toPropertyValue(circleTranslateAnchor); | ||
self.rawLayer->setCircleTranslateAnchor(mbglValue); | ||
auto mbglValue = MGLStyleValueTransformer<mbgl::style::TranslateAnchorType, | ||
NSValue *, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: funky indentation. Can this all go on one line?
This fixes an issue where the documentation for all NSValue categories were described as `MGLGeometryAdditions`.
b18b2bc
to
5ae966a
Compare
Fixes #6760