Skip to content

Commit 91229e6

Browse files
committed
Rework HID
1 parent 5f7af74 commit 91229e6

File tree

11 files changed

+419
-120
lines changed

11 files changed

+419
-120
lines changed

Sources/GateEngine/System/HID/HID.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ public struct InputRecipts {
2626
public let keyboard: Keyboard = Keyboard()
2727
public let mouse: Mouse = Mouse()
2828
public let screen: Screen = Screen()
29+
public let surfaces: SurfaceDevices = SurfaceDevices()
2930
public internal(set) lazy var gamePads: GamePadManger = GamePadManger(hid: self)
3031

3132
@inline(__always)
3233
func update(_ deltaTime: Float) {
33-
self.gamePads.update()
34-
self.screen.update()
3534
self.mouse.update()
35+
self.screen.update()
36+
self.surfaces.update()
37+
self.gamePads.update()
3638
}
3739

3840
internal init() {}
@@ -44,14 +46,18 @@ extension HID /*WindowDelegate*/ {
4446
mouse.mouseChange(event: event, position: position, delta: delta, window: window)
4547
}
4648
@_transparent
47-
func mouseClick(event: MouseClickEvent, button: MouseButton, count: Int?, position: Position2, window: Window?) {
48-
mouse.mouseClick(event: event, button: button, count: count, position: position, window: window)
49+
func mouseClick(event: MouseClickEvent, button: MouseButton, count: Int?) {
50+
mouse.mouseClick(event: event, button: button, count: count)
4951
}
5052

5153
@_transparent
52-
func touchChange(id: AnyHashable, kind: TouchKind, event: TouchChangeEvent, position: Position2) {
54+
func screenTouchChange(id: AnyHashable, kind: TouchKind, event: TouchChangeEvent, position: Position2) {
5355
screen.touchChange(id: id, kind: kind, event: event, position: position)
5456
}
57+
@_transparent
58+
func surfaceTouchChange(id: AnyHashable, event: TouchChangeEvent, surfaceID: AnyHashable, normalizedPosition: Position2) {
59+
surfaces.surfaceTouchChange(id: id, event: event, surfaceID: surfaceID, normalizedPosition: normalizedPosition)
60+
}
5561

5662
@_transparent
5763
func keyboardRequestedHandling(key: KeyboardKey,

Sources/GateEngine/System/HID/Keyboard.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,33 @@ import GameMath
1818
if let existing = buttons[keyboardKey] {
1919
return existing
2020
}
21-
let button = ButtonState(keyboard: self)
21+
let button = ButtonState(keyboard: self, key: keyboardKey)
2222
buttons[keyboardKey] = button
2323
return button
2424
}
25+
26+
@inlinable @inline(__always)
27+
public func pressedButtons() -> [KeyboardKey:ButtonState] {
28+
return buttons.filter({$0.value.isPressed})
29+
}
2530
}
2631

2732
public extension Keyboard {
28-
@MainActor final class ButtonState {
33+
@MainActor final class ButtonState: CustomStringConvertible {
2934
@usableFromInline
3035
internal unowned let keyboard: Keyboard
36+
let key: KeyboardKey
3137
@usableFromInline
3238
internal var currentRecipt: UInt8 = 0
3339

40+
nonisolated public var description: String {
41+
return "\(key)"
42+
}
43+
3444
@usableFromInline
35-
internal init(keyboard: Keyboard) {
45+
internal init(keyboard: Keyboard, key: KeyboardKey) {
3646
self.keyboard = keyboard
47+
self.key = key
3748
}
3849

3950
/// A mask representing special keys that might alter the behavior of this key.

Sources/GateEngine/System/HID/Mouse.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ extension Mouse {
185185
}
186186
}
187187
@inline(__always)
188-
func mouseClick(event: MouseClickEvent, button: MouseButton, count: Int?, position: Position2, window: Window?) {
189-
self._window = window
190-
self._position = position
188+
func mouseClick(event: MouseClickEvent, button: MouseButton, count: Int?) {
191189
let button = self.button(button)
192190
button.isPressed = (event == .buttonDown)
193191
button.pressCount = count

Sources/GateEngine/System/HID/Screen.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ public extension HID {
8181

8282
@inlinable @inline(__always)
8383
public var normalizedPosition: Position2 {
84-
#if os(macOS) && !targetEnvironment(macCatalyst)
85-
// Mac trackpad is already normalized
86-
if kind == .indirect {
87-
return position
88-
}
89-
#endif
9084
var p = position
9185
if let bounds = window?.size {
9286
p.x = (1 / bounds.width) * p.x
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright © 2023 Dustin Collins (Strega's Gate)
3+
* All Rights Reserved.
4+
*
5+
* http://stregasgate.com
6+
*/
7+
8+
import GameMath
9+
10+
public extension HID {
11+
@MainActor final class SurfaceDevices {
12+
public private(set) var surfaces: Set<Surface> = []
13+
14+
@inline(__always)
15+
internal func surfaceForID(_ surfaceID: AnyHashable) -> Surface {
16+
if let existing = surfaces.first(where: {$0.id == surfaceID}) {
17+
return existing
18+
}
19+
let new = Surface(id: surfaceID)
20+
surfaces.insert(new)
21+
return new
22+
}
23+
24+
@inline(__always)
25+
func surfaceTouchChange(id: AnyHashable, event: TouchChangeEvent, surfaceID: AnyHashable, normalizedPosition: Position2) {
26+
self.surfaceForID(surfaceID).touchChange(id: id, event: event, normalizedPosition: normalizedPosition)
27+
}
28+
29+
@inlinable @inline(__always)
30+
public var any: Surface? {
31+
return surfaces.first
32+
}
33+
34+
@inline(__always)
35+
func update() {
36+
37+
}
38+
}
39+
40+
@MainActor final class Surface {
41+
@usableFromInline
42+
nonisolated let id: AnyHashable
43+
public internal(set) var touches: Set<SurfaceTouch> = []
44+
internal var nextTouches: Set<SurfaceTouch> = []
45+
46+
internal init(id: AnyHashable) {
47+
self.id = id
48+
}
49+
50+
public enum Gesture {
51+
case touchDown
52+
case touchUp
53+
}
54+
55+
@inlinable @inline(__always)
56+
public func anyTouch(if gesture: Gesture) -> SurfaceTouch? {
57+
return touches.first { touch in
58+
return (gesture == .touchUp && touch.phase == .up) || (gesture == .touchDown && touch.phase == .down)
59+
}
60+
}
61+
62+
@inlinable @inline(__always)
63+
public func anyTouch(withPhase phase: SurfaceTouch.Phase) -> SurfaceTouch? {
64+
return touches.first(where: {$0.phase == phase})
65+
}
66+
67+
@inline(__always)
68+
private func existingTouch(_ id: AnyHashable) -> SurfaceTouch? {
69+
return touches.first(where: {$0.id == id})
70+
}
71+
72+
@usableFromInline
73+
internal func touchChange(id: AnyHashable, event: TouchChangeEvent, normalizedPosition: Position2) {
74+
let touch: SurfaceTouch
75+
if let existing = self.existingTouch(id) {
76+
touch = existing
77+
touch.position = normalizedPosition
78+
}else{
79+
touch = SurfaceTouch(id: id, normalizedPosition: normalizedPosition, phase: .down)
80+
}
81+
switch event {
82+
case .began:
83+
touch.phase = .down
84+
case .moved:
85+
touch.phase = .down
86+
case .ended:
87+
touch.phase = .up
88+
case .canceled:
89+
touch.phase = .cancelled
90+
}
91+
92+
nextTouches.insert(touch)
93+
}
94+
95+
@inline(__always)
96+
func update() {
97+
let oldTouches = touches
98+
touches = nextTouches
99+
nextTouches = oldTouches
100+
nextTouches.removeAll(keepingCapacity: true)
101+
}
102+
}
103+
}
104+
105+
extension HID.Surface: Hashable {
106+
nonisolated public func hash(into hasher: inout Hasher) {
107+
hasher.combine(id)
108+
}
109+
@_transparent
110+
nonisolated public static func ==(lhs: HID.Surface, rhs: HID.Surface) -> Bool {
111+
return lhs.id == rhs.id
112+
}
113+
}
114+
115+
@MainActor public class SurfaceTouch {
116+
public internal(set) var id: AnyHashable
117+
public internal(set) var position: Position2
118+
public internal(set) var phase: Phase
119+
public enum Phase {
120+
case down
121+
case up
122+
case cancelled
123+
}
124+
125+
init(id: AnyHashable, normalizedPosition: Position2, phase: Phase) {
126+
self.id = id
127+
self.position = normalizedPosition
128+
self.phase = phase
129+
}
130+
}
131+
132+
extension SurfaceTouch: Equatable {
133+
@_transparent
134+
public static func ==(lhs: SurfaceTouch, rhs: SurfaceTouch) -> Bool {
135+
return lhs.id == rhs.id
136+
}
137+
}
138+
extension SurfaceTouch: Hashable {
139+
public func hash(into hasher: inout Hasher) {
140+
hasher.combine(id)
141+
}
142+
}

0 commit comments

Comments
 (0)