Layouts exported from Android's DeviceLayouts (DeviceLayouts.json), iOS layouts for the devices with hand-built Android editors, SwiftUI editor shared by the extension and the app, Mac snapshot tool for the editors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
98 lines
4.7 KiB
Swift
98 lines
4.7 KiB
Swift
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import SwiftUI
|
|
|
|
/// The live drawing at the top of a card (Android's VizCanvas): an envelope's
|
|
/// shape, a filter's response, or two cycles of an LFO wave.
|
|
struct VizView: View {
|
|
let viz: LayoutPanel.Viz
|
|
let tone: Color
|
|
@ObservedObject var model: DeviceModel
|
|
|
|
private func num(_ key: String?) -> Float? {
|
|
guard let k = key, let p = model.params[k] else { return nil }
|
|
return Swift.min(p.max, Swift.max(p.min, model.value(k)))
|
|
}
|
|
private func frac(_ key: String?) -> Float? {
|
|
guard let k = key, let p = model.params[k], let v = num(k) else { return nil }
|
|
return (v - p.min) / (p.max - p.min)
|
|
}
|
|
|
|
var body: some View {
|
|
Canvas { ctx, size in
|
|
let w = size.width, h = size.height
|
|
let top = h * 0.12, bottom = h * 0.92
|
|
var path = Path()
|
|
switch viz.kind {
|
|
case "envelope":
|
|
let a = CGFloat(sqrt(frac(viz.a) ?? 0)) + 0.02
|
|
let d = viz.d != nil ? CGFloat(sqrt(frac(viz.d) ?? 0)) + 0.02 : 0
|
|
let s = viz.s != nil ? CGFloat(num(viz.s) ?? 1) : 1
|
|
let r = CGFloat(sqrt(frac(viz.r) ?? 0)) + 0.02
|
|
let hold: CGFloat = 0.45
|
|
let k = w / (a + d + hold + r)
|
|
let sy = bottom - (bottom - top) * s
|
|
path.move(to: CGPoint(x: 0, y: bottom))
|
|
path.addLine(to: CGPoint(x: a * k, y: top))
|
|
path.addLine(to: CGPoint(x: (a + d) * k, y: sy))
|
|
path.addLine(to: CGPoint(x: (a + d + hold) * k, y: sy))
|
|
path.addLine(to: CGPoint(x: w, y: bottom))
|
|
line(ctx, CGPoint(x: (a + d + hold) * k, y: top), CGPoint(x: (a + d + hold) * k, y: bottom))
|
|
case "filter":
|
|
let fc = Double(frac(viz.cutoff) ?? 0.5)
|
|
let q = 0.6 + Double(frac(viz.reso) ?? 0) * 9
|
|
let kind = viz.type.map { model.choice($0).uppercased().first ?? "L" } ?? "L"
|
|
for i in 0...96 {
|
|
let x = Double(i) / 96
|
|
let r = pow(2.0, (x - fc) * 10)
|
|
let den = sqrt(pow(1 - r * r, 2) + pow(r / q, 2))
|
|
let mag = kind == "H" ? r * r / den : kind == "B" ? (r / q) / den : 1 / den
|
|
let db = Swift.min(20, Swift.max(-30, 20 * log10(Swift.max(mag, 1e-4))))
|
|
let y = top + CGFloat((20 - db) / 50) * (bottom - top)
|
|
if i == 0 { path.move(to: CGPoint(x: 0, y: y)) } else { path.addLine(to: CGPoint(x: CGFloat(x) * w, y: y)) }
|
|
}
|
|
let zeroY = top + 20 / 50 * (bottom - top)
|
|
line(ctx, CGPoint(x: 0, y: zeroY), CGPoint(x: w, y: zeroY))
|
|
line(ctx, CGPoint(x: CGFloat(fc) * w, y: top), CGPoint(x: CGFloat(fc) * w, y: bottom))
|
|
case "lfo":
|
|
let name = viz.wave.map { model.choice($0).lowercased() } ?? "sine"
|
|
let sh: [CGFloat] = [0.6, -0.3, 0.9, -0.8, 0.2, -0.5, 0.75, -0.1]
|
|
for i in 0...160 {
|
|
let x = CGFloat(i) / 160
|
|
let p = (x * 2).truncatingRemainder(dividingBy: 1)
|
|
let v: CGFloat
|
|
if name.hasPrefix("tri") { v = 1 - 4 * abs(p - 0.5) }
|
|
else if name.hasPrefix("saw") { v = 1 - 2 * p }
|
|
else if name.hasPrefix("sq") { v = p < 0.5 ? 1 : -1 }
|
|
else if name.hasPrefix("s&h") || name.hasPrefix("sample") { v = sh[Swift.min(7, Int(x * 8))] }
|
|
else { v = sin(2 * .pi * p) }
|
|
let y = (top + bottom) / 2 - v * (bottom - top) / 2
|
|
if i == 0 { path.move(to: CGPoint(x: 0, y: y)) } else { path.addLine(to: CGPoint(x: x * w, y: y)) }
|
|
}
|
|
line(ctx, CGPoint(x: 0, y: (top + bottom) / 2), CGPoint(x: w, y: (top + bottom) / 2))
|
|
default:
|
|
return
|
|
}
|
|
if viz.kind != "lfo" {
|
|
var fill = path
|
|
fill.addLine(to: CGPoint(x: w, y: h))
|
|
fill.addLine(to: CGPoint(x: 0, y: h))
|
|
fill.closeSubpath()
|
|
ctx.fill(fill, with: .color(tone.opacity(0.15)))
|
|
}
|
|
ctx.stroke(path, with: .color(tone), lineWidth: 2)
|
|
}
|
|
.frame(height: 56)
|
|
.background(Theme.background)
|
|
.overlay(Rectangle().stroke(Theme.grid, lineWidth: 1))
|
|
}
|
|
|
|
private func line(_ ctx: GraphicsContext, _ a: CGPoint, _ b: CGPoint) {
|
|
var p = Path()
|
|
p.move(to: a)
|
|
p.addLine(to: b)
|
|
ctx.stroke(p, with: .color(Theme.grid), lineWidth: 1)
|
|
}
|
|
}
|