summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLStyleValue_Private.h
blob: cc63793d0de45cf2d49a72446b517f4e374e9ace (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#import <Foundation/Foundation.h>

#import "MGLStyleValue.h"

#import "NSValue+MGLStyleAttributeAdditions.h"
#import "NSValue+MGLAdditions.h"
#import "NSExpression+MGLPrivateAdditions.h"
#import "MGLTypes.h"

#import "MGLConversion.h"
#include <mbgl/style/conversion/property_value.hpp>
#include <mbgl/style/conversion/data_driven_property_value.hpp>
#include <mbgl/style/conversion/heatmap_color_property_value.hpp>
#include <mbgl/style/conversion/position.hpp>
#import <mbgl/style/types.hpp>

#import <mbgl/util/enum.hpp>

#include <mbgl/style/data_driven_property_value.hpp>

#include <mbgl/util/interpolate.hpp>

#if TARGET_OS_IPHONE
    #import "UIColor+MGLAdditions.h"
#else
    #import "NSColor+MGLAdditions.h"
#endif

namespace mbgl {
    namespace style {
        namespace expression {
            class Expression;
        }
    }
}

id MGLJSONObjectFromMBGLExpression(const mbgl::style::expression::Expression &mbglExpression);

template <typename MBGLType, typename ObjCType, typename MBGLElement = MBGLType, typename ObjCEnum = ObjCType>
class MGLStyleValueTransformer {
public:
    
    /// Convert an mbgl property value into an mgl style value
    NSExpression *toExpression(const mbgl::style::PropertyValue<MBGLType> &mbglValue) {
        PropertyExpressionEvaluator evaluator;
        return mbglValue.evaluate(evaluator);
    }
    
    /// Convert an mbgl data driven property value into an mgl style value
    template <typename MBGLEnum = MBGLType, typename MGLEnum = ObjCEnum>
    NSExpression *toExpression(const mbgl::style::DataDrivenPropertyValue<MBGLEnum> &mbglValue) {
        PropertyExpressionEvaluator evaluator;
        return mbglValue.evaluate(evaluator);
    }
    
    // Convert an mbgl heatmap color property value into an mgl style value
    NSExpression *toExpression(const mbgl::style::HeatmapColorPropertyValue &mbglValue) {
        if (mbglValue.isUndefined()) {
            return nil;
        }
        return [NSExpression expressionWithMGLJSONObject:MGLJSONObjectFromMBGLExpression(mbglValue.getExpression())];
    }

    /**
     Converts an NSExpression to an mbgl property value.
     */
    template <typename MBGLValue>
    typename std::enable_if_t<!std::is_same<MBGLValue, mbgl::style::HeatmapColorPropertyValue>::value,
    MBGLValue> toPropertyValue(NSExpression *expression) {
        if (!expression) {
            return {};
        }
        
        if (expression.expressionType == NSConstantValueExpressionType) {
            MBGLType mbglValue;
            getMBGLValue(expression.constantValue, mbglValue);
            return mbglValue;
        }
        if (expression.expressionType == NSAggregateExpressionType) {
            MBGLType mbglValue;
            getMBGLValue(expression.collection, mbglValue);
            return mbglValue;
        }
        
        NSArray *jsonExpression = expression.mgl_jsonExpressionObject;
        
        mbgl::style::conversion::Error valueError;
        auto value = mbgl::style::conversion::convert<MBGLValue>(
            mbgl::style::conversion::makeConvertible(jsonExpression), valueError);
        if (!value) {
            [NSException raise:NSInvalidArgumentException
                        format:@"Invalid property value: %@", @(valueError.message.c_str())];
            return {};
        }
        
        return *value;
    }
    
    /**
     Converts an NSExpression to an mbgl property value.
     */
    template <typename MBGLValue>
    typename std::enable_if_t<std::is_same<MBGLValue, mbgl::style::HeatmapColorPropertyValue>::value,
    MBGLValue> toPropertyValue(NSExpression *expression) {
        if (!expression) {
            return {};
        }
        
        NSArray *jsonExpression = expression.mgl_jsonExpressionObject;
        
        mbgl::style::conversion::Error valueError;
        auto value = mbgl::style::conversion::convert<mbgl::style::HeatmapColorPropertyValue>(
            mbgl::style::conversion::makeConvertible(jsonExpression), valueError);
        if (!value) {
            [NSException raise:NSInvalidArgumentException
                        format:@"Invalid property value: %@", @(valueError.message.c_str())];
            return {};
        }
        
        return *value;
    }

private: // Private utilities for converting from mgl to mbgl values
    
    /**
     As hack to allow converting enum => string values, we accept a second, dummy parameter in
     the toRawStyleSpecValue() methods for converting 'atomic' (non-style-function) values.
     This allows us to use `std::enable_if` to test (at compile time) whether or not MBGLType is an Enum.
     */
    template <typename MBGLEnum = MBGLType,
    class = typename std::enable_if<!std::is_enum<MBGLEnum>::value>::type,
    typename MGLEnum = ObjCEnum,
    class = typename std::enable_if<!std::is_enum<MGLEnum>::value>::type>
    NSObject* toRawStyleSpecValue(NSObject *rawMGLValue, MBGLEnum &mbglValue) {
        if ([rawMGLValue isKindOfClass:[NSValue class]]) {
            const auto rawNSValue = (NSValue *)rawMGLValue;
            if (strcmp([rawNSValue objCType], @encode(CGVector)) == 0) {
                // offset [x, y]
                std::array<float, 2> mglValue = rawNSValue.mgl_offsetArrayValue;
                return [NSArray arrayWithObjects:@(mglValue[0]), @(mglValue[1]), nil];
            }
        }
        // noop pass-through plain NSObject-based items
        return rawMGLValue;
    }
    
    template <typename MBGLEnum = MBGLType,
    class = typename std::enable_if<std::is_enum<MBGLEnum>::value>::type,
    typename MGLEnum = ObjCEnum,
    class = typename std::enable_if<std::is_enum<MGLEnum>::value>::type>
    NSString* toRawStyleSpecValue(ObjCType rawValue, MBGLEnum &mbglValue) {
        MGLEnum mglEnum;
        [rawValue getValue:&mglEnum];
        return @(mbgl::Enum<MGLEnum>::toString(mglEnum));
    }
    
    NSObject* toRawStyleSpecValue(MGLColor *color, MBGLType &mbglValue) {
        return @(color.mgl_color.stringify().c_str());
    }

    // Bool
    void getMBGLValue(NSNumber *rawValue, bool &mbglValue) {
        mbglValue = !!rawValue.boolValue;
    }

    // Float
    void getMBGLValue(NSNumber *rawValue, float &mbglValue) {
        mbglValue = rawValue.floatValue;
    }

    // String
    void getMBGLValue(NSString *rawValue, std::string &mbglValue) {
        mbglValue = rawValue.UTF8String;
    }

    // Offsets
    void getMBGLValue(id rawValue, std::array<float, 2> &mbglValue) {
        if ([rawValue isKindOfClass:[NSValue class]]) {
            mbglValue = [rawValue mgl_offsetArrayValue];
        } else if ([rawValue isKindOfClass:[NSArray class]]) {
            NSArray *array = (NSArray *)rawValue;
            getMBGLValue(array[0], mbglValue[0]);
            getMBGLValue(array[1], mbglValue[1]);
        }
    }

    // Padding
    void getMBGLValue(id rawValue, std::array<float, 4> &mbglValue) {
        if ([rawValue isKindOfClass:[NSValue class]]) {
            mbglValue = [rawValue mgl_paddingArrayValue];
        } else if ([rawValue isKindOfClass:[NSArray class]]) {
            NSArray *array = (NSArray *)rawValue;
            getMBGLValue(array[0], mbglValue[0]);
            getMBGLValue(array[1], mbglValue[1]);
            getMBGLValue(array[2], mbglValue[2]);
            getMBGLValue(array[3], mbglValue[3]);
            getMBGLValue(array[4], mbglValue[4]);
        }
    }

    // Color
    void getMBGLValue(MGLColor *rawValue, mbgl::Color &mbglValue) {
        mbglValue = rawValue.mgl_color;
    }

    // Array
    void getMBGLValue(ObjCType rawValue, std::vector<MBGLElement> &mbglValue) {
        mbglValue.reserve(rawValue.count);
        for (id obj in rawValue) {
            id constantObject = obj;
            if ([obj isKindOfClass:[NSExpression class]] && [obj expressionType] == NSConstantValueExpressionType) {
                constantObject = [constantObject constantValue];
            }
            MBGLElement mbglElement;
            getMBGLValue(constantObject, mbglElement);
            mbglValue.push_back(mbglElement);
        }
    }
    
    void getMBGLValue(NSValue *rawValue, mbgl::style::Position &mbglValue) {
        auto spherical = rawValue.mgl_lightPositionArrayValue;
        mbgl::style::Position position(spherical);
        mbglValue = position;
    }

    // Enumerations
    template <typename MBGLEnum = MBGLType,
    class = typename std::enable_if<std::is_enum<MBGLEnum>::value>::type,
    typename MGLEnum = ObjCEnum,
    class = typename std::enable_if<std::is_enum<MGLEnum>::value>::type>
    void getMBGLValue(id rawValue, MBGLEnum &mbglValue) {
        if ([rawValue isKindOfClass:[NSString class]]) {
            mbglValue = *mbgl::Enum<MBGLEnum>::toEnum([(NSString *)rawValue UTF8String]);
        } else {
            MGLEnum mglEnum;
            [(NSValue *)rawValue getValue:&mglEnum];
            auto str = mbgl::Enum<MGLEnum>::toString(mglEnum);
            mbglValue = *mbgl::Enum<MBGLEnum>::toEnum(str);
        }
    }

private: // Private utilities for converting from mbgl to mgl values

    // Bool
    static NSNumber *toMGLRawStyleValue(const bool mbglStopValue) {
        return @(mbglStopValue);
    }

    // Float
    static NSNumber *toMGLRawStyleValue(const float mbglStopValue) {
        return @(mbglStopValue);
    }

    // Integer
    static NSNumber *toMGLRawStyleValue(const int64_t mbglStopValue) {
        return @(mbglStopValue);
    }

    // String
    static NSString *toMGLRawStyleValue(const std::string &mbglStopValue) {
        return @(mbglStopValue.c_str());
    }

    // Offsets
    static NSValue *toMGLRawStyleValue(const std::array<float, 2> &mbglStopValue) {
        return [NSValue mgl_valueWithOffsetArray:mbglStopValue];
    }

    // Padding
    static NSValue *toMGLRawStyleValue(const std::array<float, 4> &mbglStopValue) {
        return [NSValue mgl_valueWithPaddingArray:mbglStopValue];
    }

    // Color
    static MGLColor *toMGLRawStyleValue(const mbgl::Color mbglStopValue) {
        return [MGLColor mgl_colorWithColor:mbglStopValue];
    }

    // Array
    static ObjCType toMGLRawStyleValue(const std::vector<MBGLElement> &mbglStopValue) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:mbglStopValue.size()];
        for (const auto &mbglElement: mbglStopValue) {
            [array addObject:[NSExpression expressionForConstantValue:toMGLRawStyleValue(mbglElement)]];
        }
        return array;
    }
    
    static NSValue *toMGLRawStyleValue(const mbgl::style::Position &mbglStopValue) {
        std::array<float, 3> spherical = mbglStopValue.getSpherical();
        MGLSphericalPosition position = MGLSphericalPositionMake(spherical[0], spherical[1], spherical[2]);
        return [NSValue valueWithMGLSphericalPosition:position];
    }

    // Enumerations
    template <typename MBGLEnum = MBGLType, typename MGLEnum = ObjCEnum>
    static NSValue *toMGLRawStyleValue(const MBGLEnum &value) {
        auto str = mbgl::Enum<MBGLEnum>::toString(value);
        MGLEnum mglType = *mbgl::Enum<MGLEnum>::toEnum(str);
        return [NSValue value:&mglType withObjCType:@encode(MGLEnum)];
    }

    /// Converts all types of mbgl property values into an equivalent NSExpression.
    class PropertyExpressionEvaluator {
    public:
        NSExpression *operator()(const mbgl::style::Undefined) const {
            return nil;
        }

        /**
         As hack to allow converting enum => string values, we accept a second, dummy parameter in
         the toRawStyleSpecValue() methods for converting 'atomic' (non-style-function) values.
         This allows us to use `std::enable_if` to test (at compile time) whether or not MBGLType is an Enum.
         */
        template <typename MBGLEnum = MBGLType,
            class = typename std::enable_if<!std::is_enum<MBGLEnum>::value>::type,
        typename MGLEnum = ObjCEnum,
            class = typename std::enable_if<!std::is_enum<MGLEnum>::value>::type>
        NSExpression *operator()(const MBGLType &value) const {
            id constantValue = toMGLRawStyleValue(value);
            if ([constantValue isKindOfClass:[NSArray class]]) {
                return [NSExpression expressionForAggregate:constantValue];
            }
            return [NSExpression expressionForConstantValue:constantValue];
        }
        
        template <typename MBGLEnum = MBGLType,
            class = typename std::enable_if<std::is_enum<MBGLEnum>::value>::type,
        typename MGLEnum = ObjCEnum,
            class = typename std::enable_if<std::is_enum<MGLEnum>::value>::type>
        NSExpression *operator()(const MBGLEnum &value) const {
            NSString *constantValue = @(mbgl::Enum<MBGLEnum>::toString(value));
            return [NSExpression expressionForConstantValue:constantValue];
        }

        NSExpression *operator()(const mbgl::style::CameraFunction<MBGLType> &mbglValue) const {
            return [NSExpression expressionWithMGLJSONObject:MGLJSONObjectFromMBGLExpression(mbglValue.getExpression())];
        }

        NSExpression *operator()(const mbgl::style::SourceFunction<MBGLType> &mbglValue) const {
            return [NSExpression expressionWithMGLJSONObject:MGLJSONObjectFromMBGLExpression(mbglValue.getExpression())];
        }

        NSExpression *operator()(const mbgl::style::CompositeFunction<MBGLType> &mbglValue) const {
            return [NSExpression expressionWithMGLJSONObject:MGLJSONObjectFromMBGLExpression(mbglValue.getExpression())];
        }
    };
};