-
Notifications
You must be signed in to change notification settings - Fork 6
/
GLLView.swift
879 lines (748 loc) · 37.1 KB
/
GLLView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//
// GLLView.swift
// GLLara
//
// Created by Torsten Kammer on 09.07.22.
// Copyright © 2022 Torsten Kammer. All rights reserved.
//
import Cocoa
import MetalKit
import GameController
import Combine
@objc class GLLView: MTKView {
override init(frame frameRect: CGRect, device: MTLDevice?) {
super.init(frame: frameRect, device: device ?? GLLResourceManager.shared.metalDevice)
clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0)
enableSetNeedsDisplay = false
isPaused = false
autoResizeDrawable = true
sampleCount = 1
notificationObservers.append(NotificationCenter.default.addObserver(forName: NSNotification.Name.GLLTextureChange, object: nil, queue: OperationQueue.main) { [weak self] notification in
self?.unpause()
})
notificationObservers.append(NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: nil, queue: OperationQueue.main) { [weak self] notification in
self?.showSelection = UserDefaults.standard.bool(forKey: GLLPrefShowSkeleton)
})
notificationObservers.append(NotificationCenter.default.addObserver(forName: NSNotification.Name.GCControllerDidBecomeCurrent, object: nil, queue: OperationQueue.main) { [weak self] notification in
self?.unpause()
})
showSelection = UserDefaults.standard.bool(forKey: GLLPrefShowSkeleton)
registerForDraggedTypes( [ NSPasteboard.PasteboardType.fileURL ] )
// Trigger loading of manager
_ = GLLGameControllerManager.shared
}
@objc func unpause() {
if isPaused {
lastPositionUpdate = Date.timeIntervalSinceReferenceDate
}
somethingChangedLastFrame = true
isPaused = false
}
required init(coder: NSCoder) {
super.init(coder: coder)
// The MetalKit implementation of this delegates to init(frame:device:) so we should never get here.
}
@objc func set(camera: GLLCamera, sceneDrawer: GLLSceneDrawer) {
self.camera = camera
sceneDrawerUpdateRegistration?.cancel()
self.sceneDrawer = sceneDrawer
sceneDrawerUpdateRegistration = sceneDrawer.$needsUpdate.sink { [weak self] value in
if value {
self?.unpause()
}
}
self.viewDrawer = GLLViewDrawer(sceneDrawer: sceneDrawer, camera: camera, view: self)
}
var camera: GLLCamera? = nil
@objc weak var document: GLLDocument? = nil
var sceneDrawer: GLLSceneDrawer? = nil
var sceneDrawerUpdateRegistration: AnyCancellable? = nil
@objc var viewDrawer: GLLViewDrawer? = nil
var showSelection: Bool = false
var keysDown = CharacterSet()
private var currentModifierFlags: NSEvent.ModifierFlags = []
var notificationObservers: [NSObjectProtocol] = []
@objc func unload() {
viewDrawer = nil
camera = nil
}
private static let wasdCharacters = CharacterSet(charactersIn: "wasd")
private static let xyzCharacters = CharacterSet(charactersIn: "xyz")
private static let arrowCharacters = CharacterSet(charactersIn: UnicodeScalar(NSUpArrowFunctionKey)! ... UnicodeScalar(NSRightArrowFunctionKey)!)
private static let interestingCharacters = wasdCharacters.union(xyzCharacters).union(arrowCharacters)
private static let unitsPerSecond = 0.2
private var inGesture = false
private var dragDestination = GLLItemDragDestination()
private var inEventLoop = false
// MARK: - Events
override var acceptsFirstResponder: Bool {
return true
}
override func rotate(with event: NSEvent) {
guard let camera = camera, !camera.cameraLocked else {
return
}
let angle = event.rotation * Float.pi / 180.0
camera.longitude -= angle
unpause()
}
override func magnify(with event: NSEvent) {
guard let camera = camera, !camera.cameraLocked else {
return
}
camera.distance *= Float(1 + event.magnification)
unpause()
}
override func beginGesture(with event: NSEvent) {
guard let camera = camera, !camera.cameraLocked else {
return
}
inGesture = true
camera.managedObjectContext?.undoManager?.beginUndoGrouping()
camera.managedObjectContext?.undoManager?.setActionIsDiscardable(true)
}
override func endGesture(with event: NSEvent) {
inGesture = false
guard let camera = camera, !camera.cameraLocked else {
return
}
camera.managedObjectContext?.undoManager?.setActionName(NSLocalizedString("Camera changed", comment: "Undo: data of camera has changed"))
camera.managedObjectContext?.undoManager?.endUndoGrouping()
unpause()
}
override func scrollWheel(with event: NSEvent) {
guard let camera = camera, !camera.cameraLocked else {
return
}
camera.currentPositionX += Float(event.deltaX / bounds.size.width)
camera.currentPositionY += Float(event.deltaY / bounds.size.height)
unpause()
}
override func mouseDragged(with event: NSEvent) {
if !GLLView.xyzCharacters.intersection(keysDown).isEmpty {
let amountX = event.deltaX / bounds.size.width
let amountY = event.deltaY / bounds.size.height
let angle = amountX + amountY
let rotation = SIMD3<Float>(x: keysDown.contains("x") ? Float(angle) : 0.0,
y: keysDown.contains("y") ? Float(angle) : 0.0,
z: keysDown.contains("z") ? Float(angle) : 0.0)
rotateBones(eulerAngles: rotation)
} else if event.modifierFlags.contains(.option) {
// Move the object in the x/z plane
let factor = event.modifierFlags.contains(.shift) ? 0.01 : 0.001
let deltaScreen = SIMD4<Float32>(Float32(event.deltaX * factor), 0.0, Float32(event.deltaY * factor), 0.0)
let deltaWorld = self.camera!.viewMatrix.inverse * deltaScreen
for item in document?.selection.selectedItems ?? [] {
item.positionX += deltaWorld.x;
item.positionZ += deltaWorld.z;
}
} else if event.modifierFlags.contains(.shift) && GLLView.wasdCharacters.intersection(keysDown).isEmpty {
// This is a move event
guard let camera = camera, !camera.cameraLocked else {
return
}
let deltaX = Float(event.deltaX / self.bounds.size.width)
let deltaY = Float(event.deltaY / self.bounds.size.height)
camera.moveLocalX(-deltaX, y: deltaY, z: 0)
} else if event.modifierFlags.contains(.control) {
self.rightMouseDragged(with: event)
} else {
// This is a rotate event
guard let camera = camera, !camera.cameraLocked else {
return
}
camera.longitude -= Float(event.deltaX * Double.pi / self.bounds.size.width);
camera.latitude -= Float(event.deltaY * Double.pi / self.bounds.size.height);
}
unpause()
}
override func rightMouseDragged(with event: NSEvent) {
let deltaX = Float(event.deltaX / self.bounds.size.width)
let deltaY = Float(event.deltaY / self.bounds.size.height)
turnAroundCamera(deltaX: deltaX, deltaY: deltaY)
}
override func keyDown(with event: NSEvent) {
keysDown.formUnion(CharacterSet(charactersIn: event.charactersIgnoringModifiers?.lowercased() ?? ""))
currentModifierFlags = event.modifierFlags
unpause()
}
override func mouseDown(with event: NSEvent) {
if self.showSelection, let document = self.document, var selectedBones = document.selection.selectedBones {
// Try to find the bone that corresponds to this event.
if let bone = closestBone(atScreenPoint: convert(event.locationInWindow, from: nil), from: document.allBones) {
if event.modifierFlags.isSuperset(of: [.shift, .command]) {
// Add to/remove from the selection
if let index = selectedBones.firstIndex(of: bone) {
selectedBones.remove(at: index)
document.selection.selectedBones = selectedBones
} else {
selectedBones.append(bone)
document.selection.selectedBones = selectedBones
}
} else {
// Set as only selection
document.selection.selectedBones = [bone]
}
}
unpause()
}
}
override func keyUp(with event: NSEvent) {
keysDown.subtract(CharacterSet(charactersIn: event.charactersIgnoringModifiers?.lowercased() ?? ""))
currentModifierFlags = event.modifierFlags
unpause()
}
override func flagsChanged(with event: NSEvent) {
currentModifierFlags = event.modifierFlags
unpause()
}
private var buttonWasDown = Set<GCControllerButtonInput>()
/**
Determine whether the given button was actually pressed first in this frame. Just looking for the "pressed" state may give wrong answers, especially since MFI gamepads have button that can report pressure levels, and so can call gamepadChanged multiple times for one press.
*/
private func buttonPressed(element: GCControllerElement) -> Bool {
guard let button = element as? GCControllerButtonInput else {
return false
}
if button.isPressed {
return !buttonWasDown.contains(button)
} else {
return false
}
}
private func boneOfDocument(first: Bool) -> GLLItemBone? {
guard let document = document, let managedObjectContext = document.managedObjectContext else {
return nil
}
let fetchRequest = NSFetchRequest<GLLItem>()
fetchRequest.entity = NSEntityDescription.entity(forEntityName: "GLLItem", in: managedObjectContext)
fetchRequest.sortDescriptors = [ NSSortDescriptor(key: "displayName", ascending: first) ]
fetchRequest.fetchLimit = 1
let result = (try? managedObjectContext.fetch(fetchRequest)) ?? []
return firstBone(item: result.first)
}
private func parentBone() -> GLLItemBone? {
guard let document = document else {
return nil
}
// If item is selected: Select its root bone
if let firstSelected = document.selection.selectedObjects.firstObject as? GLLItem {
return firstSelected.bones.firstObject as? GLLItemBone
}
// If nothing selected: Root bone of first model
if document.selection.selectedBones.isEmpty {
return boneOfDocument(first: true)
}
// If multiple selection: Parent of first item that doesn't have an ancestor in the selection
let selectedBones = document.selection.selectedBones!
var filteredBones = selectedBones
filteredBones.removeAll { bone in
bone.isChild(ofAny: selectedBones)
}
let skipUnused = UserDefaults.standard.bool(forKey: GLLPrefHideUnusedBones)
return filteredBones.first?.parent(skippingUnused: skipUnused)
}
private func isValid(bone: GLLItemBone?) -> Bool {
guard let bone = bone else {
return false
}
return !UserDefaults.standard.bool(forKey: GLLPrefHideUnusedBones) || !bone.bone.name.hasPrefix("unused")
}
private func firstBone(item: GLLItem?) -> GLLItemBone? {
return item?.bones.first(where: { isValid(bone: $0 as? GLLItemBone) }) as? GLLItemBone
}
private func lastBone(item: GLLItem?) -> GLLItemBone? {
return item?.bones.reverseObjectEnumerator().first(where: { isValid(bone: $0 as? GLLItemBone) }) as? GLLItemBone
}
private func childBone() -> GLLItemBone? {
guard let document = document else {
return nil
}
// If item is selected: Select its root bone
if let firstSelected = document.selection.selectedObjects.firstObject as? GLLItem {
return firstBone(item: firstSelected)
}
// If nothing selected: Root bone of first model
if document.selection.selectedBones.isEmpty {
return boneOfDocument(first: false)
}
// If multiple selected: First child of last item that doesn't have a child in the selection
let selectedBones = document.selection.selectedBones!
var filteredBones = selectedBones
var ancestorsOfSelected = Set<GLLItemBone>()
for selected in selectedBones {
var parent = selected.parent
while parent != nil {
ancestorsOfSelected.remove(parent!)
parent = parent?.parent
}
}
filteredBones.removeAll { bone in
ancestorsOfSelected.contains(bone)
}
let skipUnused = UserDefaults.standard.bool(forKey: GLLPrefHideUnusedBones)
return filteredBones.last?.firstChild(skippingUnused: skipUnused)
}
private func nextSiblingBone() -> GLLItemBone? {
guard let document = document, let managedObjectContext = document.managedObjectContext else {
return nil
}
// If item is selected: Select its root bone
if let firstSelected = document.selection.selectedObjects.firstObject as? GLLItem {
return firstBone(item: firstSelected)
}
// If nothing selected: Root bone of first model
if document.selection.selectedBones.isEmpty {
return boneOfDocument(first: true)
}
let skipUnused = UserDefaults.standard.bool(forKey: GLLPrefHideUnusedBones)
// Select next sibling bone of last selected bone.
let selection = document.selection.selectedBones
if let lastSelectedBone = selection?.last {
if let siblings = lastSelectedBone.siblings(skippingUnused: skipUnused) {
if let index = siblings.firstIndex(of: lastSelectedBone) {
let nextIndex = (index + 1) % siblings.count
return siblings[nextIndex]
}
} else {
// If root bone: Either next root bone or next model
let item = lastSelectedBone.item!
let rootBones = item.rootBones!
if let index = rootBones.firstIndex(of: lastSelectedBone) {
let nextIndex = (index + 1) % rootBones.count
return rootBones[nextIndex]
} else {
// Root of next item
let fetchRequest = NSFetchRequest<GLLItem>()
fetchRequest.entity = NSEntityDescription.entity(forEntityName: "GLLItem", in: managedObjectContext)
fetchRequest.sortDescriptors = [ NSSortDescriptor(key: "displayName", ascending: true) ]
if let result = try? managedObjectContext.fetch(fetchRequest), let index = result.firstIndex(of: item) {
if index < result.count - 1 {
return firstBone(item: result[index + 1])
} else {
return lastBone(item: result[0])
}
}
}
}
}
return nil
}
private func previousSiblingBone() -> GLLItemBone? {
guard let document = document, let managedObjectContext = document.managedObjectContext else {
return nil
}
// If item is selected: Select its root bone
if let firstSelected = document.selection.selectedObjects.firstObject as? GLLItem {
return firstBone(item: firstSelected)
}
// If nothing selected: Root bone of last model
if document.selection.selectedBones.isEmpty {
return boneOfDocument(first: false)
}
let skipUnused = UserDefaults.standard.bool(forKey: GLLPrefHideUnusedBones)
// Select next sibling bone of last selected bone.
let selection = document.selection.selectedBones
if let firstSelectedBone = selection?.first {
if let siblings = firstSelectedBone.siblings(skippingUnused: skipUnused) {
if let index = siblings.firstIndex(of: firstSelectedBone) {
let nextIndex = (index + siblings.count - 1) % siblings.count
return siblings[nextIndex]
}
} else {
// If root bone: Either next root bone or next model
let item = firstSelectedBone.item!
let rootBones = item.rootBones!
if let index = rootBones.firstIndex(of: firstSelectedBone) {
let nextIndex = (index + rootBones.count - 1) % rootBones.count
return item.rootBones[nextIndex]
} else {
// Root of previous item
let fetchRequest = NSFetchRequest<GLLItem>()
fetchRequest.entity = NSEntityDescription.entity(forEntityName: "GLLItem", in: managedObjectContext)
fetchRequest.sortDescriptors = [ NSSortDescriptor(key: "displayName", ascending: true) ]
if let result = try? managedObjectContext.fetch(fetchRequest), let index = result.firstIndex(of: item) {
if index > 0 {
return lastBone(item: result[index - 1])
} else {
return firstBone(item: result[0])
}
}
}
}
}
return nil
}
private func selectGamepadMode(delta: Int) {
let current = controllerRightStickMode
let orderedModes = ControllerRightStickMode.allCases
if let index = orderedModes.firstIndex(of: current) {
var nextIndex = index + delta
if nextIndex < 0 {
nextIndex = orderedModes.count - 1
}
let next = orderedModes[nextIndex % orderedModes.count]
UserDefaults.standard.set(next.rawValue, forKey: GLLPrefControllerRightStickMode)
}
}
private func selectNextGamepadCameraMode() {
let current = controllerLeftStickMode
let orderedModes = CameraMovementMode.allCases
if let index = orderedModes.firstIndex(of: current) {
let nextIndex = (index + 1) % orderedModes.count
let next = orderedModes[nextIndex % orderedModes.count]
UserDefaults.standard.set(next.rawValue, forKey: GLLPrefControllerLeftStickMode)
}
}
func gamepadChanged(gamepad: GCExtendedGamepad, element: GCControllerElement) {
// Left shoulder: Go to previous controller mode
if element == gamepad.leftShoulder && buttonPressed(element: element) {
selectGamepadMode(delta: -1)
}
// Right trigger: Go to next controller mode
if element == gamepad.rightShoulder && buttonPressed(element: element) {
selectGamepadMode(delta: +1)
}
if let leftStickButton = gamepad.leftThumbstickButton, element == leftStickButton, buttonPressed(element: element) {
selectNextGamepadCameraMode()
}
// DPad: Go to different bone
if element == gamepad.dpad {
let newSelection: GLLItemBone?
if buttonPressed(element: gamepad.dpad.up) {
newSelection = parentBone()
} else if buttonPressed(element: gamepad.dpad.down) {
newSelection = childBone()
} else if buttonPressed(element: gamepad.dpad.left) {
newSelection = previousSiblingBone()
} else if buttonPressed(element: gamepad.dpad.right) {
newSelection = nextSiblingBone()
} else {
newSelection = nil
}
if let document = document, let bone = newSelection {
document.selection.selectedBones = [ bone ]
viewDrawer?.selectViaController(bone: bone)
}
}
// Cleanup: Update "was pressed" state
if let button = element as? GCDeviceButtonInput {
if button.isPressed {
buttonWasDown.insert(button)
} else {
buttonWasDown.remove(button)
}
}
// Draw a frame
self.contiuousInputActive = true
unpause()
}
private var somethingChangedLastFrame = true
override func draw() {
updatePositions()
super.draw()
if !contiuousInputActive && !somethingChangedLastFrame && !(viewDrawer?.runningAnimation ?? false) {
isPaused = true
}
somethingChangedLastFrame = false
}
// MARK: - Drag and drop
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
guard let document = document else {
return []
}
dragDestination.document = document
return dragDestination.itemDraggingEntered(sender)
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
guard let document = document else {
return false
}
dragDestination.document = document
do {
try dragDestination.performItemDragOperation(sender)
return true
} catch {
presentError(error)
return false
}
}
// MARK: - Activity status
@objc static weak var lastActiveView: GLLView? = nil
func windowBecameKey() {
GLLView.lastActiveView = self
}
func windowResignedKey() {
}
// MARK: - Private methods
private var lastPositionUpdate: TimeInterval?
private func adjustForDeadzone(vector: SIMD3<Float>, deadzone: Double) -> (SIMD3<Float>, SIMDMask<SIMD3<Float>.MaskStorage>) {
let deadzoneVector = SIMD3<Float>(repeating: Float(deadzone))
let insideDeadzone = simd_abs(vector) .<= deadzoneVector
let adjustedRangeMin = __tg_copysign(deadzoneVector, vector);
let adjusted = (vector - adjustedRangeMin) / (SIMD3<Float>(repeating: 1.0) - deadzoneVector)
return (adjusted.replacing(with: 0.0, where: insideDeadzone), insideDeadzone)
}
// Whether any continuous input is going on, including keyboard or controllers
private var contiuousInputActive: Bool = false {
didSet {
if contiuousInputActive {
unpause()
}
}
}
private func updatePositions() {
let now = Date.timeIntervalSinceReferenceDate
var diff = now - (lastPositionUpdate ?? now)
lastPositionUpdate = now
viewDrawer?.updateHudAnimation(delta: diff)
if currentModifierFlags.contains(.shift) {
diff *= 10
}
// Get the current state for the space mouse
let rawSpaceMouseRotation = GLLConnexionManager.shared().averageRotation()
let rawSpaceMousePosition = GLLConnexionManager.shared().averagePosition()
let (adjustedRotation, rotationInsideDeadzone) = adjustForDeadzone(vector: rawSpaceMouseRotation, deadzone: spaceMouseDeadZoneRotation)
let (adjustedPosition, positionInsideDeadzone) = adjustForDeadzone(vector: rawSpaceMousePosition, deadzone: spaceMouseDeadZoneTranslation)
// Check whether we should still be moving
if GLLView.interestingCharacters.intersection(keysDown).isEmpty && currentModifierFlags.isDisjoint(with: [.shift, .option]) && all(rotationInsideDeadzone) && all(positionInsideDeadzone) && !currentControllerActive {
contiuousInputActive = false
return
}
contiuousInputActive = true
// Perform actions
// - Move
if let camera = camera, !camera.cameraLocked {
let deltaX = directionFactor(positive: ["d"], negative: ["a"])
let deltaZ = directionFactor(positive: ["s"], negative: ["w"])
if deltaX != nil || deltaZ != nil {
let diffX = (deltaX ?? 0) * Float(diff * GLLView.unitsPerSecond)
let diffZ = (deltaZ ?? 0) * Float(diff * GLLView.unitsPerSecond)
camera.moveLocalX(diffX, y: 0, z: diffZ)
}
}
// Move bones with arrow keys
if !GLLView.xyzCharacters.intersection(keysDown).isEmpty {
if let delta = directionFactor(positive: [UnicodeScalar(NSLeftArrowFunctionKey)!, UnicodeScalar(NSUpArrowFunctionKey)!], negative: [UnicodeScalar(NSRightArrowFunctionKey)!, UnicodeScalar(NSDownArrowFunctionKey)!]) {
let speed = delta * Float(diff * 0.1 * GLLView.unitsPerSecond)
for bone in document?.selection.selectedBones ?? [] {
if keysDown.contains("x") {
bone.positionX += speed
}
if keysDown.contains("y") {
bone.positionY += speed
}
if keysDown.contains("z") {
bone.positionZ += speed
}
}
}
} else if currentModifierFlags.contains(.option) {
// Move the object up or down with arrow keys
if let deltaY = directionFactor(positive: [UnicodeScalar(NSUpArrowFunctionKey)!], negative: [UnicodeScalar(NSDownArrowFunctionKey)!]) {
for item in document?.selection.selectedItems ?? [] {
item.positionY += deltaY * 0.1 * Float(diff * GLLView.unitsPerSecond)
}
}
} else if !GLLView.arrowCharacters.intersection(keysDown).isEmpty {
if let camera = camera {
let deltaX = directionFactor(positive: [UnicodeScalar(NSRightArrowFunctionKey)!], negative: [UnicodeScalar(NSLeftArrowFunctionKey)!])
let deltaZ = directionFactor(positive: [UnicodeScalar(NSDownArrowFunctionKey)!], negative: [UnicodeScalar(NSUpArrowFunctionKey)!])
if deltaX != nil || deltaZ != nil {
let diffX = (deltaX ?? 0) * 0.1 * Float(diff * GLLView.unitsPerSecond)
let diffZ = (deltaZ ?? 0) * 0.1 * Float(diff * GLLView.unitsPerSecond)
let screenDelta = SIMD4<Float32>(x: diffX,
y: 0,
z: diffZ,
w: 0)
let worldDelta = camera.viewMatrix.inverse * screenDelta
for item in document?.selection.selectedItems ?? [] {
item.positionX += worldDelta.x
item.positionZ += worldDelta.z
}
}
}
}
// Controller input
if GLLView.lastActiveView == self, let camera = camera, !camera.cameraLocked {
if !all(rotationInsideDeadzone) || !all(positionInsideDeadzone) {
// Update based on space mouse inputs
// Need to swap the coordinates because the space mouse uses a different coordinate system from us
let positionSpeed = Float(spaceMouseTranslationSpeed * diff)
camera.moveLocalX(adjustedPosition.x * positionSpeed, y: -adjustedPosition.z * positionSpeed, z: adjustedPosition.y * positionSpeed)
let rotationSpeed = Float(spaceMouseRotationSpeed * diff)
if spaceMouseMode == .rotateAroundTarget {
camera.longitude -= adjustedRotation.z * rotationSpeed;
camera.latitude += adjustedRotation.x * rotationSpeed;
} else {
turnAroundCamera(deltaX: adjustedRotation.z * rotationSpeed, deltaY: -adjustedRotation.x * rotationSpeed)
}
}
// Game controller input
if let controller = GCController.current, let extendedGamepad = controller.extendedGamepad {
// Rotate camera based on left thumbstick
let rotationX = extendedGamepad.leftThumbstick.xAxis.value
let rotationY = extendedGamepad.leftThumbstick.yAxis.value
if rotationX != 0.0 || rotationY != 0.0 {
let rotationSpeed = Float(controllerRotationSpeedCamera * diff)
let xSign: Float = UserDefaults.standard.bool(forKey: GLLPrefControllerInvertXAxis) ? -1.0 : 1.0
let ySign: Float = UserDefaults.standard.bool(forKey: GLLPrefControllerInvertYAxis) ? -1.0 : 1.0
if controllerLeftStickMode == .rotateAroundTarget {
camera.longitude -= rotationX * rotationSpeed * xSign;
camera.latitude += rotationY * rotationSpeed * ySign;
} else {
turnAroundCamera(deltaX: rotationX * rotationSpeed * xSign, deltaY: -rotationY * rotationSpeed * ySign)
}
}
let otherStickX = extendedGamepad.rightThumbstick.xAxis.value
let otherStickY = -extendedGamepad.rightThumbstick.yAxis.value
let otherStickZ = extendedGamepad.leftTrigger.value * -1 + extendedGamepad.rightTrigger.value
if otherStickX != 0.0 || otherStickZ != 0.0 || otherStickY != 0.0 {
if controllerRightStickMode == .moveCamera {
let positionSpeed = Float(controllerMoveCameraSpeed * diff)
camera.moveLocalX(otherStickX * positionSpeed, y: otherStickZ * positionSpeed, z: otherStickY * positionSpeed)
} else if let bones = document?.selection.selectedBones, !bones.isEmpty {
if controllerRightStickMode == .moveBones {
let movementSpeed = Float(controllerMoveCameraSpeed * controllerMoveBoneSpeed * diff)
for bone in bones {
// Move bone
// Note y/z swap that's on purpose
bone.positionX += otherStickX * movementSpeed
bone.positionY += otherStickZ * movementSpeed
bone.positionZ += otherStickY * movementSpeed
}
} else {
let rotationSpeed = Float(controllerRotationSpeedCamera * controllerRotationSpeedBone * diff)
// Note x/y swapped that's on purpose
rotateBones(eulerAngles: SIMD3<Float>(x: otherStickY, y: otherStickX, z: otherStickZ) * rotationSpeed)
}
}
}
}
}
}
private func rotateBones(eulerAngles: SIMD3<Float>) {
guard let bones = document?.selection.selectedBones, !bones.isEmpty, eulerAngles != SIMD3<Float>(repeating: 0) else {
return
}
for bone in bones {
// Rotate bone
let current = bone.rotation
let multiply = GLLItemBone.rotationMatrix(angles: eulerAngles)
let modified = current * multiply
let angles = GLLItemBone.eulerAngles(rotationMatrix: modified)
bone.rotationX = angles.x
bone.rotationY = angles.y
bone.rotationZ = angles.z
}
}
private var currentControllerActive: Bool {
guard let gamepad = GCController.current?.extendedGamepad else {
return false
}
return gamepad.leftTrigger.value != 0 || gamepad.rightTrigger.value != 0 || gamepad.leftThumbstick.xAxis.value != 0 || gamepad.leftThumbstick.yAxis.value != 0 || gamepad.rightThumbstick.xAxis.value != 0 || gamepad.rightThumbstick.yAxis.value != 0
}
/**
Returns 1 if any "positive" keys are pressed and none of the "negative" ones, -1 if any of the "negative" keys are pressed and none of the positive ones, and 0 if keys from neither or both sets are pressed
*/
private func directionFactor(positive: [UnicodeScalar], negative: [UnicodeScalar]) -> Float? {
let anyInPositive = !CharacterSet(positive).isDisjoint(with: keysDown)
let anyInNegative = !CharacterSet(negative).isDisjoint(with: keysDown)
if anyInPositive && !anyInNegative {
return 1.0
} else if anyInNegative && !anyInPositive {
return -1.0
} else {
return nil
}
}
private func closestBone(atScreenPoint point: NSPoint, from bones: [GLLItemBone]) -> GLLItemBone? {
guard let camera = camera else {
return nil
}
let viewProjection = camera.viewProjectionMatrix
let size = simd_float2(Float(bounds.width), Float(bounds.height))
var closestDistance = Float.infinity
var closestBone: GLLItemBone? = nil
for bone in bones {
let worldPosition = bone.globalPosition
var screenPosition = viewProjection * worldPosition
screenPosition /= screenPosition.w
let screen2D = simd_float2(x: screenPosition.x, y: screenPosition.y)
let screenXY = (screen2D * 0.5 + 0.5) * size
let distanceToRay = simd_length(screenXY)
if distanceToRay > 10 {
continue
}
if screenPosition.z < closestDistance {
closestDistance = screenPosition.z
closestBone = bone
}
}
return closestBone
}
private func turnAroundCamera(deltaX: Float, deltaY: Float) {
guard let camera = camera, !camera.cameraLocked else {
return
}
// TODO None of these maths work
// Turn camera around it's current position. To do this:
// 1. Find current position
let position = camera.cameraWorldPosition
// 2. Calculate new position of target
let cameraRelativeLatitude = camera.latitude - deltaY
let cameraRelativeLongitude = camera.longitude - deltaX
let viewDirection = simd_mat_euler(vec_float4(cameraRelativeLatitude, cameraRelativeLongitude, 0, 0), vec_float4(0, 0, 0, 1)) * vec_float4(0, 0, 1, 0)
let newTargetPosition = position - viewDirection * camera.distance
camera.positionX = newTargetPosition.x;
camera.positionY = newTargetPosition.y;
camera.positionZ = newTargetPosition.z;
// 3. Calculate new rotation of camera
camera.longitude -= deltaX;
camera.latitude -= deltaY;
unpause()
}
// TODO Read from prefs
var spaceMouseDeadZoneTranslation: Double {
UserDefaults.standard.double(forKey: GLLPrefSpaceMouseDeadzoneTranslation)
}
var spaceMouseDeadZoneRotation: Double {
UserDefaults.standard.double(forKey: GLLPrefSpaceMouseDeadzoneRotation)
}
var spaceMouseRotationSpeed: Double {
UserDefaults.standard.double(forKey: GLLPrefSpaceMouseSpeedRotation)
}
var spaceMouseTranslationSpeed: Double {
UserDefaults.standard.double(forKey: GLLPrefSpaceMouseSpeedTranslation)
}
enum CameraMovementMode: String, CaseIterable {
case rotateAroundTarget
case rotateAroundCamera
}
var spaceMouseMode: CameraMovementMode {
return CameraMovementMode(rawValue: UserDefaults.standard.string(forKey: GLLPrefSpaceMouseMode) ?? CameraMovementMode.rotateAroundCamera.rawValue) ?? .rotateAroundCamera
}
var controllerLeftStickMode: CameraMovementMode {
return CameraMovementMode(rawValue: UserDefaults.standard.string(forKey: GLLPrefControllerLeftStickMode) ?? CameraMovementMode.rotateAroundCamera.rawValue) ?? .rotateAroundCamera
}
var controllerRotationSpeedCamera: Double {
return UserDefaults.standard.double(forKey: GLLPrefControllerCameraRotationSpeed)
}
var controllerMoveCameraSpeed: Double {
return UserDefaults.standard.double(forKey: GLLPrefControllerCameraMovementSpeed)
}
var controllerRotationSpeedBone: Double {
return UserDefaults.standard.double(forKey: GLLPrefControllerBoneRotationSpeed)
}
var controllerMoveBoneSpeed: Double {
return UserDefaults.standard.double(forKey: GLLPrefControllerBoneMovementSpeed)
}
enum ControllerRightStickMode: String, CaseIterable {
case moveCamera
case rotateBones
case moveBones
}
var controllerRightStickMode: ControllerRightStickMode {
return ControllerRightStickMode(rawValue: UserDefaults.standard.string(forKey: GLLPrefControllerRightStickMode) ?? ControllerRightStickMode.moveCamera.rawValue) ?? .moveCamera
}
}