// 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 /// 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 ) { 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) } }