From 803e42714ec9c6388919a3ad547f41d0dd9752a1 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 30 Sep 2018 17:08:16 +0900 Subject: [PATCH] input: Implement wheel event on browsers Fixes #630 --- internal/input/input_js.go | 15 +++++++++++++-- internal/ui/ui_js.go | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/internal/input/input_js.go b/internal/input/input_js.go index 5712554dd..39ced91b4 100644 --- a/internal/input/input_js.go +++ b/internal/input/input_js.go @@ -35,6 +35,8 @@ type Input struct { mouseButtonPressed map[int]bool cursorX int cursorY int + wheelX float64 + wheelY float64 gamepads [16]gamePad touches []*Touch runeBuffer []rune @@ -49,6 +51,11 @@ func (i *Input) ClearRuneBuffer() { i.runeBuffer = nil } +func (i *Input) ResetWheelValues() { + i.wheelX = 0 + i.wheelY = 0 +} + func (i *Input) IsKeyPressed(key Key) bool { if i.keyPressed != nil { for _, c := range keyToCodes[key] { @@ -92,8 +99,7 @@ func (i *Input) IsMouseButtonPressed(button MouseButton) bool { } func (i *Input) Wheel() (xoff, yoff float64) { - return 0, 0 - // TODO: Mouse scroll functionality is not yet implemented in js + return i.wheelX, i.wheelY } func (i *Input) keyDown(code string) { @@ -241,6 +247,11 @@ func OnMouseMove(e js.Value) { setMouseCursorFromEvent(e) } +func OnWheel(e js.Value) { + theInput.wheelX = e.Get("deltaX").Float() + theInput.wheelY = e.Get("deltaY").Float() +} + func OnTouchStart(e js.Value) { theInput.updateTouches(e) } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index cb78217f2..37afdc500 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -207,8 +207,7 @@ func (u *userInterface) update(g GraphicsContext) error { u.updateGraphicsContext(g) if err := g.Update(func() { input.Get().ClearRuneBuffer() - // TODO: insert ResetScrollValues() counterpart to 'ui_glfw.go' here - // The offscreens must be updated every frame (#490). + input.Get().ResetWheelValues() u.updateGraphicsContext(g) }); err != nil { return err @@ -322,6 +321,7 @@ func init() { canvas.Call("addEventListener", "mousedown", js.NewEventCallback(js.PreventDefault, input.OnMouseDown)) canvas.Call("addEventListener", "mouseup", js.NewEventCallback(js.PreventDefault, input.OnMouseUp)) canvas.Call("addEventListener", "mousemove", js.NewEventCallback(js.PreventDefault, input.OnMouseMove)) + canvas.Call("addEventListener", "wheel", js.NewEventCallback(js.PreventDefault, input.OnWheel)) // Touch canvas.Call("addEventListener", "touchstart", js.NewEventCallback(js.PreventDefault, input.OnTouchStart))