Files
VCCPlusOSS/platforms/macos/App/InputControlsView.swift

159 lines
9.7 KiB
Swift
Raw Permalink 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-License-Identifier: GPL-3.0-or-later
import SwiftUI
struct InputControlsView: View {
@ObservedObject var input: InputHub
@State private var expandedPort: Int?
@State private var showControllerTest = false
private var keyboardDescription: String {
switch input.settings.keyboard {
case .natural: return "Follows the characters you type on your Mac."
case .coco: return "Uses physical US CoCo key positions."
case .compact: return "Adds Tab for Clear and the grave key for Break."
}
}
var body: some View {
VStack(alignment: .leading, spacing: 0) {
SecondaryHeader(title: "Keyboard and Joysticks", badge: "joystick", subtitle: "Changes apply immediately and are remembered.")
.padding(20)
Divider()
ScrollView {
VStack(alignment: .leading, spacing: 16) {
SecondaryCard {
Label("Keyboard", systemImage: "keyboard").font(.headline)
Picker("Keyboard mode", selection: $input.settings.keyboard) {
ForEach(KeyboardMode.allCases, id: \.self) { Text($0.rawValue).tag($0) }
}.accessibilityIdentifier("input-keyboard")
Text(keyboardDescription + " Command shortcuts stay with the Mac.")
.font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
}
HStack {
Label("CoCo joystick ports", systemImage: "gamecontroller").font(.headline)
Spacer()
Button(action: input.swap) { Label("Swap assignments", systemImage: "arrow.left.arrow.right") }
.accessibilityIdentifier("input-swap")
.help("Swap the right and left CoCo port assignments, including their options.")
}
// Display left before right; hardware port 0 remains the right joystick.
ForEach([1, 0], id: \.self) { port in portCard(port) }
SecondaryCard {
DisclosureGroup(isExpanded: $showControllerTest) {
controllerTest.padding(.top, 8)
} label: {
HStack {
Label("Controller Test", systemImage: "gamecontroller").font(.headline)
Spacer()
Text(input.devices.isEmpty ? "None connected" : "\(input.devices.count) connected")
.font(.caption).foregroundStyle(.secondary)
}
}
}
SecondaryHelp(title: "Using keyboard, mouse and controller input") {
Text("Click the CoCo display to play. Keyboard joystick keys are reserved for the selected port. Mouse movement uses the display area; left and right clicks are Fire 1 and Fire 2.")
Text("For Logitech F310, use the underside D switch and reconnect USB. Standard gamepads use the left stick or D-pad. Controller Test shows button numbers as you press them; select those numbers for Fire 1 and Fire 2.")
Text("A remembered disconnected controller stays assigned and sends neutral input until it reconnects.")
}
if let error = input.error {
Label(error, systemImage: "exclamationmark.triangle").foregroundStyle(.red)
.fixedSize(horizontal: false, vertical: true)
}
}.padding(20)
}
}.frame(minWidth: 620, minHeight: 540)
}
private func buttons(_ value: UInt32) -> String {
let pressed = (1...16).filter { value & (1 << ($0 - 1)) != 0 }.map(String.init)
return pressed.isEmpty ? "none" : pressed.joined(separator: ", ")
}
private func portSummary(_ port: Int) -> String {
let config = input.settings[port]
switch config.source {
case .none: return "No input assigned"
case .keyboard: return config.keys.rawValue + " · " + config.interface.label
case .mouse: return "Display pointer · " + config.interface.label
case .controller:
let name = config.device.isEmpty ? "Choose a controller in Options" :
input.devices.first { $0.id == config.device }?.name ?? "Remembered controller — disconnected"
return name + " · " + config.interface.label
}
}
private func portCard(_ port: Int) -> some View {
let config = Binding<PortSettings>(get: { input.settings[port] }, set: { input.settings[port] = $0 })
return SecondaryCard {
HStack(alignment: .firstTextBaseline) {
VStack(alignment: .leading, spacing: 3) {
Text(port == 0 ? "Right CoCo joystick" : "Left CoCo joystick").font(.headline)
Text(port == 0 ? "JOYSTK 0–1" : "JOYSTK 2–3").font(.caption).foregroundStyle(.secondary)
}
Spacer()
Picker("Input source", selection: config.source) {
ForEach(JoystickSource.allCases, id: \.self) { Text($0.rawValue).tag($0) }
}.labelsHidden().frame(width: 180).accessibilityLabel(port == 0 ? "Right joystick input source" : "Left joystick input source")
.accessibilityIdentifier("input-source-\(port)")
}
Text(portSummary(port)).font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
if config.wrappedValue.source != .none {
DisclosureGroup(isExpanded: Binding(get: { expandedPort == port }, set: { expandedPort = $0 ? port : nil })) {
portOptions(port, config: config).padding(.top, 8)
} label: { Label("Options", systemImage: "slider.horizontal.3") }
.accessibilityIdentifier("input-options-\(port)")
Text("X: \(input.preview[port].x >> 8) Y: \(input.preview[port].y >> 8) Fire 1: \(input.preview[port].buttons & 1 == 0 ? "up" : "down") Fire 2: \(input.preview[port].buttons & 2 == 0 ? "up" : "down")")
.font(.system(.caption, design: .monospaced))
.accessibilityIdentifier("input-port-preview-\(port)")
}
}.onChange(of: input.settings[port].source) { _, new in
if new != .none { expandedPort = port }
else if expandedPort == port { expandedPort = nil }
}
}
@ViewBuilder private func portOptions(_ port: Int, config: Binding<PortSettings>) -> some View {
VStack(alignment: .leading, spacing: 12) {
if config.wrappedValue.source == .keyboard {
Picker("Keys", selection: config.keys) {
ForEach(JoystickKeys.allCases, id: \.self) { Text($0.rawValue).tag($0) }
}
}
if config.wrappedValue.source == .controller {
Picker("Controller", selection: config.device) {
Text("Choose a controller").tag("")
if !config.wrappedValue.device.isEmpty && !input.devices.contains(where: { $0.id == config.wrappedValue.device }) {
Text("Remembered controller — disconnected").tag(config.wrappedValue.device)
}
ForEach(input.devices) { Text($0.name).tag($0.id) }
}.accessibilityIdentifier("input-controller-\(port)")
Toggle("Use right stick", isOn: config.rightStick)
HStack {
Picker("Fire 1", selection: config.fire1) { ForEach(1...16, id: \.self) { Text("Button \($0)").tag($0) } }
Picker("Fire 2", selection: config.fire2) { ForEach(1...16, id: \.self) { Text("Button \($0)").tag($0) } }
}
HStack {
Text("Dead zone")
Slider(value: config.deadZone, in: 0...0.5).accessibilityLabel("\(port == 0 ? "Right" : "Left") joystick dead zone")
Text("\(Int(config.wrappedValue.deadZone * 100))%").monospacedDigit().frame(width: 38, alignment: .trailing)
}
}
Picker("CoCo interface", selection: config.interface) {
ForEach(JoystickInterface.allCases, id: \.self) { Text($0.label).tag($0) }
}
}
}
private var controllerTest: some View {
VStack(alignment: .leading, spacing: 12) {
if input.devices.isEmpty {
Text("Connect a supported controller to see its sticks and button numbers.").foregroundStyle(.secondary)
}
ForEach(input.devices) { device in
VStack(alignment: .leading, spacing: 4) {
Text(device.name).font(.headline)
Text("Left: \(String(format: "% .2f", device.x)), \(String(format: "% .2f", device.y))\nRight: \(String(format: "% .2f", device.rx)), \(String(format: "% .2f", device.ry))\nButtons: \(buttons(device.buttons))")
.font(.system(.caption, design: .monospaced)).fixedSize(horizontal: false, vertical: true)
.accessibilityLabel("Controller preview: \(device.name), X \(device.x), Y \(device.y), buttons \(buttons(device.buttons))")
.accessibilityIdentifier("controller-preview-\(device.id)")
}
}
Text("Move either stick and press buttons to identify them. This preview does not change either port assignment.")
.font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
}
}
}