summaryrefslogtreecommitdiff
path: root/platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2016-08-18 12:19:47 -0700
committerMinh Nguyễn <mxn@1ec5.org>2016-08-19 15:45:14 -0700
commit0a4f0e40ec6820a052ae8c68b3d58b1b300bb900 (patch)
tree18087731fc875e444ac24243e143bab78f05dd05 /platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm
parent7f1b325b0c8424d82a3de209a9bdee7a8602b1e0 (diff)
downloadqtlocation-mapboxgl-0a4f0e40ec6820a052ae8c68b3d58b1b300bb900.tar.gz
[ios, macos] Offsets as vectors, padding as edge insets
An offset style attribute is now exposed publicly as an NSValue representing a CGVector instead of an NSArray of NSNumbers. A padding style attribute is now exposed publicly as an NSValue representing an NSEdgeInsets or UIEdgeInsets instead of an NSArray of NSNumbers. This change also fixes round-tripping of padding values due to a difference between the style specification and Foundation regarding the order of edges around a box. Used a designated initializer on NSEdgeInsets/UIEdgeInsets to ensure correct order when converting from C++ to Objective-C. Fixes #5947, fixes #6065.
Diffstat (limited to 'platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm')
-rw-r--r--platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm64
1 files changed, 64 insertions, 0 deletions
diff --git a/platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm b/platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm
index 633a74d276..79038a66fd 100644
--- a/platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm
+++ b/platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm
@@ -2,8 +2,36 @@
#import "NSValue+MGLStyleAttributeAdditions_Private.h"
+#include <array>
+
+#if TARGET_OS_IPHONE
+ #import <UIKit/UIKit.h>
+ #define MGLEdgeInsets UIEdgeInsets
+#else
+ #define MGLEdgeInsets NSEdgeInsets
+#endif
+
@implementation NSValue (MGLStyleAttributeAdditions)
++ (instancetype)mgl_valueWithOffsetArray:(std::array<float, 2>)offsetArray
+{
+ CGVector vector = CGVectorMake(offsetArray[0], offsetArray[1]);
+ return [NSValue value:&vector withObjCType:@encode(CGVector)];
+}
+
++ (instancetype)mgl_valueWithPaddingArray:(std::array<float, 4>)paddingArray
+{
+ // Style specification defines padding in clockwise order: top, right, bottom, left.
+ // Foundation defines padding in counterclockwise order: top, left, bottom, right.
+ MGLEdgeInsets insets = {
+ .top = paddingArray[0],
+ .right = paddingArray[1],
+ .bottom = paddingArray[2],
+ .left = paddingArray[3],
+ };
+ return [NSValue value:&insets withObjCType:@encode(MGLEdgeInsets)];
+}
+
- (BOOL)isFunction
{
return NO;
@@ -11,9 +39,45 @@
- (mbgl::style::PropertyValue<uint8_t>)mbgl_enumPropertyValue
{
+ NSAssert(strcmp(self.objCType, @encode(uint8_t)) == 0, @"Value does not represent a uint8_t");
uint8_t value = 0;
[self getValue:&value];
return mbgl::style::PropertyValue<uint8_t> { value };
}
+- (mbgl::style::PropertyValue<std::array<float, 2>>)mbgl_offsetPropertyValue
+{
+ return { self.mgl_offsetArrayValue };
+}
+
+- (std::array<float, 2>)mgl_offsetArrayValue
+{
+ NSAssert(strcmp(self.objCType, @encode(CGVector)) == 0, @"Value does not represent a CGVector");
+ CGVector vector;
+ [self getValue:&vector];
+ return {
+ static_cast<float>(vector.dx),
+ static_cast<float>(vector.dy),
+ };
+}
+
+- (mbgl::style::PropertyValue<std::array<float, 4>>)mbgl_paddingPropertyValue
+{
+ return { self.mgl_paddingArrayValue };
+}
+
+- (std::array<float, 4>)mgl_paddingArrayValue
+{
+ NSAssert(strcmp(self.objCType, @encode(MGLEdgeInsets)) == 0, @"Value does not represent an NSEdgeInsets/UIEdgeInsets");
+ MGLEdgeInsets insets;
+ [self getValue:&insets];
+ // Style specification defines padding in clockwise order: top, right, bottom, left.
+ return {
+ static_cast<float>(insets.top),
+ static_cast<float>(insets.right),
+ static_cast<float>(insets.bottom),
+ static_cast<float>(insets.left),
+ };
+}
+
@end