]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapRender/MapNotes.swift
d2d7e1f777bbdc88249a12940d2cb79d405b4739
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapNotes.swift
1 /*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
16 */
17 import SwiftUI
18
19 struct MapNotes: View {
20
21 let mapSize: CGSize
22 let lineWidth: CGFloat
23 let notes: [Note]
24
25 let maxWidth = 400.0
26
27 var body: some View {
28 ForEach(notes, id: \.id) { note in
29 Text(note.text.replacingOccurrences(of: "\\n", with: "\n")).font(.theme.note)
30 .padding(2.0)
31 .background(.white)
32 .foregroundColor(.map.labelColor)
33 .border(Color.map.vertexColor, width: lineWidth)
34 .frame(minWidth: 16.0, maxWidth: maxWidth, alignment: .topLeading)
35 .offset(
36 CGSize(
37 width: w(note.position.x),
38 height: h(note.position.y)
39 )
40 )
41 }
42 }
43
44 func h(_ dimension: CGFloat) -> CGFloat {
45 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
46 }
47
48 func w(_ dimension: CGFloat) -> CGFloat {
49 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
50 }
51 }
52
53 #Preview {
54 MapNotes(
55 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
56 notes: [
57 Note(
58 id: 0, position: CGPoint(x: 50.0, y: 50.0),
59 text:
60 "Notes can have a lot more text, so we need to make sure that they're resized correctly")
61 ])
62 }