input: Enable mouse wheel input (#628)

This commit is contained in:
Philipp Steinhaus 2018-06-17 17:38:30 +02:00 committed by Hajime Hoshi
parent a3c754d2cc
commit 39b3170185
6 changed files with 50 additions and 9 deletions

View File

@ -52,6 +52,12 @@ func CursorPosition() (x, y int) {
return ui.AdjustedCursorPosition()
}
// MouseWheel returns the x and y offset of the scroll wheel.
// It returns 0 if the wheel isn't being rolled.
func MouseWheel() (xoff, yoff float64) {
return input.Get().MouseWheel()
}
// IsMouseButtonPressed returns a boolean indicating whether mouseButton is pressed.
//
// This function is concurrent-safe.

View File

@ -27,14 +27,17 @@ import (
)
type Input struct {
keyPressed map[glfw.Key]bool
mouseButtonPressed map[glfw.MouseButton]bool
cursorX int
cursorY int
gamepads [16]gamePad
touches []*Touch // This is not updated until GLFW 3.3 is available (#417)
runeBuffer []rune
m sync.RWMutex
keyPressed map[glfw.Key]bool
mouseButtonPressed map[glfw.MouseButton]bool
callbacksInitialized bool
scrollX float64
scrollY float64
cursorX int
cursorY int
gamepads [16]gamePad
touches []*Touch // This is not updated until GLFW 3.3 is available (#417)
runeBuffer []rune
m sync.RWMutex
}
func (i *Input) RuneBuffer() []rune {
@ -49,6 +52,12 @@ func (i *Input) ClearRuneBuffer() {
i.runeBuffer = i.runeBuffer[:0]
}
func (i *Input) ResetScrollValues() {
i.m.RLock()
defer i.m.RUnlock()
i.scrollX, i.scrollY = 0, 0
}
func (i *Input) IsKeyPressed(key Key) bool {
i.m.RLock()
defer i.m.RUnlock()
@ -83,6 +92,12 @@ func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
return false
}
func (i *Input) MouseWheel() (xoff, yoff float64) {
i.m.RLock()
defer i.m.RUnlock()
return i.scrollX, i.scrollY
}
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
glfw.MouseButtonLeft: MouseButtonLeft,
glfw.MouseButtonRight: MouseButtonRight,
@ -92,7 +107,7 @@ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
func (i *Input) Update(window *glfw.Window, scale float64) {
i.m.Lock()
defer i.m.Unlock()
if i.runeBuffer == nil {
if !i.callbacksInitialized {
i.runeBuffer = make([]rune, 0, 1024)
window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
if unicode.IsPrint(char) {
@ -101,6 +116,13 @@ func (i *Input) Update(window *glfw.Window, scale float64) {
i.m.Unlock()
}
})
window.SetScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) {
i.m.Lock()
i.scrollX = xoff
i.scrollY = yoff
i.m.Unlock()
})
i.callbacksInitialized = true
}
if i.keyPressed == nil {
i.keyPressed = map[glfw.Key]bool{}

View File

@ -91,6 +91,11 @@ func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
return false
}
func (i *Input) MouseWheel() (xoff, yoff float64) {
return 0, 0
// TODO: Mouse scroll functionality is not yet implemented in js
}
func (i *Input) keyDown(code string) {
if i.keyPressed == nil {
i.keyPressed = map[string]bool{}

View File

@ -23,6 +23,8 @@ import (
type Input struct {
cursorX int
cursorY int
scrollX float64
scrollY float64
gamepads [16]gamePad
touches []*Touch
m sync.RWMutex
@ -36,6 +38,10 @@ func (i *Input) IsKeyPressed(key Key) bool {
return false
}
func (i *Input) MouseWheel() (xoff, yoff float64) {
return 0, 0
}
func (i *Input) IsMouseButtonPressed(key MouseButton) bool {
return false
}

View File

@ -567,6 +567,7 @@ func (u *userInterface) update(g GraphicsContext) error {
})
if err := g.Update(func() {
input.Get().ClearRuneBuffer()
input.Get().ResetScrollValues()
// The offscreens must be updated every frame (#490).
u.updateGraphicsContext(g)
}); err != nil {

View File

@ -194,6 +194,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).
u.updateGraphicsContext(g)
}); err != nil {