1.0.0 release
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
// MultiSlider.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
/// A row of `count` adjacent vertical sliders bound to consecutive cells of a
|
||||
/// `LyrebirdBuffer`.
|
||||
///
|
||||
/// Designed for driving per-bin parameter buffers — for example the per-bin
|
||||
/// delay times and feedbacks read by `SpectralBinDelay`, or the magnitude
|
||||
/// transfer curve read by `SpectralMagnitudeMap`. Each bar represents one
|
||||
/// `LyrebirdBuffer` cell starting at `startIndex`; dragging vertically over a
|
||||
/// bar writes the new value via `LyrebirdBuffer.setValue(_:at:)` (lock-free on
|
||||
/// the RT side, see the Lyrebird module's `BufferGuide`).
|
||||
///
|
||||
/// ```swift
|
||||
/// let delays = LyrebirdBuffer(count: 1024)
|
||||
/// delays.fill { i, n in Double(i) / Double(n) * 0.5 }
|
||||
///
|
||||
/// MultiSlider(
|
||||
/// buffer: delays,
|
||||
/// count: 64,
|
||||
/// range: 0.0...0.5,
|
||||
/// height: 120
|
||||
/// )
|
||||
/// ```
|
||||
public struct MultiSlider: View {
|
||||
|
||||
// MARK: - Configuration
|
||||
|
||||
private let buffer: LyrebirdBuffer
|
||||
private let count: Int
|
||||
private let startIndex: Int
|
||||
private let range: ClosedRange<Double>
|
||||
private let height: CGFloat
|
||||
|
||||
// MARK: - State
|
||||
|
||||
@State private var values: [Double]
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// - Parameters:
|
||||
/// - buffer: Backing `LyrebirdBuffer`. Mono only. Initial values shown by
|
||||
/// the sliders are read once from `buffer[startIndex + i]` at view
|
||||
/// construction; thereafter the buffer is treated as one-way (this
|
||||
/// view writes; external readers consume).
|
||||
/// - count: Number of sliders in the row. Must be ≥ 1 and
|
||||
/// `startIndex + count <= buffer.count`.
|
||||
/// - startIndex: First buffer index this view writes to. Defaults to `0`.
|
||||
/// - range: Value range. Drag-top of a bar maps to `range.upperBound`,
|
||||
/// drag-bottom to `range.lowerBound`.
|
||||
/// - height: View height in points. Default `100`.
|
||||
public init(
|
||||
buffer: LyrebirdBuffer,
|
||||
count: Int,
|
||||
startIndex: Int = 0,
|
||||
range: ClosedRange<Double>,
|
||||
height: CGFloat = 100
|
||||
) {
|
||||
precondition(buffer.channelCount == 1,
|
||||
"MultiSlider: buffer must be mono, got channelCount=\(buffer.channelCount)")
|
||||
precondition(count >= 1,
|
||||
"MultiSlider: count must be >= 1, got \(count)")
|
||||
precondition(startIndex >= 0 && startIndex + count <= buffer.count,
|
||||
"MultiSlider: startIndex \(startIndex) + count \(count) "
|
||||
+ "exceeds buffer.count \(buffer.count)")
|
||||
precondition(range.lowerBound < range.upperBound,
|
||||
"MultiSlider: range must be non-empty, got \(range)")
|
||||
|
||||
self.buffer = buffer
|
||||
self.count = count
|
||||
self.startIndex = startIndex
|
||||
self.range = range
|
||||
self.height = height
|
||||
|
||||
var initial: [Double] = [Double](repeating: range.lowerBound, count: count)
|
||||
for i: Int in 0..<count {
|
||||
let raw: Double = buffer[startIndex + i]
|
||||
initial[i] = min(max(raw, range.lowerBound), range.upperBound)
|
||||
}
|
||||
_values = State(initialValue: initial)
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
GeometryReader { geo in
|
||||
let columnWidth: CGFloat = geo.size.width / CGFloat(count)
|
||||
ZStack(alignment: .topLeading) {
|
||||
Color.lyrebirdControlBackground
|
||||
|
||||
Canvas { ctx, size in
|
||||
let h: CGFloat = size.height
|
||||
let w: CGFloat = columnWidth
|
||||
for i: Int in 0..<count {
|
||||
let v: Double = values[i]
|
||||
let norm: Double = (v - range.lowerBound) / (range.upperBound - range.lowerBound)
|
||||
let barH: CGFloat = h * CGFloat(norm)
|
||||
let x: CGFloat = CGFloat(i) * w
|
||||
let rect: CGRect = CGRect(x: x + 1, y: h - barH, width: max(1, w - 2), height: barH)
|
||||
ctx.fill(Path(rect), with: .color(.accentColor))
|
||||
}
|
||||
}
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
.gesture(
|
||||
DragGesture(minimumDistance: 0)
|
||||
.onChanged { drag in
|
||||
let column: Int = Int(drag.location.x / columnWidth)
|
||||
guard column >= 0 && column < count else { return }
|
||||
let yClamped: CGFloat = min(max(drag.location.y, 0), geo.size.height)
|
||||
let norm: Double = 1 - Double(yClamped / geo.size.height)
|
||||
let span: Double = range.upperBound - range.lowerBound
|
||||
let value: Double = range.lowerBound + norm * span
|
||||
// Drag fires many ticks per second; the SwiftUI diff
|
||||
// and `setValue`'s NSLock both make repeated identical
|
||||
// values cheap, so we don't bother short-circuiting on
|
||||
// equality (Double-equality on continuous drag is
|
||||
// almost always false).
|
||||
values[column] = value
|
||||
buffer.setValue(value, at: startIndex + column)
|
||||
}
|
||||
)
|
||||
}
|
||||
.frame(height: height)
|
||||
.overlay(
|
||||
Rectangle().stroke(Color.lyrebirdSeparator, lineWidth: 1)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// ParameterButton.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
/// A button control for triggering actions or toggling a `UIParamNode` between two values.
|
||||
///
|
||||
/// Two styles:
|
||||
/// - `.momentary` (default) — executes a closure on press; no node binding required.
|
||||
/// - `.toggle` — flips the bound node between `offValue` and `onValue` on each press.
|
||||
///
|
||||
/// ```swift
|
||||
/// // Momentary action button
|
||||
/// ParameterButton("Trigger") { synth.trigger() }
|
||||
///
|
||||
/// // Toggle bound to a UIParamNode
|
||||
/// ParameterButton("Mute", node: synth.$muted, style: .toggle)
|
||||
/// ```
|
||||
public struct ParameterButton: View {
|
||||
|
||||
// MARK: - Style
|
||||
|
||||
public enum Style {
|
||||
/// Executes the action closure on each press.
|
||||
case momentary
|
||||
/// Flips the node between `offValue` (0) and `onValue` (1) on each press.
|
||||
case toggle
|
||||
}
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let label: String
|
||||
private let node: UIParamNode?
|
||||
private let style: Style
|
||||
private let action: (() -> Void)?
|
||||
|
||||
/// Value representing the "off" state for `.toggle` style (default 0).
|
||||
public var offValue: Double = 0.0
|
||||
/// Value representing the "on" state for `.toggle` style (default 1).
|
||||
public var onValue: Double = 1.0
|
||||
|
||||
// MARK: - Inits
|
||||
|
||||
/// Creates a momentary button that runs `action` on press.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - label: Button text.
|
||||
/// - action: Closure called each time the button is pressed.
|
||||
public init(
|
||||
_ label: String,
|
||||
action: @escaping (
|
||||
) -> Void) {
|
||||
self.label = label
|
||||
self.node = nil
|
||||
self.style = .momentary
|
||||
self.action = action
|
||||
}
|
||||
|
||||
/// Creates a toggle button bound to a `UIParamNode`.
|
||||
///
|
||||
/// The node's value is interpreted as a boolean: any value ≥ 0.5 is "on".
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - label: Button text.
|
||||
/// - node: The `UIParamNode` to toggle (pass `synth.$paramName`).
|
||||
/// - style: Must be `.toggle`; `.momentary` is handled by the other initializer.
|
||||
public init(
|
||||
_ label: String,
|
||||
node: UIParamNode,
|
||||
style: Style = .toggle
|
||||
) {
|
||||
self.label = label
|
||||
self.node = node
|
||||
self.style = style
|
||||
self.action = nil
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
switch style {
|
||||
case .momentary:
|
||||
Button(label) { action?() }
|
||||
.buttonStyle(.bordered)
|
||||
|
||||
case .toggle:
|
||||
let isOn: Bool = (node?.value ?? 0) >= 0.5
|
||||
Button(action: {
|
||||
node?.update(isOn ? offValue : onValue)
|
||||
}) {
|
||||
Text(label)
|
||||
.frame(minWidth: 60)
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.background(isOn ? Color.accentColor.opacity(0.3) : Color.clear)
|
||||
.cornerRadius(6)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// ParameterFormatting.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Threshold above which a value renders with zero fractional digits.
|
||||
internal let parameterFormatLargeMagnitudeThreshold: Double = 1000
|
||||
|
||||
/// Threshold above which a value renders with one fractional digit.
|
||||
internal let parameterFormatMediumMagnitudeThreshold: Double = 10
|
||||
|
||||
/// Render a numeric parameter value with magnitude-adaptive precision.
|
||||
///
|
||||
/// - `|v| >= 1000`: no decimals.
|
||||
/// - `1000 > |v| >= 10`: one decimal.
|
||||
/// - `|v| < 10`: three decimals.
|
||||
///
|
||||
/// Shared by `ParameterKnob` and `ParameterXY` so the two controls agree on
|
||||
/// display formatting.
|
||||
internal func formattedParameterValue(_ v: Double) -> String {
|
||||
if abs(v) >= parameterFormatLargeMagnitudeThreshold { return String(format: "%.0f", v) }
|
||||
if abs(v) >= parameterFormatMediumMagnitudeThreshold { return String(format: "%.1f", v) }
|
||||
return String(format: "%.3f", v)
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
// ParameterKnob.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
/// A rotary knob control bound to a `UIParamNode`.
|
||||
///
|
||||
/// Drag vertically to change value: drag up increases, drag down decreases.
|
||||
/// Equivalent in behaviour to `ParameterSlider` — choose whichever fits the layout.
|
||||
///
|
||||
/// ```swift
|
||||
/// ParameterKnob("Resonance", node: synth.$resonance, range: 0...1)
|
||||
/// ```
|
||||
public struct ParameterKnob: View {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let label: String
|
||||
private let node: UIParamNode
|
||||
private let range: ClosedRange<Double>
|
||||
|
||||
/// Pixels of vertical drag that span the full parameter range. Default 200.
|
||||
public var dragSensitivity: CGFloat = 200
|
||||
|
||||
// MARK: - Private drag state
|
||||
|
||||
@State private var dragStartPosition: CGFloat = 0
|
||||
@State private var dragStartValue: Double = 0
|
||||
@State private var dragInProgress: Bool = false
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a knob bound to a `UIParamNode`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - label: Text displayed below the knob.
|
||||
/// - node: The `UIParamNode` to bind to (pass `synth.$paramName`).
|
||||
/// - range: The minimum and maximum parameter values.
|
||||
public init(
|
||||
_ label: String,
|
||||
node: UIParamNode,
|
||||
range: ClosedRange<Double>
|
||||
) {
|
||||
self.label = label
|
||||
self.node = node
|
||||
self.range = range
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
VStack(spacing: 4) {
|
||||
knobDial
|
||||
.frame(width: 50, height: 50) // matches dialSize constant inside knobDial
|
||||
.gesture(dragGesture)
|
||||
Text(label)
|
||||
.font(.caption)
|
||||
Text(formattedValue)
|
||||
.font(.caption)
|
||||
.monospacedDigit()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Knob dial drawing
|
||||
|
||||
private var knobDial: some View {
|
||||
let normalised: Double = normalise(node.value)
|
||||
// Rotation: -135° (fully CCW) to +135° (fully CW)
|
||||
let startAngle: Double = -135.0
|
||||
let totalArc: Double = 270.0
|
||||
let rotation: Double = startAngle + normalised * totalArc
|
||||
|
||||
// Layout constants
|
||||
let dialSize: CGFloat = 50
|
||||
let indicatorWidth: CGFloat = 2
|
||||
let indicatorHeight: CGFloat = 14
|
||||
// Centre the indicator tip near the dial edge: dial radius − indicator height/2 − small inset
|
||||
let indicatorInset: CGFloat = 3
|
||||
let indicatorOffset: CGFloat = -(dialSize / 2 - indicatorHeight / 2 - indicatorInset)
|
||||
|
||||
return ZStack {
|
||||
Circle()
|
||||
.fill(Color.lyrebirdControlBackground)
|
||||
.overlay(Circle().stroke(Color.lyrebirdSeparator, lineWidth: 1))
|
||||
// Indicator line
|
||||
Rectangle()
|
||||
.fill(Color.accentColor)
|
||||
.frame(width: indicatorWidth, height: indicatorHeight)
|
||||
.offset(y: indicatorOffset)
|
||||
.rotationEffect(.degrees(rotation))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Drag gesture
|
||||
|
||||
private var dragGesture: some Gesture {
|
||||
DragGesture(minimumDistance: 1)
|
||||
.onChanged { gesture in
|
||||
if !dragInProgress {
|
||||
// Capture drag origin on the first event. Using an explicit
|
||||
// flag avoids a sentinel collision when the bound value is
|
||||
// genuinely zero.
|
||||
dragInProgress = true
|
||||
dragStartPosition = gesture.location.y
|
||||
dragStartValue = node.value
|
||||
}
|
||||
let delta: CGFloat = dragStartPosition - gesture.location.y // up = positive
|
||||
let span: Double = range.upperBound - range.lowerBound
|
||||
let change: Double = Double(delta / dragSensitivity) * span
|
||||
let clamped: Double = min(max(dragStartValue + change, range.lowerBound), range.upperBound)
|
||||
node.update(clamped)
|
||||
}
|
||||
.onEnded { _ in
|
||||
dragInProgress = false
|
||||
dragStartPosition = 0
|
||||
dragStartValue = 0
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func normalise(_ value: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
guard hi > lo else { return 0 }
|
||||
return min(max((value - lo) / (hi - lo), 0), 1)
|
||||
}
|
||||
|
||||
private var formattedValue: String {
|
||||
formattedParameterValue(node.value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// ParameterLabel.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
/// A read-only display label that shows the current value of a `UIParamNode`.
|
||||
///
|
||||
/// Updates automatically via `@Observable` on the backing node — no polling.
|
||||
///
|
||||
/// ```swift
|
||||
/// ParameterLabel("Level", node: synth.$amplitude)
|
||||
/// ParameterLabel("Freq (Hz)", node: synth.$frequency, decimalPlaces: 1)
|
||||
/// ```
|
||||
public struct ParameterLabel: View {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let label: String
|
||||
private let node: UIParamNode
|
||||
private let decimalPlaces: Int
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a read-only parameter label.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - label: Descriptive text displayed before the value.
|
||||
/// - node: The `UIParamNode` to monitor (pass `synth.$paramName`).
|
||||
/// - decimalPlaces: Number of decimal places shown. Default 3.
|
||||
public init(
|
||||
_ label: String,
|
||||
node: UIParamNode,
|
||||
decimalPlaces: Int = 3
|
||||
) {
|
||||
self.label = label
|
||||
self.node = node
|
||||
self.decimalPlaces = decimalPlaces
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
HStack {
|
||||
Text(label)
|
||||
.frame(minWidth: 80, alignment: .leading)
|
||||
Text(formattedValue)
|
||||
.monospacedDigit()
|
||||
.frame(minWidth: 60, alignment: .trailing)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Formatting
|
||||
|
||||
private var formattedValue: String {
|
||||
String(format: "%.\(decimalPlaces)f", node.value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// ParameterSlider.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
/// A labelled continuous slider bound to a `UIParamNode`.
|
||||
///
|
||||
/// Changes flow through the node's atomic update path — RT-safe with no special handling.
|
||||
///
|
||||
/// ```swift
|
||||
/// ParameterSlider("Amplitude", node: synth.$amplitude, range: 0.0...1.0)
|
||||
/// ParameterSlider("Cutoff", node: synth.$cutoff, range: 20.0...20000.0, taper: .exponential)
|
||||
/// ```
|
||||
public struct ParameterSlider: View {
|
||||
|
||||
// MARK: - Taper
|
||||
|
||||
/// Value mapping applied between the slider's linear position and the parameter value.
|
||||
public enum Taper {
|
||||
/// No mapping — slider position maps directly to parameter value.
|
||||
case linear
|
||||
/// Exponential mapping — equal visual steps produce equal ratios (good for frequency, time).
|
||||
case exponential
|
||||
}
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let label: String
|
||||
private let node: UIParamNode
|
||||
private let range: ClosedRange<Double>
|
||||
private let taper: Taper
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a slider bound to a `UIParamNode`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - label: Text displayed beside the slider.
|
||||
/// - node: The `UIParamNode` to bind to (pass `synth.$paramName`).
|
||||
/// - range: The minimum and maximum parameter values.
|
||||
/// - taper: Value mapping. Default `.linear`.
|
||||
public init(
|
||||
_ label: String,
|
||||
node: UIParamNode,
|
||||
range: ClosedRange<Double>,
|
||||
taper: Taper = .linear
|
||||
) {
|
||||
|
||||
self.label = label
|
||||
self.node = node
|
||||
self.range = range
|
||||
self.taper = taper
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
HStack {
|
||||
Text(label)
|
||||
.frame(minWidth: 80, alignment: .leading)
|
||||
Slider(value: binding, in: linearRange) { _ in }
|
||||
Text(formattedValue)
|
||||
.frame(minWidth: 60, alignment: .trailing)
|
||||
.monospacedDigit()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private helpers
|
||||
|
||||
/// Linear range always 0...1; taper mapping happens inside the Binding.
|
||||
private var linearRange: ClosedRange<Double> { 0.0...1.0 }
|
||||
|
||||
private var binding: Binding<Double> {
|
||||
Binding(
|
||||
get: { toLinear(node.value) },
|
||||
set: { node.update(fromLinear($0)) }
|
||||
)
|
||||
}
|
||||
|
||||
private func toLinear(_ value: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
switch taper {
|
||||
case .linear:
|
||||
return (value - lo) / (hi - lo)
|
||||
case .exponential:
|
||||
guard lo > 0, hi > lo else { return (value - lo) / (hi - lo) }
|
||||
return log(value / lo) / log(hi / lo)
|
||||
}
|
||||
}
|
||||
|
||||
private func fromLinear(_ position: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
switch taper {
|
||||
case .linear:
|
||||
return lo + position * (hi - lo)
|
||||
case .exponential:
|
||||
guard lo > 0, hi > lo else { return lo + position * (hi - lo) }
|
||||
return lo * pow(hi / lo, position)
|
||||
}
|
||||
}
|
||||
|
||||
private var formattedValue: String {
|
||||
let v: Double = node.value
|
||||
if abs(v) >= 1000 {
|
||||
return String(format: "%.0f", v)
|
||||
} else if abs(v) >= 10 {
|
||||
return String(format: "%.1f", v)
|
||||
} else {
|
||||
return String(format: "%.3f", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
// ParameterXY.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
/// A two-dimensional control pad that simultaneously controls two `UIParamNode` values.
|
||||
///
|
||||
/// Drag within the pad to set X and Y parameter values independently.
|
||||
/// X increases left-to-right; Y increases bottom-to-top.
|
||||
///
|
||||
/// ```swift
|
||||
/// ParameterXY(
|
||||
/// xLabel: "Pan", xNode: synth.$pan, xRange: -1...1,
|
||||
/// yLabel: "Amplitude", yNode: synth.$amplitude, yRange: 0...1
|
||||
/// )
|
||||
/// ```
|
||||
public struct ParameterXY: View {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let xLabel: String
|
||||
private let xNode: UIParamNode
|
||||
private let xRange: ClosedRange<Double>
|
||||
|
||||
private let yLabel: String
|
||||
private let yNode: UIParamNode
|
||||
private let yRange: ClosedRange<Double>
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates an XY pad bound to two `UIParamNode` values.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - xLabel: Label for the X axis.
|
||||
/// - xNode: The `UIParamNode` controlled by horizontal position.
|
||||
/// - xRange: Value range for the X axis.
|
||||
/// - yLabel: Label for the Y axis.
|
||||
/// - yNode: The `UIParamNode` controlled by vertical position.
|
||||
/// - yRange: Value range for the Y axis.
|
||||
public init(
|
||||
xLabel: String,
|
||||
xNode: UIParamNode,
|
||||
xRange: ClosedRange<Double>,
|
||||
yLabel: String,
|
||||
yNode: UIParamNode,
|
||||
yRange: ClosedRange<Double>
|
||||
) {
|
||||
|
||||
self.xLabel = xLabel
|
||||
self.xNode = xNode
|
||||
self.xRange = xRange
|
||||
self.yLabel = yLabel
|
||||
self.yNode = yNode
|
||||
self.yRange = yRange
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
VStack(spacing: 4) {
|
||||
GeometryReader { geometry in
|
||||
ZStack {
|
||||
// Background
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.fill(Color.lyrebirdControlBackground)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.stroke(Color.lyrebirdSeparator, lineWidth: 1)
|
||||
)
|
||||
// Grid lines
|
||||
gridLines
|
||||
// Cursor
|
||||
cursor(in: geometry.size)
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
.gesture(dragGesture(in: geometry.size))
|
||||
}
|
||||
// Axis labels
|
||||
HStack {
|
||||
Text(yLabel)
|
||||
.font(.caption)
|
||||
Spacer()
|
||||
Text(xLabel)
|
||||
.font(.caption)
|
||||
}
|
||||
HStack {
|
||||
Text(formattedValue(yNode.value))
|
||||
.font(.caption)
|
||||
.monospacedDigit()
|
||||
Spacer()
|
||||
Text(formattedValue(xNode.value))
|
||||
.font(.caption)
|
||||
.monospacedDigit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Cursor
|
||||
|
||||
private func cursor(in size: CGSize) -> some View {
|
||||
let nx: Double = normalise(xNode.value, range: xRange)
|
||||
let ny: Double = 1.0 - normalise(yNode.value, range: yRange) // flip: bottom = 0
|
||||
let cx: Double = nx * size.width
|
||||
let cy: Double = ny * size.height
|
||||
return Circle()
|
||||
.fill(Color.accentColor)
|
||||
.frame(width: 12, height: 12)
|
||||
.position(x: cx, y: cy)
|
||||
}
|
||||
|
||||
// MARK: - Grid
|
||||
|
||||
private var gridLines: some View {
|
||||
Canvas { context, size in
|
||||
let midX: CGFloat = size.width / 2
|
||||
let midY: CGFloat = size.height / 2
|
||||
var path: Path = Path()
|
||||
path.move(to: CGPoint(x: midX, y: 0))
|
||||
path.addLine(to: CGPoint(x: midX, y: size.height))
|
||||
path.move(to: CGPoint(x: 0, y: midY))
|
||||
path.addLine(to: CGPoint(x: size.width, y: midY))
|
||||
context.stroke(path, with: .color(Color.lyrebirdSeparator.opacity(0.4)),
|
||||
lineWidth: 0.5)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Drag
|
||||
|
||||
private func dragGesture(in size: CGSize) -> some Gesture {
|
||||
DragGesture(minimumDistance: 0)
|
||||
.onChanged { gesture in
|
||||
let nx: CGFloat = min(max(gesture.location.x / size.width, 0), 1)
|
||||
let ny: CGFloat = min(max(gesture.location.y / size.height, 0), 1)
|
||||
xNode.update(denormalise(nx, range: xRange))
|
||||
yNode.update(denormalise(1.0 - ny, range: yRange)) // flip Y
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func normalise(
|
||||
_ value: Double,
|
||||
range: ClosedRange<Double>
|
||||
) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
guard hi > lo else { return 0 }
|
||||
return min(max((value - lo) / (hi - lo), 0), 1)
|
||||
}
|
||||
|
||||
private func denormalise(
|
||||
_ position: Double,
|
||||
range: ClosedRange<Double>
|
||||
) -> Double {
|
||||
range.lowerBound + position * (range.upperBound - range.lowerBound)
|
||||
}
|
||||
|
||||
private func formattedValue(_ v: Double) -> String {
|
||||
formattedParameterValue(v)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// PlatformColors.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
#if os(macOS)
|
||||
import AppKit
|
||||
#else
|
||||
import UIKit
|
||||
#endif
|
||||
|
||||
extension Color {
|
||||
#if os(macOS)
|
||||
public static let lyrebirdControlBackground: Color = Color(NSColor.controlBackgroundColor)
|
||||
public static let lyrebirdSeparator: Color = Color(NSColor.separatorColor)
|
||||
#else
|
||||
public static let lyrebirdControlBackground = Color(UIColor.secondarySystemBackground)
|
||||
public static let lyrebirdSeparator = Color(UIColor.separator)
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user