Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/PedalView.swift
T
josh b564b154a0 Flank bypass switch with Level and Mix for a symmetric pedal footer
Move the Mix knob out of the knob grid into the bottom control row, to the
right of the bypass footswitch, mirroring Level on the left. Every pedal
now shares a consistent knob-switch-knob footer. The knob grid drops the
trailing Mix and Level entries (was just Level).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-21 15:53:57 -07:00

400 lines
16 KiB
Swift

// PedalView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdUI
import LyrebirdShoeGaze
// MARK: - Accent-color mapping (UI-only, never in the core target)
/// Returns a stompbox enclosure tint colour keyed on the pedal's display name.
///
/// The core `Pedal` type cannot carry a SwiftUI `Color`, so the mapping lives
/// here in the UI target. Unrecognised names fall through to `.gray`.
private func enclosureColor(for displayName: String) -> Color {
switch displayName {
case "Overdrive": return Color(red: 0.85, green: 0.42, blue: 0.08) // warm orange
case "Delay": return Color(red: 0.10, green: 0.55, blue: 0.55) // teal
case "Reverb": return Color(red: 0.30, green: 0.18, blue: 0.65) // indigo
case "Modulation": return Color(red: 0.12, green: 0.58, blue: 0.22) // green
case "Fuzz": return Color(red: 0.55, green: 0.10, blue: 0.10) // deep crimson
case "Tremolo": return Color(red: 0.20, green: 0.38, blue: 0.72) // cobalt blue
case "Phaser": return Color(red: 0.60, green: 0.22, blue: 0.62) // violet
case "Compressor": return Color(red: 0.18, green: 0.42, blue: 0.32) // forest green
default: return Color(red: 0.36, green: 0.36, blue: 0.38) // steel gray
}
}
// MARK: - Screw decoration
/// A small circular "screw head" decoration placed in each corner of the enclosure.
private struct ScrewDot: View {
private static let size: CGFloat = 8
private static let strokeWidth: CGFloat = 1
private static let slotWidth: CGFloat = 1
private static let slotHeight: CGFloat = 4
private static let fillOpacity: Double = 0.25
private static let rimOpacity: Double = 0.18
private static let slotOpacity: Double = 0.50
var body: some View {
ZStack {
Circle()
.fill(Color.black.opacity(ScrewDot.fillOpacity))
.frame(width: ScrewDot.size, height: ScrewDot.size)
Circle()
.stroke(Color.white.opacity(ScrewDot.rimOpacity), lineWidth: ScrewDot.strokeWidth)
.frame(width: ScrewDot.size, height: ScrewDot.size)
// Slot line — Phillips-style cross
Rectangle()
.fill(Color.black.opacity(ScrewDot.slotOpacity))
.frame(width: ScrewDot.slotWidth, height: ScrewDot.slotHeight)
Rectangle()
.fill(Color.black.opacity(ScrewDot.slotOpacity))
.frame(width: ScrewDot.slotHeight, height: ScrewDot.slotWidth)
}
}
}
// MARK: - LED
/// A small circular LED that glows green when the effect is engaged.
///
/// Bypass polarity: `0` = engaged (effect ON), `1` = bypassed.
/// The LED is lit when `bypassValue < 0.5`.
private struct LEDIndicator: View {
private static let diameter: CGFloat = 10
private static let glowRadius: CGFloat = 6
private static let glowOpacity: Double = 0.75
private static let rimLineWidth: CGFloat = 0.5
private static let dimBrightness: Double = 0.15
private static let rimStrokeOpacity: Double = 0.30
let bypassValue: Double
private var isEngaged: Bool { bypassValue < 0.5 }
var body: some View {
ZStack {
if isEngaged {
Circle()
.fill(Color.green.opacity(LEDIndicator.glowOpacity))
.frame(
width: LEDIndicator.diameter + LEDIndicator.glowRadius,
height: LEDIndicator.diameter + LEDIndicator.glowRadius
)
.blur(radius: LEDIndicator.glowRadius / 2)
}
Circle()
.fill(isEngaged ? Color.green : Color(white: LEDIndicator.dimBrightness))
.frame(width: LEDIndicator.diameter, height: LEDIndicator.diameter)
.overlay(
Circle()
.stroke(Color.white.opacity(LEDIndicator.rimStrokeOpacity), lineWidth: LEDIndicator.rimLineWidth)
.frame(width: LEDIndicator.diameter, height: LEDIndicator.diameter)
)
}
.frame(width: LEDIndicator.diameter + LEDIndicator.glowRadius,
height: LEDIndicator.diameter + LEDIndicator.glowRadius)
}
}
// MARK: - Footswitch
/// A round footswitch-style button that toggles bypass on/off.
private struct FootswitchButton: View {
private static let diameter: CGFloat = 44
private static let strokeWidth: CGFloat = 2.5
private static let innerDiameter: CGFloat = 36
private static let outerFillBrightness: Double = 0.18
private static let ringBrightness: Double = 0.50
private static let innerFillBrightness: Double = 0.25
let node: UIParamNode
var body: some View {
Button(action: {
let engaged: Bool = node.value < 0.5
// Toggle: engaged (0) → bypassed (1) and vice-versa.
node.update(engaged ? 1.0 : 0.0)
}) {
ZStack {
Circle()
.fill(Color(white: FootswitchButton.outerFillBrightness))
.frame(width: FootswitchButton.diameter, height: FootswitchButton.diameter)
Circle()
.stroke(Color(white: FootswitchButton.ringBrightness),
lineWidth: FootswitchButton.strokeWidth)
.frame(width: FootswitchButton.diameter, height: FootswitchButton.diameter)
Circle()
.fill(Color(white: FootswitchButton.innerFillBrightness))
.frame(width: FootswitchButton.innerDiameter, height: FootswitchButton.innerDiameter)
}
}
.buttonStyle(.plain)
.frame(width: FootswitchButton.diameter, height: FootswitchButton.diameter)
}
}
// MARK: - PedalView
/// A stompbox-style enclosure view that renders all controls for a single `Pedal`.
///
/// The enclosure shows:
/// - Corner screw decorations.
/// - A contrasting name-plate capsule near the top with `displayName`.
/// - A grid of ``ParameterKnob`` controls (up to ``knobColumnCount`` per row) drawn from
/// the effect + `Mix` knobs of ``Pedal/knobs``.
/// - A centred bottom row pairing the `Level` knob with the bypass switch (LED + footswitch).
///
/// ## Bypass LED polarity
///
/// `Pedal.bypass` is `0` = engaged (effect active), `1` = bypassed.
/// The LED is **green/lit** when `bypass < 0.5` (effect is on) and dark when bypassed.
///
/// ## Accent color
///
/// The enclosure tint is assigned by `enclosureColor(for:)` — a private helper in
/// this file keyed on `displayName`. Core `Pedal` never imports SwiftUI.
///
/// ## Usage
///
/// ```swift
/// PedalView(pedal: overdrivePedal)
/// ```
public struct PedalView: View {
// MARK: - Layout constants
/// Wide enough for three ~55 pt knobs (dial + label + value) plus padding in a
/// single row, so the effect controls never overlap.
private static let enclosureWidth: CGFloat = 220
private static let enclosureCornerRadius: CGFloat = 12
private static let enclosurePadding: CGFloat = 14
private static let screwInset: CGFloat = 6
private static let knobColumnCount: Int = 3
private static let knobSpacing: CGFloat = 10
/// Horizontal gap between the Level knob and the bypass control in the bottom row.
private static let bottomControlsSpacing: CGFloat = 18
private static let sectionSpacing: CGFloat = 14
private static let namePlateCornerRadius: CGFloat = 8
private static let namePlatePaddingH: CGFloat = 10
private static let namePlatePaddingV: CGFloat = 4
private static let outerBorderOpacity: Double = 0.60
private static let innerHighlightOpacity: Double = 0.22
private static let shadowRadius: CGFloat = 8
private static let shadowYOffset: CGFloat = 4
private static let shadowOpacity: Double = 0.60
private static let enclosureGradientDarkFraction: Double = 0.30
private static let bypassSectionSpacing: CGFloat = 6
/// Half the screw-dot size — used to centre screws on the corner position.
private static let screwHalfSize: CGFloat = 4
private static let namePlateBackgroundOpacity: Double = 0.35
private static let fontSizeNamePlate: CGFloat = 13
private static let outerBorderLineWidth: CGFloat = 1.5
private static let innerHighlightLineWidth: CGFloat = 1
private static let innerHighlightPadding: CGFloat = 1
private static let shadowXOffset: CGFloat = 0
// MARK: - Properties
private let pedal: Pedal
// MARK: - Init
/// Creates a stompbox pedal view for the given pedal.
///
/// - Parameter pedal: The `Pedal` whose parameters will be displayed.
public init(pedal: Pedal) {
self.pedal = pedal
}
// MARK: - Body
public var body: some View {
let accent: Color = enclosureColor(for: pedal.displayName)
let darkAccent: Color = accent.opacity(PedalView.enclosureGradientDarkFraction)
ZStack(alignment: .topLeading) {
// Enclosure fill with top-to-bottom gradient
RoundedRectangle(cornerRadius: PedalView.enclosureCornerRadius)
.fill(
LinearGradient(
gradient: Gradient(colors: [accent, darkAccent]),
startPoint: .top,
endPoint: .bottom
)
)
// Outer border
RoundedRectangle(cornerRadius: PedalView.enclosureCornerRadius)
.stroke(Color.black.opacity(PedalView.outerBorderOpacity),
lineWidth: PedalView.outerBorderLineWidth)
// Inner highlight rim (subtle bevel)
RoundedRectangle(cornerRadius: PedalView.enclosureCornerRadius - PedalView.innerHighlightPadding)
.stroke(Color.white.opacity(PedalView.innerHighlightOpacity),
lineWidth: PedalView.innerHighlightLineWidth)
.padding(PedalView.innerHighlightPadding)
// Content stack
VStack(spacing: PedalView.sectionSpacing) {
namePlate
knobGrid
switchRow
Spacer(minLength: 0)
bottomControls
}
.padding(PedalView.enclosurePadding)
// Corner screws — overlaid at the four corners
screwCorners
}
.frame(width: PedalView.enclosureWidth)
.shadow(
color: Color.black.opacity(PedalView.shadowOpacity),
radius: PedalView.shadowRadius,
x: PedalView.shadowXOffset,
y: PedalView.shadowYOffset
)
}
// MARK: - Subviews
/// Capsule name plate near the top of the enclosure.
private var namePlate: some View {
Text(pedal.displayName)
.font(.system(size: PedalView.fontSizeNamePlate, weight: .bold, design: .rounded))
.foregroundStyle(Color.white)
.padding(.horizontal, PedalView.namePlatePaddingH)
.padding(.vertical, PedalView.namePlatePaddingV)
.background(Color.black.opacity(PedalView.namePlateBackgroundOpacity))
.clipShape(RoundedRectangle(cornerRadius: PedalView.namePlateCornerRadius))
}
/// Grid of the effect knobs, in rows of up to `knobColumnCount`.
///
/// The trailing `Mix` and `Level` knobs (the last two entries of `pedal.knobs`) are
/// dropped here and rendered in the bottom control row flanking the bypass switch —
/// `Level` on the left, `Mix` on the right — leaving more room for the effect controls
/// at the top and giving every pedal a symmetric footer.
private var knobGrid: some View {
let knobs: [PedalKnob] = Array(pedal.knobs.dropLast(2))
let rows: [[PedalKnob]] = stride(
from: 0,
to: knobs.count,
by: PedalView.knobColumnCount
).map { startIndex in
Array(knobs[startIndex ..< min(startIndex + PedalView.knobColumnCount, knobs.count)])
}
return VStack(spacing: PedalView.knobSpacing) {
ForEach(Array(rows.enumerated()), id: \.offset) { rowPair in
let row: [PedalKnob] = rowPair.element
HStack(spacing: PedalView.knobSpacing) {
ForEach(Array(row.enumerated()), id: \.offset) { knobPair in
let knob: PedalKnob = knobPair.element
ParameterKnob(knob.label, node: knob.node, range: knob.range)
.foregroundStyle(Color.white)
}
// Pad short rows so knobs stay aligned in their columns.
if row.count < PedalView.knobColumnCount {
ForEach(0 ..< (PedalView.knobColumnCount - row.count), id: \.self) { _ in
Spacer()
}
}
}
}
}
}
/// Row of segmented switches drawn from `pedal.effectSwitches`, one per row, beneath
/// the knob grid. Renders nothing when the pedal publishes no switches.
@ViewBuilder
private var switchRow: some View {
let switches: [PedalSwitch] = pedal.effectSwitches
if !switches.isEmpty {
VStack(spacing: PedalView.knobSpacing) {
ForEach(Array(switches.enumerated()), id: \.offset) { pair in
PedalSwitchView(pair.element)
}
}
}
}
/// Bottom control row: the `Level` knob, the bypass switch, and the `Mix` knob,
/// side-by-side and centred — a symmetric knob-switch-knob footer shared by all pedals.
private var bottomControls: some View {
HStack(alignment: .center, spacing: PedalView.bottomControlsSpacing) {
levelControl
bypassControl
mixControl
}
.frame(maxWidth: .infinity)
}
/// The `Level` knob — the last entry of `pedal.knobs` — placed left of the bypass switch.
@ViewBuilder
private var levelControl: some View {
if let level: PedalKnob = pedal.knobs.last {
ParameterKnob(level.label, node: level.node, range: level.range)
.foregroundStyle(Color.white)
}
}
/// The `Mix` knob — the second-to-last entry of `pedal.knobs` — placed right of the
/// bypass switch to mirror `Level`.
@ViewBuilder
private var mixControl: some View {
if let mix: PedalKnob = pedal.knobs.dropLast().last {
ParameterKnob(mix.label, node: mix.node, range: mix.range)
.foregroundStyle(Color.white)
}
}
/// LED + footswitch bypass control, paired beside the Level knob.
private var bypassControl: some View {
// Reading `pedal.$bypass.value` here registers this view for automatic
// re-renders whenever the bypass parameter changes on the main thread
// (UIParamNode is @Observable).
let bypassValue: Double = pedal.$bypass.value
return VStack(spacing: PedalView.bypassSectionSpacing) {
LEDIndicator(bypassValue: bypassValue)
FootswitchButton(node: pedal.$bypass)
}
}
/// Four screw dots overlaid at the enclosure corners.
private var screwCorners: some View {
GeometryReader { geometry in
let w: CGFloat = geometry.size.width
let h: CGFloat = geometry.size.height
let inset: CGFloat = PedalView.screwInset
let half: CGFloat = PedalView.screwHalfSize
ZStack(alignment: .topLeading) {
// Top-left
ScrewDot()
.position(x: inset + half, y: inset + half)
// Top-right
ScrewDot()
.position(x: w - inset - half, y: inset + half)
// Bottom-left
ScrewDot()
.position(x: inset + half, y: h - inset - half)
// Bottom-right
ScrewDot()
.position(x: w - inset - half, y: h - inset - half)
}
}
}
}