input: Implement wheel event on browsers

Fixes #630
This commit is contained in:
Hajime Hoshi 2018-09-30 17:08:16 +09:00
parent ecef9cab0f
commit 803e42714e
2 changed files with 15 additions and 4 deletions

View File

@ -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)
}

View File

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