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 mouseButtonPressed map[int]bool
cursorX int cursorX int
cursorY int cursorY int
wheelX float64
wheelY float64
gamepads [16]gamePad gamepads [16]gamePad
touches []*Touch touches []*Touch
runeBuffer []rune runeBuffer []rune
@ -49,6 +51,11 @@ func (i *Input) ClearRuneBuffer() {
i.runeBuffer = nil i.runeBuffer = nil
} }
func (i *Input) ResetWheelValues() {
i.wheelX = 0
i.wheelY = 0
}
func (i *Input) IsKeyPressed(key Key) bool { func (i *Input) IsKeyPressed(key Key) bool {
if i.keyPressed != nil { if i.keyPressed != nil {
for _, c := range keyToCodes[key] { for _, c := range keyToCodes[key] {
@ -92,8 +99,7 @@ func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
} }
func (i *Input) Wheel() (xoff, yoff float64) { func (i *Input) Wheel() (xoff, yoff float64) {
return 0, 0 return i.wheelX, i.wheelY
// TODO: Mouse scroll functionality is not yet implemented in js
} }
func (i *Input) keyDown(code string) { func (i *Input) keyDown(code string) {
@ -241,6 +247,11 @@ func OnMouseMove(e js.Value) {
setMouseCursorFromEvent(e) setMouseCursorFromEvent(e)
} }
func OnWheel(e js.Value) {
theInput.wheelX = e.Get("deltaX").Float()
theInput.wheelY = e.Get("deltaY").Float()
}
func OnTouchStart(e js.Value) { func OnTouchStart(e js.Value) {
theInput.updateTouches(e) theInput.updateTouches(e)
} }

View File

@ -207,8 +207,7 @@ func (u *userInterface) update(g GraphicsContext) error {
u.updateGraphicsContext(g) u.updateGraphicsContext(g)
if err := g.Update(func() { if err := g.Update(func() {
input.Get().ClearRuneBuffer() input.Get().ClearRuneBuffer()
// TODO: insert ResetScrollValues() counterpart to 'ui_glfw.go' here input.Get().ResetWheelValues()
// The offscreens must be updated every frame (#490).
u.updateGraphicsContext(g) u.updateGraphicsContext(g)
}); err != nil { }); err != nil {
return err return err
@ -322,6 +321,7 @@ func init() {
canvas.Call("addEventListener", "mousedown", js.NewEventCallback(js.PreventDefault, input.OnMouseDown)) canvas.Call("addEventListener", "mousedown", js.NewEventCallback(js.PreventDefault, input.OnMouseDown))
canvas.Call("addEventListener", "mouseup", js.NewEventCallback(js.PreventDefault, input.OnMouseUp)) canvas.Call("addEventListener", "mouseup", js.NewEventCallback(js.PreventDefault, input.OnMouseUp))
canvas.Call("addEventListener", "mousemove", js.NewEventCallback(js.PreventDefault, input.OnMouseMove)) canvas.Call("addEventListener", "mousemove", js.NewEventCallback(js.PreventDefault, input.OnMouseMove))
canvas.Call("addEventListener", "wheel", js.NewEventCallback(js.PreventDefault, input.OnWheel))
// Touch // Touch
canvas.Call("addEventListener", "touchstart", js.NewEventCallback(js.PreventDefault, input.OnTouchStart)) canvas.Call("addEventListener", "touchstart", js.NewEventCallback(js.PreventDefault, input.OnTouchStart))