]> git.r.bdr.sh - rbdr/map/blame - Map/MapRenderComponents/MapOpportunities.swift
Fix performance and undo
[rbdr/map] / Map / MapRenderComponents / MapOpportunities.swift
CommitLineData
5e8ff485
RBR
1//
2// MapEdges.swift
3// Map
4//
5// Created by Ruben Beltran del Rio on 2/2/21.
6//
7
8import SwiftUI
9
10struct MapOpportunities: View {
11
12 @Environment(\.colorScheme) var colorScheme
13
14 let mapSize: CGSize
15 let lineWidth: CGFloat
16 let vertexSize: CGSize
17 let opportunities: [Opportunity]
18
19 let arrowheadSize = CGFloat(10.0)
20
21 var color: Color {
22 MapColor.colorForScheme(colorScheme).opportunity
23 }
24
25 var body: some View {
26 ForEach(opportunities, id: \.id) { edge in
27 Path { path in
28
29 // First we transform edges from percentage to map coordinates
30 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
31 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
32
33 let multiplier = CGFloat(edge.destination.x > edge.origin.x ? 1.0 : -1.0)
34 let upperAngle = -CGFloat.pi / 4.0
35 let lowerAngle = CGFloat.pi / 4.0
36
37 let offsetOrigin = CGPoint(x: origin.x + multiplier * (vertexSize.width / 2.0), y: origin.y)
38 let offsetDestination = CGPoint(
39 x: destination.x - multiplier * (vertexSize.width / 2.0), y: destination.y)
40
41 path.move(to: offsetOrigin)
42 path.addLine(to: offsetDestination)
43
44 path.move(to: offsetDestination)
45 path.addLine(
46 to: CGPoint(
47 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
48 y:
49 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
50
51 path.move(to: offsetDestination)
52 path.addLine(
53 to: CGPoint(
54 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
55 y:
56 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
57
58 path.move(to: offsetDestination)
59 path.closeSubpath()
60 }.applying(
61 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
62 ).strokedPath(StrokeStyle(lineWidth: lineWidth * 2, dash: [10.0])).stroke(color)
63 }
64 }
65
66 func h(_ dimension: CGFloat) -> CGFloat {
67 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
68 }
69
70 func w(_ dimension: CGFloat) -> CGFloat {
71 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
72 }
73}
74
75struct MapOpportunities_Previews: PreviewProvider {
76 static var previews: some View {
77 MapOpportunities(
78 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
79 vertexSize: CGSize(width: 25.0, height: 25.0),
80 opportunities: [
81 Opportunity(id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2))
82 ])
83 }
84}