Files
sizzletracker-android/ios/app/Host/Input/InputAction.swift
T
ReactorcoremeltdownandClaude Opus 5.5 64e21e0bcc iOS 0.6.0: milestone 5 (recording, MIDI, input bindings, themes, bundles, backups)
- Master recording (MasterSink ring + writer thread) and the sample recorder
- CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports;
  platform-free parser/module/clock in core (midi_test)
- Hardware keyboard and gamepads via GameController; rebindable keyboard,
  gamepad and MIDI CC actions; per-parameter MIDI learn
- Colour themes (.szt), project bundles (.szgz), backup & restore;
  ZIP in core with zlib (zip_test against jar and unzip)
- Persistent app settings; audio pauses in the background unless sound module

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:26:01 +01:00

109 lines
4.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
import Foundation
/// Android's InputAction: every hardware source (keyboard, gamepad, MIDI) becomes
/// one of these, so a feature is written once (AppModel+Input) and works from all.
enum InputAction: Equatable {
case navUp, navDown, navLeft, navRight
case nextColumn, prevColumn
case increment(Int), decrement(Int)
case clearCell, noteOffCell
/// [channel] 1..16 for MIDI, -1 for a source without one (the keyboard's piano);
/// [sounded]: the MIDI sound module already played it (only show / punch it in).
case noteOn(pitch: Int, velocity: Int, channel: Int, sounded: Bool)
case noteOff(pitch: Int, channel: Int, sounded: Bool)
case playPause, stop, toggleLoop
case selectTab(Int), nextTab, prevTab
/// An unbound control (a MIDI CC, a key or a gamepad chord during learn).
/// [modifier]: the held gamepad control of a chord, 0 for none.
case raw(source: InputSource, code: Int, value: Float, modifier: Int)
/// Cursor and value steps repeat while their control is held.
var isRepeatable: Bool {
switch self {
case .navUp, .navDown, .navLeft, .navRight, .nextColumn, .prevColumn, .increment, .decrement: return true
default: return false
}
}
}
enum InputSource { case touch, keyboard, gamepad, midi }
/// Android's InputActions: the stable action ids the binding maps store, and what
/// each one does. Settings lists them in this order.
enum InputActions {
static let all: [(id: String, label: String)] = [
("playPause", "Play / Pause"), ("stop", "Stop"), ("loop", "Loop on/off"),
("up", "Cursor up"), ("down", "Cursor down"), ("left", "Cursor left"), ("right", "Cursor right"),
("nextColumn", "Next column"), ("prevColumn", "Previous column"),
("increment", "Value +1"), ("decrement", "Value −1"), ("clear", "Clear cell"), ("noteOff", "Note-off"),
("nextTab", "Next tab"), ("prevTab", "Previous tab"),
]
static func action(_ id: String) -> InputAction? {
switch id {
case "playPause": return .playPause
case "stop": return .stop
case "loop": return .toggleLoop
case "up": return .navUp
case "down": return .navDown
case "left": return .navLeft
case "right": return .navRight
case "increment": return .increment(1)
case "decrement": return .decrement(1)
case "clear": return .clearCell
case "noteOff": return .noteOffCell
case "nextColumn": return .nextColumn
case "prevColumn": return .prevColumn
case "nextTab": return .nextTab
case "prevTab": return .prevTab
default: return nil
}
}
}
/// Android's InputRouter: one dispatch point, the shared accelerating
/// hold-to-repeat (320 ms, ×0.8 per step, down to 45 ms) and the learn state.
@MainActor
final class InputRouter: ObservableObject {
static let shared = InputRouter()
/// Set by AppModel: what happens with each action.
var handler: ((InputAction) -> Void)?
/// While set, the next control of this source is captured as a binding.
@Published var learnSource: InputSource?
private var repeatKey: AnyHashable?
private var repeatTimer: Timer?
func dispatch(_ a: InputAction) { handler?(a) }
/// Fire [action] for a press and, if it repeats, keep firing while [key] is held.
func pressRepeat(_ key: AnyHashable, _ action: InputAction) {
dispatch(action)
guard action.isRepeatable else { return }
repeatTimer?.invalidate()
repeatKey = key
schedule(action, after: 0.32)
}
private func schedule(_ action: InputAction, after interval: TimeInterval) {
repeatTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false) { [weak self] _ in
Task { @MainActor in
guard let self = self, self.repeatKey != nil else { return }
self.dispatch(action)
self.schedule(action, after: max(0.045, interval * 0.8))
}
}
}
func releaseRepeat(_ key: AnyHashable) {
guard repeatKey == key else { return }
repeatTimer?.invalidate()
repeatTimer = nil
repeatKey = nil
}
}