Files
Lyrebird/Sources/LyrebirdUI/Controls/ParameterXY.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

312 lines
10 KiB
Swift

// ParameterXY.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if canImport(SwiftUI)
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)
}
}
#else
// Linux/GTK4 counterpart to `LyrebirdUI.ParameterXY`: a two-dimensional pad that
// drives two core `UIParamNode`s at once. Drawn on a single `Canvas` (background,
// centre grid, cursor) with an absolute-position `.onDrag` — the touch point maps
// straight to the cursor, X left→right, Y bottom→top.
//
// Same gesture path as `ParameterKnob`: the drag calls `node.update(_:)`,
// and because `UIParamNode` is `@Observable` the Canvas redraw takes the GTK4
// backend's narrow `.canvasContent` mutation path (the pinned fork's fix), so the
// cursor tracks continuously without the gesture being torn down by a rebuild.
//
// SwiftOpenUI + BackendGTK4 are always present in this target (see Package.swift),
// so no `canImport` guard is needed; `BackendGTK4` carries the Cairo-backed
// `DrawingContext` drawing methods.
import SwiftOpenUI
import BackendGTK4
import LyrebirdSynth
import Foundation // CGSize (Canvas renderer size)
/// A two-axis control pad for the Linux/GTK4 UI, bound to two ``UIParamNode``s.
/// Mirrors `LyrebirdUI.ParameterXY`: drag within the pad to set X (horizontal,
/// left→right) and Y (vertical, bottom→top) independently.
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>
// Presentation constants (points).
private let padSize: Double = 160
private let cursorRadius: Double = 6
// 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 {
Canvas(renderer: { (ctx: DrawingContext, size: CGSize) in
let w: Double = Double(size.width)
let h: Double = Double(size.height)
// Background fill.
ctx.setColor(r: 0.15, g: 0.15, b: 0.15)
ctx.rectangle(x: 0, y: 0, width: w, height: h)
ctx.fill()
// Border.
ctx.setColor(r: 0.40, g: 0.40, b: 0.40)
ctx.setLineWidth(1)
ctx.rectangle(x: 0, y: 0, width: w, height: h)
ctx.stroke()
// Centre grid lines (faint).
let midX: Double = w / 2
let midY: Double = h / 2
ctx.setColor(r: 0.40, g: 0.40, b: 0.40, a: 0.4)
ctx.setLineWidth(0.5)
ctx.moveTo(x: midX, y: 0)
ctx.lineTo(x: midX, y: h)
ctx.moveTo(x: 0, y: midY)
ctx.lineTo(x: w, y: midY)
ctx.stroke()
// Cursor: X left→right, Y bottom→top (flip vertical).
let nx: Double = normalise(xNode.value, range: xRange)
let ny: Double = 1.0 - normalise(yNode.value, range: yRange)
let cx: Double = nx * w
let cy: Double = ny * h
ctx.setColor(r: 0.30, g: 0.80, b: 1.0)
ctx.arc(centerX: cx, centerY: cy, radius: cursorRadius,
startAngle: 0, endAngle: 2 * Double.pi)
ctx.fill()
})
.frame(width: padSize, height: padSize)
.onDrag(
minimumDistance: 0,
onChanged: { (drag: DragGestureValue) in
// Absolute position: the touch point is the cursor.
let nx: Double = min(max(drag.location.x / padSize, 0), 1)
let ny: Double = min(max(drag.location.y / padSize, 0), 1)
xNode.update(denormalise(nx, range: xRange))
yNode.update(denormalise(1.0 - ny, range: yRange)) // flip Y
}
)
Text("\(xLabel): \(formattedParameterValue(xNode.value))")
.monospacedDigit()
Text("\(yLabel): \(formattedParameterValue(yNode.value))")
.monospacedDigit()
}
.padding()
}
// 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)
}
}
#endif