Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

iOS shell: shoulder buttons that look like a controller's

v0lumehi4aa863b2126e
diff --git a/frontend-ios/Sources/ShellUI/ButtonTestView.swift b/frontend-ios/Sources/ShellUI/ButtonTestView.swift
index 59c4eb2f..a67c1d61 100644
--- a/frontend-ios/Sources/ShellUI/ButtonTestView.swift
+++ b/frontend-ios/Sources/ShellUI/ButtonTestView.swift
@@ -124,12 +124,14 @@ struct XboxControllerDiagram: View {
}
/// Where each element sits in the design space and how large it is, shared by the drawing, the rings and the hotspots.
+ /// The shoulders' frames are their outlines' bounds (`shoulder`).
nonisolated static func frame(_ e: PadElement) -> (center: CGPoint, size: CGSize) {
- switch e {
- case .leftTrigger: (CGPoint(x: 83, y: 13), CGSize(width: 42, height: 22))
- case .rightTrigger: (CGPoint(x: 237, y: 13), CGSize(width: 42, height: 22))
- case .leftBumper: (CGPoint(x: 88, y: 32), CGSize(width: 72, height: 12))
- case .rightBumper: (CGPoint(x: 232, y: 32), CGSize(width: 72, height: 12))
+ if let outline = shoulder(e) {
+ let r = outline.boundingRect
+ return (CGPoint(x: r.midX, y: r.midY), r.size)
+ }
+ return switch e {
+ case .leftTrigger, .rightTrigger, .leftBumper, .rightBumper: (.zero, .zero)
case .leftStick: (CGPoint(x: 88, y: 92), CGSize(width: 46, height: 46))
case .rightStick: (CGPoint(x: 202, y: 140), CGSize(width: 46, height: 46))
case .up: (CGPoint(x: 118, y: 128), CGSize(width: 12, height: 12))
@@ -146,6 +148,59 @@ struct XboxControllerDiagram: View {
}
}
+ // MARK: The shoulders
+
+ /// A bumper's spine: it runs along the body's upper corner and top edge, a little outside them, so the slab drawn
+ /// round it overhangs the body as a real bumper does. The left side, the right one mirrors it.
+ private nonisolated static let bumperStart = CGPoint(x: 40, y: 55), bumperControl = CGPoint(x: 58, y: 35), bumperEnd = CGPoint(x: 116, y: 41)
+ private nonisolated static let bumperThickness: CGFloat = 13
+ /// A trigger: a paddle above and behind the bumper's outer half, tilted out, rounder on top. The left side.
+ private nonisolated static let triggerCenter = CGPoint(x: 64, y: 14)
+ private nonisolated static let triggerSize = CGSize(width: 34, height: 22)
+ nonisolated static let triggerTilt: CGFloat = -10
+ private nonisolated static let triggerRadii = (top: CGFloat(11), bottom: CGFloat(5))
+
+ nonisolated static func isRight(_ e: PadElement) -> Bool { e == .rightBumper || e == .rightTrigger }
+ private nonisolated static func mirrored(_ p: CGPoint, _ right: Bool) -> CGPoint { right ? CGPoint(x: design.width - p.x, y: p.y) : p }
+
+ /// A shoulder button's outline in the design space, `grow` points wider all round (for the ring and the hit test), nil
+ /// for every other element. The one source of the shoulders' drawing, ring, hotspot and hit test.
+ nonisolated static func shoulder(_ e: PadElement, grow: CGFloat = 0) -> Path? {
+ let right = isRight(e)
+ switch e {
+ case .leftBumper, .rightBumper:
+ var spine = Path()
+ spine.move(to: mirrored(bumperStart, right))
+ spine.addQuadCurve(to: mirrored(bumperEnd, right), control: mirrored(bumperControl, right))
+ return spine.strokedPath(StrokeStyle(lineWidth: bumperThickness + 2 * grow, lineCap: .round))
+ case .leftTrigger, .rightTrigger:
+ let size = CGSize(width: triggerSize.width + 2 * grow, height: triggerSize.height + 2 * grow)
+ let local = UnevenRoundedRectangle(topLeadingRadius: triggerRadii.top + grow, bottomLeadingRadius: triggerRadii.bottom + grow,
+ bottomTrailingRadius: triggerRadii.bottom + grow, topTrailingRadius: triggerRadii.top + grow,
+ style: .continuous)
+ .path(in: CGRect(x: -size.width / 2, y: -size.height / 2, width: size.width, height: size.height))
+ let center = mirrored(triggerCenter, right)
+ let turn = CGAffineTransform(rotationAngle: (right ? -triggerTilt : triggerTilt) * .pi / 180)
+ return local.applying(turn.concatenating(CGAffineTransform(translationX: center.x, y: center.y)))
+ default:
+ return nil
+ }
+ }
+
+ /// Where a shoulder's name sits and how it turns: on the bumper's spine, following its slope, in the trigger's middle.
+ nonisolated static func shoulderLabel(_ e: PadElement) -> (point: CGPoint, angle: CGFloat) {
+ let right = isRight(e)
+ if e == .leftTrigger || e == .rightTrigger { return (mirrored(triggerCenter, right), right ? -triggerTilt : triggerTilt) }
+ // The spine at t = 0.55, and its slope there.
+ let t: CGFloat = 0.55
+ let a = bumperStart, c = bumperControl, b = bumperEnd
+ let p = CGPoint(x: (1 - t) * (1 - t) * a.x + 2 * t * (1 - t) * c.x + t * t * b.x,
+ y: (1 - t) * (1 - t) * a.y + 2 * t * (1 - t) * c.y + t * t * b.y)
+ let dx = 2 * (1 - t) * (c.x - a.x) + 2 * t * (b.x - c.x), dy = 2 * (1 - t) * (c.y - a.y) + 2 * t * (b.y - c.y)
+ let angle = atan2(dy, dx) * 180 / .pi
+ return (mirrored(p, right), right ? -angle : angle)
+ }
+
/// The text on an element: Xbox's names for the test, the Switch names by position on the mapping card.
private func label(_ e: PadElement) -> String {
guard switchNames, let button = PadMap.button(at: e) else { return Self.name(e) }
@@ -176,6 +231,11 @@ struct XboxControllerDiagram: View {
nonisolated static func nearest(to point: CGPoint) -> PadElement? {
let candidates = PadElement.allCases.filter { PadMap.button(at: $0) != nil }
let distances = candidates.map { e -> (PadElement, CGFloat) in
+ // A shoulder by its outline, not its bounds: the bumper's bounds would cover the body under its curve.
+ if shoulder(e) != nil {
+ let reach = stride(from: 0, through: 20, by: 2).first { shoulder(e, grow: CGFloat($0))?.contains(point) == true }
+ return (e, reach.map(CGFloat.init) ?? .infinity)
+ }
let f = frame(e)
// To the element's edge, so a wide bumper is as easy to hit at its end as at its middle.
let dx = max(0, abs(point.x - f.center.x) - f.size.width / 2), dy = max(0, abs(point.y - f.center.y) - f.size.height / 2)
@@ -208,9 +268,11 @@ struct XboxControllerDiagram: View {
let line = max(1.5, 2 * s)
let size = CGSize(width: f.size.width + 7, height: f.size.height + 7)
switch e {
- case .leftBumper, .rightBumper:
- place(Capsule().stroke(ShellPalette.cream, lineWidth: line), center: f.center, size: size, s: s)
- case .leftTrigger, .rightTrigger, .up, .down, .left, .right:
+ case .leftBumper, .rightBumper, .leftTrigger, .rightTrigger:
+ // The shoulder's own outline, 3.5 points out, as far as the other rings sit from their buttons.
+ DesignShape(path: Self.shoulder(e, grow: 3.5) ?? Path()).stroke(ShellPalette.cream, lineWidth: line)
+ .frame(width: Self.design.width * s, height: Self.design.height * s)
+ case .up, .down, .left, .right:
place(RoundedRectangle(cornerRadius: 6 * s).stroke(ShellPalette.cream, lineWidth: line), center: f.center, size: size, s: s)
default:
place(Circle().stroke(ShellPalette.cream, lineWidth: line), center: f.center, size: size, s: s)
@@ -218,14 +280,15 @@ struct XboxControllerDiagram: View {
}
@ViewBuilder private func drawing(s: CGFloat) -> some View {
- trigger(.leftTrigger, value: snapshot.leftTrigger, x: 62, s: s)
- trigger(.rightTrigger, value: snapshot.rightTrigger, x: 216, s: s)
- bumper(.leftBumper, x: 52, s: s)
- bumper(.rightBumper, x: 196, s: s)
+ // Back to front: the triggers behind, the body, the bumpers over the body's edge.
+ trigger(.leftTrigger, value: snapshot.leftTrigger, s: s)
+ trigger(.rightTrigger, value: snapshot.rightTrigger, s: s)
ControllerBody()
- .fill(Color.white.opacity(0.06))
+ .fill(Color(red: 0.11, green: 0.11, blue: 0.12))
.overlay(ControllerBody().stroke(Color.white.opacity(0.35), lineWidth: max(1, 1.6 * s)))
.frame(width: Self.design.width * s, height: Self.design.height * s)
+ bumper(.leftBumper, s: s)
+ bumper(.rightBumper, s: s)
stick(.leftStick, at: CGPoint(x: 88, y: 92), offset: snapshot.leftStick, s: s)
stick(.rightStick, at: CGPoint(x: 202, y: 140), offset: snapshot.rightStick, s: s)
dpad(center: CGPoint(x: 118, y: 140), s: s)
@@ -247,29 +310,48 @@ struct XboxControllerDiagram: View {
.position(x: center.x * s, y: center.y * s)
}
- private func trigger(_ e: PadElement, value: Double, x: CGFloat, s: CGFloat) -> some View {
- let on = held(e) || value > 0.05
- let shape = UnevenRoundedRectangle(topLeadingRadius: 10 * s, bottomLeadingRadius: 3 * s, bottomTrailingRadius: 3 * s, topTrailingRadius: 10 * s)
- return place(
- ZStack(alignment: .bottom) {
- shape.fill(Color.white.opacity(0.06))
- shape.fill(ShellPalette.cream.opacity(0.9)).scaleEffect(x: 1, y: max(0, min(1, held(e) && value < 0.05 ? 1 : value)), anchor: .bottom)
- shape.stroke(Color.white.opacity(on ? 0.9 : 0.35), lineWidth: max(1, 1.4 * s))
- Text(label(e)).font(ShellFonts.fixed(9 * s, weight: .bold)).foregroundStyle(on ? .black : .white.opacity(0.7))
- .frame(maxHeight: .infinity)
- },
- center: CGPoint(x: x + 21, y: 13), size: CGSize(width: 42, height: 22), s: s)
+ /// The colour of a shoulder at rest: opaque, a step lighter than the body, so a bumper reads in front of the body's edge.
+ private static let shoulderIdle = Color(red: 0.17, green: 0.17, blue: 0.18)
+
+ /// A trigger fills from its base as far as it is pulled, and wholly while it is held as a button.
+ private func trigger(_ e: PadElement, value: Double, s: CGFloat) -> some View {
+ let outline = DesignShape(path: Self.shoulder(e) ?? Path())
+ let level = max(0, min(1, held(e) && value < 0.05 ? 1 : value))
+ let on = level > 0.05
+ let bounds = Self.frame(e)
+ let nameAt = Self.shoulderLabel(e)
+ // The pulled part: everything below the level, measured up the trigger's bounds from its base.
+ let pulled = Rectangle().frame(width: Self.design.width * s, height: bounds.size.height * level * s)
+ .position(x: Self.design.width * s / 2, y: (bounds.center.y + bounds.size.height / 2 - bounds.size.height * level / 2) * s)
+ func name(_ color: Color) -> some View {
+ Text(label(e)).font(ShellFonts.fixed(10.5 * s, weight: .heavy)).foregroundStyle(color)
+ .rotationEffect(.degrees(nameAt.angle))
+ .position(x: nameAt.point.x * s, y: nameAt.point.y * s)
+ }
+ return ZStack(alignment: .topLeading) {
+ outline.fill(Self.shoulderIdle)
+ outline.fill(ShellPalette.cream).mask { pulled }
+ outline.stroke(Color.white.opacity(on ? 0.9 : 0.4), lineWidth: max(1, 1.4 * s))
+ // The name in white over the rest and in black over the pulled part, so it reads at any pull.
+ name(.white.opacity(0.75))
+ name(.black).mask { pulled }
+ }
+ .frame(width: Self.design.width * s, height: Self.design.height * s)
}
- private func bumper(_ e: PadElement, x: CGFloat, s: CGFloat) -> some View {
+ /// A bumper: a slab along the body's top edge, over it. Cream while held.
+ private func bumper(_ e: PadElement, s: CGFloat) -> some View {
let on = held(e)
- return place(
- ZStack {
- Capsule().fill(on ? ShellPalette.cream : Color.white.opacity(0.06))
- Capsule().stroke(Color.white.opacity(on ? 0.9 : 0.35), lineWidth: max(1, 1.4 * s))
- Text(label(e)).font(ShellFonts.fixed(8 * s, weight: .bold)).foregroundStyle(on ? .black : .white.opacity(0.7))
- },
- center: CGPoint(x: x + 36, y: 32), size: CGSize(width: 72, height: 12), s: s)
+ let outline = DesignShape(path: Self.shoulder(e) ?? Path())
+ let name = Self.shoulderLabel(e)
+ return ZStack(alignment: .topLeading) {
+ outline.fill(on ? ShellPalette.cream : Self.shoulderIdle)
+ outline.stroke(Color.white.opacity(on ? 0.9 : 0.4), lineWidth: max(1, 1.4 * s))
+ Text(label(e)).font(ShellFonts.fixed(9.5 * s, weight: .heavy)).foregroundStyle(on ? .black : .white.opacity(0.75))
+ .rotationEffect(.degrees(name.angle))
+ .position(x: name.point.x * s, y: name.point.y * s)
+ }
+ .frame(width: Self.design.width * s, height: Self.design.height * s)
}
private func stick(_ e: PadElement, at c: CGPoint, offset: CGPoint, s: CGFloat) -> some View {
@@ -331,6 +413,15 @@ struct XboxControllerDiagram: View {
}
}
+/// A path of the drawing's design space, scaled to the rect it is drawn in (the whole drawing).
+private struct DesignShape: Shape {
+ let path: Path
+ func path(in rect: CGRect) -> Path {
+ let sx = rect.width / XboxControllerDiagram.design.width, sy = rect.height / XboxControllerDiagram.design.height
+ return path.applying(CGAffineTransform(scaleX: sx, y: sy).concatenating(CGAffineTransform(translationX: rect.minX, y: rect.minY)))
+ }
+}
+
/// The outline of an Xbox controller: a broad top, the two grips curving down and in, a shallow bottom between them.
private struct ControllerBody: Shape {
func path(in rect: CGRect) -> Path {
diff --git a/frontend-ios/Tests/ShellUITests/PadMapTests.swift b/frontend-ios/Tests/ShellUITests/PadMapTests.swift
index 437b5c9e..3b65f660 100644
--- a/frontend-ios/Tests/ShellUITests/PadMapTests.swift
+++ b/frontend-ios/Tests/ShellUITests/PadMapTests.swift
@@ -86,11 +86,36 @@ import Testing
}
// Between the right and the bottom face button, nearer the right one.
#expect(XboxControllerDiagram.nearest(to: CGPoint(x: 256, y: 99)) == .b)
- // The end of a bumper, the controller's body and the Xbox button pick nothing else.
- #expect(XboxControllerDiagram.nearest(to: CGPoint(x: 56, y: 32)) == .leftBumper)
+ // The outer end of a bumper, where it wraps the body's corner, is the bumper, the paddle above it the trigger.
+ #expect(XboxControllerDiagram.nearest(to: CGPoint(x: 40, y: 55)) == .leftBumper)
+ #expect(XboxControllerDiagram.nearest(to: CGPoint(x: 280, y: 55)) == .rightBumper)
+ #expect(XboxControllerDiagram.nearest(to: CGPoint(x: 64, y: 8)) == .leftTrigger)
+ // The controller's body, away from every button, and the Xbox button pick nothing.
#expect(XboxControllerDiagram.nearest(to: CGPoint(x: 160, y: 200)) == nil)
+ #expect(XboxControllerDiagram.nearest(to: CGPoint(x: 50, y: 150)) == nil)
#expect(XboxControllerDiagram.nearest(to: XboxControllerDiagram.frame(.home).center) == nil)
}
+ @Test func theShouldersAreFourShapesApartAndMirrored() throws {
+ let shoulders: [PadElement] = [.leftBumper, .rightBumper, .leftTrigger, .rightTrigger]
+ for e in shoulders {
+ let outline = try #require(XboxControllerDiagram.shoulder(e))
+ // The frame the hotspot and the ring use is the outline's bounds, inside the drawing.
+ let f = XboxControllerDiagram.frame(e)
+ #expect(abs(f.center.x - outline.boundingRect.midX) < 0.01 && abs(f.size.width - outline.boundingRect.width) < 0.01)
+ #expect(outline.boundingRect.minY >= 0 && outline.boundingRect.maxX <= XboxControllerDiagram.design.width)
+ }
+ // A trigger and its bumper do not touch: there is a gap between the paddle and the slab.
+ for (trigger, bumper) in [(PadElement.leftTrigger, PadElement.leftBumper), (.rightTrigger, .rightBumper)] {
+ let paddle = try #require(XboxControllerDiagram.shoulder(trigger)), slab = try #require(XboxControllerDiagram.shoulder(bumper))
+ // Even the bounds keep apart: the paddle ends above the slab's highest point.
+ #expect(paddle.boundingRect.maxY + 3 < slab.boundingRect.minY, "\(paddle.boundingRect) \(slab.boundingRect)")
+ }
+ // The right side mirrors the left.
+ let left = XboxControllerDiagram.frame(.leftBumper), right = XboxControllerDiagram.frame(.rightBumper)
+ #expect(abs(left.center.x + right.center.x - XboxControllerDiagram.design.width) < 0.01 && abs(left.center.y - right.center.y) < 0.01)
+ // The other elements have no outline of their own.
+ #expect(XboxControllerDiagram.shoulder(.a) == nil)
+ }
@Test func theCardGroupsEveryButtonOnce() {
let grouped = PadMap.groups.flatMap(\.buttons)
#expect(grouped.count == PadMap.buttons.count && Set(grouped) == Set(PadMap.buttons))
diff --git a/frontend-ios/Tests/ShellUITests/PadMappingCaptureTests.swift b/frontend-ios/Tests/ShellUITests/PadMappingCaptureTests.swift
index ada926bd..3cc9bdfd 100644
--- a/frontend-ios/Tests/ShellUITests/PadMappingCaptureTests.swift
+++ b/frontend-ios/Tests/ShellUITests/PadMappingCaptureTests.swift
@@ -91,6 +91,15 @@ final class PadMappingCaptureTests: XCTestCase {
try capture("pad-settings-row", width: 402, GameDetailView(detail: detail, showsPrimaryAction: false, showsHeader: false).padding(24))
}
+ /// The drawing alone and large, to look at the shoulders: the card's (Switch names, rings on changed buttons, the left
+ /// bumper held) and the test's (Xbox names, the right trigger half pulled).
+ func testCaptureTheDrawingUpClose() throws {
+ let card = XboxControllerDiagram(snapshot: PadSnapshot(pressed: [.leftBumper]), switchNames: true, marked: [.rightBumper, .leftTrigger, .b])
+ try capture("pad-drawing-card", width: 960, card.aspectRatio(320 / 220, contentMode: .fit).padding(24))
+ let test = XboxControllerDiagram(snapshot: PadSnapshot(pressed: [.rightBumper], rightTrigger: 0.5))
+ try capture("pad-drawing-test", width: 960, test.aspectRatio(320 / 220, contentMode: .fit).padding(24))
+ }
+
func testCaptureTheButtonTest() throws {
let held = PadSnapshot(name: "Xbox Wireless Controller", pressed: [.a, .leftBumper, .up], leftStick: CGPoint(x: 0.6, y: 0.5),
rightTrigger: 0.7)