mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-12 22:17:26 +01:00
input: Enable mouse wheel input (#628)
This commit is contained in:
parent
a3c754d2cc
commit
39b3170185
6
input.go
6
input.go
@ -52,6 +52,12 @@ func CursorPosition() (x, y int) {
|
|||||||
return ui.AdjustedCursorPosition()
|
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.
|
// IsMouseButtonPressed returns a boolean indicating whether mouseButton is pressed.
|
||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
|
@ -27,14 +27,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Input struct {
|
type Input struct {
|
||||||
keyPressed map[glfw.Key]bool
|
keyPressed map[glfw.Key]bool
|
||||||
mouseButtonPressed map[glfw.MouseButton]bool
|
mouseButtonPressed map[glfw.MouseButton]bool
|
||||||
cursorX int
|
callbacksInitialized bool
|
||||||
cursorY int
|
scrollX float64
|
||||||
gamepads [16]gamePad
|
scrollY float64
|
||||||
touches []*Touch // This is not updated until GLFW 3.3 is available (#417)
|
cursorX int
|
||||||
runeBuffer []rune
|
cursorY int
|
||||||
m sync.RWMutex
|
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 {
|
func (i *Input) RuneBuffer() []rune {
|
||||||
@ -49,6 +52,12 @@ func (i *Input) ClearRuneBuffer() {
|
|||||||
i.runeBuffer = i.runeBuffer[:0]
|
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 {
|
func (i *Input) IsKeyPressed(key Key) bool {
|
||||||
i.m.RLock()
|
i.m.RLock()
|
||||||
defer i.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
@ -83,6 +92,12 @@ func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
|
|||||||
return false
|
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{
|
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
|
||||||
glfw.MouseButtonLeft: MouseButtonLeft,
|
glfw.MouseButtonLeft: MouseButtonLeft,
|
||||||
glfw.MouseButtonRight: MouseButtonRight,
|
glfw.MouseButtonRight: MouseButtonRight,
|
||||||
@ -92,7 +107,7 @@ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
|
|||||||
func (i *Input) Update(window *glfw.Window, scale float64) {
|
func (i *Input) Update(window *glfw.Window, scale float64) {
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
if i.runeBuffer == nil {
|
if !i.callbacksInitialized {
|
||||||
i.runeBuffer = make([]rune, 0, 1024)
|
i.runeBuffer = make([]rune, 0, 1024)
|
||||||
window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
|
window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
|
||||||
if unicode.IsPrint(char) {
|
if unicode.IsPrint(char) {
|
||||||
@ -101,6 +116,13 @@ func (i *Input) Update(window *glfw.Window, scale float64) {
|
|||||||
i.m.Unlock()
|
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 {
|
if i.keyPressed == nil {
|
||||||
i.keyPressed = map[glfw.Key]bool{}
|
i.keyPressed = map[glfw.Key]bool{}
|
||||||
|
@ -91,6 +91,11 @@ func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
|
|||||||
return false
|
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) {
|
func (i *Input) keyDown(code string) {
|
||||||
if i.keyPressed == nil {
|
if i.keyPressed == nil {
|
||||||
i.keyPressed = map[string]bool{}
|
i.keyPressed = map[string]bool{}
|
||||||
|
@ -23,6 +23,8 @@ import (
|
|||||||
type Input struct {
|
type Input struct {
|
||||||
cursorX int
|
cursorX int
|
||||||
cursorY int
|
cursorY int
|
||||||
|
scrollX float64
|
||||||
|
scrollY float64
|
||||||
gamepads [16]gamePad
|
gamepads [16]gamePad
|
||||||
touches []*Touch
|
touches []*Touch
|
||||||
m sync.RWMutex
|
m sync.RWMutex
|
||||||
@ -36,6 +38,10 @@ func (i *Input) IsKeyPressed(key Key) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Input) MouseWheel() (xoff, yoff float64) {
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Input) IsMouseButtonPressed(key MouseButton) bool {
|
func (i *Input) IsMouseButtonPressed(key MouseButton) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -567,6 +567,7 @@ func (u *userInterface) update(g GraphicsContext) error {
|
|||||||
})
|
})
|
||||||
if err := g.Update(func() {
|
if err := g.Update(func() {
|
||||||
input.Get().ClearRuneBuffer()
|
input.Get().ClearRuneBuffer()
|
||||||
|
input.Get().ResetScrollValues()
|
||||||
// The offscreens must be updated every frame (#490).
|
// The offscreens must be updated every frame (#490).
|
||||||
u.updateGraphicsContext(g)
|
u.updateGraphicsContext(g)
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
@ -194,6 +194,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
|
||||||
// The offscreens must be updated every frame (#490).
|
// The offscreens must be updated every frame (#490).
|
||||||
u.updateGraphicsContext(g)
|
u.updateGraphicsContext(g)
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user