private void initializePinsLayer() { // Just initialize with empty so that it's guaranteed that there are sources initiated // when the pins collection is still null var mapLockedPinsSource = new MGLShapeSource(mapLockedPinsSourceKey, new NSObject[]{}, null); nStyle.AddSource(mapLockedPinsSource); // Pins with bearings layer - configure elements var mapLockedPinsLayer = new MGLSymbolStyleLayer(mapLockedPinsKey, mapLockedPinsSource) { IconImageName = NSExpression.FromKeyPath(pin_image_key), IconRotation = NSExpression.FromKeyPath(pin_rotation_key), IconScale = NSExpression.FromKeyPath(pin_size_key), IconRotationAlignment = NSExpression.FromConstant(NSObject.FromObject("map")), // Finally the map-lock flat = "map" IconAllowsOverlap = NSExpression.FromConstant(NSObject.FromObject(true)) // Always overlap }; nStyle.AddLayer(mapLockedPinsLayer); // Just initialize with empty so that it's guaranteed that there are sources initiated // when the pins collection is still null var normalPinsSource = new MGLShapeSource(normalPinsSourceKey, new NSObject[] { }, null); nStyle.AddSource(normalPinsSource); // Normal pins layer - configure elements var normalPinsLayer = new MGLSymbolStyleLayer(normalPinsKey, normalPinsSource) { IconImageName = NSExpression.FromKeyPath(pin_image_key), IconScale = NSExpression.FromKeyPath(pin_size_key), IconOffset = NSExpression.FromKeyPath(pin_offset_key), //https://www.mapbox.com/mapbox-gl-js/style-spec/#layout-symbol-icon-offset IconAnchor = NSExpression.FromConstant(NSObject.FromObject("bottom")), // https://www.mapbox.com/mapbox-gl-js/style-spec/#layout-symbol-icon-anchor = "bottom" or "center" IconAllowsOverlap = NSExpression.FromConstant(NSObject.FromObject(true)) }; nStyle.AddLayer(normalPinsLayer); }
public void KeyPathPropertiesTest() { var availableProperties = new List <string> { "KeyPath", "Operand", "Arguments" }; using (var expression = NSExpression.FromKeyPath("value")) { Assert.AreEqual(NSExpressionType.KeyPath, expression.ExpressionType); TestProperties(expression, availableProperties); } }
public NSPredicateEditorRowTemplate( IEnumerable <string> leftExpressionsFromKeyPaths, IEnumerable <string> rightExpressionsFromConstants, IEnumerable <NSPredicateOperatorType> operators, NSComparisonPredicateModifier modifier = NSComparisonPredicateModifier.Direct, NSComparisonPredicateOptions options = NSComparisonPredicateOptions.None) : this( leftExpressionsFromKeyPaths.Select(k => NSExpression.FromKeyPath(k)), rightExpressionsFromConstants.Select(k => NSExpression.FromConstant(new NSString(k))), operators, modifier, options) { }
public NSPredicateEditorRowTemplate( IEnumerable <string> leftExpressionsFromKeyPaths, NSAttributeType attributeType, IEnumerable <NSPredicateOperatorType> operators, NSComparisonPredicateModifier modifier = NSComparisonPredicateModifier.Direct, NSComparisonPredicateOptions options = NSComparisonPredicateOptions.None) : this( leftExpressionsFromKeyPaths.Select(k => NSExpression.FromKeyPath(k)), attributeType, operators, modifier, options) { }
private void initializeRoutesLayer() { // Just initialize with empty so that it's guaranteed that there are sources initiated // when the routes collection is still null var routeSource = new MGLShapeSource(line_source_key, new NSObject[] { }, null); nStyle.AddSource(routeSource); var borderLinesLayer = new MGLLineStyleLayer(border_line_layer_key, routeSource); borderLinesLayer.LineColor = NSExpression.FromKeyPath(border_line_color_key); borderLinesLayer.LineWidth = NSExpression.FromKeyPath(border_line_width_key); borderLinesLayer.LineJoin = NSExpression.FromConstant(NSObject.FromObject("round")); borderLinesLayer.LineCap = NSExpression.FromConstant(NSObject.FromObject("round")); nStyle.AddLayer(borderLinesLayer); var linesLayer = new MGLLineStyleLayer(line_layer_key, routeSource); linesLayer.LineColor = NSExpression.FromKeyPath(line_color_key); linesLayer.LineWidth = NSExpression.FromKeyPath(line_width_key); linesLayer.LineJoin = NSExpression.FromConstant(NSObject.FromObject("round")); linesLayer.LineCap = NSExpression.FromConstant(NSObject.FromObject("round")); nStyle.AddLayer(linesLayer); }
public void FromKeyPath() { using (var expression = NSExpression.FromKeyPath("value")) using (var result = expression.EvaluateWith(null, null) as NSString) Assert.IsNull(result); }
private NSCompoundPredicate FindMatches(string searchString) { /* * Each searchString creates an OR predicate for: name, yearIntroduced, introPrice. * Example if searchItems contains "Gladiolus 51.99 2001": * - name CONTAINS[c] "gladiolus" * - name CONTAINS[c] "gladiolus", yearIntroduced ==[c] 2001, introPrice ==[c] 51.99 * - name CONTAINS[c] "ginger", yearIntroduced ==[c] 2007, introPrice ==[c] 49.98 */ var searchItemsPredicate = new List <NSPredicate>(); /* * Below we use NSExpression represent expressions in our predicates. * NSPredicate is made up of smaller, atomic parts: * two NSExpressions (a left-hand value and a right-hand value). */ // Name field matching. var titleExpression = NSExpression.FromKeyPath(ExpressionKeys.Title); var searchStringExpression = NSExpression.FromConstant(new NSString(searchString)); var titleSearchComparisonPredicate = new NSComparisonPredicate(titleExpression, searchStringExpression, NSComparisonPredicateModifier.Direct, NSPredicateOperatorType.Contains, NSComparisonPredicateOptions.CaseInsensitive | NSComparisonPredicateOptions.DiacriticInsensitive); searchItemsPredicate.Add(titleSearchComparisonPredicate); var numberFormatter = new NSNumberFormatter { NumberStyle = NSNumberFormatterStyle.None, FormatterBehavior = NSNumberFormatterBehavior.Default }; // The `searchString` may fail to convert to a number. var targetNumber = numberFormatter.NumberFromString(searchString); if (targetNumber != null) { // Use `targetNumberExpression` in both the following predicates. var targetNumberExpression = NSExpression.FromConstant(targetNumber); // The `yearIntroduced` field matching. var yearIntroducedExpression = NSExpression.FromKeyPath(ExpressionKeys.YearIntroduced); var yearIntroducedPredicate = new NSComparisonPredicate(yearIntroducedExpression, targetNumberExpression, NSComparisonPredicateModifier.Direct, NSPredicateOperatorType.EqualTo, NSComparisonPredicateOptions.CaseInsensitive | NSComparisonPredicateOptions.DiacriticInsensitive); searchItemsPredicate.Add(yearIntroducedPredicate); // The `price` field matching. var priceExpression = NSExpression.FromKeyPath(ExpressionKeys.IntroPrice); var finalPredicate = new NSComparisonPredicate(priceExpression, targetNumberExpression, NSComparisonPredicateModifier.Direct, NSPredicateOperatorType.EqualTo, NSComparisonPredicateOptions.CaseInsensitive | NSComparisonPredicateOptions.DiacriticInsensitive); searchItemsPredicate.Add(finalPredicate); } return(NSCompoundPredicate.CreateOrPredicate(searchItemsPredicate.ToArray())); }
static NSExpression ToExpression(NSArray array) { var objects = SubArray(array, 0); var @operator = objects[0].ToString(); var arguments = SubArray(objects, 1); if (OperatorMap.TryGetValue(@operator, out var functionName)) { var subexpressions = ToSubexpressions(arguments); switch (functionName) { case "+": if (subexpressions.Count > 2) { var sumArguments = NSExpression.FromAggregate(subexpressions.ToArray()); return(NSExpression.FromFunction("sum:", new[] { sumArguments })); } break; case "^": if (arguments[0].ToString() == "e") { functionName = @"exp:"; subexpressions = SubArray(subexpressions, 1); } break; default: break; } return(NSExpression.FromFunction(functionName, subexpressions.ToArray())); } else { switch (@operator) { case "collator": return(NSExpression.FromFunction("MGL_FUNCTION", ToSubexpressions(objects).ToArray())); case "literal": if (arguments[0] is NSArray literalArray) { return(NSExpression.FromConstant(literalArray)); // WARNING This may be wrong //var expressions = ToSubexpressions(SubArray(literalArray, 0)); //return NSExpression.FromAggregate(expressions.ToArray()); } return(ToExpression(arguments[0])); case "to-boolean": { var operand = ToExpression(arguments[0]); return(NSExpression.FromFunction(operand, "boolValue", new NSExpression[0])); } case "to-number": case "number": { var operand = ToExpression(arguments[0]); if (arguments.Count == 1) { if (arguments[0] is NSNumber number) { return(NSExpression.FromFormat("%@", new NSObject[] { arguments[0] })); } return(NSExpression.FromFormat("CAST(%@, 'NSNumber')", new NSObject[] { operand })); } else { var innerArguments = SubArray(arguments, 1); return(NSExpression.FromFunction(operand, "mgl_numberWithFallbackValues", ToSubexpressions(innerArguments).ToArray())); } } case "to-string": case "string": { var operand = ToExpression(arguments[0]); return(NSExpression.FromFormat("CAST(%@, 'NSString')", new NSObject[] { operand })); } case "to-color": case "color": { var operand = ToExpression(arguments[0]); if (arguments.Count == 1) { #if __IOS__ return(NSExpression.FromFormat("CAST(%@, 'UIColor')", new NSObject[] { operand })); #else return(NSExpression.FromFormat("CAST(%@, 'NSColor')", new NSObject[] { operand })); #endif } return(NSExpression.FromFunction("MGL_FUNCTION", ToSubexpressions(objects).ToArray())); } case "to-rgba": { var operand = ToExpression(arguments[0]); return(NSExpression.FromFormat("CAST(noindex(%@), 'NSArray')", new NSObject[] { operand })); } case "get": { if (arguments.Count == 2) { var operand = ToExpression(arguments[arguments.Count - 1]); if (arguments[0] is NSString value) { return(NSExpression.FromFormat("%@.%K", new NSObject[] { operand, value })); } var keyExpression = ToExpression(arguments[0]); return(NSExpression.FromFormat("%@.%@", new NSObject[] { operand, keyExpression })); } return(NSExpression.FromKeyPath(arguments[0].ToString())); } case "length": { var subexpressions = ToSubexpressions(arguments); var function = @"count:"; if (subexpressions[0].ExpressionType == NSExpressionType.ConstantValue && subexpressions[0].ConstantValue is NSString value) { function = "length:"; } return(NSExpression.FromFunction(function, new NSExpression[] { subexpressions[0] })); } ; case "rgb": { var subexpressions = ToSubexpressions(arguments); return(UIColorAdditions.FromRgb(subexpressions.ToArray())); } case "rgba": { var subexpressions = ToSubexpressions(arguments); return(UIColorAdditions.FromRgba(subexpressions.ToArray())); } case "min": case "max": { var subexpressions = ToSubexpressions(arguments); var subexpression = NSExpression.FromAggregate(subexpressions.ToArray()); return(NSExpression.FromFunction(@operator, new[] { subexpression })); } case "e": { return(NSExpression.FromConstant(NSNumber.FromDouble(Math.E))); } case "pi": { return(NSExpression.FromConstant(NSNumber.FromDouble(Math.PI))); } case "concat": { var subexpressions = ToSubexpressions(arguments); var subexpression = NSExpression.FromAggregate(subexpressions.ToArray()); return(NSExpression.FromFunction("mgl_join:", new[] { subexpression })); } case "at": { var subexpressions = ToSubexpressions(arguments); return(NSExpression.FromFunction("objectFrom:withIndex:", new[] { subexpressions[1], subexpressions[0] })); } case "has": { var subexpressions = ToSubexpressions(arguments); var operand = subexpressions.Count > 1 ? subexpressions[1] : NSExpression.ExpressionForEvaluatedObject; var key = subexpressions[0]; return(NSExpression.FromFunction("mgl_does:have:", new[] { operand, key })); } case "interpolate": { var firstObject = arguments[0]; if (firstObject is NSArray == false) { return(null); } var interpolationOptions = SubArray((NSArray)firstObject); var curveType = interpolationOptions[0].ToString(); var curveTypeExpression = ToExpression(interpolationOptions[0]); NSObject curveParameters; switch (curveType) { case "linear": { var temp = new NSMutableArray(2); temp.Add((NSString)"literal"); temp.Add(NSNumber.FromDouble(1.0)); curveParameters = temp.Copy(); temp.Dispose(); break; } case "exponential": curveParameters = interpolationOptions[1]; break; case "cubic-bezier": { var temp = new NSMutableArray(5); temp.Add((NSString)"literal"); temp.Add(interpolationOptions[1]); temp.Add(interpolationOptions[2]); temp.Add(interpolationOptions[3]); temp.Add(interpolationOptions[4]); curveParameters = temp.Copy(); temp.Dispose(); break; } default: curveParameters = NSNull.Null; break; } var curveParameterExpression = ToExpression(curveParameters); arguments = SubArray(arguments, 1); var inputExpression = ToExpression(arguments[0]); var stopExpressions = SubArray(arguments, 1); var stops = new NSMutableDictionary(); for (int i = 0; i < stopExpressions.Count; i++) { var keyExpression = stopExpressions[i++]; var valueExpression = ToExpression(stopExpressions[i]); stops[keyExpression] = valueExpression; } var stopExpression = NSExpression.FromConstant(stops); return(NSExpression.FromFunction("mgl_interpolate:withCurveType:parameters:stops:", new[] { inputExpression, curveTypeExpression, curveParameterExpression, stopExpression })); } case "step": { var inputExpression = ToExpression(arguments[0]); var stopExpressions = SubArray(arguments, 1); NSExpression miniumExpression = null; if (stopExpressions.Count % 2 == 1) { miniumExpression = ToExpression(stopExpressions[0]); stopExpressions = SubArray(stopExpressions, 1); } var stops = new NSMutableDictionary(); for (int i = 0; i < stopExpressions.Count; i++) { var keyExpression = stopExpressions[i++]; var valueExpression = ToExpression(stopExpressions[i]); if (miniumExpression == null) { miniumExpression = valueExpression; continue; } stops[keyExpression] = valueExpression; } if (miniumExpression == null) { return(null); } var stopExpression = NSExpression.FromConstant(stops); return(NSExpression.FromFunction("mgl_step:from:stops:", new[] { inputExpression, miniumExpression, stopExpression })); } case "zoom": { return(NSExpression_MGLAdditions.ZoomLevelVariableExpression(null)); } case "heatmap-density": { return(NSExpression_MGLAdditions.HeatmapDensityVariableExpression(null)); } case "line-progress": { return(NSExpression_MGLAdditions.LineProgressVariableExpression(null)); } case "geometry-type": { return(NSExpression_MGLAdditions.GeometryTypeVariableExpression(null)); } case "id": { return(NSExpression_MGLAdditions.FeatureIdentifierVariableExpression(null)); } case "properties": { return(NSExpression_MGLAdditions.FeatureAttributesVariableExpression(null)); } case "var": { return(NSExpression.FromVariable(arguments[0].ToString())); } case "case": { var args = new NSMutableArray(); for (int i = 0; i < arguments.Count; i++) { if (i % 2 == 0 & i != arguments.Count - 1) { var predicate = ToPredicate(arguments[i]); var arg = NSExpression.FromConstant(predicate); args.Add(arg); } else { args.Add(ToExpression(arguments[i])); } } if (args.Count == 3) { return(NSExpression.FromConditional( (NSPredicate)ObjCRuntime.Runtime.GetNSObject <NSExpression>(args.ValueAt(0)) .ConstantValue, ObjCRuntime.Runtime.GetNSObject <NSExpression>(args.ValueAt(1)), ObjCRuntime.Runtime.GetNSObject <NSExpression>(args.ValueAt(2)))); } return(NSExpression.FromFunction("MGL_IF", NSArray.FromArray <NSExpression>((NSArray)args.Copy()))); } case "match": { var optionsArray = new NSMutableArray(); for (int i = 0; i < arguments.Count; i++) { NSExpression option = ToExpression(arguments[i]); if (i > 0 && i < arguments.Count - 1 && !(i % 2 == 0) && arguments[i] is NSArray sub) { option = NSExpression.FromAggregate(ToSubexpressions(SubArray(sub)).ToArray()); } optionsArray.Add(option); } return(NSExpression.FromFunction("MGL_MATCH", NSArray.FromArray <NSExpression>((NSArray)optionsArray.Copy()))); } case "format": { var attributedExpressions = new List <NSExpression>(); for (int i = 0; i < arguments.Count; i += 2) { var expression = ToExpression(arguments[i]); var attrs = new NSMutableDictionary(); if (i + 1 < arguments.Count) { attrs = NSMutableDictionary.FromDictionary(arguments[i + 1] as NSDictionary); } foreach (var key in attrs.Keys) { attrs[key] = ToExpression(attrs[key]); } var attributedExpression = new MGLAttributedExpression(expression, attrs); attributedExpressions.Add(NSExpression.FromConstant(attributedExpression)); } return(NSExpression.FromFunction("mgl_attributed:", attributedExpressions.ToArray())); } case "coalesce": { var subexpressions = ToSubexpressions(arguments); return(NSExpression.FromFormat("mgl_coalesce(%@)", subexpressions.ToArray())); } default: { var subexpressions = ToSubexpressions(objects); return(NSExpression.FromFunction(@"MGL_FUNCTION", subexpressions.ToArray())); } } } }