summaryrefslogtreecommitdiff
path: root/platform/ios/MGLStyleFunctionValue.m
blob: a319114eefa0d7d47ea6647f0bb16068f34bcba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#import "MGLStyleFunctionValue.h"

#import "MGLTypes.h"

@interface MGLStyleFunctionValue ()

@property (nonatomic) NSString *functionType;
@property (nonatomic) NSDictionary *stops;
@property (nonatomic) CGFloat zBase;
@property (nonatomic) CGFloat val;
@property (nonatomic) CGFloat slope;
@property (nonatomic) CGFloat min;
@property (nonatomic) CGFloat max;
@property (nonatomic) CGFloat minimumZoom;
@property (nonatomic) CGFloat maximumZoom;

- (NSDictionary *)rawStyle;

@end

@implementation MGLStyleFunctionValue

- (id)initWithFunctionType:(NSString *)functionType
                     stops:(NSDictionary *)stops
                     zBase:(CGFloat)zBase
                       val:(CGFloat)val
                     slope:(CGFloat)slope
                       min:(CGFloat)min
                       max:(CGFloat)max
               minimumZoom:(CGFloat)minimumZoom
               maximumZoom:(CGFloat)maximumZoom
{
    self = [super init];

    if (self)
    {
        _functionType = functionType;
        _stops = stops;
        _zBase = zBase;
        _val = val;
        _slope = slope;
        _min = min;
        _max = max;
        _minimumZoom = minimumZoom;
        _maximumZoom = maximumZoom;
    }

    return self;
}

- (id)rawStyle
{
    if ([self.functionType isEqualToString:MGLStyleValueTypeFunctionMinimumZoom])
    {
        return @[ @"min", @(self.minimumZoom) ];
    }
    else if ([self.functionType isEqualToString:MGLStyleValueTypeFunctionMaximumZoom])
    {
        return @[ @"max", @(self.maximumZoom) ];
    }
    else if ([self.functionType isEqualToString:MGLStyleValueTypeFunctionLinear])
    {
        return @[ @"linear", @(self.zBase), @(self.val), @(self.slope), @(self.min), @(self.max) ];
    }
    else if ([self.functionType isEqualToString:MGLStyleValueTypeFunctionExponential])
    {
        return @[ @"exponential", @(self.zBase), @(self.val), @(self.slope), @(self.min), @(self.max) ];
    }
    else if ([self.functionType isEqualToString:MGLStyleValueTypeFunctionStops])
    {
        NSMutableArray *returnArray = [NSMutableArray array];

        for (NSNumber *z in [self.stops allKeys])
        {
            [returnArray addObject:@{ @"z" : z, @"val" : self.stops[z] }];
        }

        [returnArray insertObject:@"stops" atIndex:0];

        return returnArray;
    }

    return nil;
}

+ (instancetype)stopsFunctionWithZoomLevelsAndValues:(NSNumber *)firstZoom, ...
{
    NSMutableArray *numbersArray = [NSMutableArray array];

    va_list args;
    va_start(args, firstZoom);

    for (NSNumber *arg = firstZoom; arg != nil; arg = va_arg(args, NSNumber *))
    {
        [numbersArray addObject:arg];
    }

    va_end(args);

    NSAssert([numbersArray count] % 2 == 0, @"invalid number of arguments");

    NSMutableDictionary *stops = [NSMutableDictionary dictionary];

    for (NSUInteger i = 0; i < [numbersArray count]; i = i + 2)
    {
        stops[numbersArray[i]] = stops[numbersArray[i + 1]];
    }

    return [[self alloc] initWithFunctionType:MGLStyleValueTypeFunctionStops
                                        stops:stops
                                        zBase:0
                                          val:0
                                        slope:0
                                          min:0
                                          max:0
                                  minimumZoom:0
                                  maximumZoom:0];
}

+ (instancetype)linearFunctionWithBaseZoomLevel:(CGFloat)zBase
                                   initialValue:(CGFloat)val
                                          slope:(CGFloat)slope
                                   minimumValue:(CGFloat)min
                                   maximumValue:(CGFloat)max
{
    NSAssert(zBase >= 0 && zBase <= 18, @"invalid base zoom level");
    NSAssert(min < max, @"minimum value must be less than maximum value");

    return [[self alloc] initWithFunctionType:MGLStyleValueTypeFunctionLinear
                                        stops:nil
                                        zBase:zBase
                                          val:val
                                        slope:slope
                                          min:min
                                          max:max
                                  minimumZoom:0
                                  maximumZoom:0];
}

+ (instancetype)exponentialFunctionWithBaseZoomLevel:(CGFloat)zBase
                                        initialValue:(CGFloat)val
                                               slope:(CGFloat)slope
                                        minimumValue:(CGFloat)min
                                        maximumValue:(CGFloat)max
{
    NSAssert(zBase >= 0 && zBase <= 18, @"invalid base zoom level");
    NSAssert(min < max, @"minimum value must be less than maximum value");

    return [[self alloc] initWithFunctionType:MGLStyleValueTypeFunctionExponential
                                        stops:nil
                                        zBase:zBase
                                          val:val
                                        slope:slope
                                          min:min
                                          max:max
                                  minimumZoom:0
                                  maximumZoom:0];
}

+ (instancetype)minimumZoomLevelFunction:(CGFloat)minimumZoom
{
    NSAssert(minimumZoom >= 0 && minimumZoom <= 18, @"invalid minimum zoom value");

    return [[self alloc] initWithFunctionType:MGLStyleValueTypeFunctionMinimumZoom
                                        stops:nil
                                        zBase:0
                                          val:0
                                        slope:0
                                          min:0
                                          max:0
                                  minimumZoom:minimumZoom
                                  maximumZoom:0];
}

+ (instancetype)maximumZoomLevelFunction:(CGFloat)maximumZoom
{
    NSAssert(maximumZoom >= 0 && maximumZoom <= 18, @"invalid maximum zoom value");

    return [[self alloc] initWithFunctionType:MGLStyleValueTypeFunctionMaximumZoom
                                        stops:nil
                                        zBase:0
                                          val:0
                                        slope:0
                                          min:0
                                          max:0
                                  minimumZoom:0
                                  maximumZoom:maximumZoom];
}

@end