From 94fe4019fd2459abdd8ce50250c6f63f89ae6794 Mon Sep 17 00:00:00 2001 From: faiface Date: Thu, 9 Mar 2017 00:56:57 +0100 Subject: [PATCH] optimize Window.MousePosition --- pixelgl/input.go | 21 ++++++++++----------- pixelgl/window.go | 1 + 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pixelgl/input.go b/pixelgl/input.go index 94f2406..6668ec5 100644 --- a/pixelgl/input.go +++ b/pixelgl/input.go @@ -23,17 +23,7 @@ func (w *Window) JustReleased(button Button) bool { // MousePosition returns the current mouse position relative to the window. func (w *Window) MousePosition() pixel.Vec { - var x, y, width, height float64 - mainthread.Call(func() { - x, y = w.window.GetCursorPos() - wi, hi := w.window.GetSize() - width, height = float64(wi), float64(hi) - }) - - return pixel.V( - x/width*w.bounds.W()+w.bounds.X(), - (height-y)/height*w.bounds.H()+w.bounds.Y(), - ) + return w.currInp.mouse } // MouseScroll returns the scroll amount (in both axis) since the last call to Window.Update. @@ -363,6 +353,15 @@ func (w *Window) updateInput() { // get events (usually calls callbacks, but callbacks can be called outside too) mainthread.Call(func() { glfw.PollEvents() + + x, y := w.window.GetCursorPos() + wi, hi := w.window.GetSize() + width, height := float64(wi), float64(hi) + + w.currInp.mouse = pixel.V( + x/width*w.bounds.W()+w.bounds.X(), + (height-y)/height*w.bounds.H()+w.bounds.Y(), + ) }) // cache current state to temp (so that if there are callbacks outside this function, diff --git a/pixelgl/window.go b/pixelgl/window.go index 731fee2..70dc422 100644 --- a/pixelgl/window.go +++ b/pixelgl/window.go @@ -62,6 +62,7 @@ type Window struct { } prevInp, tempInp, currInp struct { + mouse pixel.Vec buttons [KeyLast + 1]bool scroll pixel.Vec }