Commit a2c75668f8
Unsigned
Layout: unified · split
App/Sources/FoldGesture.swift +7 −20
| @@ -1,5 +1,4 @@ | |||
| 1 | import FoldFormat | 1 | import FoldFormat |
| 2 | import Folding | ||
| 3 | import SwiftUI | 2 | import SwiftUI |
| 4 | 3 | ||
| 5 | extension View { | 4 | extension View { |
| @@ -13,35 +12,23 @@ extension View { | |||
| 13 | 12 | ||
| 14 | private struct FoldGesture: ViewModifier { | 13 | private struct FoldGesture: ViewModifier { |
| 15 | let session: FoldSession | 14 | let session: FoldSession |
| 16 | @GestureState private var grab: Grab? | 15 | @GestureState private var isDragging = false |
| 17 | |||
| 18 | /// The mapping, the moving faces' screen point and t when the drag began. | ||
| 19 | private struct Grab { | ||
| 20 | let mapping: FoldParameterMapping | ||
| 21 | let point: SIMD2<Double> | ||
| 22 | let t: Double | ||
| 23 | } | ||
| 24 | 16 | ||
| 25 | func body(content: Content) -> some View { | 17 | func body(content: Content) -> some View { |
| 26 | let folds = session.step.kind == .fold || session.step.kind == .foldAndUnfold | 18 | let folds = session.step.kind == .fold || session.step.kind == .foldAndUnfold |
| 27 | content | 19 | content |
| 28 | .gesture(drag, isEnabled: folds) | 20 | .gesture(drag, isEnabled: folds) |
| 29 | .onChange(of: grab == nil) { _, ended in if ended { session.cancelDrag() } } | 21 | .onChange(of: isDragging) { _, dragging in if !dragging { session.cancelDrag() } } |
| 30 | } | 22 | } |
| 31 | 23 | ||
| 32 | /// A two-finger gesture during the drag suspends it (`FoldSession.beginOrbit()`). | 24 | /// A two-finger gesture during the drag suspends it (`FoldSession.beginOrbit()`). |
| 33 | private var drag: some Gesture { | 25 | private var drag: some Gesture { |
| 34 | DragGesture() | 26 | DragGesture() |
| 35 | .updating($grab) { value, grab, _ in | 27 | .updating($isDragging) { value, isDragging, _ in |
| 36 | if grab == nil { session.beginDrag() } | 28 | if !isDragging { session.beginFoldDrag() } |
| 37 | let start = grab ?? Grab(mapping: session.mapping, point: session.mapping.point(at: session.t), t: session.t) | 29 | isDragging = true |
| 38 | grab = start | 30 | session.drag(translation: SIMD2(value.translation.width, value.translation.height)) |
| 39 | guard !session.isDragSuspended else { return } | ||
| 40 | session.t = start.mapping.parameter( | ||
| 41 | grabbedAt: start.point, | ||
| 42 | translation: SIMD2(value.translation.width, value.translation.height), | ||
| 43 | current: start.mapping.isDegenerate ? start.t : session.t) | ||
| 44 | } | 31 | } |
| 45 | .onEnded { _ in session.endDrag() } | 32 | .onEnded { value in session.endDrag(translation: SIMD2(value.translation.width, value.translation.height)) } |
| 46 | } | 33 | } |
| 47 | } | 34 | } |
App/Sources/FoldSession.swift +44 −10
| @@ -88,29 +88,63 @@ final class FoldSession { | |||
| 88 | t = 0 | 88 | t = 0 |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | /// `t` when the current one-finger drag began, while one is in progress. | 91 | /// What maps a one-finger drag's translation to `t`. |
| 92 | @ObservationIgnored private var dragStart: Double? | 92 | private enum DragMapping { |
| 93 | case fold(FoldParameterMapping, grabbedAt: SIMD2<Double>) | ||
| 94 | case reorient(ReorientMapping) | ||
| 95 | } | ||
| 96 | |||
| 97 | /// The current one-finger drag's mapping and `t` when it began, while one is in progress. | ||
| 98 | @ObservationIgnored private var grab: (mapping: DragMapping, start: Double)? | ||
| 93 | /// Two-finger gestures in progress. | 99 | /// Two-finger gestures in progress. |
| 94 | @ObservationIgnored private var orbits = 0 | 100 | @ObservationIgnored private var orbits = 0 |
| 95 | /// Whether a two-finger gesture began during the current drag. The drag then leaves `t` as it | 101 | /// Whether a two-finger gesture began during the current drag. The drag then leaves `t` as it |
| 96 | /// was when the drag began until the finger lifts, and does not complete the step. | 102 | /// was when the drag began until the finger lifts, and does not complete the step. |
| 97 | @ObservationIgnored private(set) var isDragSuspended = false | 103 | @ObservationIgnored private(set) var isDragSuspended = false |
| 98 | 104 | ||
| 99 | /// Called when a one-finger drag that drives `t` begins. | 105 | /// Called when a one-finger drag on a fold step begins: the moving faces then follow the |
| 100 | func beginDrag() { | 106 | /// finger along `mapping`'s path. |
| 101 | dragStart = t | 107 | func beginFoldDrag() { |
| 108 | let mapping = self.mapping | ||
| 109 | beginDrag(.fold(mapping, grabbedAt: mapping.point(at: t))) | ||
| 110 | } | ||
| 111 | |||
| 112 | /// Called when a one-finger drag on a turn-over or rotate step begins: its horizontal | ||
| 113 | /// component then turns the model across its on-screen width, measured on the frame the step | ||
| 114 | /// starts from, which a vertical turn-over would otherwise shrink to an edge midway. | ||
| 115 | func beginReorientDrag() { | ||
| 116 | beginDrag(.reorient(ReorientMapping(positions: interpolator.positions(at: 0), camera: camera, | ||
| 117 | viewportSize: viewportSize))) | ||
| 118 | } | ||
| 119 | |||
| 120 | private func beginDrag(_ mapping: DragMapping) { | ||
| 121 | grab = (mapping, t) | ||
| 102 | isDragSuspended = orbits > 0 | 122 | isDragSuspended = orbits > 0 |
| 103 | } | 123 | } |
| 104 | 124 | ||
| 105 | /// Called when the drag's finger lifts: completes the step unless the drag was suspended. | 125 | /// Moves `t` to where the drag's `translation` maps it, unless the drag is suspended. |
| 106 | func endDrag() { | 126 | func drag(translation: SIMD2<Double>) { |
| 127 | guard let grab, !isDragSuspended else { return } | ||
| 128 | switch grab.mapping { | ||
| 129 | case let .fold(mapping, grabbedAt): | ||
| 130 | t = mapping.parameter(grabbedAt: grabbedAt, translation: translation, | ||
| 131 | current: mapping.isDegenerate ? grab.start : t) | ||
| 132 | case let .reorient(mapping): | ||
| 133 | t = mapping.parameter(translation: translation.x, current: grab.start) | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | /// Called when the drag's finger lifts at `translation`: moves `t` there as `drag(translation:)` | ||
| 138 | /// does, then completes the step unless the drag was suspended. | ||
| 139 | func endDrag(translation: SIMD2<Double>) { | ||
| 140 | drag(translation: translation) | ||
| 107 | if !isDragSuspended { completeStep() } | 141 | if !isDragSuspended { completeStep() } |
| 108 | cancelDrag() | 142 | cancelDrag() |
| 109 | } | 143 | } |
| 110 | 144 | ||
| 111 | /// Called when the drag ends by any means, including cancellation. | 145 | /// Called when the drag ends by any means, including cancellation. |
| 112 | func cancelDrag() { | 146 | func cancelDrag() { |
| 113 | dragStart = nil | 147 | grab = nil |
| 114 | isDragSuspended = false | 148 | isDragSuspended = false |
| 115 | } | 149 | } |
| 116 | 150 | ||
| @@ -118,8 +152,8 @@ final class FoldSession { | |||
| 118 | /// `t` undone. | 152 | /// `t` undone. |
| 119 | func beginOrbit() { | 153 | func beginOrbit() { |
| 120 | orbits += 1 | 154 | orbits += 1 |
| 121 | if let dragStart { | 155 | if let grab { |
| 122 | t = dragStart | 156 | t = grab.start |
| 123 | isDragSuspended = true | 157 | isDragSuspended = true |
| 124 | } | 158 | } |
| 125 | } | 159 | } |
App/Sources/ReorientGesture.swift +8 −22
| @@ -1,5 +1,4 @@ | |||
| 1 | import FoldFormat | 1 | import FoldFormat |
| 2 | import Folding | ||
| 3 | import SwiftUI | 2 | import SwiftUI |
| 4 | 3 | ||
| 5 | extension View { | 4 | extension View { |
| @@ -13,36 +12,23 @@ extension View { | |||
| 13 | 12 | ||
| 14 | struct ReorientGesture: ViewModifier { | 13 | struct ReorientGesture: ViewModifier { |
| 15 | let session: FoldSession | 14 | let session: FoldSession |
| 16 | @GestureState private var grab: Grab? | 15 | @GestureState private var isDragging = false |
| 17 | |||
| 18 | /// The mapping and t when the drag began. | ||
| 19 | private struct Grab { | ||
| 20 | let mapping: ReorientMapping | ||
| 21 | let t: Double | ||
| 22 | } | ||
| 23 | 16 | ||
| 24 | func body(content: Content) -> some View { | 17 | func body(content: Content) -> some View { |
| 25 | let reorients = session.step.kind == .turnOver || session.step.kind == .rotate | 18 | let reorients = session.step.kind == .turnOver || session.step.kind == .rotate |
| 26 | content | 19 | content |
| 27 | .gesture(drag, isEnabled: reorients) | 20 | .gesture(drag, isEnabled: reorients) |
| 28 | .onChange(of: grab == nil) { _, ended in if ended { session.cancelDrag() } } | 21 | .onChange(of: isDragging) { _, dragging in if !dragging { session.cancelDrag() } } |
| 29 | } | 22 | } |
| 30 | 23 | ||
| 31 | /// The width is measured on the frame the step starts from, which a vertical turn-over would | 24 | /// A two-finger gesture during the drag suspends it (`FoldSession.beginOrbit()`). |
| 32 | /// otherwise shrink to an edge midway. A two-finger gesture during the drag suspends it | ||
| 33 | /// (`FoldSession.beginOrbit()`). | ||
| 34 | private var drag: some Gesture { | 25 | private var drag: some Gesture { |
| 35 | DragGesture() | 26 | DragGesture() |
| 36 | .updating($grab) { value, grab, _ in | 27 | .updating($isDragging) { value, isDragging, _ in |
| 37 | if grab == nil { session.beginDrag() } | 28 | if !isDragging { session.beginReorientDrag() } |
| 38 | let start = grab ?? Grab( | 29 | isDragging = true |
| 39 | mapping: ReorientMapping(positions: session.interpolator.positions(at: 0), camera: session.camera, | 30 | session.drag(translation: SIMD2(value.translation.width, value.translation.height)) |
| 40 | viewportSize: session.viewportSize), | ||
| 41 | t: session.t) | ||
| 42 | grab = start | ||
| 43 | guard !session.isDragSuspended else { return } | ||
| 44 | session.t = start.mapping.parameter(translation: value.translation.width, current: start.t) | ||
| 45 | } | 31 | } |
| 46 | .onEnded { _ in session.endDrag() } | 32 | .onEnded { value in session.endDrag(translation: SIMD2(value.translation.width, value.translation.height)) } |
| 47 | } | 33 | } |
| 48 | } | 34 | } |
App/Tests/FoldSessionTests.swift +61 −15
| @@ -1,6 +1,7 @@ | |||
| 1 | import FoldFormat | 1 | import FoldFormat |
| 2 | import Models | 2 | import Models |
| 3 | import Testing | 3 | import Testing |
| 4 | import simd | ||
| 4 | @testable import Fold | 5 | @testable import Fold |
| 5 | 6 | ||
| 6 | @MainActor | 7 | @MainActor |
| @@ -69,48 +70,93 @@ private func cup(step: Int = 1) throws -> FoldSession { | |||
| 69 | #expect(session.camera.fieldOfView == start.fieldOfView) | 70 | #expect(session.camera.fieldOfView == start.fieldOfView) |
| 70 | } | 71 | } |
| 71 | 72 | ||
| 73 | /// The translation that takes a fold drag begun at `start` to `t` along the session's path, and | ||
| 74 | /// 30 points past its end when `t` is 1. | ||
| 75 | @MainActor | ||
| 76 | private func translation(_ session: FoldSession, from start: Double = 0, to t: Double) -> SIMD2<Double> { | ||
| 77 | let m = session.mapping | ||
| 78 | let past = t == 1 ? simd_normalize(m.point(at: 1) - m.point(at: 127.0 / 128)) * 30 : .zero | ||
| 79 | return m.point(at: t) - m.point(at: start) + past | ||
| 80 | } | ||
| 81 | |||
| 82 | @MainActor | ||
| 83 | private func cupInView(step: Int = 1) throws -> FoldSession { | ||
| 84 | let session = try cup(step: step) | ||
| 85 | session.viewportSize = SIMD2(400, 400) | ||
| 86 | return session | ||
| 87 | } | ||
| 88 | |||
| 72 | @MainActor @Test func anOrbitDuringADragRestoresTAndTheDragDoesNotComplete() throws { | 89 | @MainActor @Test func anOrbitDuringADragRestoresTAndTheDragDoesNotComplete() throws { |
| 73 | let session = try cup() | 90 | let session = try cupInView() |
| 74 | session.t = 0.2 | 91 | session.t = 0.2 |
| 75 | session.beginDrag() | 92 | session.beginFoldDrag() |
| 76 | session.t = 1 | 93 | session.drag(translation: translation(session, from: 0.2, to: 1)) |
| 94 | #expect(session.t == 1) | ||
| 77 | session.beginOrbit() | 95 | session.beginOrbit() |
| 78 | #expect(session.t == 0.2) | 96 | #expect(session.t == 0.2) |
| 79 | #expect(session.isDragSuspended) | 97 | #expect(session.isDragSuspended) |
| 80 | session.endOrbit() | 98 | session.endOrbit() |
| 81 | #expect(session.isDragSuspended) | 99 | #expect(session.isDragSuspended) |
| 82 | session.endDrag() | 100 | session.endDrag(translation: translation(session, from: 0.2, to: 1)) |
| 83 | #expect(session.step.index == 1) | 101 | #expect(session.step.index == 1) |
| 84 | #expect(session.t == 0.2) | 102 | #expect(session.t == 0.2) |
| 85 | #expect(!session.isDragSuspended) | 103 | #expect(!session.isDragSuspended) |
| 86 | } | 104 | } |
| 87 | 105 | ||
| 88 | @MainActor @Test func aDragThatStartsDuringAnOrbitIsSuspended() throws { | 106 | @MainActor @Test func aDragThatStartsDuringAnOrbitIsSuspended() throws { |
| 89 | let session = try cup() | 107 | let session = try cupInView() |
| 90 | session.beginOrbit() | 108 | session.beginOrbit() |
| 91 | session.beginDrag() | 109 | session.beginFoldDrag() |
| 92 | #expect(session.isDragSuspended) | 110 | #expect(session.isDragSuspended) |
| 93 | session.endOrbit() | 111 | session.endOrbit() |
| 94 | session.endDrag() | 112 | session.endDrag(translation: translation(session, to: 1)) |
| 95 | #expect(session.step.index == 1) | 113 | #expect(session.step.index == 1) |
| 114 | #expect(session.t == 0) | ||
| 96 | } | 115 | } |
| 97 | 116 | ||
| 98 | @MainActor @Test func aDragWithoutAnOrbitCompletesAtTheEnd() throws { | 117 | @MainActor @Test func aDragWithoutAnOrbitCompletesAtTheEnd() throws { |
| 99 | let session = try cup() | 118 | let session = try cupInView() |
| 100 | session.beginDrag() | 119 | session.beginFoldDrag() |
| 101 | session.t = 1 | 120 | session.drag(translation: translation(session, to: 1)) |
| 102 | session.endDrag() | 121 | session.endDrag(translation: translation(session, to: 1)) |
| 122 | #expect(session.step.index == 2) | ||
| 123 | } | ||
| 124 | |||
| 125 | @MainActor @Test func theReleasePositionIsAppliedBeforeTheLift() throws { | ||
| 126 | // the last movement can arrive only with the lift | ||
| 127 | let session = try cupInView() | ||
| 128 | session.beginFoldDrag() | ||
| 129 | session.drag(translation: translation(session, to: 0.5)) | ||
| 130 | #expect(abs(session.t - 0.5) < 0.01) | ||
| 131 | session.endDrag(translation: translation(session, to: 1)) | ||
| 103 | #expect(session.step.index == 2) | 132 | #expect(session.step.index == 2) |
| 133 | #expect(session.t == 0) | ||
| 134 | } | ||
| 135 | |||
| 136 | @MainActor @Test func aReleaseShortOfTheEndLeavesTWhereTheFingerLifted() throws { | ||
| 137 | let session = try cupInView() | ||
| 138 | session.beginFoldDrag() | ||
| 139 | session.endDrag(translation: translation(session, to: 0.5)) | ||
| 140 | #expect(session.step.index == 1) | ||
| 141 | #expect(abs(session.t - 0.5) < 0.01) | ||
| 142 | } | ||
| 143 | |||
| 144 | @MainActor @Test func aTurnOverReleasedPastTheModelsWidthCompletes() throws { | ||
| 145 | let session = try cupInView(step: 5) | ||
| 146 | #expect(session.step.kind == .turnOver) | ||
| 147 | session.beginReorientDrag() | ||
| 148 | session.endDrag(translation: SIMD2(1000, 0)) | ||
| 149 | #expect(session.step.index == 6) | ||
| 104 | } | 150 | } |
| 105 | 151 | ||
| 106 | @MainActor @Test func anOrbitAfterACancelledDragLeavesTAlone() throws { | 152 | @MainActor @Test func anOrbitAfterACancelledDragLeavesTAlone() throws { |
| 107 | let session = try cup() | 153 | let session = try cupInView() |
| 108 | session.t = 0.2 | 154 | session.t = 0.2 |
| 109 | session.beginDrag() | 155 | session.beginFoldDrag() |
| 110 | session.t = 0.6 | 156 | session.drag(translation: translation(session, from: 0.2, to: 0.6)) |
| 111 | session.cancelDrag() | 157 | session.cancelDrag() |
| 112 | session.beginOrbit() | 158 | session.beginOrbit() |
| 113 | #expect(session.t == 0.6) | 159 | #expect(abs(session.t - 0.6) < 0.01) |
| 114 | #expect(!session.isDragSuspended) | 160 | #expect(!session.isDragSuspended) |
| 115 | session.endOrbit() | 161 | session.endOrbit() |
| 116 | } | 162 | } |
App/UITests/FoldGestureTests.swift +25 −7
| @@ -31,8 +31,9 @@ func completeCurrentFold(_ app: XCUIApplication) { | |||
| 31 | 31 | ||
| 32 | /// Drags the current step's moving faces along their path, read from `fold.path` (the app must be | 32 | /// Drags the current step's moving faces along their path, read from `fold.path` (the app must be |
| 33 | /// launched with `-UITesting`), to `fraction` of it, then lifts. Each drag is one straight | 33 | /// launched with `-UITesting`), to `fraction` of it, then lifts. Each drag is one straight |
| 34 | /// press-and-drag through a quarter of the path's samples; a drag to the end continues 30 points | 34 | /// press-and-drag through every 32nd sample, with short ones merged so each moves at least |
| 35 | /// past it. | 35 | /// `minimumChord` points: a DragGesture does not begin under 10. A drag to the end continues 30 |
| 36 | /// points past it. Each holds before lifting, so its last position arrives before the lift. | ||
| 36 | func dragCurrentFold(_ app: XCUIApplication, to fraction: Double) { | 37 | func dragCurrentFold(_ app: XCUIApplication, to fraction: Double) { |
| 37 | let paper = app.otherElements["fold.paper"] | 38 | let paper = app.otherElements["fold.paper"] |
| 38 | let marker = app.descendants(matching: .any)["fold.path"] | 39 | let marker = app.descendants(matching: .any)["fold.path"] |
| @@ -43,15 +44,32 @@ func dragCurrentFold(_ app: XCUIApplication, to fraction: Double) { | |||
| 43 | } | 44 | } |
| 44 | XCTAssertFalse(samples.isEmpty) | 45 | XCTAssertFalse(samples.isEmpty) |
| 45 | let size = paper.frame.size | 46 | let size = paper.frame.size |
| 47 | func chord(_ a: Int, _ b: Int) -> CGVector { | ||
| 48 | CGVector(dx: (samples[b].dx - samples[a].dx) * size.width, dy: (samples[b].dy - samples[a].dy) * size.height) | ||
| 49 | } | ||
| 50 | func length(_ v: CGVector) -> Double { (v.dx * v.dx + v.dy * v.dy).squareRoot() } | ||
| 46 | let last = Int((fraction * Double(samples.count - 1)).rounded()) | 51 | let last = Int((fraction * Double(samples.count - 1)).rounded()) |
| 47 | let stops = Array(stride(from: 0, to: last, by: 32)) + [last] | 52 | var stops = [0] |
| 53 | for i in stride(from: 32, through: last, by: 32) where length(chord(stops.last!, i)) >= minimumChord { | ||
| 54 | stops.append(i) | ||
| 55 | } | ||
| 56 | if stops.last != last { | ||
| 57 | if stops.count > 1 && length(chord(stops.last!, last)) < minimumChord { | ||
| 58 | stops[stops.count - 1] = last | ||
| 59 | } else { | ||
| 60 | stops.append(last) | ||
| 61 | } | ||
| 62 | } | ||
| 48 | for (a, b) in zip(stops, stops.dropFirst()) { | 63 | for (a, b) in zip(stops, stops.dropFirst()) { |
| 49 | var end = paper.coordinate(withNormalizedOffset: samples[b]) | 64 | var end = paper.coordinate(withNormalizedOffset: samples[b]) |
| 50 | if b == samples.count - 1 { | 65 | if b == samples.count - 1 { |
| 51 | let dx = (samples[b].dx - samples[a].dx) * size.width, dy = (samples[b].dy - samples[a].dy) * size.height | 66 | let d = chord(a, b), l = max(length(d), 1) |
| 52 | let length = max((dx * dx + dy * dy).squareRoot(), 1) | 67 | end = end.withOffset(CGVector(dx: d.dx / l * 30, dy: d.dy / l * 30)) |
| 53 | end = end.withOffset(CGVector(dx: dx / length * 30, dy: dy / length * 30)) | ||
| 54 | } | 68 | } |
| 55 | paper.coordinate(withNormalizedOffset: samples[a]).press(forDuration: 0.1, thenDragTo: end) | 69 | paper.coordinate(withNormalizedOffset: samples[a]) |
| 70 | .press(forDuration: 0.1, thenDragTo: end, withVelocity: .default, thenHoldForDuration: 0.3) | ||
| 56 | } | 71 | } |
| 57 | } | 72 | } |
| 73 | |||
| 74 | /// The shortest straight drag `dragCurrentFold` makes, in points. | ||
| 75 | private let minimumChord = 12.0 | ||