[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.
This commit is contained in:
Minh Nguyễn
2016-08-18 12:19:47 -07:00
committed by Minh Nguyễn
parent 7f1b325b0c
commit 0a4f0e40ec
8 changed files with 103 additions and 48 deletions

View File

@@ -176,10 +176,10 @@ global.describeValue = function (value, property, layerType) {
units = ` ${units}`.replace(/pixel/, 'point');
}
if (property.name.indexOf('padding') !== -1) {
//if (value.reduce((a, b) => a + b, 0) === 0) {
// return '`NSEdgeInsetsZero` or `UIEdgeInsetsZero`';
//}
return `${value[0]}${units} on the top, ${value[1]}${units} on the right, ${value[2]}${units} on the bottom, and ${value[3]}${units} on the left`;
if (value.reduce((a, b) => a + b, 0) === 0) {
return '`NSEdgeInsetsZero` or `UIEdgeInsetsZero`';
}
return `${value[0]}${units} on the top, ${value[3]}${units} on the left, ${value[2]}${units} on the bottom, and ${value[1]}${units} on the right`;
}
if (property.name.indexOf('offset') !== -1 || property.name.indexOf('translate') !== -1) {
return `${value[0]}${units} from the left and ${value[1]}${units} from the top`;

View File

@@ -2,8 +2,7 @@
#import "MGLStyleAttributeValue_Private.h"
#import "MGLStyleAttributeFunction_Private.h"
#import "MGLTypes.h"
#import "NSValue+MGLStyleAttributeAdditions_Private.h"
@interface MGLStyleAttribute()
@end
@@ -58,7 +57,7 @@
{
if (property.isConstant()) {
auto offset = property.asConstant();
return @[@(offset[0]), @(offset[1])];
return [NSValue mgl_valueWithOffsetArray:offset];
} else if (property.isFunction()) {
return [MGLStyleAttributeFunction functionWithOffsetPropertyValue:property.asFunction()];
} else {
@@ -70,7 +69,7 @@
{
if (property.isConstant()) {
auto padding = property.asConstant();
return @[@(padding[0]), @(padding[1]), @(padding[2]), @(padding[3])];
return [NSValue mgl_valueWithPaddingArray:padding];
} else if (property.isFunction()) {
return [MGLStyleAttributeFunction functionWithPaddingPropertyValue:property.asFunction()];
} else {

View File

@@ -93,14 +93,9 @@
- (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);
[self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSValue * _Nonnull padding, BOOL * _Nonnull stop) {
NSAssert([padding isKindOfClass:[NSArray class]], @"Stops should be NSValue");
stops.emplace_back(zoomKey.floatValue, padding.mgl_paddingArrayValue);
}];
return mbgl::style::Function<std::array<float, 4>>({{stops}}, _base.floatValue);
}
@@ -108,12 +103,9 @@
- (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);
[self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSValue * _Nonnull offset, BOOL * _Nonnull stop) {
NSAssert([offset isKindOfClass:[NSValue class]], @"Stops should be NSValue");
stops.emplace_back(zoomKey.floatValue, offset.mgl_offsetArrayValue);
}];
return mbgl::style::Function<std::array<float, 2>>({{stops}}, _base.floatValue);
}
@@ -176,7 +168,7 @@
auto stops = property.getStops();
NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
for (auto stop : stops) {
convertedStops[@(stop.first)] = @[@(stop.second[0]), @(stop.second[1])];
convertedStops[@(stop.first)] = [NSValue mgl_valueWithOffsetArray:stop.second];
}
function.base = @(property.getBase());
function.stops = convertedStops;

View File

@@ -155,7 +155,7 @@ typedef NS_ENUM(NSUInteger, MGLSymbolStyleLayerTextTranslateAnchor) {
This property is measured in points.
If this property is set to `nil`, the layer uses an implicit default value of 0 points on the top, 0 points on the right, 0 points on the bottom, and 0 points on the left.
If this property is set to `nil`, the layer uses an implicit default value of `NSEdgeInsetsZero` or `UIEdgeInsetsZero`.
This property is only applied to the style if `iconImage` is non-`nil`, and `iconTextFit` is non-`nil`, and `textField` is non-`nil`. Otherwise, it is ignored.
*/

View File

@@ -10,24 +10,6 @@
@implementation NSArray (MGLStyleAttributeAdditions)
- (mbgl::style::PropertyValue<std::array<float, 2>>)mbgl_offsetPropertyValue
{
NSAssert(self.count == 2, @"Offset must contain 2 values (dx, dy)");
NSNumber *dx = self[0];
NSNumber *dy = self[1];
return {{dx.floatValue, dy.floatValue}};
}
- (mbgl::style::PropertyValue<std::array<float, 4> >)mbgl_paddingPropertyValue
{
NSAssert(self.count == 4, @"Padding must contain 4 values (top, left, bottom & right)");
NSNumber *top = self[0];
NSNumber *left = self[1];
NSNumber *bottom = self[2];
NSNumber *right = self[3];
return {{top.floatValue, left.floatValue, bottom.floatValue, right.floatValue}};
}
- (mbgl::style::PropertyValue<std::vector<std::string> >)mbgl_stringArrayPropertyValue
{
std::vector<std::string>fonts;

View File

@@ -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

View File

@@ -5,5 +5,14 @@
#include <mbgl/style/property_value.hpp>
@interface NSValue (MGLStyleAttributeAdditions_Private) <MGLStyleAttributeValue>
+ (instancetype)mgl_valueWithOffsetArray:(std::array<float, 2>)offsetArray;
+ (instancetype)mgl_valueWithPaddingArray:(std::array<float, 4>)paddingArray;
- (mbgl::style::PropertyValue<uint8_t>)mbgl_enumPropertyValue;
- (mbgl::style::PropertyValue<std::array<float, 2>>)mbgl_offsetPropertyValue;
- (std::array<float, 2>)mgl_offsetArrayValue;
- (std::array<float, 4>)mgl_paddingArrayValue;
@end

View File

@@ -1,21 +1,30 @@
#import "MGLRuntimeStylingHelper.h"
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
#import <UIKit/UIKit.h>
#import <UIKit/UIKit.h>
#define MGLEdgeInsets UIEdgeInsets
#else
#import <Cocoa/Cocoa.h>
#import <Cocoa/Cocoa.h>
#define MGLEdgeInsets NSEdgeInsets
#endif
@implementation MGLRuntimeStylingHelper
+ (NSArray *)testPadding
+ (NSValue *)testPadding
{
return @[@1.0f, @1.0f, @1.0f, @1.0f];
MGLEdgeInsets insets = {
.top = 1,
.left = 1,
.bottom = 1,
.right = 1,
};
return [NSValue value:&insets withObjCType:@encode(MGLEdgeInsets)];
}
+ (NSArray *)testOffset
+ (NSValue *)testOffset
{
return @[@1.0f, @1.0f];
CGVector vector = CGVectorMake(1, 1);
return [NSValue value:&vector withObjCType:@encode(CGVector)];
}
+ (NSArray *)testFont