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

Commit

Permalink
[ios, macos] Add the MGLLight generation templates
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-guerra committed Jun 1, 2017
1 parent b2f277a commit 6cde1a4
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 0 deletions.
15 changes: 15 additions & 0 deletions platform/darwin/scripts/generate-style-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,28 @@ global.setSourceLayer = function() {
return `_layer->setSourceLayer(sourceLayer.UTF8String);`
};

const lightProperties = Object.keys(spec[`light`]).reduce((memo, name) => {
var property = spec[`light`][name];
property.name = name;
property['light-property'] = true;
memo.push(property);
return memo;
}, []);

const layerH = ejs.compile(fs.readFileSync('platform/darwin/src/MGLStyleLayer.h.ejs', 'utf8'), { strict: true });
const layerM = ejs.compile(fs.readFileSync('platform/darwin/src/MGLStyleLayer.mm.ejs', 'utf8'), { strict: true});
const testLayers = ejs.compile(fs.readFileSync('platform/darwin/test/MGLStyleLayerTests.mm.ejs', 'utf8'), { strict: true});
const forStyleAuthorsMD = ejs.compile(fs.readFileSync('platform/darwin/docs/guides/For Style Authors.md.ejs', 'utf8'), { strict: true });
const ddsGuideMD = ejs.compile(fs.readFileSync('platform/darwin/docs/guides/Using Style Functions at Runtime.md.ejs', 'utf8'), { strict: true });
const templatesMD = ejs.compile(fs.readFileSync('platform/darwin/docs/guides/Tile URL Templates.md.ejs', 'utf8'), { strict: true });

const lightH = ejs.compile(fs.readFileSync('platform/darwin/src/MGLLight.h.ejs', 'utf8'), {strict: true});;
const lightM = ejs.compile(fs.readFileSync('platform/darwin/src/MGLLight.mm.ejs', 'utf8'), {strict: true});;
fs.writeFileSync(`platform/darwin/src/MGLLight.h`, lightH(lightProperties));
fs.writeFileSync(`platform/darwin/src/MGLLight.mm`, lightM(lightProperties));



const layers = _(spec.layer.type.values).map((value, layerType) => {
const layoutProperties = Object.keys(spec[`layout_${layerType}`]).reduce((memo, name) => {
if (name !== 'visibility') {
Expand Down
125 changes: 125 additions & 0 deletions platform/darwin/src/MGLLight.h.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#import <CoreLocation/CoreLocation.h>

#import "MGLFoundation.h"
#import "MGLStyleValue.h"

NS_ASSUME_NONNULL_BEGIN


/** Options to specify extruded geometries are lit relative to the map or viewport. */
typedef NS_ENUM(NSUInteger, MGLLightAnchor) {
/** The position of the light source is aligned to the rotation of the map. */
MGLLightAnchorMap,
/** The position of the light source is aligned to the rotation of the viewport. */
MGLLightAnchorViewport
};

/**
A structure containing information about the position of the light source
relative to lit geometries.
*/
typedef struct MGLSphericalPosition {
/** Distance from the center of the base of an object to its light. */
CLLocationDistance radial;
/** Position of the light relative to 0° (0° when `MGLLight.anchor` is set to viewport corresponds
to the top of the viewport, or 0° when `MGLLight.anchor` is set to map corresponds to due north,
and degrees proceed clockwise). */
CLLocationDirection azimuthal;
/** Indicates the height of the light (from 0°, directly above, to 180°, directly below). */
CLLocationDirection polar;
} MGLSphericalPosition;

/**
Creates a new `MGLSphericalPosition` from the given radial, azimuthal, polar.

@param radial The radial coordinate.
@param azimuthal The azimuthal angle.
@param polar The polar angle.

@return Returns a `MGLSphericalPosition` struct containing the position attributes.
*/
NS_INLINE MGLSphericalPosition MGLSphericalPositionMake(CLLocationDistance radial, CLLocationDirection azimuthal, CLLocationDirection polar) {
MGLSphericalPosition position;
position.radial = radial;
position.azimuthal = azimuthal;
position.polar = polar;

return position;
}

/**
An `MGLLight` object represents the light source for extruded geometries in `MGLStyle`.
*/
MGL_EXPORT
@interface MGLLight : NSObject

/**
`anchor` Whether extruded geometries are lit relative to the map or viewport.

This property corresponds to the <a
href="https://www.mapbox.com/mapbox-gl-js/style-spec/#light-anchor"><code>anchor</code></a>
light property in the Mapbox Style Specification.
*/
@property (nonatomic) MGLStyleValue<NSValue *> *anchor;

/**
Values describing animated transitions to `anchor` property.
*/
@property (nonatomic) MGLTransition anchorTransition;


/**
Position of the light source relative to lit (extruded) geometries.

This property corresponds to the <a
href="https://www.mapbox.com/mapbox-gl-js/style-spec/#light-position"><code>position</code></a>
light property in the Mapbox Style Specification.
*/
@property (nonatomic) MGLStyleValue<NSValue *> *position;

/**
Values describing animated transitions to `position` property.
*/
@property (nonatomic) MGLTransition positionTransiton;


#if TARGET_OS_IPHONE
/**
Color tint for lighting extruded geometries.

This property corresponds to the <a
href="https://www.mapbox.com/mapbox-gl-js/style-spec/#light-color"><code>color</code></a>
light property in the Mapbox Style Specification.
*/
@property (nonatomic) MGLStyleValue<UIColor *> *color;
#else

/**
Color tint for lighting extruded geometries.
*/
@property (nonatomic) MGLStyleValue<NSColor *> *color;
#endif

/**
Values describing animated transitions to `color` property.
*/
@property (nonatomic) MGLTransition colorTransiton;


/**
Intensity of lighting (on a scale from 0 to 1). Higher numbers will present as more extreme contrast.

This property corresponds to the <a
href="https://www.mapbox.com/mapbox-gl-js/style-spec/#light-intensity"><code>intensity</code></a>
light property in the Mapbox Style Specification.
*/
@property(nonatomic) MGLStyleValue<NSNumber *> *intensity;

/**
Values describing animated transitions to `intensity` property.
*/
@property (nonatomic) MGLTransition intensityTransition;

@end

NS_ASSUME_NONNULL_END
113 changes: 113 additions & 0 deletions platform/darwin/src/MGLLight.mm.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#import "MGLLight.h"

#import "MGLTypes.h"
#import "NSDate+MGLAdditions.h"
#import "MGLStyleValue_Private.h"
#import "NSValue+MGLAdditions.h"

#import <mbgl/style/light.hpp>
#import <mbgl/style/types.hpp>

namespace mbgl {

MBGL_DEFINE_ENUM(MGLLightAnchor, {
{ MGLLightAnchorMap, "map" },
{ MGLLightAnchorViewport, "viewport" },
});

}

NS_INLINE MGLTransition MGLTransitionFromOptions(const mbgl::style::TransitionOptions& options) {
MGLTransition transition;
transition.duration = MGLTimeIntervalFromDuration(options.duration.value_or(mbgl::Duration::zero()));
transition.delay = MGLTimeIntervalFromDuration(options.delay.value_or(mbgl::Duration::zero()));

return transition;
}

NS_INLINE mbgl::style::TransitionOptions MGLOptionsFromTransition(MGLTransition transition) {
mbgl::style::TransitionOptions options { { MGLDurationFromTimeInterval(transition.duration) }, { MGLDurationFromTimeInterval(transition.delay) } };
return options;
}

@interface MGLLight()

@end

@implementation MGLLight

- (instancetype)initWithMBGLLight:(const mbgl::style::Light *)mbglLight
{
if (self = [super init]) {
auto anchor = mbglLight->getAnchor();
MGLStyleValue<NSValue *> *anchorStyleValue;
if (anchor.isUndefined()) {
mbgl::style::PropertyValue<mbgl::style::LightAnchorType> defaultAnchor = mbglLight->getDefaultAnchor();
anchorStyleValue = MGLStyleValueTransformer<mbgl::style::LightAnchorType, NSValue *, mbgl::style::LightAnchorType, MGLLightAnchor>().toEnumStyleValue(defaultAnchor);
} else {
anchorStyleValue = MGLStyleValueTransformer<mbgl::style::LightAnchorType, NSValue *, mbgl::style::LightAnchorType, MGLLightAnchor>().toEnumStyleValue(anchor);
}

_anchor = anchorStyleValue;

_anchorTransition = MGLTransitionFromOptions(mbglLight->getAnchorTransition());

auto positionValue = mbglLight->getPosition();
if (positionValue.isUndefined()) {
_position = MGLStyleValueTransformer<mbgl::style::Position, NSValue *>().toStyleValue(mbglLight->getDefaultPosition());
} else {
_position = MGLStyleValueTransformer<mbgl::style::Position, NSValue *>().toStyleValue(positionValue);
}

_positionTransiton = MGLTransitionFromOptions(mbglLight->getPositionTransition());

auto colorValue = mbglLight->getColor();
if (colorValue.isUndefined()) {
_color = MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toStyleValue(mbglLight->getDefaultColor());
} else {
_color = MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toStyleValue(colorValue);
}

_colorTransiton = MGLTransitionFromOptions(mbglLight->getColorTransition());

auto intensityValue = mbglLight->getIntensity();
if (intensityValue.isUndefined()) {
_intensity = MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(mbglLight->getDefaultIntensity());
} else {
_intensity = MGLStyleValueTransformer<float, NSNumber *>().toStyleValue(intensityValue);
}

_intensityTransition = MGLTransitionFromOptions(mbglLight->getIntensityTransition());
}

return self;
}

- (mbgl::style::Light)mbglLight
{
mbgl::style::Light mbglLight;

auto anchor = MGLStyleValueTransformer<mbgl::style::LightAnchorType, NSValue *, mbgl::style::LightAnchorType, MGLLightAnchor>().toEnumPropertyValue(self.anchor);
mbglLight.setAnchor(anchor);

mbglLight.setAnchorTransition(MGLOptionsFromTransition(self.anchorTransition));

auto position = MGLStyleValueTransformer<mbgl::style::Position, NSValue *>().toInterpolatablePropertyValue(self.position);
mbglLight.setPosition(position);

mbglLight.setPositionTransition(MGLOptionsFromTransition(self.positionTransiton));

auto color = MGLStyleValueTransformer<mbgl::Color, MGLColor *>().toInterpolatablePropertyValue(self.color);
mbglLight.setColor(color);

mbglLight.setColorTransition(MGLOptionsFromTransition(self.colorTransiton));

auto intensity = MGLStyleValueTransformer<float, NSNumber *>().toInterpolatablePropertyValue(self.intensity);
mbglLight.setIntensity(intensity);

mbglLight.setIntensityTransition(MGLOptionsFromTransition(self.intensityTransition));

return mbglLight;
}

@end

0 comments on commit 6cde1a4

Please sign in to comment.