Open History to see the last commit
1#if DEBUG
2import AppKit
3import SpriteKit
4import ExpeditionCore
5
6@MainActor
7final class AnimationPlaygroundScene: SKScene {
8 let rig = ExplorerRig()
9 private let actualSizeRig = ExplorerRig()
10 private let terrain = SKNode()
11 private let handles = SKNode()
12 private let rope = SKShapeNode()
13 private let status = SKLabelNode(fontNamed: "Menlo")
14 private let scaleCaption = SKLabelNode(fontNamed: "Menlo")
15 private var selected: ExplorerMotion = .idle
16 private var previousTime: Double?
17 private var activeHandle: String?
18 private var rigDragOffset = CGPoint.zero
19 private var handholds = [CGPoint(x: 20, y: 55), CGPoint(x: 20, y: 65)]
20 private var ropeAnchor = CGPoint(x: 52, y: 91)
21 private var grabPoint = CGPoint(x: 0, y: 64)
22 private var hasCustomOrigin = false
23 private var stageOrigin = CGPoint.zero
24 private var shadowWindow = CGRect.zero
25 var playbackPaused = false
26 var playbackRate: Double = 1
27 var zoom: CGFloat = 3 { didSet { updateScale(); layoutStage() } }
28 var facing: CGFloat = 1 { didSet { updateScale(); rebuildStage() } }
29 var showJoints = false {
30 didSet { rig.showsJoints = showJoints; actualSizeRig.showsJoints = showJoints }
31 }
32 var flatShading = false {
33 didSet { rig.setPainted(!flatShading); actualSizeRig.setPainted(!flatShading) }
34 }
35 var tuning = AnimationTuning() {
36 didSet { rig.tuning = tuning; actualSizeRig.tuning = tuning }
37 }
38 var darkBackground = true {
39 didSet { backgroundColor = darkBackground ? NSColor(calibratedWhite: 0.09, alpha: 1) : NSColor(calibratedWhite: 0.94, alpha: 1); rebuildStage() }
40 }
41
42 override init(size: CGSize) {
43 super.init(size: size)
44 scaleMode = .resizeFill
45 backgroundColor = NSColor(calibratedWhite: 0.09, alpha: 1)
46 addChild(terrain)
47 addChild(rope)
48 rope.zPosition = 1
49 addChild(rig)
50 addChild(actualSizeRig)
51 addChild(handles)
52 status.fontSize = 12
53 status.horizontalAlignmentMode = .left
54 status.verticalAlignmentMode = .top
55 addChild(status)
56 scaleCaption.fontSize = 11
57 scaleCaption.horizontalAlignmentMode = .left
58 addChild(scaleCaption)
59 updateScale()
60 layoutStage()
61 }
62 required init?(coder: NSCoder) { fatalError("Use init(size:)") }
63
64 override func didChangeSize(_ oldSize: CGSize) { layoutStage() }
65
66 private func updateScale() {
67 rig.xScale = zoom * facing; rig.yScale = zoom
68 actualSizeRig.xScale = facing; actualSizeRig.yScale = 1
69 }
70
71 private func layoutStage() {
72 let tall = selected == .parachuting || selected == .abseiling || selected == .jumping
73 let y = tall ? max(25, min(size.height * 0.25, size.height - 140 * zoom - 55)) : max(105, size.height * 0.25)
74 stageOrigin = CGPoint(x: size.width * 0.53, y: y)
75 if !hasCustomOrigin { rig.position = stageOrigin }
76 actualSizeRig.position = CGPoint(x: 90, y: 80)
77 scaleCaption.position = CGPoint(x: 25, y: 45)
78 status.position = CGPoint(x: 22, y: size.height - 20)
79 rebuildStage()
80 }
81
82 func select(_ motion: ExplorerMotion, restart: Bool = false) {
83 activeHandle = nil
84 selected = motion
85 hasCustomOrigin = false
86 rig.select(motion, restart: restart)
87 actualSizeRig.select(motion, restart: restart)
88 if restart {
89 handholds = [CGPoint(x: 20, y: 55), CGPoint(x: 20, y: 65)]
90 ropeAnchor = CGPoint(x: 52, y: 91)
91 grabPoint = CGPoint(x: 0, y: 64)
92 hasCustomOrigin = false
93 layoutStage()
94 }
95 previousTime = nil
96 layoutStage()
97 }
98
99 func restart() { select(selected, restart: true) }
100
101 func seek(to time: Double) {
102 playbackPaused = true
103 previousTime = nil
104 rig.seek(to: time)
105 actualSizeRig.seek(to: time)
106 }
107
108 private func handle(_ name: String, at local: CGPoint, color: NSColor) {
109 let point = rig.convert(local, to: self)
110 let node = SKShapeNode(circleOfRadius: 7)
111 node.name = name
112 node.position = point
113 node.strokeColor = color
114 node.fillColor = color.withAlphaComponent(0.3)
115 node.lineWidth = 2
116 handles.addChild(node)
117 let label = SKLabelNode(fontNamed: "Menlo")
118 label.text = name
119 label.fontSize = 10
120 label.fontColor = darkBackground ? .white : .black
121 label.position = CGPoint(x: point.x + 12 * facing, y: point.y + 10)
122 label.horizontalAlignmentMode = facing > 0 ? .left : .right
123 handles.addChild(label)
124 }
125
126 private func rebuildStage() {
127 terrain.removeAllChildren()
128 handles.removeAllChildren()
129 let textColor: NSColor = darkBackground ? .white : .black
130 status.fontColor = textColor
131 scaleCaption.fontColor = textColor
132 scaleCaption.text = "ACTUAL DESKTOP SIZE · 74 pt"
133 let platformY = stageOrigin.y - (selected == .held ? 30 * zoom : 0)
134 let vertical = selected == .climbing || selected == .abseiling
135 let wallX = stageOrigin.x + (selected == .climbing ? 20 : 24) * zoom * facing
136 var frame = vertical
137 ? CGRect(x: facing > 0 ? wallX : wallX - 200, y: platformY - 100, width: 200, height: 100 + 90 * zoom)
138 : CGRect(x: stageOrigin.x - 200, y: platformY - 120, width: 400, height: 120)
139 if selected == .mantling || selected == .lowering {
140 let cornerX = stageOrigin.x + 24 * zoom * facing
141 frame = CGRect(x: facing > 0 ? cornerX : cornerX - 300, y: stageOrigin.y + 52 * zoom - 120, width: 300, height: 120)
142 } else if selected == .launching {
143 let cornerX = stageOrigin.x + 24 * zoom * facing
144 frame = CGRect(x: facing > 0 ? cornerX - 400 : cornerX, y: stageOrigin.y - 120, width: 400, height: 120)
145 }
146 if selected == .comic {
147 frame = CGRect(x: stageOrigin.x + (facing > 0 ? -200 : -20), y: platformY - 120, width: 220, height: 120)
148 }
149 let radius = [ExplorerMotion.abseiling, .mantling, .lowering, .launching].contains(selected) ? AbseilRope.cornerRadius * zoom : 8
150 shadowWindow = frame
151 let window = SKShapeNode(rect: frame, cornerRadius: radius)
152 window.fillColor = darkBackground ? NSColor(calibratedWhite: 0.17, alpha: 1) : .white
153 window.strokeColor = .systemTeal
154 window.lineWidth = 2
155 terrain.addChild(window)
156 let edgeLabel = SKLabelNode(fontNamed: "Menlo")
157 edgeLabel.text = "FAKE WINDOW — animation only"
158 edgeLabel.fontSize = 11
159 edgeLabel.fontColor = textColor.withAlphaComponent(0.6)
160 edgeLabel.position = CGPoint(x: frame.midX, y: frame.minY + 22)
161 terrain.addChild(edgeLabel)
162 if selected == .climbing {
163 for (index, point) in handholds.enumerated() { handle(index == 0 ? "left handhold" : "right handhold", at: point, color: .systemGreen) }
164 }
165 if selected == .abseiling { handle("rope anchor", at: ropeAnchor, color: .systemYellow) }
166 if selected == .held || selected == .comic { handle("grab point", at: grabPoint, color: .systemPink) }
167 rebuildRope()
168 }
169
170 private func rebuildRope() {
171 rope.isHidden = selected != .abseiling
172 guard selected == .abseiling else { return }
173 let path = CGMutablePath()
174 let gripX = min(max(ropeAnchor.x, -10), 10)
175 let guide = AbseilRope(window: .init(x: 24, y: -100, width: 100, height: 190), edge: .left, grip: .init(x: gripX, y: 61.5))
176 func point(_ value: WorldPoint) -> CGPoint { rig.convert(.init(x: value.x, y: value.y), to: self) }
177 path.move(to: rig.convert(ropeAnchor, to: self))
178 path.addLine(to: point(guide.topTangent))
179 path.addCurve(to: point(guide.sideTangent), control1: point(guide.control1), control2: point(guide.control2))
180 path.addLine(to: rig.convert(CGPoint(x: gripX, y: 61.5), to: self))
181 path.addLine(to: rig.convert(CGPoint(x: gripX, y: -30), to: self))
182 rope.path = path
183 rope.strokeColor = .systemYellow
184 rope.lineWidth = 2
185 }
186
187 override func update(_ currentTime: TimeInterval) {
188 let realDelta = previousTime.map { min(max(currentTime - $0, 0), 0.1) } ?? 0
189 previousTime = currentTime
190 let delta = playbackPaused ? 0 : realDelta * playbackRate
191 let liveMotion: ExplorerMotion = activeHandle == "body" ? .held : selected
192 rig.select(liveMotion)
193 actualSizeRig.select(liveMotion)
194 var targets: (WorldPoint, WorldPoint)?
195 if liveMotion == .climbing {
196 targets = (.init(x: handholds[0].x, y: handholds[0].y), .init(x: handholds[1].x, y: handholds[1].y))
197 } else if liveMotion == .abseiling {
198 let x = min(max(ropeAnchor.x, -10), 10)
199 targets = (.init(x: x - 4, y: 61), .init(x: x + 4, y: 62))
200 }
201 for character in [rig, actualSizeRig] {
202 character.handTargets = targets
203 if liveMotion == .climbing {
204 let contacts = ClimbingContacts(heightAlongWall: ClimbingGait.height(at: character.animationTime))
205 character.footTargets = (contacts.leftFoot, contacts.rightFoot)
206 } else { character.footTargets = nil }
207 character.grabPoint = grabPoint
208 character.render(delta: delta)
209 }
210 if [.mantling, .lowering, .launching].contains(selected), !hasCustomOrigin {
211 let duration = EdgeTraversal.duration(selected)
212 let progress = min(rig.animationTime.truncatingRemainder(dividingBy: duration + 1) / duration, 1)
213 let from = WorldPoint(x: 0, y: 0), inside = WorldPoint(x: 48, y: selected == .launching ? 0 : 52)
214 let offset = EdgeTraversal.position(selected, progress: progress, from: selected == .lowering ? inside : from, to: selected == .lowering ? from : inside)
215 rig.position = CGPoint(x: stageOrigin.x + offset.x * zoom * facing, y: stageOrigin.y + offset.y * zoom)
216 actualSizeRig.position = CGPoint(x: 90 + offset.x * facing, y: 80 + offset.y)
217 }
218 if selected == .jumping, !hasCustomOrigin {
219 let time = rig.animationTime.truncatingRemainder(dividingBy: JumpAnimation.previewDuration)
220 let height = JumpAnimation.previewHeight(at: time)
221 rig.position = .init(x: stageOrigin.x, y: stageOrigin.y + height * zoom)
222 actualSizeRig.position = .init(x: 90, y: 80 + height)
223 }
224 rig.setShadowOcclusion(windows: [shadowWindow], from: self)
225 // The actual-size preview has no illustrated supporting window.
226 actualSizeRig.setShadowOcclusion(windows: [], from: self)
227 var beat = "Drag the body to interrupt any pose"
228 if selected == .jumping { beat = "Plant → compress → push through legs → tuck → reach → absorb" }
229 if selected == .mantling { beat = "Grip the lip → pull up → plant a boot → stand" }
230 if selected == .lowering { beat = "Crouch → back over the lip → take the rope" }
231 if selected == .launching { beat = "Look down → crouch → push clear → deploy" }
232 if selected == .comic {
233 let t = rig.animationTime.truncatingRemainder(dividingBy: 9)
234 beat = t < 2 ? "Approach the edge" : (t < 3.5 ? "Look down. Reconsider." : (t < 5 ? "Back away with dignity" : "Picked up by the mysterious giant"))
235 }
236 status.text = "\(liveMotion.rawValue.uppercased()) \(facing > 0 ? "→" : "←") · \(String(format: "%.2f", playbackRate))× · \(rig.clampedContacts) clamped contacts · \(beat)"
237 }
238
239 override func mouseDown(with event: NSEvent) {
240 let point = event.location(in: self)
241 if let target = handles.children.first(where: { $0.name != nil && $0.contains(point) })?.name {
242 activeHandle = target
243 return
244 }
245 let local = rig.convert(point, from: self)
246 if CGRect(x: -26, y: -4, width: 52, height: 82).contains(local) {
247 activeHandle = "body"
248 rigDragOffset = CGPoint(x: point.x - rig.position.x, y: point.y - rig.position.y)
249 grabPoint = local
250 rig.select(.held)
251 actualSizeRig.select(.held)
252 }
253 }
254
255 override func mouseDragged(with event: NSEvent) {
256 guard let activeHandle else { return }
257 let point = event.location(in: self)
258 let local = rig.convert(point, from: self)
259 switch activeHandle {
260 case "left handhold": handholds[0] = local
261 case "right handhold": handholds[1] = local
262 case "rope anchor": ropeAnchor = local
263 case "grab point": grabPoint = local
264 case "body":
265 let fixedHolds = handholds.map { rig.convert($0, to: self) }
266 let fixedAnchor = rig.convert(ropeAnchor, to: self)
267 rig.position = CGPoint(x: min(max(point.x - rigDragOffset.x, 100), size.width - 100), y: min(max(point.y - rigDragOffset.y, 40), size.height - 100))
268 handholds = fixedHolds.map { rig.convert($0, from: self) }
269 ropeAnchor = rig.convert(fixedAnchor, from: self)
270 hasCustomOrigin = true
271 default: break
272 }
273 rebuildStage()
274 }
275
276 override func mouseUp(with event: NSEvent) {
277 guard activeHandle != nil else { return }
278 activeHandle = nil
279 rig.select(selected)
280 actualSizeRig.select(selected)
281 rebuildStage()
282 }
283}
284#endif