input: Use sync.Once

This commit is contained in:
Hajime Hoshi 2019-03-31 20:20:52 +09:00
parent f5c68d2a61
commit d56668bfdb

View File

@ -28,17 +28,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
callbacksInitialized bool onceCallback sync.Once
scrollX float64 scrollX float64
scrollY float64 scrollY float64
cursorX int cursorX int
cursorY int cursorY int
gamepads [16]gamePad gamepads [16]gamePad
touches map[int]pos // This is not updated until GLFW 3.3 is available (#417) touches map[int]pos // This is not updated until GLFW 3.3 is available (#417)
runeBuffer []rune runeBuffer []rune
m sync.RWMutex m sync.RWMutex
} }
func (i *Input) RuneBuffer() []rune { func (i *Input) RuneBuffer() []rune {
@ -120,15 +120,14 @@ 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.callbacksInitialized { i.onceCallback.Do(func() {
window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) { window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
i.appendRuneBuffer(char) i.appendRuneBuffer(char)
}) })
window.SetScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) { window.SetScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) {
i.setWheel(xoff, yoff) i.setWheel(xoff, yoff)
}) })
i.callbacksInitialized = true })
}
if i.keyPressed == nil { if i.keyPressed == nil {
i.keyPressed = map[glfw.Key]bool{} i.keyPressed = map[glfw.Key]bool{}
} }