2 Copyright (C) 2024 Rubén Beltrán del Río
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.
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.
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.
19 struct MapEditor: View {
20 @Binding var document: MapDocument
22 @State var selectedEvolution: StageType = .behavior
23 @State var isSearching: Bool = false
25 @AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
27 let zoomRange = Constants.kMinZoom...Constants.kMaxZoom
28 @AppStorage("zoom") var zoom = 1.0
29 @State var lastZoom = 1.0
30 @State var searchTerm = ""
31 @State var selectedTerm = 0
33 var results: [Range<String.Index>] {
34 if !isSearching || searchTerm.isEmpty {
37 let options: NSString.CompareOptions = [.caseInsensitive, .diacriticInsensitive]
38 var searchRange = document.text.startIndex..<document.text.endIndex
39 var ranges: [Range<String.Index>] = []
41 while let range = document.text.range(of: searchTerm, options: options, range: searchRange) {
43 searchRange = range.upperBound..<document.text.endIndex
57 if results.count > 0 {
58 selectedTerm = (selectedTerm + 1) % results.count
64 if results.count > 0 {
65 if selectedTerm == 0 {
66 selectedTerm = results.count - 1
68 selectedTerm = (selectedTerm - 1) % results.count
90 ZStack(alignment: .topLeading) {
91 MapTextEditor(document: $document, highlightRanges: results, selectedRange: selectedTerm)
92 .background(Color.ui.background)
93 .foregroundColor(Color.ui.foreground)
94 .frame(minHeight: 96.0)
95 }.padding(.top, 8.0).padding(.leading, 8.0).background(Color.ui.background).cornerRadius(
97 GeometryReader { geometry in
98 ScrollView([.horizontal, .vertical]) {
100 document: $document, evolution: $selectedEvolution, onDragVertex: onDragVertex
101 ).scaleEffect(zoom, anchor: .center).frame(
102 width: (Dimensions.mapSize.width + 2 * Dimensions.mapPadding) * zoom,
103 height: (Dimensions.mapSize.height + 2 * Dimensions.mapPadding) * zoom)
104 }.background(Color.ui.background)
106 MagnificationGesture()
107 .onChanged { value in
108 let delta = value / lastZoom
110 zoom = min(max(zoom * delta, zoomRange.lowerBound), zoomRange.upperBound)
122 value: $zoom, in: zoomRange, step: 0.1,
124 Text(formatZoom(zoom))
125 .font(.theme.smallControl)
128 Image(systemName: "minus.magnifyingglass")
129 .font(.theme.smallControl)
130 .help("Zoom Out (⌘-)")
133 Image(systemName: "plus.magnifyingglass")
134 .font(.theme.smallControl)
135 .help("Zoom In (⌘+)")
137 ).frame(width: 200).padding(.trailing, 10.0)
141 EvolutionPicker(selectedEvolution: $selectedEvolution)
143 }.focusedSceneValue(\.isSearching, $isSearching)
144 .focusedSceneValue(\.selectedEvolution, $selectedEvolution)
148 func adaptiveStack<Content: View>(@ViewBuilder content: () -> Content) -> some View {
149 if viewStyle == .horizontal {
160 private func formatZoom(_ number: CGFloat) -> String {
161 let formatter = NumberFormatter()
162 formatter.numberStyle = .decimal
163 formatter.maximumFractionDigits = 1
164 formatter.minimumFractionDigits = 1
165 return (formatter.string(from: NSNumber(value: number)) ?? "") + "x"
168 private func onDragVertex(vertex: Vertex, x: CGFloat, y: CGFloat) {
173 MapEditor(document: .constant(MapDocument()), url: URL(filePath: "test.png")!)