diff options
Diffstat (limited to 'platform/darwin/src')
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.h | 124 | ||||
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 80 |
2 files changed, 199 insertions, 5 deletions
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.h b/platform/darwin/src/NSExpression+MGLAdditions.h index a0081cb428..39a841543d 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.h +++ b/platform/darwin/src/NSExpression+MGLAdditions.h @@ -9,6 +9,130 @@ NS_ASSUME_NONNULL_BEGIN +typedef NSString *MGLExpressionInterpolationMode NS_TYPED_ENUM; + +/** + 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 (MGLVariableAdditions) + +/** + `NSExpression` variable that 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. + */ +@property (class, nonatomic, readonly) NSExpression *zoomLevelVariableExpression; + +/** + `NSExpression` variable that 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. + */ +@property (class, nonatomic, readonly) NSExpression *heatmapDensityVariableExpression; + +/** + `NSExpression` variable that corresponds to the + <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#eexpressions-geometry-type"><code>geometry-type</code></a> + expression reference in the Mapbox Style Specification. + */ +@property (class, nonatomic, readonly) NSExpression *geometryTypeVariableExpression; + +/** + `NSExpression` variable that corresponds to the + <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-id"><code>id</code></a> + expression reference in the Mapbox Style Specification. + */ +@property (class, nonatomic, readonly) NSExpression *featureIdentifierVariableExpression; + +/** + `NSExpression` variable that corresponds to the + <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-properties"><code>properties</code></a> + expression reference in the Mapbox Style Specification. + */ +@property (class, nonatomic, readonly) NSExpression *featurePropertiesVariableExpression; + +@end + +@interface NSExpression (MGLInitializerAdditions) + +/** + Returns a conditional function expression specifying the string predicate, and + expressions for each condition. + + @param conditionPredicate The predicate to get evaluated. + @param trueExpression The expression for conditions equal to true. + @param falseExpression The expression for conditions equal to false. + */ ++ (instancetype)mgl_expressionForConditional:(nonnull NSPredicate *)conditionPredicate trueExpression:(nonnull NSExpression *)trueExpression falseExpresssion:(nonnull NSExpression *)falseExpression NS_SWIFT_NAME(init(forMGLConditional:trueExpression:falseExpression:)); + +/** + Returns a step function expression specifying the stepping, from expression + and stops. + + @param steppingExpression The stepping expression. + @param minimumExpression The expression which could be a constant or function expression. + @param stops The stops must be an `NSDictionary` constant `NSExpression`. + */ ++ (instancetype)mgl_expressionForSteppingExpression:(nonnull NSExpression*)steppingExpression fromExpression:(nonnull NSExpression *)minimumExpression stops:(nonnull NSExpression*)stops NS_SWIFT_NAME(init(forMGLStepping:from:stops:)); + +/** + Returns an interpolated function expression specifying the function operator, curve type, + parameters and steps. + + @param inputExpression The interpolating expression input. + @param curveType The curve type could be `MGLExpressionInterpolationModeLinear`, + `MGLExpressionInterpolationModeExponential` and + `MGLExpressionInterpolationModeCubicBezier`. + @param parameters The parameters expression. + @param stops The stops expression. + */ ++ (instancetype)mgl_expressionForInterpolatingExpression:(nonnull NSExpression*)inputExpression withCurveType:(nonnull MGLExpressionInterpolationMode)curveType parameters:(nullable NSExpression *)parameters stops:(nonnull NSExpression*)stops NS_SWIFT_NAME(init(forMGLInterpolating:curveType:parameters:stops:)); + +/** + Returns a match function expression specifying the input, matching values, + and default value. + + @param inputExpression The matching expression. + @param matchedExpressions The matched values expression dictionary must be condition : value. + @param defaultExpression The defaultValue expression to be used in case there is no match. + */ ++ (instancetype)mgl_expressionForMatchingExpression:(nonnull NSExpression *)inputExpression inDictionary:(nonnull NSDictionary<NSExpression *, NSExpression *> *)matchedExpressions defaultExpression:(nonnull NSExpression *)defaultExpression NS_SWIFT_NAME(init(forMGLMatchingKey:in:default:)); +/** + Returns a constant expression appending the passed expression. + + @note Both the receiver and the given expression must be an `NSString` constant + expression type; otherwise, an exception is rised. + + @param expression The expression to append to the receiver. + */ +- (instancetype)mgl_expressionByAppendingExpression:(nonnull NSExpression *)expression NS_SWIFT_NAME(mgl_appending(_:)); + +@end + @interface NSExpression (MGLAdditions) /** diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index be6eef7f47..54f98c6ab9 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -16,6 +16,10 @@ #import <mbgl/style/expression/expression.hpp> +const MGLExpressionInterpolationMode MGLExpressionInterpolationModeLinear = @"linear"; +const MGLExpressionInterpolationMode MGLExpressionInterpolationModeExponential = @"exponential"; +const MGLExpressionInterpolationMode MGLExpressionInterpolationModeCubicBezier = @"cubic-bezier"; + @interface MGLAftermarketExpressionInstaller: NSObject @end @@ -557,6 +561,72 @@ NS_DICTIONARY_OF(NSNumber *, NSExpression *) *MGLStopDictionaryByReplacingTokens [NSException raise:NSInvalidArgumentException format:@"Has expressions lack underlying Objective-C implementations."]; return nil; + +} + +@end +@implementation NSExpression (MGLVariableAdditions) + ++ (NSExpression *)zoomLevelVariableExpression { + return [NSExpression expressionForVariable:@"zoomLevel"]; +} + ++ (NSExpression *)heatmapDensityVariableExpression { + return [NSExpression expressionForVariable:@"heatmapDensity"]; +} + ++ (NSExpression *)geometryTypeVariableExpression { + return [NSExpression expressionForVariable:@"geometryType"]; +} + ++ (NSExpression *)featureIdentifierVariableExpression { + return [NSExpression expressionForVariable:@"featureIdentifier"]; +} + ++ (NSExpression *)featurePropertiesVariableExpression { + return [NSExpression expressionForVariable:@"featureProperties"]; +} + +@end + +@implementation NSExpression (MGLInitializerAdditions) + ++ (instancetype)mgl_expressionForConditional:(nonnull NSPredicate *)conditionPredicate trueExpression:(nonnull NSExpression *)trueExpression falseExpresssion:(nonnull NSExpression *)falseExpression { + if (@available(iOS 9.0, *)) { + return [NSExpression expressionForConditional:conditionPredicate trueExpression:trueExpression falseExpression:falseExpression]; + } else { + return [NSExpression expressionForFunction:@"MGL_IF" arguments:@[[NSExpression expressionWithFormat:@"%@", conditionPredicate], trueExpression, falseExpression]]; + } +} + ++ (instancetype)mgl_expressionForSteppingExpression:(nonnull NSExpression*)steppingExpression fromExpression:(nonnull NSExpression *)minimumExpression stops:(nonnull NSExpression*)stops { + return [NSExpression expressionForFunction:@"mgl_step:from:stops:" + arguments:@[steppingExpression, minimumExpression, stops]]; +} + ++ (instancetype)mgl_expressionForInterpolatingExpression:(nonnull NSExpression*)inputExpression withCurveType:(nonnull MGLExpressionInterpolationMode)curveType parameters:(nullable NSExpression *)parameters stops:(nonnull NSExpression*)stops { + NSExpression *sanitizeParams = parameters ? parameters : [NSExpression expressionForConstantValue:nil]; + return [NSExpression expressionForFunction:@"mgl_interpolate:withCurveType:parameters:stops:" + arguments:@[inputExpression, [NSExpression expressionForConstantValue:curveType], sanitizeParams, stops]]; +} + ++ (instancetype)mgl_expressionForMatchingExpression:(nonnull NSExpression *)inputExpression inDictionary:(nonnull NSDictionary<NSExpression *, NSExpression *> *)matchedExpressions defaultExpression:(nonnull NSExpression *)defaultExpression { + NSMutableArray *optionsArray = [NSMutableArray arrayWithObjects:inputExpression, nil]; + + NSEnumerator *matchEnumerator = matchedExpressions.keyEnumerator; + while (NSExpression *key = matchEnumerator.nextObject) { + [optionsArray addObject:key]; + [optionsArray addObject:[matchedExpressions objectForKey:key]]; + } + + [optionsArray addObject:defaultExpression]; + return [NSExpression expressionForFunction:@"MGL_MATCH" + arguments:optionsArray]; +} + +- (instancetype)mgl_expressionByAppendingExpression:(nonnull NSExpression *)expression { + NSExpression *subexpression = [NSExpression expressionForAggregate:@[self, expression]]; + return [NSExpression expressionForFunction:@"mgl_join:" arguments:@[subexpression]]; } @end @@ -767,15 +837,15 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { return [NSExpression expressionForFunction:@"mgl_step:from:stops:" arguments:@[inputExpression, minimum, stopExpression]]; } else if ([op isEqualToString:@"zoom"]) { - return [NSExpression expressionForVariable:@"zoomLevel"]; + return NSExpression.zoomLevelVariableExpression; } else if ([op isEqualToString:@"heatmap-density"]) { - return [NSExpression expressionForVariable:@"heatmapDensity"]; + return NSExpression.heatmapDensityVariableExpression; } else if ([op isEqualToString:@"geometry-type"]) { - return [NSExpression expressionForVariable:@"geometryType"]; + return NSExpression.geometryTypeVariableExpression; } else if ([op isEqualToString:@"id"]) { - return [NSExpression expressionForVariable:@"featureIdentifier"]; + return NSExpression.featureIdentifierVariableExpression; } else if ([op isEqualToString:@"properties"]) { - return [NSExpression expressionForVariable:@"featureProperties"]; + return NSExpression.featurePropertiesVariableExpression; } else if ([op isEqualToString:@"var"]) { return [NSExpression expressionForVariable:argumentObjects.firstObject]; } else if ([op isEqualToString:@"case"]) { |