aboutsummaryrefslogtreecommitdiff
path: root/Map/Views/MapRender.swift
blob: aa97e22ab12a883a247a5a259b8dae64dce5a8c4 (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
61
62
63
64
65
66
//
//  ContentView.swift
//  Map
//
//  Created by Ruben Beltran del Rio on 2/1/21.
//

import Combine
import CoreData
import CoreGraphics
import SwiftUI

struct MapRenderView: View {

  @Environment(\.colorScheme) var colorScheme

  @Binding var content: String
  @Binding var evolution: Stage

  @State var parsedMap: ParsedMap = ParsedMap.empty

  let mapSize = CGSize(width: 1300.0, height: 1000.0)

  let lineWidth = CGFloat(1.0)
  let vertexSize = CGSize(width: 25.0, height: 25.0)
  let padding = CGFloat(30.0)

  var body: some View {
    ZStack(alignment: .topLeading) {

      Path { path in
        path.addRect(
          CGRect(
            x: -padding, y: -padding, width: mapSize.width + padding * 2,
            height: mapSize.height + padding * 4))
      }.fill(MapColor.colorForScheme(colorScheme).background)

      MapStages(mapSize: mapSize, lineWidth: lineWidth, stages: parsedMap.stages)
      MapAxes(
        mapSize: mapSize, lineWidth: lineWidth, evolution: evolution, stages: parsedMap.stages)
      MapOpportunities(
        mapSize: mapSize, lineWidth: lineWidth, vertexSize: vertexSize,
        opportunities: parsedMap.opportunities)
      MapBlockers(mapSize: mapSize, vertexSize: vertexSize, blockers: parsedMap.blockers)
      MapVertices(mapSize: mapSize, vertexSize: vertexSize, vertices: parsedMap.vertices)
      MapEdges(
        mapSize: mapSize, lineWidth: lineWidth, vertexSize: vertexSize, edges: parsedMap.edges)
    }.frame(
      width: mapSize.width,
      height: mapSize.height + 2 * padding, alignment: .topLeading
    ).onAppear {
      self.parsedMap = Map.parse(content: content)
    }.padding(padding).onChange(of: content) { newState in
      self.parsedMap = Map.parse(content: newState)
    }
  }
}

struct MapRenderView_Previews: PreviewProvider {
  static var previews: some View {
    MapRenderView(
      content: Binding.constant(""), evolution: Binding.constant(Stage.stages(.general))
    ).environment(
      \.managedObjectContext, PersistenceController.preview.container.viewContext)
  }
}