]> git.r.bdr.sh - rbdr/map/blob - Map/Views/MapRender.swift
aa97e22ab12a883a247a5a259b8dae64dce5a8c4
[rbdr/map] / Map / Views / MapRender.swift
1 //
2 // ContentView.swift
3 // Map
4 //
5 // Created by Ruben Beltran del Rio on 2/1/21.
6 //
7
8 import Combine
9 import CoreData
10 import CoreGraphics
11 import SwiftUI
12
13 struct MapRenderView: View {
14
15 @Environment(\.colorScheme) var colorScheme
16
17 @Binding var content: String
18 @Binding var evolution: Stage
19
20 @State var parsedMap: ParsedMap = ParsedMap.empty
21
22 let mapSize = CGSize(width: 1300.0, height: 1000.0)
23
24 let lineWidth = CGFloat(1.0)
25 let vertexSize = CGSize(width: 25.0, height: 25.0)
26 let padding = CGFloat(30.0)
27
28 var body: some View {
29 ZStack(alignment: .topLeading) {
30
31 Path { path in
32 path.addRect(
33 CGRect(
34 x: -padding, y: -padding, width: mapSize.width + padding * 2,
35 height: mapSize.height + padding * 4))
36 }.fill(MapColor.colorForScheme(colorScheme).background)
37
38 MapStages(mapSize: mapSize, lineWidth: lineWidth, stages: parsedMap.stages)
39 MapAxes(
40 mapSize: mapSize, lineWidth: lineWidth, evolution: evolution, stages: parsedMap.stages)
41 MapOpportunities(
42 mapSize: mapSize, lineWidth: lineWidth, vertexSize: vertexSize,
43 opportunities: parsedMap.opportunities)
44 MapBlockers(mapSize: mapSize, vertexSize: vertexSize, blockers: parsedMap.blockers)
45 MapVertices(mapSize: mapSize, vertexSize: vertexSize, vertices: parsedMap.vertices)
46 MapEdges(
47 mapSize: mapSize, lineWidth: lineWidth, vertexSize: vertexSize, edges: parsedMap.edges)
48 }.frame(
49 width: mapSize.width,
50 height: mapSize.height + 2 * padding, alignment: .topLeading
51 ).onAppear {
52 self.parsedMap = Map.parse(content: content)
53 }.padding(padding).onChange(of: content) { newState in
54 self.parsedMap = Map.parse(content: newState)
55 }
56 }
57 }
58
59 struct MapRenderView_Previews: PreviewProvider {
60 static var previews: some View {
61 MapRenderView(
62 content: Binding.constant(""), evolution: Binding.constant(Stage.stages(.general))
63 ).environment(
64 \.managedObjectContext, PersistenceController.preview.container.viewContext)
65 }
66 }