|
| 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