summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLStyleAttributeFunction.mm
blob: 5216e32d97cac5aac324f0d44b4b8d62c6084367 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#import "MGLStyleAttributeFunction.h"

#import "MGLStyleLayer_Private.h"
#import "MGLStyleAttributeValue_Private.h"
#import "MGLStyleAttributeFunction_Private.h"

@interface MGLStyleAttributeFunction() <MGLStyleAttributeValue_Private>
@end

@implementation MGLStyleAttributeFunction

- (instancetype)init
{
    if (self = [super init]) {
        _base = @1;
    }
    return self;
}

- (BOOL)isFunction
{
    return YES;
}

- (mbgl::style::PropertyValue<mbgl::Color>)mbgl_colorPropertyValue
{
    __block std::vector<std::pair<float, mbgl::Color>> stops;
    [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, MGLColor * _Nonnull color, BOOL * _Nonnull stop) {
        NSAssert([color isKindOfClass:[MGLColor class]], @"Stops should be colors");
        stops.emplace_back(zoomKey.floatValue, color.mbgl_color);
    }];
    return mbgl::style::Function<mbgl::Color>({{stops}}, _base.floatValue);
}

- (mbgl::style::PropertyValue<float>)mbgl_floatPropertyValue
{
    __block std::vector<std::pair<float, float>> stops;
    [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSNumber * _Nonnull number, BOOL * _Nonnull stop) {
        NSAssert([number isKindOfClass:[NSNumber class]], @"Stops should be NSNumbers");
        stops.emplace_back(zoomKey.floatValue, number.floatValue);
    }];
    return mbgl::style::Function<float>({{stops}}, _base.floatValue);
}

- (mbgl::style::PropertyValue<bool>)mbgl_boolPropertyValue
{
    __block std::vector<std::pair<float, bool>> stops;
    [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSNumber * _Nonnull number, BOOL * _Nonnull stop) {
        NSAssert([number isKindOfClass:[NSNumber class]], @"Stops should be NSNumbers");
        stops.emplace_back(zoomKey.floatValue, number.boolValue);
    }];
    return mbgl::style::Function<bool>({{stops}}, _base.floatValue);
}

- (mbgl::style::PropertyValue<std::string>)mbgl_stringPropertyValue
{
    __block std::vector<std::pair<float, std::string>> stops;
    [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSString * _Nonnull string, BOOL * _Nonnull stop) {
        NSAssert([string isKindOfClass:[NSString class]], @"Stops should be strings");
        stops.emplace_back(zoomKey.floatValue, string.UTF8String);
    }];
    return mbgl::style::Function<std::string>({{stops}}, _base.floatValue);
}

- (mbgl::style::PropertyValue<std::vector<std::string> >)mbgl_stringArrayPropertyValue
{
    __block std::vector<std::pair<float, std::vector<std::string>>> stops;
    [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSArray *  _Nonnull strings, BOOL * _Nonnull stop) {
        NSAssert([strings isKindOfClass:[NSArray class]], @"Stops should be NSArray");
        std::vector<std::string>convertedStrings;
        for (NSString *string in strings) {
            convertedStrings.emplace_back(string.UTF8String);
        }
        stops.emplace_back(zoomKey.floatValue, convertedStrings);
    }];
    return mbgl::style::Function<std::vector<std::string>>({{stops}}, _base.floatValue);
}

- (mbgl::style::PropertyValue<std::vector<float> >)mbgl_numberArrayPropertyValue
{
    __block std::vector<std::pair<float, std::vector<float>>> stops;
    [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSArray *  _Nonnull numbers, BOOL * _Nonnull stop) {
        NSAssert([numbers isKindOfClass:[NSArray class]], @"Stops should be NSArray");
        std::vector<float>convertedNumbers;
        for (NSNumber *number in numbers) {
            convertedNumbers.emplace_back(number.floatValue);
        }
        stops.emplace_back(zoomKey.floatValue, convertedNumbers);
    }];
    return mbgl::style::Function<std::vector<float>>({{stops}}, _base.floatValue);
}

- (mbgl::style::PropertyValue<std::array<float, 4> >)mbgl_paddingPropertyValue
{
    __block std::vector<std::pair<float, std::array<float, 4>>> stops;
    [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSArray *  _Nonnull padding, BOOL * _Nonnull stop) {
        NSAssert([padding isKindOfClass:[NSArray class]], @"Stops should be NSArray");
        NSNumber *top = padding[0];
        NSNumber *left = padding[1];
        NSNumber *bottom = padding[2];
        NSNumber *right = padding[2];
        auto pad = std::array<float, 4>({{top.floatValue, left.floatValue, bottom.floatValue, right.floatValue}});
        stops.emplace_back(zoomKey.floatValue, pad);
    }];
    return mbgl::style::Function<std::array<float, 4>>({{stops}}, _base.floatValue);
}

- (mbgl::style::PropertyValue<std::array<float, 2> >)mbgl_offsetPropertyValue
{
    __block std::vector<std::pair<float, std::array<float, 2>>> stops;
    [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSArray *  _Nonnull offset, BOOL * _Nonnull stop) {
        NSAssert([offset isKindOfClass:[NSArray class]], @"Stops should be NSArray");
        NSNumber *dx = offset[0];
        NSNumber *dy = offset[1];
        auto off = std::array<float, 2>({{dx.floatValue, dy.floatValue}});
        stops.emplace_back(zoomKey.floatValue, off);
    }];
    return mbgl::style::Function<std::array<float, 2>>({{stops}}, _base.floatValue);
}

+ (instancetype)functionWithColorPropertyValue:(mbgl::style::Function<mbgl::Color>)property
{
    MGLStyleAttributeFunction *function = [[MGLStyleAttributeFunction alloc] init];
    auto stops = property.getStops();
    NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
    for (auto stop : stops) {
        convertedStops[@(stop.first)] = [MGLColor mbgl_colorWithColor:stop.second];
    }
    function.base = @(property.getBase());
    function.stops = convertedStops;
    return function;
}

+ (instancetype)functionWithNumberPropertyValue:(mbgl::style::Function<float>)property
{
    MGLStyleAttributeFunction *function = [[MGLStyleAttributeFunction alloc] init];
    auto stops = property.getStops();
    NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
    for (auto stop : stops) {
        convertedStops[@(stop.first)] = @(stop.second);
    }
    function.base = @(property.getBase());
    function.stops = convertedStops;
    return function;
}

+ (instancetype)functionWithBoolPropertyValue:(mbgl::style::Function<bool>)property
{
    MGLStyleAttributeFunction *function = [[MGLStyleAttributeFunction alloc] init];
    auto stops = property.getStops();
    NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
    for (auto stop : stops) {
        convertedStops[@(stop.first)] = @(stop.second);
    }
    function.base = @(property.getBase());
    function.stops = convertedStops;
    return function;
}

+ (instancetype)functionWithStringPropertyValue:(mbgl::style::Function<std::string>)property
{
    MGLStyleAttributeFunction *function = [[MGLStyleAttributeFunction alloc] init];
    auto stops = property.getStops();
    NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
    for (auto stop : stops) {
        convertedStops[@(stop.first)] = @(stop.second.c_str());
    }
    function.base = @(property.getBase());
    function.stops = convertedStops;
    return function;
}

+ (instancetype)functionWithOffsetPropertyValue:(mbgl::style::Function<std::array<float, 2> >)property
{
    MGLStyleAttributeFunction *function = [[MGLStyleAttributeFunction alloc] init];
    auto stops = property.getStops();
    NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
    for (auto stop : stops) {
        convertedStops[@(stop.first)] = @[@(stop.second[0]), @(stop.second[1])];
    }
    function.base = @(property.getBase());
    function.stops = convertedStops;
    return function;
}

+ (instancetype)functionWithPaddingPropertyValue:(mbgl::style::Function<std::array<float, 4> >)property
{
    MGLStyleAttributeFunction *function = [[MGLStyleAttributeFunction alloc] init];
    auto stops = property.getStops();
    NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
    for (auto stop : stops) {
        convertedStops[@(stop.first)] = @[@(stop.second[0]), @(stop.second[1]), @(stop.second[2]), @(stop.second[3])];
    }
    function.base = @(property.getBase());
    function.stops = convertedStops;
    return function;
}

+ (instancetype)functionWithStringArrayPropertyValue:(mbgl::style::Function<std::vector<std::string> >)property
{
    MGLStyleAttributeFunction *function = [[MGLStyleAttributeFunction alloc] init];
    auto stops = property.getStops();
    NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
    for (auto stop : stops) {
        auto strings = stop.second;
        NSMutableArray *convertedStrings = [NSMutableArray arrayWithCapacity:strings.size()];
        for (auto const& string: strings) {
            [convertedStrings addObject:@(string.c_str())];
        }
        convertedStops[@(stop.first)] = convertedStrings;
    }
    function.base = @(property.getBase());
    function.stops = convertedStops;
    return function;
}

+ (instancetype)functionWithNumberArrayPropertyValue:(mbgl::style::Function<std::vector<float> >)property
{
    MGLStyleAttributeFunction *function = [[MGLStyleAttributeFunction alloc] init];
    auto stops = property.getStops();
    NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
    for (auto stop : stops) {
        auto numbers = stop.second;
        NSMutableArray *convertedNumbers = [NSMutableArray arrayWithCapacity:numbers.size()];
        for (auto const& number: numbers) {
            [convertedNumbers addObject:@(number)];
        }
        convertedStops[@(stop.first)] = convertedNumbers;
    }
    function.base = @(property.getBase());
    function.stops = convertedStops;
    return function;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"<%@: %p, base = %@; stops = %@>",
            NSStringFromClass([self class]), (void *)self,
            self.base,
            self.stops];
}

@end