Add segmented pedal switch control; Mu-Tron sweep + mode switches
Introduces a discrete multi-position switch control alongside the rotary knobs, and uses it to expose the Mu-Tron's sweep direction and filter mode. - PedalSwitch: UI-agnostic descriptor (label, node, ordered options of caption + value), parallel to PedalKnob. Two options make a two-state switch, three a three-state switch. - PedalSwitchView: segmented SwiftUI control bound to a UIParamNode. Lit segment is the option nearest the node's value; tapping writes that option's value. Serves both two- and three-state switches. - Pedal gains an effectSwitches hook; PedalView renders a switch row between the knob grid and the footswitch. - MuTronPedal: sweep direction is now a two-state switch (Down/Up) and filter mode a three-state switch (LP/BP/HP). The effect builds six MuTronIII instances (three modes x two directions) selected by a nested Select so every filter state stays warmed for click-free switching. - Presets: PedalState gains a backward-compatible switchValues array (decodes to empty for older presets); captureState/applyState persist and restore switch positions. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
// PedalSwitchView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
// MARK: - PedalSwitchView
|
||||
|
||||
/// A segmented multi-position switch bound to a live ``UIParamNode``.
|
||||
///
|
||||
/// Renders one button per ``PedalSwitch/Option`` in a horizontal pill, with a caption
|
||||
/// label beneath — mirroring the look of ``ParameterKnob``. The same view serves both a
|
||||
/// two-state toggle (two options, e.g. sweep Up / Down) and a three-state selector (three
|
||||
/// options, e.g. filter LP / BP / HP); the segment count follows `pedalSwitch.options`.
|
||||
///
|
||||
/// The lit segment is the option whose `value` is nearest the node's current value.
|
||||
/// Tapping a segment writes that option's exact `value` via `node.update(_:)`. Reading
|
||||
/// `node.value` in `body` registers the view for automatic re-render (UIParamNode is
|
||||
/// `@Observable`).
|
||||
///
|
||||
/// ## Usage
|
||||
/// ```swift
|
||||
/// PedalSwitchView(pedalSwitch)
|
||||
/// ```
|
||||
public struct PedalSwitchView: View {
|
||||
|
||||
// MARK: - Layout constants
|
||||
|
||||
private static let cornerRadius: CGFloat = 6
|
||||
private static let segmentSpacing: CGFloat = 3
|
||||
private static let labelSpacing: CGFloat = 4
|
||||
private static let segmentPaddingV: CGFloat = 5
|
||||
private static let segmentPaddingH: CGFloat = 6
|
||||
private static let fontSize: CGFloat = 11
|
||||
private static let captionFontSize: CGFloat = 11
|
||||
private static let activeFillOpacity: Double = 0.88
|
||||
private static let inactiveFillOpacity: Double = 0.16
|
||||
private static let borderOpacity: Double = 0.35
|
||||
private static let borderLineWidth: CGFloat = 1
|
||||
private static let activeTextBrightness: Double = 0.12
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let pedalSwitch: PedalSwitch
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a segmented switch view.
|
||||
///
|
||||
/// - Parameter pedalSwitch: The discrete switch descriptor to render.
|
||||
public init(_ pedalSwitch: PedalSwitch) {
|
||||
self.pedalSwitch = pedalSwitch
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
// Reading `node.value` registers this view for @Observable re-render on change.
|
||||
let current: Double = pedalSwitch.node.value
|
||||
let selectedIndex: Int = nearestIndex(to: current)
|
||||
|
||||
VStack(spacing: PedalSwitchView.labelSpacing) {
|
||||
HStack(spacing: PedalSwitchView.segmentSpacing) {
|
||||
ForEach(Array(pedalSwitch.options.enumerated()), id: \.offset) { pair in
|
||||
let option: PedalSwitch.Option = pair.element
|
||||
let isActive: Bool = pair.offset == selectedIndex
|
||||
segment(option: option, isActive: isActive)
|
||||
}
|
||||
}
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: PedalSwitchView.cornerRadius)
|
||||
.stroke(Color.black.opacity(PedalSwitchView.borderOpacity),
|
||||
lineWidth: PedalSwitchView.borderLineWidth)
|
||||
)
|
||||
|
||||
Text(pedalSwitch.label)
|
||||
.font(.system(size: PedalSwitchView.captionFontSize))
|
||||
.foregroundStyle(Color.white)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Subviews
|
||||
|
||||
private func segment(option: PedalSwitch.Option, isActive: Bool) -> some View {
|
||||
Button(action: {
|
||||
pedalSwitch.node.update(option.value)
|
||||
}) {
|
||||
Text(option.title)
|
||||
.font(.system(size: PedalSwitchView.fontSize, weight: .semibold))
|
||||
.foregroundStyle(isActive
|
||||
? Color(white: PedalSwitchView.activeTextBrightness)
|
||||
: Color.white)
|
||||
.padding(.vertical, PedalSwitchView.segmentPaddingV)
|
||||
.padding(.horizontal, PedalSwitchView.segmentPaddingH)
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: PedalSwitchView.cornerRadius)
|
||||
.fill(Color.white.opacity(isActive
|
||||
? PedalSwitchView.activeFillOpacity
|
||||
: PedalSwitchView.inactiveFillOpacity))
|
||||
)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
/// Index of the option whose `value` is nearest `value` — the lit segment.
|
||||
private func nearestIndex(to value: Double) -> Int {
|
||||
var bestIndex: Int = 0
|
||||
var bestDistance: Double = .greatestFiniteMagnitude
|
||||
for index: Int in pedalSwitch.options.indices {
|
||||
let distance: Double = abs(pedalSwitch.options[index].value - value)
|
||||
if distance < bestDistance {
|
||||
bestDistance = distance
|
||||
bestIndex = index
|
||||
}
|
||||
}
|
||||
return bestIndex
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user