Commit 36c6469a8a
Verified · cmc
Layout: unified · split
App/Sources/ReorientGesture.swift added +43
| @@ -0,0 +1,43 @@ | |||
| 1 | import FoldFormat | ||
| 2 | import Folding | ||
| 3 | import SwiftUI | ||
| 4 | |||
| 5 | extension View { | ||
| 6 | /// Lets a one-finger horizontal drag drive the session's turn-over or rotate step: a rightward | ||
| 7 | /// drag across the model's on-screen width turns it fully, a leftward drag turns it back, and | ||
| 8 | /// the step completes when the finger lifts at t = 1. Releasing below 1 leaves it where it is. | ||
| 9 | func reorientGesture(_ session: FoldSession) -> some View { | ||
| 10 | modifier(ReorientGesture(session: session)) | ||
| 11 | } | ||
| 12 | } | ||
| 13 | |||
| 14 | struct ReorientGesture: ViewModifier { | ||
| 15 | let session: FoldSession | ||
| 16 | @GestureState private var grab: Grab? | ||
| 17 | |||
| 18 | /// The mapping and t when the drag began. | ||
| 19 | private struct Grab { | ||
| 20 | let mapping: ReorientMapping | ||
| 21 | let t: Double | ||
| 22 | } | ||
| 23 | |||
| 24 | func body(content: Content) -> some View { | ||
| 25 | let reorients = session.step.kind == .turnOver || session.step.kind == .rotate | ||
| 26 | content.gesture(drag, isEnabled: reorients) | ||
| 27 | } | ||
| 28 | |||
| 29 | /// The width is measured on the frame the step starts from, which a vertical turn-over would | ||
| 30 | /// otherwise shrink to an edge midway. | ||
| 31 | private var drag: some Gesture { | ||
| 32 | DragGesture() | ||
| 33 | .updating($grab) { value, grab, _ in | ||
| 34 | let start = grab ?? Grab( | ||
| 35 | mapping: ReorientMapping(positions: session.interpolator.positions(at: 0), camera: session.camera, | ||
| 36 | viewportSize: session.viewportSize), | ||
| 37 | t: session.t) | ||
| 38 | grab = start | ||
| 39 | session.t = start.mapping.parameter(translation: value.translation.width, current: start.t) | ||
| 40 | } | ||
| 41 | .onEnded { _ in session.completeStep() } | ||
| 42 | } | ||
| 43 | } | ||
App/Sources/StepView.swift +19 −1
| @@ -58,12 +58,16 @@ struct StepView: View { | |||
| 58 | if size.width > 0 && size.height > 0 { session.viewportSize = SIMD2(size.width, size.height) } | 58 | if size.width > 0 && size.height > 0 { session.viewportSize = SIMD2(size.width, size.height) } |
| 59 | } | 59 | } |
| 60 | .foldGesture(session) | 60 | .foldGesture(session) |
| 61 | .reorientGesture(session) | ||
| 61 | .accessibilityElement() | 62 | .accessibilityElement() |
| 62 | .accessibilityLabel(session.step.instruction) | 63 | .accessibilityLabel(session.step.instruction) |
| 63 | .accessibilityValue(session.t.formatted(.percent.precision(.fractionLength(0)))) | 64 | .accessibilityValue(session.t.formatted(.percent.precision(.fractionLength(0)))) |
| 64 | .accessibilityIdentifier("fold.paper") | 65 | .accessibilityIdentifier("fold.paper") |
| 65 | .overlay(alignment: .topLeading) { | 66 | .overlay(alignment: .topLeading) { |
| 66 | if Self.isUITesting { foldPath(session) } | 67 | if Self.isUITesting { |
| 68 | foldPath(session) | ||
| 69 | cameraValue(session) | ||
| 70 | } | ||
| 67 | } | 71 | } |
| 68 | } | 72 | } |
| 69 | 73 | ||
| @@ -82,4 +86,18 @@ struct StepView: View { | |||
| 82 | .accessibilityValue(value) | 86 | .accessibilityValue(value) |
| 83 | .accessibilityIdentifier("fold.path") | 87 | .accessibilityIdentifier("fold.path") |
| 84 | } | 88 | } |
| 89 | |||
| 90 | /// For UI tests: the camera's eye and up, to 3 decimals. | ||
| 91 | private func cameraValue(_ session: FoldSession) -> some View { | ||
| 92 | let camera = session.camera | ||
| 93 | let value = [camera.eye, camera.up].map { v in | ||
| 94 | String(format: "%.3f,%.3f,%.3f", v.x, v.y, v.z) | ||
| 95 | }.joined(separator: " ") | ||
| 96 | return Color.clear | ||
| 97 | .frame(width: 1, height: 1) | ||
| 98 | .allowsHitTesting(false) | ||
| 99 | .accessibilityElement() | ||
| 100 | .accessibilityValue(value) | ||
| 101 | .accessibilityIdentifier("paper.camera") | ||
| 102 | } | ||
| 85 | } | 103 | } |
App/UITests/ReorientGestureTests.swift added +84
| @@ -0,0 +1,84 @@ | |||
| 1 | import XCTest | ||
| 2 | |||
| 3 | final class ReorientGestureTests: XCTestCase { | ||
| 4 | override func setUp() { | ||
| 5 | continueAfterFailure = false | ||
| 6 | } | ||
| 7 | |||
| 8 | func testDraggingAcrossTheModelCompletesATurnOverStep() { | ||
| 9 | let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() | ||
| 10 | app.buttons["Cup"].tap() | ||
| 11 | advanceToFirstTurnOverStep(app) | ||
| 12 | let before = app.staticTexts["step.index"].label | ||
| 13 | dragAcross(app.otherElements["fold.paper"]) | ||
| 14 | XCTAssertNotEqual(before, app.staticTexts["step.index"].label) | ||
| 15 | } | ||
| 16 | |||
| 17 | func testFoldDragDoesNothingOnATurnOverStep() { | ||
| 18 | let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() | ||
| 19 | app.buttons["Cup"].tap() | ||
| 20 | advanceToFirstTurnOverStep(app) | ||
| 21 | let before = app.staticTexts["step.index"].label | ||
| 22 | let paper = app.otherElements["fold.paper"] | ||
| 23 | // a downward drag longer than the fold drag's 160-point fallback throw | ||
| 24 | drag(paper, from: CGVector(dx: 0.5, dy: 0.05), to: CGVector(dx: 0.5, dy: 0.95)) | ||
| 25 | XCTAssertEqual(app.staticTexts["step.index"].label, before) | ||
| 26 | XCTAssertEqual(paper.value as? String, "0%") | ||
| 27 | } | ||
| 28 | |||
| 29 | func testReorientDragDoesNothingOnAFoldStep() { | ||
| 30 | let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() | ||
| 31 | app.buttons["Cup"].tap() | ||
| 32 | // step 1 folds the bottom corner up; this drag is across that path | ||
| 33 | dragAcross(app.otherElements["fold.paper"]) | ||
| 34 | XCTAssertEqual(app.staticTexts["step.index"].label, "1") | ||
| 35 | } | ||
| 36 | |||
| 37 | func testTurnOverReorientsTheModelNotTheCamera() { | ||
| 38 | let app = XCUIApplication(); app.launchArguments = ["-UITesting"]; app.launch() | ||
| 39 | app.buttons["Cup"].tap() | ||
| 40 | advanceToFirstTurnOverStep(app) | ||
| 41 | let paper = app.otherElements["fold.paper"] | ||
| 42 | let diagram = app.descendants(matching: .any)["diagram"] | ||
| 43 | let camera = app.descendants(matching: .any)["paper.camera"] | ||
| 44 | XCTAssertTrue(camera.waitForExistence(timeout: 5)) | ||
| 45 | let step = app.staticTexts["step.index"].label | ||
| 46 | let diagramBefore = diagram.screenshot().pngRepresentation | ||
| 47 | let cameraBefore = camera.value as? String | ||
| 48 | let paperBefore = paper.screenshot().pngRepresentation | ||
| 49 | |||
| 50 | drag(paper, from: CGVector(dx: 0.3, dy: 0.5), to: CGVector(dx: 0.5, dy: 0.5)) | ||
| 51 | let midway = paper.value as? String | ||
| 52 | XCTAssertNotEqual(midway, "0%") | ||
| 53 | XCTAssertNotEqual(midway, "100%") | ||
| 54 | XCTAssertEqual(app.staticTexts["step.index"].label, step) | ||
| 55 | XCTAssertEqual(diagram.screenshot().pngRepresentation, diagramBefore) | ||
| 56 | XCTAssertEqual(camera.value as? String, cameraBefore) | ||
| 57 | |||
| 58 | dragAcross(paper) | ||
| 59 | XCTAssertNotEqual(app.staticTexts["step.index"].label, step) | ||
| 60 | XCTAssertNotEqual(paper.screenshot().pngRepresentation, paperBefore) | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | /// Completes the current model's fold steps up to its first turn-over. The app must be launched | ||
| 65 | /// with `-UITesting`. | ||
| 66 | func advanceToFirstTurnOverStep(_ app: XCUIApplication) { | ||
| 67 | while !app.staticTexts["diagram.caption"].label.hasPrefix("Turn over") { | ||
| 68 | let step = app.staticTexts["step.index"].label | ||
| 69 | completeCurrentFold(app) | ||
| 70 | XCTAssertNotEqual(app.staticTexts["step.index"].label, step) | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | /// A rightward drag across most of the element's width, at its middle. It starts clear of the | ||
| 75 | /// screen's left edge, where a drag would go back in the navigation stack. | ||
| 76 | func dragAcross(_ element: XCUIElement) { | ||
| 77 | drag(element, from: CGVector(dx: 0.1, dy: 0.5), to: CGVector(dx: 0.9, dy: 0.5)) | ||
| 78 | } | ||
| 79 | |||
| 80 | /// A straight drag between two points given as fractions of the element's frame, then a lift. | ||
| 81 | func drag(_ element: XCUIElement, from start: CGVector, to end: CGVector) { | ||
| 82 | element.coordinate(withNormalizedOffset: start) | ||
| 83 | .press(forDuration: 0.1, thenDragTo: element.coordinate(withNormalizedOffset: end)) | ||
| 84 | } | ||
Packages/FoldKit/Sources/Folding/ReorientMapping.swift added +27
| @@ -0,0 +1,27 @@ | |||
| 1 | import simd | ||
| 2 | |||
| 3 | /// Maps a horizontal drag on the paper view to the parameter of a `turnOver` or `rotate` step: the | ||
| 4 | /// drag's rightward distance over the model's on-screen width. A leftward drag turns it back. | ||
| 5 | /// Screen points are UIKit's, in a viewport of `viewportSize`. | ||
| 6 | public struct ReorientMapping: Sendable { | ||
| 7 | /// The width in points of the projected bounding box of `positions`, at least 44 points. | ||
| 8 | public let width: Double | ||
| 9 | |||
| 10 | private static let minimumWidth = 44.0 | ||
| 11 | |||
| 12 | /// `positions` are the model's vertices in the frame the step starts from. | ||
| 13 | public init(positions: [SIMD3<Double>], camera: Camera, viewportSize: SIMD2<Double>) { | ||
| 14 | let viewProjection = camera.viewProjection(aspect: viewportSize.x / viewportSize.y) | ||
| 15 | let xs = positions.map { p in | ||
| 16 | let clip = viewProjection * SIMD4(p, 1) | ||
| 17 | return (clip.x / clip.w + 1) / 2 * viewportSize.x | ||
| 18 | } | ||
| 19 | width = max(xs.max()! - xs.min()!, Self.minimumWidth) | ||
| 20 | } | ||
| 21 | |||
| 22 | /// The parameter, in 0...1, after a drag of `translation` points rightward from where the | ||
| 23 | /// parameter was `current`. | ||
| 24 | public func parameter(translation: Double, current: Double) -> Double { | ||
| 25 | min(max(current + translation / width, 0), 1) | ||
| 26 | } | ||
| 27 | } | ||
Packages/FoldKit/Tests/FoldingTests/ReorientMappingTests.swift added +40
| @@ -0,0 +1,40 @@ | |||
| 1 | import FoldFormat | ||
| 2 | import FoldKitTestSupport | ||
| 3 | import Folding | ||
| 4 | import Testing | ||
| 5 | |||
| 6 | /// `half.fold`'s 2 × 2 sheet from 4 units above its centre, with a 90° field of view in an | ||
| 7 | /// 800 × 600 viewport: 150 points wide. | ||
| 8 | private func sheetFromAbove() throws -> ReorientMapping { | ||
| 9 | let file = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 10 | let camera = Camera(eye: SIMD3(1, 1, 4), target: SIMD3(1, 1, 0), up: SIMD3(0, 1, 0), fieldOfView: .pi / 2) | ||
| 11 | return ReorientMapping(positions: file.frames[0].verticesCoords, camera: camera, viewportSize: SIMD2(800, 600)) | ||
| 12 | } | ||
| 13 | |||
| 14 | @Test func theWidthIsTheModelsProjectedWidth() throws { | ||
| 15 | #expect(abs(try sheetFromAbove().width - 150) < 1e-9) | ||
| 16 | } | ||
| 17 | |||
| 18 | @Test func aRightwardDragAcrossTheWidthCompletesTheStep() throws { | ||
| 19 | let m = try sheetFromAbove() | ||
| 20 | #expect(abs(m.parameter(translation: 75, current: 0) - 0.5) < 1e-12) | ||
| 21 | #expect(m.parameter(translation: 150, current: 0) == 1) | ||
| 22 | } | ||
| 23 | |||
| 24 | @Test func theReorientParameterIsClampedToUnitRange() throws { | ||
| 25 | let m = try sheetFromAbove() | ||
| 26 | #expect(m.parameter(translation: 1000, current: 0.5) == 1) | ||
| 27 | #expect(m.parameter(translation: -1000, current: 0.5) == 0) | ||
| 28 | } | ||
| 29 | |||
| 30 | @Test func aLeftwardDragTurnsTheModelBack() throws { | ||
| 31 | #expect(abs(try sheetFromAbove().parameter(translation: -37.5, current: 0.5) - 0.25) < 1e-12) | ||
| 32 | } | ||
| 33 | |||
| 34 | @Test func aModelSeenEdgeOnStillTakesADeliberateDrag() throws { | ||
| 35 | let file = try FoldFile(contentsOf: fixture("half.fold")) | ||
| 36 | let camera = Camera(eye: SIMD3(1, 5, 0), target: SIMD3(1, 1, 0), up: SIMD3(1, 0, 0), fieldOfView: .pi / 2) | ||
| 37 | let m = ReorientMapping(positions: file.frames[0].verticesCoords, camera: camera, viewportSize: SIMD2(800, 600)) | ||
| 38 | #expect(m.width == 44) | ||
| 39 | #expect(m.parameter(translation: 0, current: 0) == 0) | ||
| 40 | } | ||