This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
forked from maplibre/maplibre-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLNImageSource.mm
112 lines (89 loc) · 3.49 KB
/
MLNImageSource.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#import "MLNImageSource.h"
#import "MLNGeometry_Private.h"
#import "MLNLoggingConfiguration_Private.h"
#import "MLNSource_Private.h"
#import "MLNTileSource_Private.h"
#import "NSURL+MLNAdditions.h"
#if TARGET_OS_IPHONE
#import "UIImage+MLNAdditions.h"
#else
#import "NSImage+MLNAdditions.h"
#endif
#include <mbgl/style/sources/image_source.hpp>
#include <mbgl/util/premultiply.hpp>
@interface MLNImageSource ()
- (instancetype)initWithIdentifier:(NSString *)identifier coordinateQuad:(MLNCoordinateQuad)coordinateQuad NS_DESIGNATED_INITIALIZER;
@property (nonatomic, readonly) mbgl::style::ImageSource *rawSource;
@end
@implementation MLNImageSource
- (instancetype)initWithIdentifier:(NSString *)identifier coordinateQuad:(MLNCoordinateQuad)coordinateQuad {
const auto coordsArray = MLNLatLngArrayFromCoordinateQuad(coordinateQuad);
auto source = std::make_unique<mbgl::style::ImageSource>(identifier.UTF8String, coordsArray);
return self = [super initWithPendingSource:std::move(source)];
}
- (instancetype)initWithIdentifier:(NSString *)identifier coordinateQuad:(MLNCoordinateQuad)coordinateQuad URL:(NSURL *)url {
self = [self initWithIdentifier:identifier coordinateQuad: coordinateQuad];
self.URL = url;
return self;
}
- (instancetype)initWithIdentifier:(NSString *)identifier coordinateQuad:(MLNCoordinateQuad)coordinateQuad image:(MLNImage *)image {
self = [self initWithIdentifier:identifier coordinateQuad: coordinateQuad];
self.image = image;
return self;
}
- (NSURL *)URL {
MLNAssertStyleSourceIsValid();
auto url = self.rawSource->getURL();
return url ? [NSURL URLWithString:@(url->c_str())] : nil;
}
- (void)setURL:(NSURL *)url {
MLNAssertStyleSourceIsValid();
if (url) {
self.rawSource->setURL(url.mgl_URLByStandardizingScheme.absoluteString.UTF8String);
_image = nil;
} else {
self.image = nullptr;
}
}
- (void)setImage:(MLNImage *)image {
MLNAssertStyleSourceIsValid();
if (image != nullptr) {
self.rawSource->setImage(image.mgl_premultipliedImage);
} else {
self.rawSource->setImage(mbgl::PremultipliedImage({0,0}));
}
_image = image;
}
- (MLNCoordinateQuad)coordinates {
MLNAssertStyleSourceIsValid();
return MLNCoordinateQuadFromLatLngArray(self.rawSource->getCoordinates());
}
- (void)setCoordinates: (MLNCoordinateQuad)coordinateQuad {
MLNAssertStyleSourceIsValid();
self.rawSource->setCoordinates(MLNLatLngArrayFromCoordinateQuad(coordinateQuad));
}
- (NSString *)description {
if (self.rawSource) {
return [NSString stringWithFormat:@"<%@: %p; identifier = %@; coordinates = %@; URL = %@; image = %@>",
NSStringFromClass([self class]), (void *)self, self.identifier,
MLNStringFromCoordinateQuad(self.coordinates),
self.URL,
self.image];
}
else {
return [NSString stringWithFormat:@"<%@: %p; identifier = %@; coordinates = <unknown>; URL = <unknown>; image = %@>",
NSStringFromClass([self class]), (void *)self, self.identifier, self.image];
}
}
- (mbgl::style::ImageSource *)rawSource {
return (mbgl::style::ImageSource *)super.rawSource;
}
- (NSString *)attributionHTMLString {
if (!self.rawSource) {
MLNAssert(0, @"Source with identifier `%@` was invalidated after a style change", self.identifier);
return nil;
}
auto attribution = self.rawSource->getAttribution();
return attribution ? @(attribution->c_str()) : nil;
}
@end