import SwiftUI private let amber = Color(red: 0xE0 / 255, green: 0xA3 / 255, blue: 0x40 / 255) struct ContentView: View { @StateObject private var cam = Camera() @AppStorage("titolo") private var titolo = "NOME GIOCO" @AppStorage("griglia") private var griglia = true @State private var focusPoint: CGPoint? @State private var focusLocked = false @State private var showExposure = false @State private var hideWork: DispatchWorkItem? @State private var pinchStart: CGFloat = 1 @State private var editingTitle = false @State private var flashAnim = false var body: some View { GeometryReader { geo in let W = geo.size.width let s = W / Cover.canvasW VStack(spacing: 0) { topBar .frame(height: 52) Spacer(minLength: 0) frame(W: W, s: s) Spacer(minLength: 0) Text(cam.lensName) .font(.system(size: 11)) .foregroundStyle(.white.opacity(0.55)) .padding(.top, 8) zoomBar .padding(.top, 6) bottomBar .padding(.vertical, 18) } .frame(width: W, height: geo.size.height) .background(Color.black) .overlay(alignment: .top) { toast } } .background(Color.black.ignoresSafeArea()) .statusBarHidden() .onAppear { cam.start() } .alert("Nome del gioco", isPresented: $editingTitle) { TextField("NOME GIOCO", text: $titolo) .textInputAutocapitalization(.characters) Button("Fatto") { titolo = titolo.uppercased() } Button("Ripristina", role: .destructive) { titolo = "NOME GIOCO" } } message: { Text("Solo per l'anteprima: nella foto salvata non c'è.") } } // MARK: riquadro della copertina @ViewBuilder private func frame(W: CGFloat, s: CGFloat) -> some View { let photoH = Cover.photoH * s let bandH = (Cover.canvasH - Cover.photoH) * s VStack(spacing: 0) { ZStack(alignment: .topLeading) { CameraPreview(camera: cam, onTap: { p, dp in tapFocus(p, dp, lock: false) }, onLongPress: { p, dp in tapFocus(p, dp, lock: true) }, onPinch: { scale, st in if st == .began { pinchStart = cam.currentDisplayZoom } cam.setDisplayZoomContinuous(pinchStart * scale) }) if griglia { GridLines().allowsHitTesting(false) } // sfumatura sul 40% inferiore, come in slide.py VStack(spacing: 0) { Spacer(minLength: 0) LinearGradient(stops: [ .init(color: .black.opacity(0), location: 0), .init(color: .black.opacity(0.30), location: 0.38), .init(color: .black.opacity(0.74), location: 0.72), .init(color: .black.opacity(0.93), location: 1), ], startPoint: .top, endPoint: .bottom) .frame(height: photoH * 0.4) } .allowsHitTesting(false) // titolo segnaposto: Bebas 136 a 64 dal bordo e 26 dal fondo della foto VStack(alignment: .leading, spacing: 0) { Spacer(minLength: 0) Text(titolo) .font(.custom("BebasNeue-Regular", size: (titolo.count > 16 ? 120 : 136) * s)) .foregroundStyle(.white) .lineLimit(2) .minimumScaleFactor(0.5) .padding(.leading, 64 * s) .padding(.trailing, 64 * s) .padding(.bottom, 26 * s - 6 * s) } .frame(maxWidth: .infinity, alignment: .leading) .allowsHitTesting(false) if let fp = focusPoint { FocusSquare(locked: focusLocked) .position(fp) .allowsHitTesting(false) } if showExposure { ExposureSlider(value: cam.exposureBias) { v in cam.setBias(v); scheduleHide() } .frame(width: 44, height: photoH * 0.55) .position(x: W - 30, y: photoH / 2) } if cam.aeafLocked { Text("BLOCCO AE/AF") .font(.system(size: 12, weight: .semibold)) .padding(.horizontal, 8).padding(.vertical, 4) .background(amber, in: RoundedRectangle(cornerRadius: 4)) .foregroundStyle(.black) .padding(10) } if flashAnim { Color.black.allowsHitTesting(false) } } .frame(width: W, height: photoH) .clipped() band(s: s) .frame(width: W, height: bandH) } .overlay(Rectangle().stroke(Color.white.opacity(0.18), lineWidth: 1)) } /// Banda nera con i crediti e le tre colonne finte, per avere l'idea degli spazi. private func band(s: CGFloat) -> some View { VStack(spacing: 0) { Spacer(minLength: 0) Text("Autore · 2025 · Editore italiano") .font(.system(size: 30 * s)) .foregroundStyle(Color(white: 0.55)) .frame(maxWidth: .infinity, alignment: .leading) Spacer(minLength: 0) Rectangle().fill(Color(white: 0.22)).frame(height: max(1, 2 * s)) HStack(spacing: 0) { col("GIOCATORI", "2–4", s) Rectangle().fill(Color(white: 0.22)).frame(width: max(1, 2 * s), height: 70 * s) col("DURATA", "60′", s) Rectangle().fill(Color(white: 0.22)).frame(width: max(1, 2 * s), height: 70 * s) col("PESO", "Medio", s) } .padding(.vertical, 44 * s) } .padding(.horizontal, 64 * s) .background(Color.black) } private func col(_ k: String, _ v: String, _ s: CGFloat) -> some View { VStack(spacing: 10 * s) { Text(k).font(.system(size: 30 * s, weight: .bold)).kerning(30 * s * 0.13).foregroundStyle(amber) Text(v).font(.system(size: 54 * s, weight: .semibold)).foregroundStyle(.white) } .frame(maxWidth: .infinity) } // MARK: barre private var topBar: some View { HStack(spacing: 22) { Button { cycleFlash() } label: { Image(systemName: cam.flash.icon) .foregroundStyle(cam.flash == .off ? .white : amber) } if cam.macroAvailable { Button { cam.toggleMacro() } label: { Image(systemName: cam.macroEnabled ? "camera.macro" : "camera.macro.slash") .foregroundStyle(cam.macroActive ? amber : (cam.macroEnabled ? .white : .gray)) } } Button { griglia.toggle() } label: { Image(systemName: "grid").foregroundStyle(griglia ? amber : .white) } Spacer() Button { cam.setResolution48(!cam.use48MP) } label: { Text(cam.use48MP ? "48 MP" : "24 MP") .font(.system(size: 13, weight: .semibold)) .padding(.horizontal, 9).padding(.vertical, 5) .overlay(RoundedRectangle(cornerRadius: 6).stroke(cam.use48MP ? amber : .white.opacity(0.6))) .foregroundStyle(cam.use48MP ? amber : .white) } if cam.aeafLocked || focusPoint != nil { Button { cam.resetFocus(); focusPoint = nil; showExposure = false } label: { Image(systemName: "scope").foregroundStyle(.white) } } } .font(.system(size: 21)) .padding(.horizontal, 22) } private var zoomBar: some View { HStack(spacing: 10) { ForEach(cam.presets, id: \.self) { p in let active = isActive(p) Button { cam.setZoom(p) } label: { Text(active ? label(cam.displayZoom) : label(p, short: true)) .font(.system(size: active ? 14 : 12, weight: .bold)) .foregroundStyle(active ? amber : .white) .frame(width: active ? 44 : 36, height: active ? 44 : 36) .background(Circle().fill(Color.white.opacity(0.14))) } } } .animation(.easeOut(duration: 0.15), value: cam.displayZoom) } private var bottomBar: some View { HStack { Button { if let u = URL(string: "photos-redirect://") { UIApplication.shared.open(u) } } label: { Group { if let t = cam.lastThumb { Image(uiImage: t).resizable().scaledToFill() } else { Color.white.opacity(0.1) } } .frame(width: 54, height: 57) .clipShape(RoundedRectangle(cornerRadius: 8)) .overlay(RoundedRectangle(cornerRadius: 8).stroke(.white.opacity(0.4))) } Spacer() Button { shoot() } label: { ZStack { Circle().stroke(.white, lineWidth: 4).frame(width: 78, height: 78) Circle().fill(cam.busy ? Color.gray : .white).frame(width: 64, height: 64) } } .disabled(cam.busy) Spacer() Button { editingTitle = true } label: { Image(systemName: "character.cursor.ibeam") .font(.system(size: 22)) .foregroundStyle(.white) .frame(width: 54, height: 57) } } .padding(.horizontal, 30) } @ViewBuilder private var toast: some View { if let m = cam.message { Text(m) .font(.system(size: 14, weight: .semibold)) .padding(.horizontal, 14).padding(.vertical, 8) .background(.ultraThinMaterial, in: Capsule()) .padding(.top, 60) .onAppear { DispatchQueue.main.asyncAfter(deadline: .now() + 2.2) { cam.message = nil } } } if !cam.authorized { Text("Consenti l'accesso alla fotocamera in Impostazioni › Copertina") .font(.system(size: 15)) .multilineTextAlignment(.center) .padding() .padding(.top, 200) } } // MARK: azioni private func shoot() { withAnimation(.easeOut(duration: 0.08)) { flashAnim = true } DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { withAnimation { flashAnim = false } } UIImpactFeedbackGenerator(style: .light).impactOccurred() cam.shoot() } private func tapFocus(_ p: CGPoint, _ dp: CGPoint, lock: Bool) { focusPoint = p focusLocked = lock showExposure = true cam.focus(at: dp, lock: lock) if lock { UIImpactFeedbackGenerator(style: .medium).impactOccurred() } scheduleHide() } private func scheduleHide() { hideWork?.cancel() let w = DispatchWorkItem { withAnimation { showExposure = false } if !cam.aeafLocked { focusPoint = nil } } hideWork = w DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: w) } private func cycleFlash() { let all = FlashChoice.allCases cam.flash = all[(all.firstIndex(of: cam.flash)! + 1) % all.count] } private func isActive(_ p: CGFloat) -> Bool { // il preset attivo è il più alto che non supera lo zoom attuale let z = cam.displayZoom + 0.001 let lower = cam.presets.filter { $0 <= z }.max() ?? cam.presets.first! return lower == p } private func label(_ z: CGFloat, short: Bool = false) -> String { if z < 1 { return short ? ".5" : String(format: "%.1f×", z).replacingOccurrences(of: "0.", with: ".") } if abs(z - z.rounded()) < 0.05 { return "\(Int(z.rounded()))" + (short ? "" : "×") } return String(format: "%.1f×", z) } } struct GridLines: View { var body: some View { GeometryReader { g in Path { p in for i in 1...2 { let x = g.size.width * CGFloat(i) / 3, y = g.size.height * CGFloat(i) / 3 p.move(to: CGPoint(x: x, y: 0)); p.addLine(to: CGPoint(x: x, y: g.size.height)) p.move(to: CGPoint(x: 0, y: y)); p.addLine(to: CGPoint(x: g.size.width, y: y)) } } .stroke(Color.white.opacity(0.28), lineWidth: 0.6) } } } struct FocusSquare: View { var locked: Bool @State private var scale: CGFloat = 1.4 var body: some View { Rectangle() .stroke(amber, lineWidth: 1.2) .frame(width: 74, height: 74) .scaleEffect(scale) .onAppear { withAnimation(.easeOut(duration: 0.25)) { scale = 1 } } .id(locked) } } struct ExposureSlider: View { var value: Float // -2 … +2 var onChange: (Float) -> Void @State private var start: Float = 0 @State private var dragging = false var body: some View { GeometryReader { g in let h = g.size.height let y = h / 2 - CGFloat(value) / 2 * (h / 2) ZStack { Rectangle().fill(amber.opacity(0.7)).frame(width: 1.2) Image(systemName: "sun.max.fill") .font(.system(size: 22)) .foregroundStyle(amber) .position(x: g.size.width / 2, y: y) } .frame(width: g.size.width, height: h) .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0) .onChanged { d in if !dragging { start = value; dragging = true } let delta = Float(-d.translation.height / (h / 2)) * 2 onChange(max(-2, min(2, start + delta))) } .onEnded { _ in dragging = false }) } } }