App/Sources/OrbitGesture.swift
94 lines · 3517 bytes
14 symbols in this file
1import Folding
2import SwiftUI
3import UIKit
4
5extension View {
6 /// Lets a two-finger drag on the view orbit the session's camera around the model, and a pinch
7 /// change its field of view. Neither changes `t` or the diagram: either one beginning suspends
8 /// a one-finger drag in progress and undoes what it did to `t`.
9 func orbitGesture(_ session: FoldSession) -> some View {
10 modifier(OrbitGesture(session: session))
11 }
12}
13
14struct OrbitGesture: ViewModifier {
15 let session: FoldSession
16 /// How far the model turns per point of drag, in radians.
17 static let radiansPerPoint = 0.01
18
19 func body(content: Content) -> some View {
20 content
21 .gesture(TwoFingerPan(session: session) { translation in
22 session.orbit(by: SIMD2(translation.x, translation.y) * Self.radiansPerPoint)
23 })
24 .gesture(Pinch(session: session) { scale in session.zoom(by: scale) })
25 }
26}
27
28/// The names of the two recognizers, which may recognize together.
29private let orbitRecognizers: Set<String> = ["orbit.pan", "orbit.pinch"]
30
31private final class Simultaneous: NSObject, UIGestureRecognizerDelegate {
32 func gestureRecognizer(_ recognizer: UIGestureRecognizer,
33 shouldRecognizeSimultaneouslyWith other: UIGestureRecognizer) -> Bool {
34 orbitRecognizers.contains(other.name ?? "")
35 }
36}
37
38/// Tells the session when the recognizer begins and ends.
39@MainActor
40private func track(_ state: UIGestureRecognizer.State, _ session: FoldSession) {
41 switch state {
42 case .began: session.beginOrbit()
43 case .ended, .cancelled, .failed: session.endOrbit()
44 default: break
45 }
46}
47
48/// Reports a two-finger drag's translation in points since the last report.
49private struct TwoFingerPan: UIGestureRecognizerRepresentable {
50 let session: FoldSession
51 let changed: (CGPoint) -> Void
52
53 func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() }
54
55 func makeUIGestureRecognizer(context: Context) -> UIPanGestureRecognizer {
56 let pan = UIPanGestureRecognizer()
57 pan.name = "orbit.pan"
58 pan.minimumNumberOfTouches = 2
59 pan.maximumNumberOfTouches = 2
60 pan.delegate = context.coordinator
61 return pan
62 }
63
64 func handleUIGestureRecognizerAction(_ recognizer: UIPanGestureRecognizer, context: Context) {
65 track(recognizer.state, session)
66 guard recognizer.state == .began || recognizer.state == .changed else { return }
67 let translation = recognizer.translation(in: recognizer.view)
68 recognizer.setTranslation(.zero, in: recognizer.view)
69 changed(translation)
70 }
71}
72
73/// Reports a pinch's scale since the last report.
74private struct Pinch: UIGestureRecognizerRepresentable {
75 let session: FoldSession
76 let changed: (Double) -> Void
77
78 func makeCoordinator(converter: CoordinateSpaceConverter) -> Simultaneous { Simultaneous() }
79
80 func makeUIGestureRecognizer(context: Context) -> UIPinchGestureRecognizer {
81 let pinch = UIPinchGestureRecognizer()
82 pinch.name = "orbit.pinch"
83 pinch.delegate = context.coordinator
84 return pinch
85 }
86
87 func handleUIGestureRecognizerAction(_ recognizer: UIPinchGestureRecognizer, context: Context) {
88 track(recognizer.state, session)
89 guard recognizer.state == .began || recognizer.state == .changed else { return }
90 let scale = recognizer.scale
91 recognizer.scale = 1
92 changed(scale)
93 }
94}