From b15c10298e21df1cdd444a64105d24b44de284c4 Mon Sep 17 00:00:00 2001 From: faiface Date: Wed, 10 May 2017 21:22:47 +0200 Subject: [PATCH] fix and simplify input handling in Window --- pixelgl/input.go | 34 ++++++++++++---------------------- pixelgl/window.go | 2 +- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/pixelgl/input.go b/pixelgl/input.go index 0a4ed9c..cd701df 100644 --- a/pixelgl/input.go +++ b/pixelgl/input.go @@ -327,9 +327,9 @@ func (w *Window) initInput() { w.window.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) { switch action { case glfw.Press: - w.currInp.buttons[Button(button)] = true + w.tempInp.buttons[Button(button)] = true case glfw.Release: - w.currInp.buttons[Button(button)] = false + w.tempInp.buttons[Button(button)] = false } }) @@ -339,47 +339,37 @@ func (w *Window) initInput() { } switch action { case glfw.Press: - w.currInp.buttons[Button(key)] = true + w.tempInp.buttons[Button(key)] = true case glfw.Release: - w.currInp.buttons[Button(key)] = false + w.tempInp.buttons[Button(key)] = false } }) w.window.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) { - w.currInp.mouse = pixel.V( + w.tempInp.mouse = pixel.V( x+w.bounds.Min.X(), (w.bounds.H()-y)+w.bounds.Min.Y(), ) }) w.window.SetScrollCallback(func(_ *glfw.Window, xoff, yoff float64) { - w.currInp.scroll += pixel.V(xoff, yoff) + w.tempInp.scroll += pixel.V(xoff, yoff) }) w.window.SetCharCallback(func(_ *glfw.Window, r rune) { - w.currInp.typed += string(r) + w.tempInp.typed += string(r) }) }) } func (w *Window) updateInput() { - //FIXME: rething this, currInp can be changed outside this function, which may lead to inconsistencies - - // copy temp to prev - w.prevInp = w.tempInp - - // zero current scroll (but keep what was added in callbacks outside of this function) - w.currInp.scroll -= w.tempInp.scroll - - // erase typed string - w.currInp.typed = "" - - // get events (usually calls callbacks, but callbacks can be called outside too) mainthread.Call(func() { glfw.PollEvents() }) - // cache current state to temp (so that if there are callbacks outside this function, - // everything works) - w.tempInp = w.currInp + w.prevInp = w.currInp + w.currInp = w.tempInp + + w.tempInp.scroll = 0 + w.tempInp.typed = "" } diff --git a/pixelgl/window.go b/pixelgl/window.go index 7ad4f1b..68d0afc 100644 --- a/pixelgl/window.go +++ b/pixelgl/window.go @@ -65,7 +65,7 @@ type Window struct { xpos, ypos, width, height int } - prevInp, tempInp, currInp struct { + prevInp, currInp, tempInp struct { mouse pixel.Vec buttons [KeyLast + 1]bool scroll pixel.Vec