This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ios, macos] Add the MGLLight generation templates
- Loading branch information
1 parent
b2f277a
commit 6cde1a4
Showing
3 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |