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

Commit

Permalink
[ios, macos] Template images as style images
Browse files Browse the repository at this point in the history
Convert template images to SDF icons and back when storing them as style images.
  • Loading branch information
1ec5 committed Dec 7, 2016
1 parent e6d51c3 commit 88f91a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
12 changes: 10 additions & 2 deletions platform/ios/src/UIImage+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ - (nullable instancetype)initWithMGLSpriteImage:(const mbgl::SpriteImage *)sprit
{
std::string png = encodePNG(spriteImage->image);
NSData *data = [[NSData alloc] initWithBytes:png.data() length:png.size()];
return [self initWithData:data scale:spriteImage->pixelRatio];
if (self = [self initWithData:data scale:spriteImage->pixelRatio])
{
if (spriteImage->sdf)
{
self = [self imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
}
return self;
}

- (std::unique_ptr<mbgl::SpriteImage>)mgl_spriteImage
Expand All @@ -28,7 +35,8 @@ - (nullable instancetype)initWithMGLSpriteImage:(const mbgl::SpriteImage *)sprit
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

return std::make_unique<mbgl::SpriteImage>(std::move(cPremultipliedImage), float(self.scale));
BOOL isTemplate = self.renderingMode == UIImageRenderingModeAlwaysTemplate;
return std::make_unique<mbgl::SpriteImage>(std::move(cPremultipliedImage), float(self.scale), isTemplate);
}

@end
16 changes: 16 additions & 0 deletions platform/macos/app/MapDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,22 @@ - (IBAction)manipulateStyle:(id)sender {
fillLayer.fillColor = [MGLStyleValue<NSColor *> valueWithRawValue:[NSColor greenColor]];
fillLayer.predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"type", @"park"];
[self.mapView.style addLayer:fillLayer];

NSImage *image = [NSImage imageNamed:NSImageNameIChatTheaterTemplate];
[self.mapView.style setImage:image forName:NSImageNameIChatTheaterTemplate];

MGLSource *streetsSource = [self.mapView.style sourceWithIdentifier:@"composite"];
MGLSymbolStyleLayer *theaterLayer = [[MGLSymbolStyleLayer alloc] initWithIdentifier:@"theaters" source:streetsSource];
theaterLayer.sourceLayerIdentifier = @"poi_label";
theaterLayer.predicate = [NSPredicate predicateWithFormat:@"maki == 'theatre'"];
theaterLayer.iconImageName = [MGLStyleValue valueWithRawValue:NSImageNameIChatTheaterTemplate];
theaterLayer.iconSize = [MGLStyleValue valueWithRawValue:@2];
theaterLayer.iconColor = [MGLStyleValue valueWithStops:@{
@16.0: [MGLStyleValue valueWithRawValue:[NSColor redColor]],
@18.0: [MGLStyleValue valueWithRawValue:[NSColor yellowColor]],
@20.0: [MGLStyleValue valueWithRawValue:[NSColor blackColor]],
}];
[self.mapView.style addLayer:theaterLayer];
}

- (IBAction)dropPin:(NSMenuItem *)sender {
Expand Down
4 changes: 3 additions & 1 deletion platform/macos/src/NSImage+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ - (nullable instancetype)initWithMGLSpriteImage:(const mbgl::SpriteImage *)sprit
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:data];
if ([self initWithSize:NSMakeSize(spriteImage->getWidth(), spriteImage->getHeight())]) {
[self addRepresentation:rep];
[self setTemplate:spriteImage->sdf];
}
return self;
}
Expand All @@ -23,7 +24,8 @@ - (nullable instancetype)initWithMGLSpriteImage:(const mbgl::SpriteImage *)sprit
mbgl::PremultipliedImage cPremultipliedImage(rep.pixelsWide, rep.pixelsHigh);
std::copy(rep.bitmapData, rep.bitmapData + cPremultipliedImage.size(), cPremultipliedImage.data.get());
return std::make_unique<mbgl::SpriteImage>(std::move(cPremultipliedImage),
(float)(rep.pixelsWide / self.size.width));
(float)(rep.pixelsWide / self.size.width),
[self isTemplate]);
}

@end

0 comments on commit 88f91a4

Please sign in to comment.