]> git.r.bdr.sh - rbdr/map/blame_incremental - Map/MapRenderComponents/MapVertices.swift
Add code for first release
[rbdr/map] / Map / MapRenderComponents / MapVertices.swift
... / ...
CommitLineData
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
18 Path { path in
19 path.addEllipse(
20 in: CGRect(
21 origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
22 ))
23 }.fill(color.foreground)
24 Text(vertex.label).foregroundColor(color.secondary).offset(
25 CGSize(
26 width: w(vertex.position.x) + vertexSize.width + padding,
27 height: h(vertex.position.y)))
28 }
29 }
30
31 func h(_ dimension: CGFloat) -> CGFloat {
32 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
33 }
34
35 func w(_ dimension: CGFloat) -> CGFloat {
36 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
37 }
38}
39
40struct MapVertices_Previews: PreviewProvider {
41 static var previews: some View {
42 MapVertices(
43 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
44 vertices: [
45 Vertex(id: 0, label: "A", position: CGPoint(x: 50.0, y: 50.0)),
46 Vertex(id: 0, label: "A", position: CGPoint(x: 10.0, y: 20.0)),
47 ])
48 }
49}