diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-02-21 18:22:09 -0500 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-03-14 19:47:00 -0400 |
commit | 399a3ee18dcdaa8f9d436823db45ce761a429f2d (patch) | |
tree | 5fa2ea284b6d18e6c8862e6d15bff5661fe3533d | |
parent | 449c68e329448196c976fade12a8c0d58a0c7af9 (diff) | |
download | qtlocation-mapboxgl-399a3ee18dcdaa8f9d436823db45ce761a429f2d.tar.gz |
[ios, macos] Add NSExpression convinient expression constructors.
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.h | 147 | ||||
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 65 | ||||
-rw-r--r-- | platform/ios/app/MBXViewController.m | 12 | ||||
-rw-r--r-- | platform/ios/ios.xcodeproj/project.pbxproj | 6 |
4 files changed, 221 insertions, 9 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.h b/platform/darwin/src/NSExpression+MGLAdditions.h index b4d36bc1b1..df8129811d 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.h +++ b/platform/darwin/src/NSExpression+MGLAdditions.h @@ -9,6 +9,54 @@ NS_ASSUME_NONNULL_BEGIN +typedef NSString *MGLExpressionStyleFunction NS_STRING_ENUM; +typedef NSString *MGLExpressionInterpolationMode NS_STRING_ENUM; + +/** + An `NSString` identifying the `zoomLevel` operator in an `NSExpression`. + + This attribute corresponds to the + <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-zoom"><code>zoom</code></a> + expression reference in the Mapbox Style Specification. + */ +extern MGL_EXPORT const MGLExpressionStyleFunction MGLExpressionStyleFunctionZoomLevel; + +/** + An `NSString` identifying the `heatmapDensity` operator in an `NSExpression`. + + This attribute corresponds to the + <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-heatmap-density"><code>heatmap-density</code></a> + expression reference in the Mapbox Style Specification. + */ +extern MGL_EXPORT const MGLExpressionStyleFunction MGLExpressionStyleFunctionHeatmapDensity; + +/** + An `NSString` identifying the `linear` interpolation type in an `NSExpression`. + + This attribute corresponds to the `linear` value in the + <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-interpolate"><code>interpolate</code></a> + expression reference in the Mapbox Style Specification. + */ +extern MGL_EXPORT const MGLExpressionInterpolationMode MGLExpressionInterpolationModeLinear; + +/** + An `NSString` identifying the `expotential` interpolation type in an `NSExpression`. + + This attribute corresponds to the `exponential` value in the + <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-interpolate"><code>interpolate</code></a> + expression reference in the Mapbox Style Specification. + */ +extern MGL_EXPORT const MGLExpressionInterpolationMode MGLExpressionInterpolationModeExponential; + +/** + An `NSString` identifying the `cubic-bezier` interpolation type in an `NSExpression`. + + This attribute corresponds to the `cubic-bezier` value in the + <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-interpolate"><code>interpolate</code></a> + expression reference in the Mapbox Style Specification. + */ +extern MGL_EXPORT const MGLExpressionInterpolationMode MGLExpressionInterpolationModeCubicBezier; + @interface NSExpression (MGLAdditions) /** @@ -42,6 +90,105 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, readonly) id mgl_jsonExpressionObject; +/** + Returns a constant expression containing an `NSString`. + + This is equivalent to call `[NSExpression expressionForConstant:]`. + + @param string The string constant. + */ ++ (instancetype)mgl_expressionForString:(nonnull NSString *)string; + +/** + Returns a constant expression containing an `MGLColor`. + + This is equivalent to call `[NSExpression expressionForConstant:]`. + + @param color The color constant. + */ ++ (instancetype)mgl_expressionForColor:(nonnull MGLColor *)color; + +/** + Returns a constant expression containing an `NSValue`. + + This is equivalent to call `[NSExpression expressionForConstant:]`. + + @param value The value constant. + */ ++ (instancetype)mgl_expressionForValue:(nonnull NSValue *)value; + ++ (instancetype)mgl_expressionForTernaryFunction:(nonnull NSString *)conditionString trueExpression:(nonnull NSExpression *)trueExpression falseExpresssion:(nonnull NSExpression *)falseExpression; + +/** + Returns a step function expression specifying the function operator, default value + and stops. + + @param function The operator type in which this expression is applied. + @param value The default value can be boolean or numeric. + @param stops The stops dictionay must be numeric literals in strictly ascending order. + */ ++ (instancetype)mgl_expressionForStepFunction:(nonnull MGLExpressionStyleFunction)function defaultValue:(nonnull NSValue *)value stops:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)stops; + +/** + Returns a step function expression specifying the function operator, default color + and stops. + + @param function The operator type in which this expression is applied. + @param color The default color. + @param stops The stops dictionay must be numeric literals in strictly ascending order. + */ ++ (instancetype)mgl_expressionForStepFunction:(nonnull MGLExpressionStyleFunction)function defaultColor:(nonnull MGLColor *)color stops:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)stops; + +/** + Returns a step function expression specifying the function operator, default expression + and stops. + + @param function The operator type in which this expression is applied. + @param expression The expression which could be a constant or function expression. + @param stops The stops dictionay must be numeric literals in strictly ascending order. + */ ++ (instancetype)mgl_expressionForStepFunction:(nonnull MGLExpressionStyleFunction)function defaultExpression:(nonnull NSExpression *)expression stops:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)stops; + +/** + Returns an interpolated function expression specifying the function operator, curve type + and steps. + + @param function The operator type in which this expression is applied. + @param curveType The curve type could be `MGLExpressionInterpolationModeLinear`, + `MGLExpressionInterpolationModeExponential` and + `MGLExpressionInterpolationModeCubicBezier`. + @param steps The stops dictionay must be numeric literals in strictly ascending order. + */ ++ (instancetype)mgl_expressionForInterpolateFunction:(nonnull MGLExpressionStyleFunction)function curveType:(nonnull MGLExpressionInterpolationMode)curveType steps:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)steps; + +/** + Returns an interpolated function expression specifying the function operator, curve type, + parameters and steps. + + @param function The operator type in which this expression is applied. + @param curveType The curve type could be `MGLExpressionInterpolationModeLinear`, + `MGLExpressionInterpolationModeExponential` and + `MGLExpressionInterpolationModeCubicBezier`. + @param parameters The parameters expression. + @param steps The stops dictionay must be numeric literals in strictly ascending order. + */ ++ (instancetype)mgl_expressionForInterpolateFunction:(nonnull MGLExpressionStyleFunction)function curveType:(nonnull MGLExpressionInterpolationMode)curveType parameters:(nullable NSExpression *)parameters steps:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)steps; + +/** + Returns a string constant expression appending the passed string. + + @param string The string to append. + */ +- (instancetype)mgl_appendingString:(NSString *)string; + +/** + Returns a string constant expression appending the passed expression. + + @param expression The evaluated expression must return a string. + */ +- (instancetype)mgl_appendingExpression:(NSExpression *)expression; + + @end NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 5ad565c398..ed501d0e18 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -13,6 +13,13 @@ #import <mbgl/style/expression/expression.hpp> +const MGLExpressionStyleFunction MGLExpressionStyleFunctionZoomLevel = @"$zoomLevel"; +const MGLExpressionStyleFunction MGLExpressionStyleFunctionHeatmapDensity = @"$heatmapDensity"; + +const MGLExpressionInterpolationMode MGLExpressionInterpolationModeLinear = @"linear"; +const MGLExpressionInterpolationMode MGLExpressionInterpolationModeExponential = @"exponential"; +const MGLExpressionInterpolationMode MGLExpressionInterpolationModeCubicBezier = @"cubic-bezier"; + @implementation NSExpression (MGLPrivateAdditions) - (std::vector<mbgl::Value>)mgl_aggregateMBGLValue { @@ -297,6 +304,55 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { return subexpressions; } ++ (instancetype)mgl_expressionForString:(NSString *)string { + return [NSExpression expressionForConstantValue:string]; +} + ++ (instancetype)mgl_expressionForColor:(MGLColor *)color { + return [NSExpression expressionForConstantValue:color]; +} ++ (instancetype)mgl_expressionForValue:(NSValue *)value { + return [NSExpression expressionForConstantValue:value]; +} + ++ (instancetype)mgl_expressionForTernaryFunction:(NSString *)conditionString trueExpression:(NSExpression *)trueExpression falseExpresssion:(NSExpression *)falseExpression { + NSString *funtionFormat = [NSString stringWithFormat:@"TERNARY(%@, %%@, %%@)", conditionString]; + return [NSExpression expressionWithFormat:funtionFormat, trueExpression, falseExpression]; +} + ++ (instancetype)mgl_expressionForStepFunction:(MGLExpressionStyleFunction)function defaultValue:(NSValue *)value stops:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)stops { + return [NSExpression mgl_expressionForStepFunction:function + defaultExpression:[NSExpression mgl_expressionForValue:value] + stops:stops]; +} + ++ (instancetype)mgl_expressionForStepFunction:(MGLExpressionStyleFunction)function defaultString:(NSString *)string stops:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)stops { + return [NSExpression mgl_expressionForStepFunction:function + defaultExpression:[NSExpression mgl_expressionForString:string] + stops:stops]; +} + ++ (instancetype)mgl_expressionForStepFunction:(MGLExpressionStyleFunction)function defaultColor:(MGLColor *)color stops:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)stops { + return [NSExpression mgl_expressionForStepFunction:function + defaultExpression:[NSExpression mgl_expressionForColor:color] + stops:stops]; +} + ++ (instancetype)mgl_expressionForStepFunction:(MGLExpressionStyleFunction)function defaultExpression:(NSExpression *)expression stops:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)stops { + NSString *functionFormat = [NSString stringWithFormat:@"FUNCTION(%@, 'mgl_stepWithMinimum:stops:', %%@, %%@, %%@)", function]; + return [NSExpression expressionWithFormat:functionFormat, expression, stops]; +} + ++ (instancetype)mgl_expressionForInterpolateFunction:(MGLExpressionStyleFunction)function curveType:(nonnull MGLExpressionInterpolationMode)curveType steps:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)steps; { + return [NSExpression mgl_expressionForInterpolateFunction:function curveType:curveType parameters:nil steps:steps]; +} + ++ (instancetype)mgl_expressionForInterpolateFunction:(MGLExpressionStyleFunction)function curveType:(nonnull MGLExpressionInterpolationMode)curveType parameters:(nullable NSExpression *)parameters steps:(nonnull NS_DICTIONARY_OF(NSNumber *, id) *)steps { + NSString *functionFormat = [NSString stringWithFormat:@"FUNCTION(%@, 'mgl_interpolateWithCurveType:parameters:stops:', %%@, %%@, %%@)", function]; + return [NSExpression expressionWithFormat:functionFormat, curveType, parameters, steps]; +} + + + (instancetype)mgl_expressionWithJSONObject:(id)object { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ @@ -487,6 +543,15 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { return nil; } + + +- (instancetype)mgl_appendingString:(NSString *)string { + return [self mgl_appendingExpression:[NSExpression mgl_expressionForString:string]]; +} +- (instancetype)mgl_appendingExpression:(NSExpression *)expression { + return [NSExpression expressionForFunction:self selectorName:@"stringByAppendingString:" arguments:@[expression]]; +} + - (id)mgl_jsonExpressionObject { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index a472e3a221..18e8229c8f 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -945,14 +945,20 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) { - (void)styleWaterLayer { MGLFillStyleLayer *waterLayer = (MGLFillStyleLayer *)[self.mapView.style layerWithIdentifier:@"water"]; + NSDictionary *waterColorStops = @{@6.0f: [UIColor yellowColor], @8.0f: [UIColor blueColor], @10.0f: [UIColor redColor], @12.0f: [UIColor greenColor], @14.0f: [UIColor blueColor]}; - waterLayer.fillColor = [NSExpression expressionWithFormat: - @"FUNCTION($zoomLevel, 'mgl_interpolateWithCurveType:parameters:stops:', 'linear', nil, %@)", - waterColorStops]; + MGLNSExpressionBuilder *expressionBuilder = [MGLNSExpressionBuilder expressionBuilderWithFunction:MGLExpressionStyleFunctionZoomLevel + interpolateCurveType:MGLInterpolateCurveTypeLinear + parameters:nil + steps:waterColorStops]; +// waterLayer.fillColor = [NSExpression expressionWithFormat: +// @"FUNCTION($zoomLevel, 'mgl_interpolateWithCurveType:parameters:stops:', 'linear', nil, %@)", +// waterColorStops]; + waterLayer.fillColor = expressionBuilder.expression; NSDictionary *fillAntialiasedStops = @{@11: @YES, @12: @NO, diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj index b2d89fe962..29a5b74a70 100644 --- a/platform/ios/ios.xcodeproj/project.pbxproj +++ b/platform/ios/ios.xcodeproj/project.pbxproj @@ -511,8 +511,6 @@ DA8963381CC549A100684375 /* sprites in Resources */ = {isa = PBXBuildFile; fileRef = DA8963341CC549A100684375 /* sprites */; }; DA8963391CC549A100684375 /* styles in Resources */ = {isa = PBXBuildFile; fileRef = DA8963351CC549A100684375 /* styles */; }; DA89633A1CC549A100684375 /* tiles in Resources */ = {isa = PBXBuildFile; fileRef = DA8963361CC549A100684375 /* tiles */; }; - DA9EA82B201C0C0C00F9874D /* NSExpression+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = DA9EA82A201C0C0B00F9874D /* NSExpression+MGLAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; }; - DA9EA82C201C0C0C00F9874D /* NSExpression+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = DA9EA82A201C0C0B00F9874D /* NSExpression+MGLAdditions.h */; }; DAA32CC31E4C6B65006F8D24 /* MGLDistanceFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 3557F7AF1E1D27D300CCA5E6 /* MGLDistanceFormatter.m */; }; DAA4E4081CBB6C9500178DFB /* Mapbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA8847D21CBAF91600AB86E3 /* Mapbox.framework */; }; DAA4E4091CBB6C9500178DFB /* Mapbox.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DA8847D21CBAF91600AB86E3 /* Mapbox.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -1164,7 +1162,6 @@ DA9C012C1E4C7ADB00C4742A /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = "pt-BR"; path = "pt-BR.lproj/Foundation.stringsdict"; sourceTree = "<group>"; }; DA9C012D1E4C7B1F00C4742A /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-BR"; path = "pt-BR.lproj/Localizable.strings"; sourceTree = "<group>"; }; DA9C012E1E4C7B6100C4742A /* pt-BR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-BR"; path = "pt-BR.lproj/Root.strings"; sourceTree = "<group>"; }; - DA9EA82A201C0C0B00F9874D /* NSExpression+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSExpression+MGLAdditions.h"; sourceTree = "<group>"; }; DAA32CA11E4C44DB006F8D24 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = de; path = de.lproj/Foundation.stringsdict; sourceTree = "<group>"; }; DAA32CA21E4C44DD006F8D24 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = de; path = de.lproj/Localizable.stringsdict; sourceTree = "<group>"; }; DAA32CA31E4C44F1006F8D24 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/Foundation.strings; sourceTree = "<group>"; }; @@ -2073,7 +2070,6 @@ 408AA8561DAEDA0800022900 /* NSDictionary+MGLAdditions.mm */, DA8848141CBAFA6200AB86E3 /* NSException+MGLAdditions.h */, 3510FFEE1D6D9D8C00F413B2 /* NSExpression+MGLAdditions.h */, - DA9EA82A201C0C0B00F9874D /* NSExpression+MGLAdditions.h */, DAC25FCB200FD83E009BE98E /* NSExpression+MGLPrivateAdditions.h */, 3510FFEF1D6D9D8C00F413B2 /* NSExpression+MGLAdditions.mm */, 35B82BF61D6C5F8400B1B721 /* NSPredicate+MGLAdditions.h */, @@ -2264,7 +2260,6 @@ DA8848841CBB033F00AB86E3 /* FABAttributes.h in Headers */, DA8847FD1CBAFA5100AB86E3 /* MGLTilePyramidOfflineRegion.h in Headers */, DA88482F1CBAFA6200AB86E3 /* NSProcessInfo+MGLAdditions.h in Headers */, - DA9EA82B201C0C0C00F9874D /* NSExpression+MGLAdditions.h in Headers */, DA8848601CBAFC2E00AB86E3 /* Mapbox.h in Headers */, DAF0D8101DFE0EA000B28378 /* MGLRasterSource_Private.h in Headers */, 350098BB1D480108004B2AF0 /* MGLVectorSource.h in Headers */, @@ -2294,7 +2289,6 @@ DABFB8641CBE99E500D62B32 /* MGLOfflineStorage.h in Headers */, 96E516E32000552A00A02306 /* MGLAccountManager_Private.h in Headers */, 96E5170420005A6B00A02306 /* SMCalloutView.h in Headers */, - DA9EA82C201C0C0C00F9874D /* NSExpression+MGLAdditions.h in Headers */, 96036A02200565C700510F3D /* NSOrthography+MGLAdditions.h in Headers */, DAD165791CF4CDFF001FF4B9 /* MGLShapeCollection.h in Headers */, 4049C29E1DB6CD6C00B3F799 /* MGLPointCollection.h in Headers */, |