// PedalSwitch.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdUI /// A UI-agnostic description of one multi-position pedal switch. /// /// Where ``PedalKnob`` describes a continuous rotary control, `PedalSwitch` describes a /// discrete segmented switch with a fixed set of labelled positions — a two-state toggle /// (e.g. sweep Up / Down) or a three-state selector (e.g. filter LP / BP / HP). A `Pedal` /// publishes an array of these via ``Pedal/effectSwitches`` so a generic pedalboard UI can /// render a segmented control per discrete parameter without knowing the concrete type. /// /// Each option pairs a short caption with the exact `Double` written to the live /// ``UIParamNode`` when that position is selected; the currently-lit segment is the option /// whose `value` is nearest the node's current value. public struct PedalSwitch { /// One labelled position of a ``PedalSwitch``. public struct Option { /// Short caption shown inside the segment (e.g. "Up", "BP"). public let title: String /// Value written to the switch's node when this position is selected. public let value: Double /// Creates a switch position. /// /// - Parameters: /// - title: Short caption shown inside the segment. /// - value: Value written to the node when this position is selected. public init(title: String, value: Double) { self.title = title self.value = value } } /// Short label shown beneath the segmented control (e.g. "Mode", "Sweep"). public let label: String /// The live parameter node the switch reads and writes. Pass `pedal.$paramName`. public let node: UIParamNode /// Ordered positions, left to right. Two entries make a two-state switch, three a /// three-state switch. Must contain at least two options. public let options: [Option] /// `true` when this switch controls a tempo-synced behaviour (e.g. the looper's /// Sync-to-Tempo mode). A generic UI can disable such switches — and the host can force them /// to their first (off) option — while the tempo indicator is turned off. Defaults to `false`. public let tempoDependent: Bool /// When `true`, committing a new position asks the host to rebuild the board — for switches that /// change the *graph topology* (e.g. a clean/drive channel that adds or removes a gain stage), /// the same mechanism as ``PedalSelectorKnob/rebuildsOnChange``. Defaults to `false`. public let rebuildsOnChange: Bool /// Creates a switch descriptor. /// /// - Parameters: /// - label: Short label shown beneath the segmented control. /// - node: The `UIParamNode` projected value of an `@UIParam` property /// (`pedal.$mode`, `pedal.$direction`, …). /// - options: Ordered switch positions, left to right. At least two. /// - tempoDependent: `true` if the switch depends on the tempo/clock. Defaults to `false`. /// - rebuildsOnChange: Ask the host to rebuild the board when a new position is committed. Use /// for switches that change graph topology (e.g. a channel switch). Defaults to `false`. public init( label: String, node: UIParamNode, options: [Option], tempoDependent: Bool = false, rebuildsOnChange: Bool = false ) { precondition(options.count >= 2, "PedalSwitch requires at least two options") self.label = label self.node = node self.options = options self.tempoDependent = tempoDependent self.rebuildsOnChange = rebuildsOnChange } /// The value of the next option after the one nearest `current`, wrapping to the first — so a /// momentary trigger (e.g. a MIDI switch) can oscillate through the positions on each press. public func nextValue(after current: Double) -> Double { guard !options.isEmpty else { return current } let currentIndex: Int = options.indices.min(by: { abs(options[$0].value - current) < abs(options[$1].value - current) }) ?? 0 let nextIndex: Int = (currentIndex + 1) % options.count return options[nextIndex].value } }