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
24 @AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
26 let zoomRange = Constants.kMinZoom...Constants.kMaxZoom
27 @AppStorage("zoom") var zoom = 1.0
28 @State var lastZoom = 1.0
33 ZStack(alignment: .topLeading) {
34 MapTextEditor(document: $document)
35 .background(Color.ui.background)
36 .foregroundColor(Color.ui.foreground)
37 .frame(minHeight: 96.0)
38 }.padding(.top, 8.0).padding(.leading, 8.0).background(Color.ui.background).cornerRadius(
40 GeometryReader { geometry in
41 ScrollView([.horizontal, .vertical]) {
43 document: $document, evolution: $selectedEvolution, onDragVertex: onDragVertex
44 ).scaleEffect(zoom, anchor: .center).frame(
45 width: (Dimensions.mapSize.width + 2 * Dimensions.mapPadding) * zoom,
46 height: (Dimensions.mapSize.height + 2 * Dimensions.mapPadding) * zoom)
47 }.background(Color.ui.background)
49 MagnificationGesture()
51 let delta = value / lastZoom
53 zoom = min(max(zoom * delta, zoomRange.lowerBound), zoomRange.upperBound)
65 value: $zoom, in: zoomRange, step: 0.1,
67 Text(formatZoom(zoom))
68 .font(.theme.smallControl)
71 Image(systemName: "minus.magnifyingglass")
72 .font(.theme.smallControl)
73 .help("Zoom Out (⌘-)")
76 Image(systemName: "plus.magnifyingglass")
77 .font(.theme.smallControl)
80 ).frame(width: 200).padding(.trailing, 10.0)
84 Button(action: saveImage) {
85 Image(systemName: "photo")
87 .help("Export Image (⌘E)")
88 .padding(.vertical, 4.0).padding(.leading, 4.0).padding(.trailing, 8.0)
90 EvolutionPicker(selectedEvolution: $selectedEvolution)
95 func adaptiveStack<Content: View>(@ViewBuilder content: () -> Content) -> some View {
96 if viewStyle == .horizontal {
107 private func formatZoom(_ number: CGFloat) -> String {
108 let formatter = NumberFormatter()
109 formatter.numberStyle = .decimal
110 formatter.maximumFractionDigits = 1
111 formatter.minimumFractionDigits = 1
112 return (formatter.string(from: NSNumber(value: number)) ?? "") + "x"
115 private func onDragVertex(vertex: Vertex, x: CGFloat, y: CGFloat) {
118 private func saveImage() {
119 if let image = document.exportAsImage(withEvolution: selectedEvolution) {
121 let filename = url?.deletingPathExtension().lastPathComponent ?? "Untitled"
123 let savePanel = NSSavePanel()
124 savePanel.allowedContentTypes = [.png]
125 savePanel.canCreateDirectories = true
126 savePanel.isExtensionHidden = false
127 savePanel.title = "Save \(filename) as image"
128 savePanel.message = "Choose a location to save the image"
129 savePanel.nameFieldStringValue = "\(filename).png"
130 savePanel.begin { result in
131 if result == .OK, let url = savePanel.url {
132 if let tiffRepresentation = image.tiffRepresentation {
133 let bitmapImage = NSBitmapImageRep(data: tiffRepresentation)
134 let pngData = bitmapImage?.representation(using: .png, properties: [:])
136 try pngData?.write(to: url)
148 MapEditor(document: .constant(MapDocument()), url: URL(filePath: "test.png")!)