Skip to content

Commit 79643e7

Browse files
committed
Add window enter/leave event in SDLBackend
1 parent 8db2fbc commit 79643e7

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Sources/ShaftSDL3/SDLBackend.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public class SDLBackend: Backend {
101101
onMouseButton(event.button, isDown: true)
102102
case SDL_EVENT_MOUSE_WHEEL:
103103
onMouseWheel(event.wheel)
104+
case SDL_EVENT_WINDOW_MOUSE_ENTER:
105+
onMouseEnter(event.window)
106+
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
107+
onMouseLeave(event.window)
104108
// case SDL_EVENT_WINDOWEVENT:
105109
// switch SDL_WindowEventID(Uint32(event.window.event)) {
106110
// case SDL_EVENT_WINDOWEVENT_RESIZED:
@@ -424,6 +428,60 @@ public class SDLBackend: Backend {
424428
onPointerData?(packet)
425429
}
426430

431+
/// Fires ``onPointerData``
432+
private func onMouseEnter(_ event: SDL_WindowEvent) {
433+
guard let view = viewByID[Int(event.windowID)] else {
434+
return
435+
}
436+
437+
// Get the current mouse position
438+
var x: Float = 0
439+
var y: Float = 0
440+
SDL_GetMouseState(&x, &y)
441+
442+
// Convert to physical coordinates
443+
let sdlPixelDensity = view.sdlPixelDensity
444+
let packet = PointerData(
445+
viewId: Int(event.windowID),
446+
timeStamp: Duration.milliseconds(event.timestamp),
447+
change: .add,
448+
kind: .mouse,
449+
device: 0,
450+
pointerIdentifier: 0,
451+
physicalX: Int(x * sdlPixelDensity),
452+
physicalY: Int(y * sdlPixelDensity),
453+
buttons: buttonState
454+
)
455+
onPointerData?(packet)
456+
}
457+
458+
/// Fires ``onPointerData``
459+
private func onMouseLeave(_ event: SDL_WindowEvent) {
460+
guard let view = viewByID[Int(event.windowID)] else {
461+
return
462+
}
463+
464+
// Get the current mouse position
465+
var x: Float = 0
466+
var y: Float = 0
467+
SDL_GetMouseState(&x, &y)
468+
469+
// Convert to physical coordinates
470+
let sdlPixelDensity = view.sdlPixelDensity
471+
let packet = PointerData(
472+
viewId: Int(event.windowID),
473+
timeStamp: Duration.milliseconds(event.timestamp),
474+
change: .remove,
475+
kind: .mouse,
476+
device: 0,
477+
pointerIdentifier: 0,
478+
physicalX: Int(x * sdlPixelDensity),
479+
physicalY: Int(y * sdlPixelDensity),
480+
buttons: buttonState
481+
)
482+
onPointerData?(packet)
483+
}
484+
427485
/// Currently, it's the view's responsibility to call this method when the
428486
/// window is resized.
429487
public var onMetricsChanged: MetricsChangedCallback?

0 commit comments

Comments
 (0)