From 893bf513add7bc1c660d139aad9fa341230dfc76 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Fri, 15 May 2020 09:59:53 +0200 Subject: [PATCH] Few mouse interaction extensions for sdlui. - Added Mouse{Enter,Leave}Event support. - Simulating MouseWheel for MouseMotionEvent. --- sdlui/renderer.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sdlui/renderer.go b/sdlui/renderer.go index e0f96e7..fbdffc4 100644 --- a/sdlui/renderer.go +++ b/sdlui/renderer.go @@ -24,6 +24,7 @@ type Renderer struct { refresh uint32 fonts map[string]*Font + mouse geom.PointF32 cursor ui.MouseCursor cursors map[sdl.SystemCursor]*sdl.Cursor } @@ -114,7 +115,6 @@ func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) { r.cursor = ui.MouseCursorDefault var unhandled bool - // TODO: simulate ui.MouseEnter & ui.MouseLeave? switch e := event.(type) { case *sdl.WindowEvent: switch e.Event { @@ -122,7 +122,12 @@ func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) { t.Handle(&ui.DisplayCloseEvent{EventBase: eventBase(e)}) case sdl.WINDOWEVENT_RESIZED: t.Handle(&ui.DisplayResizeEvent{EventBase: eventBase(e), Bounds: r.WindowBounds()}) - + case sdl.WINDOWEVENT_ENTER: + t.Handle(&ui.MouseEnterEvent{MouseEvent: ui.MouseEvent{EventBase: eventBase(e), X: r.mouse.X, Y: r.mouse.Y}}) + case sdl.WINDOWEVENT_LEAVE: + t.Handle(&ui.MouseLeaveEvent{MouseEvent: ui.MouseEvent{EventBase: eventBase(e), X: r.mouse.X, Y: r.mouse.Y}}) + default: + unhandled = true } case *sdl.KeyboardEvent: @@ -153,7 +158,10 @@ func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) { t.Handle(&ui.MouseButtonUpEvent{MouseEvent: mouseEvent(e, e.X, e.Y), Button: mouseButton(e.Button)}) } case *sdl.MouseMotionEvent: - t.Handle(&ui.MouseMoveEvent{MouseEvent: ui.MouseEvent{EventBase: eventBase(e), X: float32(e.X), Y: float32(e.Y)}, MouseWheel: 0}) + r.mouse = geom.PtF32(float32(e.X), float32(e.Y)) + t.Handle(&ui.MouseMoveEvent{MouseEvent: ui.MouseEvent{EventBase: eventBase(e), X: r.mouse.X, Y: r.mouse.Y}, MouseWheel: 0}) + case *sdl.MouseWheelEvent: + t.Handle(&ui.MouseMoveEvent{MouseEvent: ui.MouseEvent{EventBase: eventBase(e), X: r.mouse.X, Y: r.mouse.Y}, MouseWheel: float32(e.Y)}) case *sdl.UserEvent: if r.refresh == e.Type { t.Handle(&ui.RefreshEvent{EventBase: eventBase(e)})