aboutsummaryrefslogtreecommitdiff
path: root/Map/Presentation/Base Components/MapRender
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2023-05-07 15:22:54 +0200
committerRuben Beltran del Rio <ruben@unlimited.pizza>2023-05-07 15:22:54 +0200
commitfdb4633d3e9158e457d57e820df6e1efb4df39c2 (patch)
tree176cad99cb9befc0a65a7af02561019e811814de /Map/Presentation/Base Components/MapRender
parent75a0e4509a70055851b085f3f7293ae1cf48164c (diff)
Update to support notes + new style2.0.0
Diffstat (limited to 'Map/Presentation/Base Components/MapRender')
-rw-r--r--Map/Presentation/Base Components/MapRender/MapAxes.swift81
-rw-r--r--Map/Presentation/Base Components/MapRender/MapBlockers.swift43
-rw-r--r--Map/Presentation/Base Components/MapRender/MapEdges.swift79
-rw-r--r--Map/Presentation/Base Components/MapRender/MapNotes.swift46
-rw-r--r--Map/Presentation/Base Components/MapRender/MapOpportunities.swift71
-rw-r--r--Map/Presentation/Base Components/MapRender/MapStages.swift52
-rw-r--r--Map/Presentation/Base Components/MapRender/MapVertices.swift92
7 files changed, 464 insertions, 0 deletions
diff --git a/Map/Presentation/Base Components/MapRender/MapAxes.swift b/Map/Presentation/Base Components/MapRender/MapAxes.swift
new file mode 100644
index 0000000..6ba758f
--- /dev/null
+++ b/Map/Presentation/Base Components/MapRender/MapAxes.swift
@@ -0,0 +1,81 @@
+import SwiftUI
+
+struct MapAxes: View {
+
+ let mapSize: CGSize
+ let lineWidth: CGFloat
+ let evolution: Stage
+ let stages: [CGFloat]
+ let stageHeight = CGFloat(100.0)
+ let padding = CGFloat(5.0)
+
+ 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.map.axisColor, lineWidth: lineWidth * 2)
+
+ // Y Labels
+ Text("Visible").font(.theme.axisLabel).foregroundColor(.map.labelColor).rotationEffect(Angle(degrees: -90.0))
+ .offset(CGSize(width: -35.0, height: 0.0))
+ Text("Invisible").font(.theme.axisLabel).foregroundColor(.map.labelColor).rotationEffect(Angle(degrees: -90.0))
+ .offset(CGSize(width: -40.0, height: mapSize.height - 20))
+
+ // X Labels
+
+ Text("Uncharted")
+ .font(.theme.axisLabel)
+ .foregroundColor(.map.labelColor)
+ .frame(width: mapSize.width / 4, height: stageHeight / 2.0, alignment: .topLeading)
+ .offset(CGSize(width: 0.0, height: -stageHeight / 4.0))
+ Text("Industrialised")
+ .font(.theme.axisLabel)
+ .foregroundColor(.map.labelColor)
+ .frame(width: mapSize.width / 4, height: stageHeight / 2.0, alignment: .topLeading)
+ .offset(CGSize(width: mapSize.width - 100.0, height: -stageHeight / 4.0))
+
+ Text(evolution.i)
+ .font(.theme.axisLabel)
+ .foregroundColor(.map.labelColor)
+ .frame(width: w(stages[0]), height: stageHeight, alignment: .topLeading)
+ .offset(CGSize(width: 0.0, height: mapSize.height + padding))
+
+ Text(evolution.ii)
+ .font(.theme.axisLabel)
+ .foregroundColor(.map.labelColor)
+ .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(.theme.axisLabel)
+ .foregroundColor(.map.labelColor)
+ .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(.theme.axisLabel)
+ .foregroundColor(.map.labelColor)
+ .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/Presentation/Base Components/MapRender/MapBlockers.swift b/Map/Presentation/Base Components/MapRender/MapBlockers.swift
new file mode 100644
index 0000000..d943ba8
--- /dev/null
+++ b/Map/Presentation/Base Components/MapRender/MapBlockers.swift
@@ -0,0 +1,43 @@
+import SwiftUI
+
+struct MapBlockers: View {
+
+ let mapSize: CGSize
+ let vertexSize: CGSize
+ let blockers: [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.map.blockerColor)
+ }
+ }
+
+ 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/Presentation/Base Components/MapRender/MapEdges.swift b/Map/Presentation/Base Components/MapRender/MapEdges.swift
new file mode 100644
index 0000000..3814495
--- /dev/null
+++ b/Map/Presentation/Base Components/MapRender/MapEdges.swift
@@ -0,0 +1,79 @@
+import SwiftUI
+
+struct MapEdges: View {
+
+ let mapSize: CGSize
+ let lineWidth: CGFloat
+ let vertexSize: CGSize
+ let edges: [MapEdge]
+
+ let arrowheadSize = CGFloat(10.0)
+
+ 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.map.vertexColor, 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/Presentation/Base Components/MapRender/MapNotes.swift b/Map/Presentation/Base Components/MapRender/MapNotes.swift
new file mode 100644
index 0000000..f35b3fe
--- /dev/null
+++ b/Map/Presentation/Base Components/MapRender/MapNotes.swift
@@ -0,0 +1,46 @@
+import SwiftUI
+
+struct MapNotes: View {
+
+ let mapSize: CGSize
+ let lineWidth: CGFloat
+ let notes: [Note]
+
+ let maxWidth = 400.0
+
+
+ var body: some View {
+ ForEach(notes, id: \.id) { note in
+ Text(note.text.replacingOccurrences(of: "\\n", with: "\n")).font(.theme.axisLabel)
+ .padding(2.0)
+ .background(.white)
+ .foregroundColor(.map.labelColor)
+ .border(Color.map.vertexColor, width: lineWidth)
+ .frame(minWidth: 16.0, maxWidth: maxWidth, alignment: .topLeading)
+ .offset(
+ CGSize(
+ width: w(note.position.x),
+ height: h(note.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 MapNotes_Previews: PreviewProvider {
+ static var previews: some View {
+ MapNotes(
+ mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
+ notes: [
+ Note(id: 0, position: CGPoint(x: 50.0, y: 50.0), text: "Notes can have a lot more text, so we need to make sure that they're resized correctly"),
+ ])
+ }
+}
diff --git a/Map/Presentation/Base Components/MapRender/MapOpportunities.swift b/Map/Presentation/Base Components/MapRender/MapOpportunities.swift
new file mode 100644
index 0000000..7fcadff
--- /dev/null
+++ b/Map/Presentation/Base Components/MapRender/MapOpportunities.swift
@@ -0,0 +1,71 @@
+import SwiftUI
+
+struct MapOpportunities: View {
+
+ let mapSize: CGSize
+ let lineWidth: CGFloat
+ let vertexSize: CGSize
+ let opportunities: [Opportunity]
+
+ let arrowheadSize = CGFloat(10.0)
+
+ 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 / 4, dash: [10.0])).stroke(Color.map.opportunityColor)
+ }
+ }
+
+ 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/Presentation/Base Components/MapRender/MapStages.swift b/Map/Presentation/Base Components/MapRender/MapStages.swift
new file mode 100644
index 0000000..0fc8f48
--- /dev/null
+++ b/Map/Presentation/Base Components/MapRender/MapStages.swift
@@ -0,0 +1,52 @@
+import SwiftUI
+import Patterns
+
+struct MapStages: View {
+
+ let mapSize: CGSize
+ let lineWidth: CGFloat
+ let stages: [CGFloat]
+ let opacity = 0.1
+
+ var body: some View {
+ ZStack(alignment: .topLeading) {
+ PatternView(design: .constant(.stitch), pixelSize: 1.0, foregroundColor: .map.stageForeground, backgroundColor: .map.stageBackground)
+ .frame(width: w(stages[0]), height: mapSize.height)
+ PatternView(design: .constant(.shingles), pixelSize: 1.0, foregroundColor: .map.stageForeground, backgroundColor: .map.stageBackground)
+ .offset(CGSize(width: w(stages[0]), height: 0))
+ .frame(width: w(stages[1]) - w(stages[0]), height: mapSize.height)
+ PatternView(design: .constant(.shadowGrid), pixelSize: 1.0, foregroundColor: .map.stageForeground, backgroundColor: .map.stageBackground)
+ .offset(CGSize(width: w(stages[1]), height: 0))
+ .frame(width: w(stages[2]) - w(stages[1]), height: mapSize.height)
+ PatternView(design: .constant(.wicker), pixelSize: 1.0, foregroundColor: .map.stageForeground, backgroundColor: .map.stageBackground)
+ .offset(CGSize(width: w(stages[2]), height: 0))
+ .frame(width: mapSize.width - w(stages[2]), height: mapSize.height)
+
+ 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.closeSubpath()
+ path.move(to: CGPoint(x: w(stages[1]), y: 0))
+ path.addLine(to: CGPoint(x: w(stages[1]), y: mapSize.height))
+ path.closeSubpath()
+ path.move(to: CGPoint(x: w(stages[2]), y: 0))
+ path.addLine(to: CGPoint(x: w(stages[2]), y: mapSize.height))
+ path.closeSubpath()
+ path.move(to: CGPoint(x: w(stages[0]), y: 0))
+ path.closeSubpath()
+ }.strokedPath(StrokeStyle(lineWidth: lineWidth / 4, dash: [10.0, 18.0])).stroke(Color.map.axisColor)
+ }
+ }
+
+ 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(0.5),
+ stages: [25.0, 50.0, 75.0])
+ }
+}
diff --git a/Map/Presentation/Base Components/MapRender/MapVertices.swift b/Map/Presentation/Base Components/MapRender/MapVertices.swift
new file mode 100644
index 0000000..74cac6d
--- /dev/null
+++ b/Map/Presentation/Base Components/MapRender/MapVertices.swift
@@ -0,0 +1,92 @@
+import SwiftUI
+
+struct MapVertices: View {
+
+ let mapSize: CGSize
+ let vertexSize: CGSize
+ let vertices: [Vertex]
+ let padding = CGFloat(5.0)
+
+ var body: some View {
+ ZStack(alignment: .topLeading) {
+ ForEach(vertices, id: \.id) { vertex in
+ getVertexShape(vertex).fill(Color.map.vertexColor)
+ Text(vertex.label.replacingOccurrences(of: "\\n", with: "\n")).font(.theme.vertexLabel)
+ .foregroundColor(.map.labelColor)
+ .shadow(color: .white, radius: 0, x: -0.5, y: -0.5)
+ .shadow(color: .white, radius: 0, x: 0.5, y: 0.5)
+ .offset(
+ CGSize(
+ width: w(vertex.position.x) + vertexSize.width + padding,
+ height: h(vertex.position.y) + 7.0))
+ }
+ }
+ }
+
+ 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))
+ }
+
+ func getVertexShape(_ vertex: Vertex) -> Path {
+ switch vertex.shape {
+ case .circle:
+ return Path { path in
+ path.addEllipse(
+ in: CGRect(
+ origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
+ ))
+ }
+ case .square:
+ return Path { path in
+ path.addRect(
+ CGRect(
+ x: w(vertex.position.x), y: h(vertex.position.y), width: vertexSize.width,
+ height: vertexSize.height
+ ))
+ }
+ case .triangle:
+ return Path { path in
+ path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
+ path.addLine(
+ to: CGPoint(
+ x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
+ )
+ path.addLine(
+ to: CGPoint(x: w(vertex.position.x) + vertexSize.width / 2.0, y: h(vertex.position.y)))
+ path.addLine(
+ to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
+ path.closeSubpath()
+ }
+ case .x:
+ return Path { path in
+ path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)))
+ path.addLine(
+ to: CGPoint(
+ x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
+ )
+ path.closeSubpath()
+ path.move(to: CGPoint(x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y)))
+ path.addLine(
+ to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
+ path.closeSubpath()
+ }.strokedPath(StrokeStyle(lineWidth: 2.0, lineCap: .butt))
+ }
+ }
+}
+
+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 Circle", position: CGPoint(x: 50.0, y: 50.0)),
+ Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
+ Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
+ Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),
+ ])
+ }
+}