aboutsummaryrefslogtreecommitdiff
path: root/Map/MapRenderComponents
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2021-02-03 23:53:12 +0100
committerRuben Beltran del Rio <ruben@unlimited.pizza>2021-02-03 23:53:12 +0100
commit5e8ff4850c4827125fe12788dd5b153c4f636f48 (patch)
treef37c90338f33e7ddc84cc855a20dca1ca503476e /Map/MapRenderComponents
parent1b85f723b48d38cf345bb9a4f3fd01aa6039b50b (diff)
Add code for first release
Diffstat (limited to 'Map/MapRenderComponents')
-rw-r--r--Map/MapRenderComponents/MapAxes.swift89
-rw-r--r--Map/MapRenderComponents/MapBlockers.swift49
-rw-r--r--Map/MapRenderComponents/MapColor.swift45
-rw-r--r--Map/MapRenderComponents/MapEdges.swift92
-rw-r--r--Map/MapRenderComponents/MapOpportunities.swift84
-rw-r--r--Map/MapRenderComponents/MapStages.swift60
-rw-r--r--Map/MapRenderComponents/MapVertices.swift49
7 files changed, 468 insertions, 0 deletions
diff --git a/Map/MapRenderComponents/MapAxes.swift b/Map/MapRenderComponents/MapAxes.swift
new file mode 100644
index 0000000..e7d65f4
--- /dev/null
+++ b/Map/MapRenderComponents/MapAxes.swift
@@ -0,0 +1,89 @@
+import SwiftUI
+
+struct MapAxes: View {
+
+ @Environment(\.colorScheme) var colorScheme
+
+ let mapSize: CGSize
+ let lineWidth: CGFloat
+ let evolution: Stage
+ let stages: [CGFloat]
+ let stageHeight = CGFloat(50.0)
+ let padding = CGFloat(5.0)
+
+ var color: Color {
+ MapColor.colorForScheme(colorScheme).foreground
+ }
+
+ var body: some View {
+ ZStack(alignment: .topLeading) {
+
+ // Axis Lines
+ Path { path in
+ path.move(to: CGPoint(x: 0, y: 0))
+ path.addLine(to: CGPoint(x: 0, y: mapSize.height))
+ path.addLine(to: CGPoint(x: mapSize.width, y: mapSize.height))
+ path.move(to: CGPoint(x: mapSize.width, y: mapSize.height))
+ path.closeSubpath()
+ }.stroke(color, lineWidth: lineWidth * 2)
+
+ // Y Labels
+ Text("Visible").font(.title3).foregroundColor(color).rotationEffect(Angle(degrees: -90.0))
+ .offset(CGSize(width: -35.0, height: 0.0))
+ Text("Value Chain").font(.title).foregroundColor(color).rotationEffect(Angle(degrees: -90.0))
+ .offset(CGSize(width: -72.0, height: mapSize.height / 2 - 20))
+ Text("Invisible").font(.title3).foregroundColor(color).rotationEffect(Angle(degrees: -90.0))
+ .offset(CGSize(width: -40.0, height: mapSize.height - 20))
+
+ // X Labels
+
+ Text("Uncharted")
+ .font(.title3)
+ .foregroundColor(color)
+ .frame(width: mapSize.width / 4, height: stageHeight, alignment: .topLeading)
+ .offset(CGSize(width: 0.0, height: -stageHeight / 2.0))
+ Text("Industrialised")
+ .font(.title3)
+ .foregroundColor(color)
+ .frame(width: mapSize.width / 4, height: stageHeight, alignment: .topLeading)
+ .offset(CGSize(width: mapSize.width - 100.0, height: -stageHeight / 2.0))
+
+ Text(evolution.i)
+ .font(.title3)
+ .foregroundColor(color)
+ .frame(width: w(stages[0]), height: stageHeight, alignment: .topLeading)
+ .offset(CGSize(width: 0.0, height: mapSize.height + padding))
+
+ Text(evolution.ii)
+ .font(.title3)
+ .foregroundColor(color)
+ .frame(width: w(stages[1]) - w(stages[0]), height: stageHeight, alignment: .topLeading)
+ .offset(CGSize(width: w(stages[0]), height: mapSize.height + padding))
+
+ Text(evolution.iii)
+ .font(.title3)
+ .foregroundColor(color)
+ .frame(width: w(stages[2]) - w(stages[1]), height: stageHeight, alignment: .topLeading)
+ .offset(CGSize(width: w(stages[1]), height: mapSize.height + padding))
+
+ Text(evolution.iv)
+ .font(.title3)
+ .foregroundColor(color)
+ .frame(width: mapSize.width - w(stages[2]), height: stageHeight, alignment: .topLeading)
+ .offset(CGSize(width: w(stages[2]), height: mapSize.height + padding))
+ }
+ }
+
+ func w(_ dimension: CGFloat) -> CGFloat {
+ max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
+ }
+}
+
+struct MapAxes_Previews: PreviewProvider {
+ static var previews: some View {
+ MapAxes(
+ mapSize: CGSize(width: 200.0, height: 200.0), lineWidth: CGFloat(1.0),
+ evolution: Stage.stages(.general), stages: [25.0, 50.0, 75.0]
+ ).padding(50.0)
+ }
+}
diff --git a/Map/MapRenderComponents/MapBlockers.swift b/Map/MapRenderComponents/MapBlockers.swift
new file mode 100644
index 0000000..b668b30
--- /dev/null
+++ b/Map/MapRenderComponents/MapBlockers.swift
@@ -0,0 +1,49 @@
+import SwiftUI
+
+struct MapBlockers: View {
+
+ @Environment(\.colorScheme) var colorScheme
+
+ let mapSize: CGSize
+ let vertexSize: CGSize
+ let blockers: [Blocker]
+
+ var color: Color {
+ MapColor.colorForScheme(colorScheme).blocker
+ }
+
+ let cornerSize = CGSize(width: 2.0, height: 2.0)
+
+ var body: some View {
+ ForEach(blockers, id: \.id) { vertex in
+ Path { path in
+ path.addRoundedRect(
+ in: CGRect(
+ origin: CGPoint(
+ x: w(vertex.position.x) + 3 * vertexSize.width,
+ y: h(vertex.position.y) - vertexSize.height * 2 / 3),
+ size: CGSize(width: vertexSize.width / 2, height: vertexSize.height * 2)
+ ), cornerSize: cornerSize)
+ }.fill(color)
+ }
+ }
+
+ func h(_ dimension: CGFloat) -> CGFloat {
+ max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
+ }
+
+ func w(_ dimension: CGFloat) -> CGFloat {
+ max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
+ }
+}
+
+struct MapBlockers_Previews: PreviewProvider {
+ static var previews: some View {
+ MapBlockers(
+ mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
+ blockers: [
+ Blocker(id: 0, position: CGPoint(x: 50.0, y: 50.0)),
+ Blocker(id: 1, position: CGPoint(x: 10.0, y: 20.0)),
+ ])
+ }
+}
diff --git a/Map/MapRenderComponents/MapColor.swift b/Map/MapRenderComponents/MapColor.swift
new file mode 100644
index 0000000..ce3d453
--- /dev/null
+++ b/Map/MapRenderComponents/MapColor.swift
@@ -0,0 +1,45 @@
+import SwiftUI
+
+struct MapColor {
+ let foreground: Color
+ let background: Color
+ let secondary: Color
+ let blocker: Color
+ let opportunity: Color
+ let stages: StageColor
+
+ static func colorForScheme(_ colorScheme: ColorScheme) -> MapColor {
+ if colorScheme == .dark {
+ return MapColor(
+ foreground: Color.white,
+ background: Color(.sRGB, red: 0.13, green: 0.13, blue: 0.13),
+ secondary: Color(.sRGB, red: 0.81, green: 0.78, blue: 0.79),
+ blocker: Color(.sRGB, red: 0.13, green: 0.13, blue: 0.13),
+ opportunity: Color(.sRGB, red: 1.0, green: 0.37, blue: 0.34),
+ stages: StageColor(
+ i: Color(.sRGB, red: 0.37, green: 0.16, blue: 0.25),
+ ii: Color(.sRGB, red: 0.30, green: 0.29, blue: 0.26),
+ iii: Color(.sRGB, red: 0.15, green: 0.29, blue: 0.23),
+ iv: Color(.sRGB, red: 0.14, green: 0.22, blue: 0.31)))
+ } else {
+ return MapColor(
+ foreground: Color(.sRGB, red: 0.13, green: 0.13, blue: 0.13),
+ background: Color.white,
+ secondary: Color.gray,
+ blocker: Color(.sRGB, red: 0.60, green: 0.52, blue: 0.51),
+ opportunity: Color(.sRGB, red: 1.0, green: 0.37, blue: 0.34),
+ stages: StageColor(
+ i: Color(.sRGB, red: 1.00, green: 0.93, blue: 0.97),
+ ii: Color(.sRGB, red: 1.00, green: 0.98, blue: 0.92),
+ iii: Color(.sRGB, red: 0.93, green: 1.00, blue: 0.97),
+ iv: Color(.sRGB, red: 0.93, green: 0.96, blue: 1.00)))
+ }
+ }
+}
+
+struct StageColor {
+ let i: Color
+ let ii: Color
+ let iii: Color
+ let iv: Color
+}
diff --git a/Map/MapRenderComponents/MapEdges.swift b/Map/MapRenderComponents/MapEdges.swift
new file mode 100644
index 0000000..d40d2aa
--- /dev/null
+++ b/Map/MapRenderComponents/MapEdges.swift
@@ -0,0 +1,92 @@
+//
+// MapEdges.swift
+// Map
+//
+// Created by Ruben Beltran del Rio on 2/2/21.
+//
+
+import SwiftUI
+
+struct MapEdges: View {
+
+ @Environment(\.colorScheme) var colorScheme
+
+ let mapSize: CGSize
+ let lineWidth: CGFloat
+ let vertexSize: CGSize
+ let edges: [MapEdge]
+
+ let arrowheadSize = CGFloat(10.0)
+
+ var color: Color {
+ MapColor.colorForScheme(colorScheme).foreground
+ }
+
+ var body: some View {
+ ForEach(edges, id: \.id) { edge in
+ Path { path in
+
+ // First we transform edges from percentage to map coordinates
+ let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
+ let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
+
+ let slope = (destination.y - origin.y) / (destination.x - 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: origin.x + multiplier * (vertexSize.width / 2.0) * cos(angle),
+ y: origin.y + multiplier * (vertexSize.height / 2.0) * sin(angle))
+ let offsetDestination = CGPoint(
+ x: destination.x - multiplier * (vertexSize.width / 2.0) * cos(angle),
+ y: destination.y - multiplier * (vertexSize.height / 2.0) * sin(angle))
+
+ path.move(to: offsetOrigin)
+ path.addLine(to: offsetDestination)
+
+ if edge.arrowhead {
+ path.move(to: offsetDestination)
+ path.addLine(
+ to: CGPoint(
+ x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
+ y:
+ offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
+
+ path.move(to: offsetDestination)
+ path.addLine(
+ to: CGPoint(
+ x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
+ y:
+ offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
+ }
+ path.move(to: offsetDestination)
+ path.closeSubpath()
+ }.applying(
+ CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
+ ).stroke(color, lineWidth: lineWidth)
+ }
+ }
+
+ func h(_ dimension: CGFloat) -> CGFloat {
+ max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
+ }
+
+ func w(_ dimension: CGFloat) -> CGFloat {
+ max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
+ }
+}
+
+struct MapEdges_Previews: PreviewProvider {
+ static var previews: some View {
+ MapEdges(
+ mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
+ vertexSize: CGSize(width: 25.0, height: 25.0),
+ edges: [
+ MapEdge(
+ id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2),
+ arrowhead: true)
+ ])
+ }
+}
diff --git a/Map/MapRenderComponents/MapOpportunities.swift b/Map/MapRenderComponents/MapOpportunities.swift
new file mode 100644
index 0000000..68d3fb5
--- /dev/null
+++ b/Map/MapRenderComponents/MapOpportunities.swift
@@ -0,0 +1,84 @@
+//
+// MapEdges.swift
+// Map
+//
+// Created by Ruben Beltran del Rio on 2/2/21.
+//
+
+import SwiftUI
+
+struct MapOpportunities: View {
+
+ @Environment(\.colorScheme) var colorScheme
+
+ let mapSize: CGSize
+ let lineWidth: CGFloat
+ let vertexSize: CGSize
+ let opportunities: [Opportunity]
+
+ let arrowheadSize = CGFloat(10.0)
+
+ var color: Color {
+ MapColor.colorForScheme(colorScheme).opportunity
+ }
+
+ var body: some View {
+ ForEach(opportunities, id: \.id) { edge in
+ Path { path in
+
+ // First we transform edges from percentage to map coordinates
+ let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
+ let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
+
+ let multiplier = CGFloat(edge.destination.x > edge.origin.x ? 1.0 : -1.0)
+ let upperAngle = -CGFloat.pi / 4.0
+ let lowerAngle = CGFloat.pi / 4.0
+
+ let offsetOrigin = CGPoint(x: origin.x + multiplier * (vertexSize.width / 2.0), y: origin.y)
+ let offsetDestination = CGPoint(
+ x: destination.x - multiplier * (vertexSize.width / 2.0), y: destination.y)
+
+ path.move(to: offsetOrigin)
+ path.addLine(to: offsetDestination)
+
+ path.move(to: offsetDestination)
+ path.addLine(
+ to: CGPoint(
+ x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
+ y:
+ offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
+
+ path.move(to: offsetDestination)
+ path.addLine(
+ to: CGPoint(
+ x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
+ y:
+ offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
+
+ path.move(to: offsetDestination)
+ path.closeSubpath()
+ }.applying(
+ CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
+ ).strokedPath(StrokeStyle(lineWidth: lineWidth * 2, dash: [10.0])).stroke(color)
+ }
+ }
+
+ func h(_ dimension: CGFloat) -> CGFloat {
+ max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
+ }
+
+ func w(_ dimension: CGFloat) -> CGFloat {
+ max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
+ }
+}
+
+struct MapOpportunities_Previews: PreviewProvider {
+ static var previews: some View {
+ MapOpportunities(
+ mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
+ vertexSize: CGSize(width: 25.0, height: 25.0),
+ opportunities: [
+ Opportunity(id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2))
+ ])
+ }
+}
diff --git a/Map/MapRenderComponents/MapStages.swift b/Map/MapRenderComponents/MapStages.swift
new file mode 100644
index 0000000..052a315
--- /dev/null
+++ b/Map/MapRenderComponents/MapStages.swift
@@ -0,0 +1,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])
+ }
+}
diff --git a/Map/MapRenderComponents/MapVertices.swift b/Map/MapRenderComponents/MapVertices.swift
new file mode 100644
index 0000000..71f85be
--- /dev/null
+++ b/Map/MapRenderComponents/MapVertices.swift
@@ -0,0 +1,49 @@
+import SwiftUI
+
+struct MapVertices: View {
+
+ @Environment(\.colorScheme) var colorScheme
+
+ let mapSize: CGSize
+ let vertexSize: CGSize
+ let vertices: [Vertex]
+ let padding = CGFloat(4.0)
+
+ var color: MapColor {
+ MapColor.colorForScheme(colorScheme)
+ }
+
+ var body: some View {
+ ForEach(vertices, id: \.id) { vertex in
+ Path { path in
+ path.addEllipse(
+ in: CGRect(
+ origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
+ ))
+ }.fill(color.foreground)
+ Text(vertex.label).foregroundColor(color.secondary).offset(
+ CGSize(
+ width: w(vertex.position.x) + vertexSize.width + padding,
+ height: h(vertex.position.y)))
+ }
+ }
+
+ func h(_ dimension: CGFloat) -> CGFloat {
+ max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
+ }
+
+ func w(_ dimension: CGFloat) -> CGFloat {
+ max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
+ }
+}
+
+struct MapVertices_Previews: PreviewProvider {
+ static var previews: some View {
+ MapVertices(
+ mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
+ vertices: [
+ Vertex(id: 0, label: "A", position: CGPoint(x: 50.0, y: 50.0)),
+ Vertex(id: 0, label: "A", position: CGPoint(x: 10.0, y: 20.0)),
+ ])
+ }
+}