aboutsummaryrefslogtreecommitdiff
path: root/Map/MapRenderComponents/MapStages.swift
blob: 052a3157af8a2ef2386a28ca4375704be38f63cd (plain)
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
import SwiftUI

struct MapStages: View {

  @Environment(\.colorScheme) var colorScheme

  let mapSize: CGSize
  let lineWidth: CGFloat
  let stages: [CGFloat]
  let opacity = 0.1

  var color: MapColor {
    MapColor.colorForScheme(colorScheme)
  }

  var body: some View {

    ZStack(alignment: .topLeading) {
      Path { path in
        path.addRect(CGRect(x: 0, y: 0, width: w(stages[0]), height: mapSize.height))
      }.fill(color.stages.i)
      Path { path in
        path.addRect(
          CGRect(x: w(stages[0]), y: 0, width: w(stages[1]) - w(stages[0]), height: mapSize.height))
      }.fill(color.stages.ii)
      Path { path in
        path.addRect(
          CGRect(x: w(stages[1]), y: 0, width: w(stages[2]) - w(stages[1]), height: mapSize.height))
      }.fill(color.stages.iii)
      Path { path in
        path.addRect(
          CGRect(x: w(stages[2]), y: 0, width: mapSize.width - w(stages[2]), height: mapSize.height)
        )
      }.fill(color.stages.iv)

      Path { path in
        path.move(to: CGPoint(x: w(stages[0]), y: 0))
        path.addLine(to: CGPoint(x: w(stages[0]), y: mapSize.height))
        path.move(to: CGPoint(x: w(stages[1]), y: 0))
        path.addLine(to: CGPoint(x: w(stages[1]), y: mapSize.height))
        path.move(to: CGPoint(x: w(stages[2]), y: 0))
        path.addLine(to: CGPoint(x: w(stages[2]), y: mapSize.height))
        path.move(to: CGPoint(x: w(stages[0]), y: 0))
        path.closeSubpath()
      }.strokedPath(StrokeStyle(lineWidth: lineWidth, dash: [10.0])).stroke(color.foreground)
    }
  }

  func w(_ dimension: CGFloat) -> CGFloat {
    max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
  }
}

struct MapStages_Previews: PreviewProvider {
  static var previews: some View {
    MapStages(
      mapSize: CGSize(width: 200.0, height: 200.0), lineWidth: CGFloat(1.0),
      stages: [25.0, 50.0, 75.0])
  }
}