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
|
import SwiftUI
struct MapEditor: View {
@Binding var document: MapDocument
var url: URL?
@State var selectedEvolution: StageType = .behavior
@AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
let zoomRange = Constants.kMinZoom...Constants.kMaxZoom
@AppStorage("zoom") var zoom = 1.0
@State var lastZoom = 1.0
var body: some View {
VStack(spacing: 0) {
adaptiveStack {
ZStack(alignment: .topLeading) {
MapTextEditor(document: $document)
.background(Color.ui.background)
.foregroundColor(Color.ui.foreground)
.frame(minHeight: 96.0)
}.padding(.top, 8.0).padding(.leading, 8.0).background(Color.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.mapSize.width + 2 * Dimensions.mapPadding) * zoom,
height: (Dimensions.mapSize.height + 2 * Dimensions.mapPadding) * zoom)
}.background(Color.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)
},
minimumValueLabel: {
Image(systemName: "minus.magnifyingglass")
.font(.theme.smallControl)
.help("Zoom Out (⌘-)")
},
maximumValueLabel: {
Image(systemName: "plus.magnifyingglass")
.font(.theme.smallControl)
.help("Zoom In (⌘+)")
}
).frame(width: 200).padding(.trailing, 10.0)
}.padding(4.0)
}.toolbar {
HStack {
Button(action: saveImage) {
Image(systemName: "photo")
}
.help("Export Image (⌘E)")
.padding(.vertical, 4.0).padding(.leading, 4.0).padding(.trailing, 8.0)
}
EvolutionPicker(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) {
print("Dragging: \(vertex), \(x), \(y)")
}
private func saveImage() {
if let image = document.exportAsImage(withEvolution: selectedEvolution) {
let filename = url?.deletingPathExtension().lastPathComponent ?? "Untitled"
let savePanel = NSSavePanel()
savePanel.allowedContentTypes = [.png]
savePanel.canCreateDirectories = true
savePanel.isExtensionHidden = false
savePanel.title = "Save \(filename) as image"
savePanel.message = "Choose a location to save the image"
savePanel.nameFieldStringValue = "\(filename).png"
savePanel.begin { result in
if result == .OK, let url = savePanel.url {
if let tiffRepresentation = image.tiffRepresentation {
let bitmapImage = NSBitmapImageRep(data: tiffRepresentation)
let pngData = bitmapImage?.representation(using: .png, properties: [:])
do {
try pngData?.write(to: url)
} catch {
return
}
}
}
}
}
}
}
#Preview {
MapEditor(document: .constant(MapDocument()), url: URL(filePath: "test.png")!)
}
|