Added input event tracking to prevent missed inputs

This commit is contained in:
Jacob Stewart 2020-12-27 11:54:34 -05:00
parent f931f69d6e
commit b31c294d4d
2 changed files with 17 additions and 4 deletions

View File

@ -13,14 +13,14 @@ func (w *Window) Pressed(button Button) bool {
return w.currInp.buttons[button] return w.currInp.buttons[button]
} }
// JustPressed returns whether the Button has just been pressed down. // JustPressed returns whether the Button has been pressed in the last frame.
func (w *Window) JustPressed(button Button) bool { func (w *Window) JustPressed(button Button) bool {
return w.currInp.buttons[button] && !w.prevInp.buttons[button] return w.pressEvents[button]
} }
// JustReleased returns whether the Button has just been released up. // JustReleased returns whether the Button has been released in the last frame.
func (w *Window) JustReleased(button Button) bool { func (w *Window) JustReleased(button Button) bool {
return !w.currInp.buttons[button] && w.prevInp.buttons[button] return w.releaseEvents[button]
} }
// Repeated returns whether a repeat event has been triggered on button. // Repeated returns whether a repeat event has been triggered on button.
@ -362,8 +362,10 @@ func (w *Window) initInput() {
w.window.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) { w.window.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
switch action { switch action {
case glfw.Press: case glfw.Press:
w.tempPressEvents[Button(button)] = true
w.tempInp.buttons[Button(button)] = true w.tempInp.buttons[Button(button)] = true
case glfw.Release: case glfw.Release:
w.tempReleaseEvents[Button(button)] = true
w.tempInp.buttons[Button(button)] = false w.tempInp.buttons[Button(button)] = false
} }
}) })
@ -374,8 +376,10 @@ func (w *Window) initInput() {
} }
switch action { switch action {
case glfw.Press: case glfw.Press:
w.tempPressEvents[Button(key)] = true
w.tempInp.buttons[Button(key)] = true w.tempInp.buttons[Button(key)] = true
case glfw.Release: case glfw.Release:
w.tempReleaseEvents[Button(key)] = true
w.tempInp.buttons[Button(key)] = false w.tempInp.buttons[Button(key)] = false
case glfw.Repeat: case glfw.Repeat:
w.tempInp.repeat[Button(key)] = true w.tempInp.repeat[Button(key)] = true
@ -431,6 +435,12 @@ func (w *Window) doUpdateInput() {
w.prevInp = w.currInp w.prevInp = w.currInp
w.currInp = w.tempInp w.currInp = w.tempInp
w.pressEvents = w.tempPressEvents
w.releaseEvents = w.tempReleaseEvents
// Clear last frame's temporary status
w.tempPressEvents = [KeyLast + 1]bool{}
w.tempReleaseEvents = [KeyLast + 1]bool{}
w.tempInp.repeat = [KeyLast + 1]bool{} w.tempInp.repeat = [KeyLast + 1]bool{}
w.tempInp.scroll = pixel.ZV w.tempInp.scroll = pixel.ZV
w.tempInp.typed = "" w.tempInp.typed = ""

View File

@ -100,6 +100,9 @@ type Window struct {
typed string typed string
} }
pressEvents, tempPressEvents [KeyLast + 1]bool
releaseEvents, tempReleaseEvents [KeyLast + 1]bool
prevJoy, currJoy, tempJoy joystickState prevJoy, currJoy, tempJoy joystickState
} }