add Window.Typed

This commit is contained in:
faiface 2017-05-10 21:10:10 +02:00
parent 9554cd9c20
commit c9319763d7
2 changed files with 15 additions and 0 deletions

View File

@ -31,6 +31,11 @@ func (w *Window) MouseScroll() pixel.Vec {
return w.currInp.scroll
}
// Typed returns the text typed on the keyboard since the last call to Window.Update.
func (w *Window) Typed() string {
return w.currInp.typed
}
// Button is a keyboard or mouse button. Why distinguish?
type Button int
@ -350,16 +355,25 @@ func (w *Window) initInput() {
w.window.SetScrollCallback(func(_ *glfw.Window, xoff, yoff float64) {
w.currInp.scroll += pixel.V(xoff, yoff)
})
w.window.SetCharCallback(func(_ *glfw.Window, r rune) {
w.currInp.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()

View File

@ -69,6 +69,7 @@ type Window struct {
mouse pixel.Vec
buttons [KeyLast + 1]bool
scroll pixel.Vec
typed string
}
}