Skip to content

Commit 210dd0b

Browse files
committed
Enhance touch input for UI
Adds support for trackpads
1 parent f2cf248 commit 210dd0b

File tree

6 files changed

+106
-44
lines changed

6 files changed

+106
-44
lines changed

Sources/GateEngine/System/HID/HID.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ extension HID {
132132
id: AnyHashable,
133133
event: TouchChangeEvent,
134134
surfaceID: AnyHashable,
135-
normalizedPosition: Position2
135+
normalizedPosition: Position2,
136+
pressure: Float,
137+
window: Window?
136138
) {
137139
recentInputMethod = .touchSurface
138140
surfaces.surfaceTouchChange(
@@ -141,6 +143,7 @@ extension HID {
141143
surfaceID: surfaceID,
142144
normalizedPosition: normalizedPosition
143145
)
146+
window?.surfaceTouchChange(id: id, kind: .unknown, event: event, normalizedPosition: normalizedPosition, pressure: pressure, mouse: self.mouse)
144147
}
145148

146149
@_transparent

Sources/GateEngine/System/HID/Touch/Touch.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import GameMath
1111
public internal(set) var id: AnyHashable
1212
public internal(set) weak var window: Window?
1313
public internal(set) var position: Position2
14-
private var _precisionPosition: Position2?
14+
internal var _precisionPosition: Position2?
1515
public var precisionPosition: Position2 {
1616
return _precisionPosition ?? position
1717
}

Sources/GateEngine/System/Platforms/Apple/AppKit/AppKit/AppKitWindow.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,9 @@ extension UGNSWindow {
609609
id: id,
610610
event: .began,
611611
surfaceID: ObjectIdentifier(device),
612-
normalizedPosition: position
612+
normalizedPosition: position,
613+
pressure: 0,
614+
window: window
613615
)
614616
}
615617
default:
@@ -643,7 +645,9 @@ extension UGNSWindow {
643645
id: id,
644646
event: .moved,
645647
surfaceID: ObjectIdentifier(device),
646-
normalizedPosition: position
648+
normalizedPosition: position,
649+
pressure: 0,
650+
window: window
647651
)
648652
}
649653
default:
@@ -677,7 +681,9 @@ extension UGNSWindow {
677681
id: id,
678682
event: .ended,
679683
surfaceID: ObjectIdentifier(device),
680-
normalizedPosition: position
684+
normalizedPosition: position,
685+
pressure: 0,
686+
window: window
681687
)
682688
}
683689
default:
@@ -712,7 +718,9 @@ extension UGNSWindow {
712718
id: id,
713719
event: .canceled,
714720
surfaceID: ObjectIdentifier(device),
715-
normalizedPosition: position
721+
normalizedPosition: position,
722+
pressure: 0,
723+
window: window
716724
)
717725
}
718726
default:

Sources/GateEngine/System/Platforms/Apple/UIKit/UIKit/UIKitViewController.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ internal class UIKitViewController: GCEventViewController {
164164
id: id,
165165
event: .began,
166166
surfaceID: ObjectIdentifier(UIDevice.current),
167-
normalizedPosition: location.position
167+
normalizedPosition: location.position,
168+
pressure: Float(touch.force / touch.maximumPossibleForce),
169+
window: self.window.window
168170
)
169171
default:
170172
break
@@ -205,7 +207,9 @@ internal class UIKitViewController: GCEventViewController {
205207
id: id,
206208
event: .moved,
207209
surfaceID: ObjectIdentifier(UIDevice.current),
208-
normalizedPosition: location.position
210+
normalizedPosition: location.position,
211+
pressure: Float(touch.force / touch.maximumPossibleForce),
212+
window: self.window.window
209213
)
210214
default:
211215
break
@@ -246,7 +250,9 @@ internal class UIKitViewController: GCEventViewController {
246250
id: id,
247251
event: .ended,
248252
surfaceID: ObjectIdentifier(UIDevice.current),
249-
normalizedPosition: location.position
253+
normalizedPosition: location.position,
254+
pressure: Float(touch.force / touch.maximumPossibleForce),
255+
window: self.window.window
250256
)
251257
default:
252258
break
@@ -287,7 +293,9 @@ internal class UIKitViewController: GCEventViewController {
287293
id: id,
288294
event: .canceled,
289295
surfaceID: ObjectIdentifier(UIDevice.current),
290-
normalizedPosition: location.position
296+
normalizedPosition: location.position,
297+
pressure: Float(touch.force / touch.maximumPossibleForce),
298+
window: self.window.window
291299
)
292300
default:
293301
break

Sources/GateEngine/UI/View.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,19 @@ open class View {
259259

260260
}
261261

262+
open func surfaceTouchesBegan(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
263+
264+
}
265+
open func surfaceTouchesMoved(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
266+
267+
}
268+
open func surfaceTouchesEnded(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
269+
270+
}
271+
open func surfaceTouchesCanceled(_ touches: Set<SurfaceTouch>, mouse: Mouse) {
272+
273+
}
274+
262275
open func cursorEntered(_ cursor: Mouse) {
263276

264277
}

Sources/GateEngine/UI/Window.swift

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -554,45 +554,75 @@ public struct WindowOptions: OptionSet {
554554
view.touchesBegan([touch])
555555
}
556556
case .moved:
557-
let touch = Touch(
558-
id: id,
559-
window: self,
560-
position: position,
561-
precisionPosition: precisionPosition,
562-
pressure: pressure,
563-
phase: .down,
564-
kind: kind
565-
)
566-
if let view = currentlyHitTouchViews[touch] {
567-
view.touchesMoved([touch])
557+
if let touch = currentlyHitTouchViews.keys.first(where: {$0.id == id}) {
558+
touch.position = position
559+
touch._precisionPosition = precisionPosition
560+
touch.pressure = pressure
561+
if let view = currentlyHitTouchViews[touch] {
562+
view.touchesMoved([touch])
563+
}
568564
}
569565
case .ended:
570-
let touch = Touch(
571-
id: id,
572-
window: self,
573-
position: position,
574-
precisionPosition: precisionPosition,
575-
pressure: pressure,
576-
phase: .down,
577-
kind: kind
578-
)
579-
if let view = currentlyHitTouchViews[touch] {
580-
view.touchesEnded([touch])
581-
currentlyHitTouchViews[touch] = nil
566+
if let touch = currentlyHitTouchViews.keys.first(where: {$0.id == id}) {
567+
if let view = currentlyHitTouchViews[touch] {
568+
view.touchesEnded([touch])
569+
currentlyHitTouchViews[touch] = nil
570+
}
582571
}
583572
case .canceled:
584-
let touch = Touch(
585-
id: id,
586-
window: self,
587-
position: position,
588-
precisionPosition: precisionPosition,
589-
pressure: pressure,
590-
phase: .down,
591-
kind: kind
573+
if let touch = currentlyHitTouchViews.keys.first(where: {$0.id == id}) {
574+
if let view = currentlyHitTouchViews[touch] {
575+
view.touchesCanceled([touch])
576+
currentlyHitTouchViews[touch] = nil
577+
}
578+
}
579+
}
580+
}
581+
582+
var currentlyHitSurfaceTouchViews: [SurfaceTouch:View] = [:]
583+
584+
internal func surfaceTouchChange(
585+
id: AnyHashable,
586+
kind: TouchKind,
587+
event: TouchChangeEvent,
588+
normalizedPosition: Position2,
589+
pressure: Float,
590+
mouse: Mouse
591+
) {
592+
593+
switch event {
594+
case .began:
595+
let touch = SurfaceTouch(
596+
id: id,
597+
normalizedPosition: normalizedPosition,
598+
phase: .down
592599
)
593-
if let view = currentlyHitTouchViews[touch] {
594-
view.touchesCanceled([touch])
595-
currentlyHitTouchViews[touch] = nil
600+
if let mousePosition = mouse.position {
601+
if let view = self.hitTest(mousePosition, clipRect: Rect(size: self.size)) {
602+
currentlyHitSurfaceTouchViews[touch] = view
603+
view.surfaceTouchesBegan([touch], mouse: mouse)
604+
}
605+
}
606+
case .moved:
607+
if let touch = currentlyHitSurfaceTouchViews.keys.first(where: {$0.id == id}) {
608+
touch.position = normalizedPosition
609+
if let view = currentlyHitSurfaceTouchViews[touch] {
610+
view.surfaceTouchesMoved([touch], mouse: mouse)
611+
}
612+
}
613+
case .ended:
614+
if let touch = currentlyHitSurfaceTouchViews.keys.first(where: {$0.id == id}) {
615+
if let view = currentlyHitSurfaceTouchViews[touch] {
616+
view.surfaceTouchesEnded([touch], mouse: mouse)
617+
currentlyHitSurfaceTouchViews[touch] = nil
618+
}
619+
}
620+
case .canceled:
621+
if let touch = currentlyHitSurfaceTouchViews.keys.first(where: {$0.id == id}) {
622+
if let view = currentlyHitSurfaceTouchViews[touch] {
623+
view.surfaceTouchesCanceled([touch], mouse: mouse)
624+
currentlyHitSurfaceTouchViews[touch] = nil
625+
}
596626
}
597627
}
598628
}

0 commit comments

Comments
 (0)