|
| 1 | +import Foundation |
| 2 | +import MapLibre |
| 3 | + |
| 4 | +public protocol MapControl { |
| 5 | + /// Overrides the position of the control. Default values are control-specfiic. |
| 6 | + var position: MLNOrnamentPosition? { get set } |
| 7 | + /// Overrides the offset of the control. |
| 8 | + var margins: CGPoint? { get set } |
| 9 | + /// Overrides whether the control is hidden. |
| 10 | + var isHidden: Bool { get set } |
| 11 | + |
| 12 | + @MainActor func configureMapView(_ mapView: MLNMapView) |
| 13 | +} |
| 14 | + |
| 15 | +public extension MapControl { |
| 16 | + /// Sets position of the control. |
| 17 | + func position(_ position: MLNOrnamentPosition) -> Self { |
| 18 | + var result = self |
| 19 | + |
| 20 | + result.position = position |
| 21 | + |
| 22 | + return result |
| 23 | + } |
| 24 | + |
| 25 | + /// Sets the position offset of the control. |
| 26 | + func margins(_ margins: CGPoint) -> Self { |
| 27 | + var result = self |
| 28 | + |
| 29 | + result.margins = margins |
| 30 | + |
| 31 | + return result |
| 32 | + } |
| 33 | + |
| 34 | + /// Hides the control. |
| 35 | + func hidden(_: Bool) -> Self { |
| 36 | + var result = self |
| 37 | + |
| 38 | + result.isHidden = true |
| 39 | + |
| 40 | + return result |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +public struct CompassView: MapControl { |
| 45 | + public var position: MLNOrnamentPosition? |
| 46 | + public var margins: CGPoint? |
| 47 | + public var isHidden: Bool = false |
| 48 | + |
| 49 | + public func configureMapView(_ mapView: MLNMapView) { |
| 50 | + if let position { |
| 51 | + mapView.compassViewPosition = position |
| 52 | + } |
| 53 | + |
| 54 | + if let margins { |
| 55 | + mapView.compassViewMargins = margins |
| 56 | + } |
| 57 | + |
| 58 | + mapView.compassView.isHidden = isHidden |
| 59 | + } |
| 60 | + |
| 61 | + public init() {} |
| 62 | +} |
| 63 | + |
| 64 | +public struct LogoView: MapControl { |
| 65 | + public var position: MLNOrnamentPosition? |
| 66 | + public var margins: CGPoint? |
| 67 | + public var isHidden: Bool = false |
| 68 | + public var image: UIImage? |
| 69 | + |
| 70 | + public init() {} |
| 71 | + |
| 72 | + public func configureMapView(_ mapView: MLNMapView) { |
| 73 | + if let position { |
| 74 | + mapView.logoViewPosition = position |
| 75 | + } |
| 76 | + |
| 77 | + if let margins { |
| 78 | + mapView.logoViewMargins = margins |
| 79 | + } |
| 80 | + |
| 81 | + mapView.logoView.isHidden = isHidden |
| 82 | + |
| 83 | + if let image { |
| 84 | + mapView.logoView.image = image |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +public extension LogoView { |
| 90 | + /// Sets the logo image (defaults to the MapLibre logo). |
| 91 | + func image(_ image: UIImage?) -> Self { |
| 92 | + var result = self |
| 93 | + |
| 94 | + result.image = image |
| 95 | + |
| 96 | + return result |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +@resultBuilder |
| 101 | +public enum MapControlsBuilder: DefaultResultBuilder { |
| 102 | + public static func buildExpression(_ expression: MapControl) -> [MapControl] { |
| 103 | + [expression] |
| 104 | + } |
| 105 | + |
| 106 | + public static func buildExpression(_ expression: [MapControl]) -> [MapControl] { |
| 107 | + expression |
| 108 | + } |
| 109 | + |
| 110 | + public static func buildExpression(_: Void) -> [MapControl] { |
| 111 | + [] |
| 112 | + } |
| 113 | + |
| 114 | + public static func buildBlock(_ components: [MapControl]...) -> [MapControl] { |
| 115 | + components.flatMap { $0 } |
| 116 | + } |
| 117 | + |
| 118 | + public static func buildArray(_ components: [MapControl]) -> [MapControl] { |
| 119 | + components |
| 120 | + } |
| 121 | + |
| 122 | + public static func buildArray(_ components: [[MapControl]]) -> [MapControl] { |
| 123 | + components.flatMap { $0 } |
| 124 | + } |
| 125 | + |
| 126 | + public static func buildEither(first components: [MapControl]) -> [MapControl] { |
| 127 | + components |
| 128 | + } |
| 129 | + |
| 130 | + public static func buildEither(second components: [MapControl]) -> [MapControl] { |
| 131 | + components |
| 132 | + } |
| 133 | + |
| 134 | + public static func buildOptional(_ components: [MapControl]?) -> [MapControl] { |
| 135 | + components ?? [] |
| 136 | + } |
| 137 | +} |
0 commit comments