Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[ios, macos] Implement default value for identity functions #7933

Merged
merged 1 commit into from
Feb 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions platform/darwin/src/MGLStyleValue_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,7 @@ class MGLStyleValueTransformer {
}];
mbgl::style::CategoricalStops<MBGLType> categoricalStops = {stops};
mbgl::style::SourceFunction<MBGLType> sourceFunction = {sourceStyleFunction.attributeName.UTF8String, categoricalStops};
if (sourceStyleFunction.defaultValue) {
NSCAssert([sourceStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
MBGLType mbglValue;
id mglValue = [(MGLStyleConstantValue<ObjCType> *)sourceStyleFunction.defaultValue rawValue];
getMBGLValue(mglValue, mbglValue);
sourceFunction.defaultValue = mbglValue;
}
setDefaultMBGLValue(sourceStyleFunction, sourceFunction);
return sourceFunction;
}

Expand All @@ -387,13 +381,7 @@ class MGLStyleValueTransformer {
}];
mbgl::style::ExponentialStops<MBGLType> exponentialStops = {stops, (float)sourceStyleFunction.interpolationBase};
mbgl::style::SourceFunction<MBGLType> sourceFunction = {sourceStyleFunction.attributeName.UTF8String, exponentialStops};
if (sourceStyleFunction.defaultValue) {
NSCAssert([sourceStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
MBGLType mbglValue;
id mglValue = [(MGLStyleConstantValue<ObjCType> *)sourceStyleFunction.defaultValue rawValue];
getMBGLValue(mglValue, mbglValue);
sourceFunction.defaultValue = mbglValue;
}
setDefaultMBGLValue(sourceStyleFunction, sourceFunction);
return sourceFunction;
}

Expand All @@ -407,22 +395,27 @@ class MGLStyleValueTransformer {
}];
mbgl::style::IntervalStops<MBGLType> intervalStops = {stops};
mbgl::style::SourceFunction<MBGLType> sourceFunction = {sourceStyleFunction.attributeName.UTF8String, intervalStops};
if (sourceStyleFunction.defaultValue) {
NSCAssert([sourceStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
MBGLType mbglValue;
id mglValue = [(MGLStyleConstantValue<ObjCType> *)sourceStyleFunction.defaultValue rawValue];
getMBGLValue(mglValue, mbglValue);
sourceFunction.defaultValue = mbglValue;
}
setDefaultMBGLValue(sourceStyleFunction, sourceFunction);
return sourceFunction;
}

mbgl::style::SourceFunction<MBGLType> toMBGLIdentitySourceFunction(MGLSourceStyleFunction<ObjCType> *sourceStyleFunction) {
mbgl::style::IdentityStops<MBGLType> identityStops;
mbgl::style::SourceFunction<MBGLType> sourceFunction = {sourceStyleFunction.attributeName.UTF8String, identityStops};
setDefaultMBGLValue(sourceStyleFunction, sourceFunction);
return sourceFunction;
}

void setDefaultMBGLValue(MGLSourceStyleFunction<ObjCType> *sourceStyleFunction, mbgl::style::SourceFunction<MBGLType> &sourceFunction) {
if (sourceStyleFunction.defaultValue) {
NSCAssert([sourceStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
MBGLType mbglValue;
id mglValue = [(MGLStyleConstantValue<ObjCType> *)sourceStyleFunction.defaultValue rawValue];
getMBGLValue(mglValue, mbglValue);
sourceFunction.defaultValue = mbglValue;
}
}

// Bool
void getMBGLValue(NSNumber *rawValue, bool &mbglValue) {
mbglValue = !!rawValue.boolValue;
Expand Down Expand Up @@ -638,9 +631,13 @@ class MGLStyleValueTransformer {
}

id operator()(const mbgl::style::IdentityStops<MBGLType> &mbglStops) {
return [MGLSourceStyleFunction functionWithInterpolationMode:MGLInterpolationModeIdentity
stops:nil
attributeName:@(mbglFunction.property.c_str()) options:nil];
MGLSourceStyleFunction *sourceFunction = [MGLSourceStyleFunction functionWithInterpolationMode:MGLInterpolationModeIdentity
stops:nil
attributeName:@(mbglFunction.property.c_str()) options:nil];
if (mbglFunction.defaultValue) {
sourceFunction.defaultValue = [MGLStyleValue valueWithRawValue:toMGLRawStyleValue(*mbglFunction.defaultValue)];
}
return sourceFunction;
}

const mbgl::style::SourceFunction<MBGLType> &mbglFunction;
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/test/MGLStyleValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ extension MGLStyleValueTests {
interpolationMode: .identity,
sourceStops: nil,
attributeName: "size",
options: nil
options: [.defaultValue: MGLStyleValue<UIColor>(rawValue: .green)]
)
circleStyleLayer.circleColor = expectedSourceIdentityValue
XCTAssertEqual(circleStyleLayer.circleColor, expectedSourceIdentityValue)
Expand Down