1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
// Copyright (C) 2024 Rubén Beltrán del Río
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see https://map.tranquil.systems.
import SwiftUI
struct MapEditor: View {
@Binding var document: MapDocument
var url: URL?
@State var selectedEvolution: StageType = .behavior
@State var isSearching: Bool = false
private let changeDebouncer: Debouncer = Debouncer(seconds: 0.05)
@AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
let zoomRange = Constants.kMinZoom...Constants.kMaxZoom
@AppStorage("zoom") var zoom = 1.0
@State var lastZoom = 1.0
@State var searchTerm = ""
@State var selectedTerm = 0
@State var results: [Range<String.Index>] = []
private func updateRanges() {
if !isSearching || searchTerm.isEmpty {
results = []
}
let options: NSString.CompareOptions = [.caseInsensitive, .diacriticInsensitive]
var searchRange = document.text.startIndex..<document.text.endIndex
var ranges: [Range<String.Index>] = []
while let range = document.text.range(of: searchTerm, options: options, range: searchRange) {
ranges.append(range)
searchRange = range.upperBound..<document.text.endIndex
}
results = ranges
}
var body: some View {
VStack(spacing: 0) {
if isSearching {
SearchBar(
term: $searchTerm,
onNext: {
withAnimation {
if results.count > 0 {
selectedTerm = (selectedTerm + 1) % results.count
}
}
},
onPrevious: {
withAnimation {
if results.count > 0 {
if selectedTerm == 0 {
selectedTerm = results.count - 1
} else {
selectedTerm = (selectedTerm - 1) % results.count
}
}
}
},
onSubmit: {
},
onDismiss: {
isSearching = false
}
)
.onChange(
of: searchTerm,
{
changeDebouncer.debounce {
updateRanges()
selectedTerm = 0
}
})
Divider()
}
adaptiveStack {
ZStack(alignment: .topLeading) {
MapTextEditor(
text: Binding(
get: { document.text },
set: { document.text = $0 }
),
highlightRanges: results,
selectedRange: selectedTerm
)
.background(Color.Theme.UI.background)
.foregroundColor(Color.Theme.UI.foreground)
.frame(minHeight: 96.0)
}
.background(Color.Theme.UI.background)
.cornerRadius(5.0)
GeometryReader { geometry in
ScrollView([.horizontal, .vertical]) {
MapRenderView(
document: $document, evolution: $selectedEvolution, onDragVertex: onDragVertex
).scaleEffect(zoom, anchor: .center).frame(
width: (Dimensions.Map.size.width + 2 * Dimensions.Map.padding) * zoom,
height: (Dimensions.Map.size.height + 2 * Dimensions.Map.padding) * zoom)
}.background(Color.Theme.UI.background)
.gesture(
MagnificationGesture()
.onChanged { value in
let delta = value / lastZoom
lastZoom = value
zoom = min(max(zoom * delta, zoomRange.lowerBound), zoomRange.upperBound)
}
.onEnded { _ in
lastZoom = 1.0
}
)
}
}
Divider()
HStack {
Spacer()
Slider(
value: $zoom, in: zoomRange, step: 0.1,
label: {
Text(formatZoom(zoom))
.font(.Theme.SmallControl.regular)
},
minimumValueLabel: {
Image(systemName: "minus.magnifyingglass")
.font(.Theme.SmallControl.regular)
.help("Zoom Out (⌘-)")
},
maximumValueLabel: {
Image(systemName: "plus.magnifyingglass")
.font(.Theme.SmallControl.regular)
.help("Zoom In (⌘+)")
}
).frame(width: 200).padding(.trailing, 10.0)
}.padding(4.0)
}.toolbar {
ToolbarItem(placement: .primaryAction) {
EvolutionPicker(selectedEvolution: $selectedEvolution)
}
}
.focusedSceneValue(\.isSearching, $isSearching)
.focusedSceneValue(\.selectedEvolution, $selectedEvolution)
}
@ViewBuilder
func adaptiveStack<Content: View>(@ViewBuilder content: () -> Content) -> some View {
if viewStyle == .horizontal {
VSplitView {
content()
}
} else {
HSplitView {
content()
}
}
}
private func formatZoom(_ number: CGFloat) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 1
formatter.minimumFractionDigits = 1
return (formatter.string(from: NSNumber(value: number)) ?? "") + "x"
}
private func onDragVertex(vertex: Vertex, x: CGFloat, y: CGFloat) {
}
}
#Preview {
MapEditor(document: .constant(MapDocument(text: nil)), url: URL(filePath: "test.wmap")!)
}
|