Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift
T
josh 4d8464282e Add per-binding CC invert toggle
Each CC binding gains an "Invert" toggle in the mapping sheet (shown once the
control is bound). Inverting swaps the sweep — CC 0 drives the control to its
max, 127 to its min — so a single expression pedal can crossfade two controls:
map both to the same CC and invert one.

The MIDICCBinding.inverted field and applyCC already honored inversion; this
adds isCCInverted / setCCInverted plus the sheet toggle, and the binding label
now notes "· inverted".

Test: an inverted binding reverses the mapped output. 321 green; both app
targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 13:09:18 -07:00

256 lines
9.9 KiB
Swift

// MIDIMappingSheet.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdShoeGaze
import LyrebirdUI
/// The MIDI mapping panel: shows connected devices and lets the user bind expression-pedal CCs to
/// pedal knobs via MIDI Learn.
///
/// Presented from the transport-row ``MIDIIndicatorView``. Rows are generated by walking the live
/// `ShoeGaze.boards` / `board.pedals` and each pedal's controls, so bindings address controls
/// positionally. A Switches section binds Program Change to toggle actions; an Expression section
/// binds Control Change to continuous pedal controls.
public struct MIDIMappingSheet: View {
// MARK: - Constants
private static let sheetMinWidth: CGFloat = 460
private static let sheetMinHeight: CGFloat = 520
private static let rowVerticalPadding: CGFloat = 2
// MARK: - Inputs
private let controller: MIDIController
private let shoeGaze: ShoeGaze
@Environment(\.dismiss) private var dismiss
/// Creates a MIDI mapping sheet.
///
/// - Parameters:
/// - controller: The MIDI controller that owns the connection, learn state, and bindings.
/// - shoeGaze: The rig whose boards / pedals / knobs are enumerated as binding targets.
public init(controller: MIDIController, shoeGaze: ShoeGaze) {
self.controller = controller
self.shoeGaze = shoeGaze
}
// MARK: - Body
public var body: some View {
NavigationStack {
List {
devicesSection
switchesSection
expressionSection
}
.navigationTitle("MIDI Mapping")
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") {
controller.cancelLearn()
dismiss()
}
}
}
}
.frame(minWidth: MIDIMappingSheet.sheetMinWidth, minHeight: MIDIMappingSheet.sheetMinHeight)
}
// MARK: - Devices
private var devicesSection: some View {
Section("Devices") {
if controller.isConnected {
ForEach(controller.connectedDeviceNames, id: \.self) { name in
Label(name, systemImage: "pianokeys")
.foregroundStyle(Color.green)
}
} else {
Label("No MIDI device detected", systemImage: "pianokeys.inverse")
.foregroundStyle(.secondary)
}
}
}
// MARK: - Row models
/// A Program-Change switch row: a toggle action + its display label. `id` is globally unique so
/// SwiftUI never reuses a row view across the two sections (which would leak the wrong Learn
/// action into a row).
private struct SwitchRowModel: Identifiable {
let id: String
let action: MIDIMap.Action
let label: String
}
/// A Control-Change row: a continuous target (address-only, hints `nil`) + its display label.
private struct CCRowModel: Identifiable {
let id: String
let target: MIDIMap.CCTarget
let label: String
}
private var switchRowModels: [SwitchRowModel] {
var rows: [SwitchRowModel] = [
SwitchRowModel(id: "sw-rig", action: .rigBypass, label: "Rig Bypass"),
SwitchRowModel(id: "sw-all", action: .allBoards, label: "All Boards On"),
]
for boardPair in shoeGaze.boards.enumerated() {
let boardIndex: Int = boardPair.offset
let board: PedalBoard = boardPair.element
rows.append(SwitchRowModel(id: "sw-solo-\(boardIndex)",
action: .boardSolo(boardIndex: boardIndex),
label: "\(board.label) · Solo"))
rows.append(SwitchRowModel(id: "sw-aud-\(boardIndex)",
action: .boardAudible(boardIndex: boardIndex),
label: "\(board.label) · Audible"))
rows.append(SwitchRowModel(id: "sw-byp-\(boardIndex)",
action: .boardBypass(boardIndex: boardIndex),
label: "\(board.label) · Bypass"))
for pedalPair in board.pedals.enumerated() {
rows.append(SwitchRowModel(
id: "sw-pb-\(boardIndex)-\(pedalPair.offset)",
action: .pedalBypass(boardIndex: boardIndex, pedalIndex: pedalPair.offset),
label: "\(board.label) · \(pedalPair.element.displayName) · Bypass"))
}
}
return rows
}
private var ccRowModels: [CCRowModel] {
var rows: [CCRowModel] = []
for boardPair in shoeGaze.boards.enumerated() {
let boardIndex: Int = boardPair.offset
let board: PedalBoard = boardPair.element
// Board master level (the board fader).
rows.append(CCRowModel(
id: "cc-blvl-\(boardIndex)",
target: .boardLevel(boardIndex: boardIndex, boardLabelHint: nil),
label: "\(board.label) · Level"))
for pedalPair in board.pedals.enumerated() {
let pedalIndex: Int = pedalPair.offset
let pedal: Pedal = pedalPair.element
for controlPair in pedal.ccMappableControls.enumerated() {
rows.append(CCRowModel(
id: "cc-\(boardIndex)-\(pedalIndex)-\(controlPair.offset)",
target: .pedalControl(
boardIndex: boardIndex,
pedalIndex: pedalIndex,
controlIndex: controlPair.offset,
pedalNameHint: nil,
controlLabelHint: nil),
label: "\(pedal.displayName) · \(controlPair.element.label)"))
}
}
}
return rows
}
// MARK: - Switches (Program Change)
private var switchesSection: some View {
Section("Switches (Program Change)") {
ForEach(switchRowModels) { row in
switchRow(row)
}
}
}
private func switchRow(_ row: SwitchRowModel) -> some View {
let isArmed: Bool = controller.learnTarget == .action(row.action)
let binding: MIDIMap.SwitchBinding? = controller.switchBinding(for: row.action)
return HStack {
VStack(alignment: .leading, spacing: 1) {
Text(row.label)
Text(switchBindingLabel(isArmed: isArmed, binding: binding))
.font(.caption)
.foregroundStyle(isArmed ? Color.orange : .secondary)
}
Spacer()
if binding != nil {
Button("Clear") { controller.clearSwitchBinding(for: row.action) }
.buttonStyle(.borderless)
}
Button(isArmed ? "Cancel" : "Learn") {
if isArmed {
controller.cancelLearn()
} else {
controller.armSwitchLearn(row.action)
}
}
.buttonStyle(.bordered)
.tint(.orange)
}
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
}
private func switchBindingLabel(isArmed: Bool, binding: MIDIMap.SwitchBinding?) -> String {
if isArmed { return "Listening — press a footswitch…" }
guard let binding: MIDIMap.SwitchBinding = binding else { return "Unassigned" }
let channelText: String = binding.channel.map { " · ch \($0 + 1)" } ?? ""
return "PC \(binding.program)\(channelText)"
}
// MARK: - Expression (CC)
private var expressionSection: some View {
Section("Expression (Control Change)") {
if shoeGaze.boards.isEmpty {
Text("Add a pedal to assign expression control.")
.foregroundStyle(.secondary)
}
ForEach(ccRowModels) { row in
ccRow(row)
}
}
}
private func ccRow(_ row: CCRowModel) -> some View {
let isArmed: Bool = controller.learnTarget == .cc(row.target)
let binding: MIDIMap.CCBinding? = controller.ccBinding(for: row.target)
return HStack {
VStack(alignment: .leading, spacing: 1) {
Text(row.label)
Text(ccBindingLabel(isArmed: isArmed, binding: binding))
.font(.caption)
.foregroundStyle(isArmed ? Color.orange : .secondary)
}
Spacer()
if binding != nil {
Toggle("Invert", isOn: Binding(
get: { controller.isCCInverted(for: row.target) },
set: { controller.setCCInverted(for: row.target, $0) }
))
.toggleStyle(.button)
.tint(.orange)
Button("Clear") { controller.clearCCBinding(for: row.target) }
.buttonStyle(.borderless)
}
Button(isArmed ? "Cancel" : "Learn") {
if isArmed {
controller.cancelLearn()
} else {
controller.armCCLearn(row.target)
}
}
.buttonStyle(.bordered)
.tint(.orange)
}
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
}
private func ccBindingLabel(isArmed: Bool, binding: MIDIMap.CCBinding?) -> String {
if isArmed { return "Listening — move an expression pedal…" }
guard let binding: MIDIMap.CCBinding = binding else { return "Unassigned" }
let channelText: String = binding.channel.map { " · ch \($0 + 1)" } ?? ""
let invertText: String = binding.inverted ? " · inverted" : ""
return "CC \(binding.controller)\(channelText)\(invertText)"
}
}