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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
//
// 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)
}
}
|