-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.swift
94 lines (89 loc) · 3.75 KB
/
input.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
import GameController
import simd
class InputController {
#if os(macOS)
var mouse = simd_float2.zero
var keys: [Bool] = Array(repeating: false, count: 1024)
var cursorHidden = false
var mouseInWindow: (() -> Bool)?
#else
var leftControllerDelta = simd_float2.zero
var rightControllerDelta = simd_float2.zero
var controller: GCVirtualController?
#endif
func setupInput() {
#if os(macOS)
NotificationCenter.default.addObserver(forName: .GCKeyboardDidConnect, object: nil, queue: nil, using: keyboardDidConnect)
NotificationCenter.default.addObserver(forName: .GCMouseDidConnect, object: nil, queue: nil, using: mouseDidConnect)
#else
NotificationCenter.default.addObserver(forName: .GCControllerDidConnect, object: nil, queue: nil, using: controllerDidConnect)
let config = GCVirtualController.Configuration()
config.elements = [GCInputLeftThumbstick, GCInputRightThumbstick]
controller = GCVirtualController(configuration: config)
controller?.connect()
#endif
}
#if os(macOS)
func keyboardDidConnect(notification: Notification) {
guard let keyboard = notification.object as? GCKeyboard,
let keyboardInput = keyboard.keyboardInput else { return }
keyboardInput.keyChangedHandler = { [weak self] _, _, keyCode, pressed in
self?.keys[keyCode.rawValue] = pressed
}
}
func mouseDidConnect(notification: Notification) {
guard let mouse = notification.object as? GCMouse,
let mouseInput = mouse.mouseInput else { return }
mouseInput.mouseMovedHandler = { [weak self] _, deltaX, deltaY in
if self?.cursorHidden == true {
self?.mouse.x += deltaX
self?.mouse.y += deltaY
}
}
mouseInput.leftButton.pressedChangedHandler = { [weak self] _, _, pressed in
if pressed {
if self?.cursorHidden == true {
self?.cursorHidden = false
NSCursor.unhide()
CGAssociateMouseAndMouseCursorPosition(1)
} else if self?.mouseInWindow?() == true {
self?.cursorHidden = true
NSCursor.hide()
CGAssociateMouseAndMouseCursorPosition(0)
}
}
}
}
#else
func controllerDidConnect(notification: Notification) {
guard let controller = notification.object as? GCController else { return }
controller.physicalInputProfile.dpads[GCInputLeftThumbstick]?.valueChangedHandler = { [weak self] pad, deltaX, deltaY in
self?.leftControllerDelta.x = deltaX
self?.leftControllerDelta.y = deltaY
}
controller.physicalInputProfile.dpads[GCInputRightThumbstick]?.valueChangedHandler = { [weak self] pad, deltaX, deltaY in
self?.rightControllerDelta.x = deltaX
self?.rightControllerDelta.y = deltaY
}
}
#endif
func updateInput(_ input: inout Input) {
#if os(macOS)
if keys[GCKeyCode.escape.rawValue] { NSApplication.shared.terminate(nil) }
let speed: Float = keys[GCKeyCode.leftShift.rawValue] || keys[GCKeyCode.rightShift.rawValue] ? 2 : 1
input.left = keys[GCKeyCode.keyA.rawValue] ? speed : 0
input.right = keys[GCKeyCode.keyD.rawValue] ? speed : 0
input.up = keys[GCKeyCode.keyW.rawValue] ? speed : 0
input.down = keys[GCKeyCode.keyS.rawValue] ? speed : 0
if cursorHidden {
input.mouse = mouse
}
#else
input.left = -leftControllerDelta.x
input.right = leftControllerDelta.x
input.up = leftControllerDelta.y
input.down = -leftControllerDelta.y
input.mouse += 6 * rightControllerDelta
#endif
}
}