1.0.0 release
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user