]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapRender/MapOpportunities.swift
7fcadff8ffb46bfe26aa52b336a90ff6da08e24b
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapOpportunities.swift
1 import SwiftUI
2
3 struct MapOpportunities: View {
4
5 let mapSize: CGSize
6 let lineWidth: CGFloat
7 let vertexSize: CGSize
8 let opportunities: [Opportunity]
9
10 let arrowheadSize = CGFloat(10.0)
11
12 var body: some View {
13 ForEach(opportunities, id: \.id) { edge in
14 Path { path in
15
16 // First we transform edges from percentage to map coordinates
17 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
18 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
19
20 let multiplier = CGFloat(edge.destination.x > edge.origin.x ? 1.0 : -1.0)
21 let upperAngle = -CGFloat.pi / 4.0
22 let lowerAngle = CGFloat.pi / 4.0
23
24 let offsetOrigin = CGPoint(x: origin.x + multiplier * (vertexSize.width / 2.0), y: origin.y)
25 let offsetDestination = CGPoint(
26 x: destination.x - multiplier * (vertexSize.width / 2.0), y: destination.y)
27
28 path.move(to: offsetOrigin)
29 path.addLine(to: offsetDestination)
30
31 path.move(to: offsetDestination)
32 path.addLine(
33 to: CGPoint(
34 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
35 y:
36 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
37
38 path.move(to: offsetDestination)
39 path.addLine(
40 to: CGPoint(
41 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
42 y:
43 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
44
45 path.move(to: offsetDestination)
46 path.closeSubpath()
47 }.applying(
48 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
49 ).strokedPath(StrokeStyle(lineWidth: lineWidth / 4, dash: [10.0])).stroke(Color.map.opportunityColor)
50 }
51 }
52
53 func h(_ dimension: CGFloat) -> CGFloat {
54 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
55 }
56
57 func w(_ dimension: CGFloat) -> CGFloat {
58 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
59 }
60 }
61
62 struct MapOpportunities_Previews: PreviewProvider {
63 static var previews: some View {
64 MapOpportunities(
65 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
66 vertexSize: CGSize(width: 25.0, height: 25.0),
67 opportunities: [
68 Opportunity(id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2))
69 ])
70 }
71 }