// PedalSelectorKnobView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import SwiftUI import LyrebirdShoeGaze /// Renders a ``PedalSelectorKnob`` — a rotary knob that snaps to a fixed set of labelled stops. /// /// Visually matches `ParameterKnob` (same dial + indicator), but drag quantises to discrete /// positions and the caption shows the current stop's title rather than a numeric value. Drag up to /// advance clockwise through the stops, down to go back. struct PedalSelectorKnobView: View { // MARK: - Layout constants private static let dialSize: CGFloat = 50 private static let indicatorWidth: CGFloat = 2 private static let indicatorHeight: CGFloat = 14 private static let indicatorInset: CGFloat = 3 private static let startAngle: Double = -135.0 private static let totalArc: Double = 270.0 /// Pixels of vertical drag per stop step. private static let pixelsPerStep: CGFloat = 22 private static let labelSpacing: CGFloat = 4 // MARK: - Input private let selector: PedalSelectorKnob // MARK: - Drag state @State private var dragStartIndex: Int = 0 @State private var dragInProgress: Bool = false // MARK: - Init init(_ selector: PedalSelectorKnob) { self.selector = selector } // MARK: - Body var body: some View { VStack(spacing: PedalSelectorKnobView.labelSpacing) { dial .frame(width: PedalSelectorKnobView.dialSize, height: PedalSelectorKnobView.dialSize) .gesture(dragGesture) Text(selector.label) .font(.caption) .foregroundStyle(Color.white) Text(currentStop.title) .font(.caption) .foregroundStyle(Color.white) } } // MARK: - Dial private var dial: some View { let count: Int = selector.stops.count let normalised: Double = count > 1 ? Double(currentIndex) / Double(count - 1) : 0 let rotation: Double = PedalSelectorKnobView.startAngle + normalised * PedalSelectorKnobView.totalArc let offset: CGFloat = -(PedalSelectorKnobView.dialSize / 2 - PedalSelectorKnobView.indicatorHeight / 2 - PedalSelectorKnobView.indicatorInset) return ZStack { Circle() .fill(Color.lyrebirdControlBackground) .overlay(Circle().stroke(Color.lyrebirdSeparator, lineWidth: 1)) Rectangle() .fill(Color.accentColor) .frame( width: PedalSelectorKnobView.indicatorWidth, height: PedalSelectorKnobView.indicatorHeight ) .offset(y: offset) .rotationEffect(.degrees(rotation)) } } // MARK: - Drag private var dragGesture: some Gesture { DragGesture(minimumDistance: 1) .onChanged { gesture in if !dragInProgress { dragInProgress = true dragStartIndex = currentIndex } let delta: CGFloat = gesture.translation.height // down = positive // Up should advance (increase index), so subtract the downward delta. let steps: Int = Int((-delta / PedalSelectorKnobView.pixelsPerStep).rounded()) let target: Int = min(max(dragStartIndex + steps, 0), selector.stops.count - 1) let value: Double = selector.stops[target].value if value != selector.node.value { selector.node.update(value) } } .onEnded { _ in dragInProgress = false } } // MARK: - Helpers /// Index of the stop whose value is nearest the node's current value. private var currentIndex: Int { let value: Double = selector.node.value var bestIndex: Int = 0 var bestDistance: Double = .greatestFiniteMagnitude for (index, stop) in selector.stops.enumerated() { let distance: Double = abs(stop.value - value) if distance < bestDistance { bestDistance = distance bestIndex = index } } return bestIndex } private var currentStop: PedalSelectorKnob.Stop { selector.stops[currentIndex] } }