// // ContentView.swift // Map // // Created by Ruben Beltran del Rio on 2/1/21. // import SwiftUI import CoreData import CoreGraphics struct MapRenderView: View { @ObservedObject var map: Map let evolution: Stage let VERTEX_SIZE = CGSize(width: 25.0, height: 25.0) let ARROWHEAD_SIZE = CGFloat(10.0) let LINE_WIDTH = CGFloat(1.0) let PADDING = CGFloat(4.0) let STAGE_FRAME = CGSize(width: 245, height: 50) var parsedMap: ParsedMap { return map.parse() } var body: some View { ZStack(alignment: .topLeading) { ZStack (alignment: .topLeading) { // The Axes Path { path in path.move(to: CGPoint(x: 0, y: 0)) path.addLine(to: CGPoint(x: 0, y: 1000)) path.addLine(to: CGPoint(x: 1000, y: 1000)) path.move(to: CGPoint(x: 1000, y: 1000)) path.closeSubpath() }.strokedPath(StrokeStyle(lineWidth: LINE_WIDTH * 2)) Text("Visible").font(.title3).rotationEffect(Angle(degrees: -90.0)).offset(CGSize(width: -35.0, height: 0.0)) Text("Value Chain").font(.title).rotationEffect(Angle(degrees: -90.0)).offset(CGSize(width: -72.0, height: 480.0)) Text("Invisible").font(.title3).rotationEffect(Angle(degrees: -90.0)).offset(CGSize(width: -40.0, height: 980.0)) Text(evolution.I).font(.title3).frame(width: STAGE_FRAME.width, height: STAGE_FRAME.height, alignment: .topLeading).offset(CGSize(width: 0.0, height: 1000.0)) Text(evolution.II).font(.title3).frame(width: STAGE_FRAME.width, height: STAGE_FRAME.height, alignment: .topLeading).offset(CGSize(width: 250.0, height: 1000.0)) Text(evolution.III).font(.title3).frame(width: STAGE_FRAME.width, height: STAGE_FRAME.height, alignment: .topLeading).offset(CGSize(width: 500.0, height: 1000.0)) Text(evolution.IV).font(.title3).frame(width: STAGE_FRAME.width, height: STAGE_FRAME.height, alignment: .topLeading).offset(CGSize(width: 750.0, height: 1000.0)) }.offset(CGSize(width: 0.0, height: 0.0)) // The Lanes Path { path in path.move(to: CGPoint(x: 250, y: 0)) path.addLine(to: CGPoint(x: 250, y: 1000)) path.move(to: CGPoint(x: 500, y: 0)) path.addLine(to: CGPoint(x: 500, y: 1000)) path.move(to: CGPoint(x: 750, y: 0)) path.addLine(to: CGPoint(x: 750, y: 1000)) path.move(to: CGPoint(x: 250, y: 0)) path.closeSubpath() }.strokedPath(StrokeStyle(lineWidth: LINE_WIDTH, dash: [10.0])) Path { path in path.addRect(CGRect(x: 0, y: 0, width: 250, height: 1000)) }.fill(Color.red).opacity(0.1) Path { path in path.addRect(CGRect(x: 250, y: 0, width: 250, height: 1000)) }.fill(Color.orange).opacity(0.1) Path { path in path.addRect(CGRect(x: 500, y: 0, width: 250, height: 1000)) }.fill(Color.yellow).opacity(0.1) Path { path in path.addRect(CGRect(x: 750, y: 0, width: 250, height: 1000)) }.fill(Color.green).opacity(0.1) // The Vertices ForEach(parsedMap.vertices, id: \.id) { vertex in Path { path in path.addEllipse(in: CGRect( origin: vertex.position, size: VERTEX_SIZE )) } Text(vertex.label).foregroundColor(Color.gray).offset(CGSize( width: vertex.position.x + VERTEX_SIZE.width + PADDING, height: vertex.position.y)) } // The Edges ForEach(parsedMap.edges, id: \.id) { edge in Path { path in let slope = (edge.destination.y - edge.origin.y) / (edge.destination.x - edge.origin.x) let angle = atan(slope) let multiplier = CGFloat(slope < 0 ? -1.0 : 1.0) let upperAngle = angle - CGFloat.pi / 4.0 let lowerAngle = angle + CGFloat.pi / 4.0 let offsetOrigin = CGPoint(x: edge.origin.x + multiplier * (VERTEX_SIZE.width / 2.0) * cos(angle), y: edge.origin.y + multiplier * (VERTEX_SIZE.height / 2.0) * sin(angle)) let offsetDestination = CGPoint(x: edge.destination.x - multiplier * (VERTEX_SIZE.width / 2.0) * cos(angle), y: edge.destination.y - multiplier * (VERTEX_SIZE.height / 2.0) * sin(angle)) path.move(to: offsetOrigin) path.addLine(to: offsetDestination) // Arrowheads path.move(to: offsetDestination) path.addLine(to: CGPoint(x: offsetDestination.x - multiplier * ARROWHEAD_SIZE * cos(upperAngle), y: offsetDestination.y - multiplier * ARROWHEAD_SIZE * sin(upperAngle))) path.move(to: offsetDestination) path.addLine(to: CGPoint(x: offsetDestination.x - multiplier * ARROWHEAD_SIZE * cos(lowerAngle), y: offsetDestination.y - multiplier * ARROWHEAD_SIZE * sin(lowerAngle))) path.move(to: offsetDestination) path.closeSubpath() }.applying(CGAffineTransform(translationX: VERTEX_SIZE.width / 2.0, y: VERTEX_SIZE.height / 2.0)).strokedPath(StrokeStyle(lineWidth: LINE_WIDTH)) } }.frame( minWidth: 1050.0, maxWidth: .infinity, minHeight: 1050.0, maxHeight: .infinity, alignment: .topLeading ).padding(30.0) } } struct MapRenderView_Previews: PreviewProvider { static var previews: some View { MapDetailView(map: Map()).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } }