]> git.r.bdr.sh - rbdr/map/blame - Map/MapRenderComponents/MapVertices.swift
Make delete a context action
[rbdr/map] / Map / MapRenderComponents / MapVertices.swift
CommitLineData
5e8ff485
RBR
1import SwiftUI
2
3struct MapVertices: View {
4
5 @Environment(\.colorScheme) var colorScheme
6
7 let mapSize: CGSize
8 let vertexSize: CGSize
9 let vertices: [Vertex]
10 let padding = CGFloat(4.0)
11
12 var color: MapColor {
13 MapColor.colorForScheme(colorScheme)
14 }
15
16 var body: some View {
17 ForEach(vertices, id: \.id) { vertex in
77d0155b 18 getVertexShape(vertex).fill(color.foreground)
5e8ff485
RBR
19 Text(vertex.label).foregroundColor(color.secondary).offset(
20 CGSize(
21 width: w(vertex.position.x) + vertexSize.width + padding,
22 height: h(vertex.position.y)))
23 }
24 }
25
26 func h(_ dimension: CGFloat) -> CGFloat {
27 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
28 }
29
30 func w(_ dimension: CGFloat) -> CGFloat {
31 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
32 }
77d0155b
RBR
33
34 func getVertexShape(_ vertex: Vertex) -> Path {
35 switch vertex.shape {
36 case .circle:
37 return Path { path in
38 path.addEllipse(
39 in: CGRect(
40 origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
41 ))
42 }
43 case .square:
44 return Path { path in
45 path.addRect(
46 CGRect(
47 x: w(vertex.position.x), y: h(vertex.position.y), width: vertexSize.width,
48 height: vertexSize.height
49 ))
50 }
51 case .triangle:
52 return Path { path in
53 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
54 path.addLine(
55 to: CGPoint(
56 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
57 )
58 path.addLine(
59 to: CGPoint(x: w(vertex.position.x) + vertexSize.width / 2.0, y: h(vertex.position.y)))
60 path.addLine(
61 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
62 path.closeSubpath()
63 }
64 case .x:
65 return Path { path in
66 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)))
67 path.addLine(
68 to: CGPoint(
69 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
70 )
71 path.closeSubpath()
72 path.move(to: CGPoint(x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y)))
73 path.addLine(
74 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
75 path.closeSubpath()
76 }.strokedPath(StrokeStyle(lineWidth: 5.0, lineCap: .butt))
77 }
78 }
5e8ff485
RBR
79}
80
81struct MapVertices_Previews: PreviewProvider {
82 static var previews: some View {
83 MapVertices(
84 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
85 vertices: [
86 Vertex(id: 0, label: "A", position: CGPoint(x: 50.0, y: 50.0)),
87 Vertex(id: 0, label: "A", position: CGPoint(x: 10.0, y: 20.0)),
88 ])
89 }
90}