Skip to content

Commit

Permalink
Style tweaks (#29)
Browse files Browse the repository at this point in the history
* Remove hard-coded zoom out factor in UIKit

* Allow setting more default styling

* Also allow specifying a point alpha
  • Loading branch information
nighthawk authored Sep 20, 2024
1 parent d677644 commit 0645933
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Examples/Cassini/CassiniApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct CassiniApp: App {
name: "Continents",
contents: try! GeoDrawer.Content.content(
for: GeoDrawer.Content.countries(),
color: CassiniApp.Colors.continents.cgColor
style: .init(color: CassiniApp.Colors.continents.cgColor)
),
color: CassiniApp.Colors.continents.cgColor
)
Expand Down
2 changes: 1 addition & 1 deletion Examples/Cassini/ContentView+Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ extension ContentView {

layers.append(.init(
name: preferredName ?? "New Layer",
contents: GeoDrawer.Content.content(for: geoJSON, color: color),
contents: GeoDrawer.Content.content(for: geoJSON, style: .init(color: color)),
color: color
))
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/Cassini/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ struct ContentView_Previews: PreviewProvider {
name: "Continents",
contents: try! GeoDrawer.Content.content(
for: GeoDrawer.Content.countries(),
color: CassiniApp.Colors.continents.cgColor
style: .init(color: CassiniApp.Colors.continents.cgColor)
),
color: CassiniApp.Colors.continents.cgColor
)
Expand Down
15 changes: 11 additions & 4 deletions Sources/GeoDrawer/GeoDrawer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,17 @@ extension GeoDrawer {
self.alpha = alpha
}

public let red: Double
public let green: Double
public let blue: Double
public let alpha: Double
public var red: Double
public var green: Double
public var blue: Double
public var alpha: Double

public func copy(alpha: Double) -> Self? {
guard alpha != self.alpha else { return nil }
var updated = self
updated.alpha = alpha
return updated
}
}
#endif

Expand Down
5 changes: 4 additions & 1 deletion Sources/GeoDrawer/GeoMap+AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ public struct GeoMap: NSViewRepresentable {
struct GeoMap_Previews: PreviewProvider {
static var previews: some View {
GeoMap(
contents: try! GeoDrawer.Content.content(for: GeoDrawer.Content.countries(), color: .init(red: 0, green: 1, blue: 0, alpha: 0)),
contents: try! GeoDrawer.Content.content(
for: GeoDrawer.Content.countries(),
style: .init(color: .init(red: 0, green: 1, blue: 0, alpha: 0))
),
projection: Projections.Cassini()
)
.previewLayout(.fixed(width: 300, height: 300))
Expand Down
6 changes: 4 additions & 2 deletions Sources/GeoDrawer/GeoMap+UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public class GeoMapView: UIView {
size: .init(frame.size),
projection: projection,
zoomTo: zoomTo,
zoomOutFactor: 5,
insets: insets
)
_drawer = drawer
Expand Down Expand Up @@ -175,7 +174,10 @@ public struct GeoMap: UIViewRepresentable {
struct GeoMap_Previews: PreviewProvider {
static var previews: some View {
GeoMap(
contents: try! GeoDrawer.Content.content(for: GeoDrawer.Content.countries(), color: .init(red: 0, green: 1, blue: 0, alpha: 0)),
contents: try! GeoDrawer.Content.content(
for: GeoDrawer.Content.countries(),
style: .init(color: .init(red: 0, green: 1, blue: 0, alpha: 0))
),
projection: Projections.Cassini()
)
.previewLayout(.fixed(width: 300, height: 300))
Expand Down
57 changes: 42 additions & 15 deletions Sources/GeoDrawer/styling/GeoDrawerContent+Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,32 @@ extension GeoJSON.GeometryObject {

extension GeoDrawer.Content {

public static func content(for geoJSON: GeoJSON, color: GeoDrawer.Color, polygonStroke: (GeoDrawer.Color, width: Double)? = nil) -> [GeoDrawer.Content] {
public struct DefaultStyle {
/// Fallback color to use for points, lines, and **fill** of polygons.
public var color: GeoDrawer.Color

/// Fallback color and width to use for polygons.
public var polygonStroke: (GeoDrawer.Color, width: Double)? = nil

/// Fallback stroke width for lines
public var lineWidth: Double = 2

/// Fallback radius to use for points.
public var pointRadius: Double = 1

/// Alpha to apply on every point or circle
public var pointAlpha: Double = 1

public init(color: GeoDrawer.Color, polygonStroke: (GeoDrawer.Color, width: Double)? = nil, lineWidth: Double = 2, pointRadius: Double = 1, pointAlpha: Double = 1) {
self.color = color
self.polygonStroke = polygonStroke
self.lineWidth = lineWidth
self.pointRadius = pointRadius
self.pointAlpha = pointAlpha
}
}

public static func content(for geoJSON: GeoJSON, style: DefaultStyle) -> [GeoDrawer.Content] {
let elements: [(GeoJSON.Geometry, [String: AnyHashable]?)]
switch geoJSON.object {
case .geometry(let geo):
Expand All @@ -40,13 +65,13 @@ extension GeoDrawer.Content {
}

return elements.map { geometry, properties in
Self.content(for: geometry, properties: properties, color: color, polygonStroke: polygonStroke)
Self.content(for: geometry, properties: properties, style: style)
}
}

public static func content(for geometry: GeoJSON.GeometryObject, properties: [String: AnyHashable]? = nil, color: GeoDrawer.Color, polygonStroke: (GeoDrawer.Color, width: Double)? = nil) -> [GeoDrawer.Content] {
public static func content(for geometry: GeoJSON.GeometryObject, properties: [String: AnyHashable]? = nil, style: DefaultStyle) -> [GeoDrawer.Content] {
return geometry.geometries.map {
Self.content(for: $0, properties: properties, color: color, polygonStroke: polygonStroke)
Self.content(for: $0, properties: properties, style: style)
}
}

Expand All @@ -56,31 +81,33 @@ extension GeoDrawer.Content {
/// - Parameters:
/// - geometry: The geometry
/// - properties: Optional properties. If these follow [GeoJSON simplespec 1.1](https://github.com/mapbox/simplestyle-spec/tree/master/1.1.0), then those style is preferred over explicitly set style. Points can additionally be adjusted in size by setting `circle-radius`.
/// - color: Color to use for points, lines, and **fill** of polygons.
/// - polygonStroke: Color and width to use for polygons.
/// - style: Default style to use if not otherwise specified in `properties`
/// - Returns: Drawable content.
public static func content(for geometry: GeoJSON.Geometry, properties: [String: AnyHashable]? = nil, color: GeoDrawer.Color, polygonStroke: (GeoDrawer.Color, width: Double)? = nil) -> GeoDrawer.Content {
public static func content(for geometry: GeoJSON.Geometry, properties: [String: AnyHashable]? = nil, style: DefaultStyle) -> GeoDrawer.Content {
switch geometry {
case .polygon(let polygon):
return .polygon(
polygon,
fill: Self.color(properties, colorKey: "fill", alphaKey: "fill-opacity") ?? color,
stroke: Self.color(properties, colorKey: "stroke", alphaKey: "stroke-opacity") ?? polygonStroke?.0,
strokeWidth: (properties?["stroke-width"] as? Double) ?? polygonStroke?.width ?? 0
fill: Self.color(properties, colorKey: "fill", alphaKey: "fill-opacity") ?? style.color,
stroke: Self.color(properties, colorKey: "stroke", alphaKey: "stroke-opacity") ?? style.polygonStroke?.0,
strokeWidth: (properties?["stroke-width"] as? Double) ?? style.polygonStroke?.width ?? 0
)

case .lineString(let line):
return .line(
line,
stroke: Self.color(properties, colorKey: "stroke", alphaKey: "stroke-opacity") ?? color,
strokeWidth: properties?["stroke-width"] as? Double ?? 2
stroke: Self.color(properties, colorKey: "stroke", alphaKey: "stroke-opacity") ?? style.color,
strokeWidth: properties?["stroke-width"] as? Double ?? style.lineWidth
)

case .point(let position):
let baseFill = Self.color(properties, colorKey: "fill", alphaKey: "fill-opacity") ?? style.color
return .circle(
position,
radius: properties?["circle-radius"] as? Double ?? 1,
fill: Self.color(properties, colorKey: "fill", alphaKey: "fill-opacity") ?? color,
radius: properties?["circle-radius"] as? Double ?? style.pointRadius,
fill: baseFill.copy(alpha: baseFill.alpha * style.pointAlpha) ?? baseFill,
stroke: Self.color(properties, colorKey: "stroke", alphaKey: "stroke-opacity"),
strokeWidth: (properties?["stroke-width"] as? Double) ?? 0
strokeWidth: (properties?["stroke-width"] as? Double) ?? style.lineWidth
)
}
}
Expand Down

0 comments on commit 0645933

Please sign in to comment.