Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
iOS shell: quiet face buttons, a trigger fill that follows the paddle
v0lumehi194e07dd87a8
diff --git a/frontend-ios/Sources/ShellUI/ButtonTestView.swift b/frontend-ios/Sources/ShellUI/ButtonTestView.swiftindex a67c1d61..391f0c9f 100644--- a/frontend-ios/Sources/ShellUI/ButtonTestView.swift+++ b/frontend-ios/Sources/ShellUI/ButtonTestView.swift@@ -174,19 +174,48 @@ struct XboxControllerDiagram: View {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)))+ return paddle(grow: grow).applying(triggerPlacement(e))default:return nil}}+ /// A trigger's paddle in its own frame: centred on the origin, upright, its base (the edge a pull fills from) at the+ /// bottom, y = +height / 2.+ private nonisolated static func paddle(grow: CGFloat = 0) -> Path {+ let size = CGSize(width: triggerSize.width + 2 * grow, height: triggerSize.height + 2 * grow)+ return 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))+ }++ /// Turns a paddle's own frame into the drawing's: tilted out with the shoulder, then moved to its place.+ private nonisolated static func triggerPlacement(_ e: PadElement) -> CGAffineTransform {+ let right = isRight(e)+ let center = mirrored(triggerCenter, right)+ return CGAffineTransform(rotationAngle: (right ? -triggerTilt : triggerTilt) * .pi / 180)+ .concatenating(CGAffineTransform(translationX: center.x, y: center.y))+ }++ /// The pulled part of a trigger, `level` from 0 to 1, in the drawing's space: the paddle cut across its own axis at+ /// that height from its base, so it tilts with the paddle and meets its outline exactly. 0 is empty, 1 the whole paddle.+ nonisolated static func triggerFill(_ e: PadElement, level: Double) -> Path {+ guard e == .leftTrigger || e == .rightTrigger, level > 0 else { return Path() }+ let outline = paddle()+ guard level < 1 else { return outline.applying(triggerPlacement(e)) }+ let h = triggerSize.height, w = triggerSize.width+ // Wider and deeper than the paddle below the cut, so only the cut itself comes from this rectangle.+ let below = Path(CGRect(x: -w, y: h / 2 - h * level, width: 2 * w, height: h * level + h))+ return outline.intersection(below).applying(triggerPlacement(e))+ }++ /// The rest of a trigger, the part of the paddle a pull has not reached.+ nonisolated static func triggerRest(_ e: PadElement, level: Double) -> Path {+ guard let outline = shoulder(e) else { return Path() }+ return level > 0 ? outline.subtracting(triggerFill(e, level: level)) : outline+ }+/// 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)@@ -292,11 +321,11 @@ struct XboxControllerDiagram: View {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)- // Xbox's colours in the test. On the mapping card the letters are the Switch's, at other places, so one quiet colour.- face(.y, at: CGPoint(x: 240, y: 70), color: switchNames ? Self.neutral : Color(red: 0.98, green: 0.80, blue: 0.20), s: s)- face(.x, at: CGPoint(x: 219, y: 92), color: switchNames ? Self.neutral : Color(red: 0.25, green: 0.55, blue: 1.0), s: s)- face(.b, at: CGPoint(x: 261, y: 92), color: switchNames ? Self.neutral : Color(red: 0.95, green: 0.30, blue: 0.28), s: s)- face(.a, at: CGPoint(x: 240, y: 114), color: switchNames ? Self.neutral : Color(red: 0.35, green: 0.80, blue: 0.30), s: s)+ // One quiet colour for the face buttons, in the test as on the card: held, they turn cream like every other button.+ face(.y, at: CGPoint(x: 240, y: 70), s: s)+ face(.x, at: CGPoint(x: 219, y: 92), s: s)+ face(.b, at: CGPoint(x: 261, y: 92), s: s)+ face(.a, at: CGPoint(x: 240, y: 114), s: s)small(.view, symbol: switchNames ? "minus" : "rectangle.on.rectangle", at: CGPoint(x: 138, y: 92), s: s)small(.menu, symbol: switchNames ? "plus" : "line.3.horizontal", at: CGPoint(x: 182, y: 92), s: s)home(at: CGPoint(x: 160, y: 60), s: s)@@ -318,23 +347,21 @@ struct XboxControllerDiagram: 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)+ // The pulled part and the rest, cut along the paddle's own axis and bounded by its outline (Self.triggerFill).+ let pulled = DesignShape(path: Self.triggerFill(e, level: level)), rest = DesignShape(path: Self.triggerRest(e, level: level))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 }+ rest.fill(Self.shoulderIdle)+ pulled.fill(ShellPalette.cream)+ // The name in white over the rest and in black over the pulled part, split by the same tilted cut.+ name(.white.opacity(0.75)).mask { rest.fill() }+ name(.black).mask { pulled.fill() }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)}@@ -378,15 +405,15 @@ struct XboxControllerDiagram: View {}}- private func face(_ e: PadElement, at c: CGPoint, color: Color, s: CGFloat) -> some View {+ private func face(_ e: PadElement, at c: CGPoint, s: CGFloat) -> some View {let on = held(e)return place(ZStack {- Circle().fill(on ? color : Color.black.opacity(0.35))- Circle().stroke(on ? Color.white : color.opacity(0.8), lineWidth: max(1, 1.5 * s))- Text(label(e)).font(ShellFonts.fixed(11 * s, weight: .heavy)).foregroundStyle(on ? .black : color)+ Circle().fill(on ? ShellPalette.cream : Color.black.opacity(0.35))+ Circle().stroke(on ? Color.white.opacity(0.9) : Self.neutral.opacity(0.8), lineWidth: max(1, 1.5 * s))+ Text(label(e)).font(ShellFonts.fixed(11 * s, weight: .heavy)).foregroundStyle(on ? .black : Self.neutral)}- .shadow(color: on ? color.opacity(0.8) : .clear, radius: 8 * s),+ .shadow(color: on ? ShellPalette.cream.opacity(0.6) : .clear, radius: 8 * s),center: c, size: CGSize(width: 20, height: 20), s: s)}diff --git a/frontend-ios/Tests/ShellUITests/PadMapTests.swift b/frontend-ios/Tests/ShellUITests/PadMapTests.swiftindex 3b65f660..fe132540 100644--- a/frontend-ios/Tests/ShellUITests/PadMapTests.swift+++ b/frontend-ios/Tests/ShellUITests/PadMapTests.swift@@ -116,6 +116,40 @@ import Testing// The other elements have no outline of their own.#expect(XboxControllerDiagram.shoulder(.a) == nil)}+ /// The pulled part of a trigger is cut along the paddle's own tilted axis and bounded by its outline: 0 fills nothing,+ /// 1 fills every point of the paddle, a half pull about half (a little more, the paddle is rounder on top), and every+ /// point of the paddle is either pulled or rest, never both and never neither.+ @Test func aTriggerFillsItsOwnOutline() throws {+ for trigger in [PadElement.leftTrigger, .rightTrigger] {+ let outline = try #require(XboxControllerDiagram.shoulder(trigger))+ let box = outline.boundingRect+ let inside = stride(from: box.minX, through: box.maxX, by: 0.5).flatMap { x in+ stride(from: box.minY, through: box.maxY, by: 0.5).map { CGPoint(x: x, y: $0) }+ }.filter { outline.contains($0) }+ #expect(inside.count > 1000)+ func share(_ level: Double) -> Double {+ let fill = XboxControllerDiagram.triggerFill(trigger, level: level)+ return Double(inside.filter { fill.contains($0) }.count) / Double(inside.count)+ }+ #expect(share(0) == 0)+ #expect(share(1) == 1)+ let half = share(0.5), quarter = share(0.25), most = share(0.75)+ #expect(half > 0.48 && half < 0.66, "\(half)")+ #expect(quarter > 0.2 && quarter < half && most > half && most < 1, "\(quarter) \(most)")+ let fill = XboxControllerDiagram.triggerFill(trigger, level: 0.5), rest = XboxControllerDiagram.triggerRest(trigger, level: 0.5)+ let both = inside.filter { fill.contains($0) && rest.contains($0) }.count+ let neither = inside.filter { !fill.contains($0) && !rest.contains($0) }.count+ // A point on the cut itself may count either way: allow a sliver, nothing more.+ #expect(Double(both + neither) / Double(inside.count) < 0.02, "\(both) \(neither)")+ // The cut tilts with the paddle: its outer end (where the shoulder slopes down) sits lower in the drawing than+ // its inner end, never a flat line across.+ let pulled = inside.filter { fill.contains($0) }+ let leftTop = pulled.filter { $0.x < box.minX + box.width * 0.3 }.map(\.y).min() ?? 0+ let rightTop = pulled.filter { $0.x > box.maxX - box.width * 0.3 }.map(\.y).min() ?? 0+ let outerTop = trigger == .leftTrigger ? leftTop : rightTop, innerTop = trigger == .leftTrigger ? rightTop : leftTop+ #expect(outerTop > innerTop + 2, "\(outerTop) \(innerTop)")+ }+ }@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.swiftindex 3cc9bdfd..a05b0588 100644--- a/frontend-ios/Tests/ShellUITests/PadMappingCaptureTests.swift+++ b/frontend-ios/Tests/ShellUITests/PadMappingCaptureTests.swift@@ -96,8 +96,11 @@ final class PadMappingCaptureTests: XCTestCase {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))+ // The triggers at 25, 50, 75 and 100 percent, LT and RT side by side in each, a face button held.+ for (name, left, right) in [("25-50", 0.25, 0.5), ("75-100", 0.75, 1.0)] {+ let test = XboxControllerDiagram(snapshot: PadSnapshot(pressed: [.rightBumper, .a], leftTrigger: left, rightTrigger: right))+ try capture("pad-drawing-test-\(name)", width: 960, test.aspectRatio(320 / 220, contentMode: .fit).padding(24))+ }}func testCaptureTheButtonTest() throws {