Skip to content

Commit

Permalink
Merge pull request faiface#272 from jstewart7/master
Browse files Browse the repository at this point in the history
Added input event tracking to prevent missed inputs
  • Loading branch information
dusk125 committed Jun 21, 2021
2 parents 842ae8d + b31c294 commit 98f3e74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pixelgl/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ func (w *Window) Pressed(button Button) bool {
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 {
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 {
return !w.currInp.buttons[button] && w.prevInp.buttons[button]
return w.releaseEvents[button]
}

// Repeated returns whether a repeat event has been triggered on button.
Expand Down Expand Up @@ -362,8 +362,10 @@ 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.tempPressEvents[Button(button)] = true
w.tempInp.buttons[Button(button)] = true
case glfw.Release:
w.tempReleaseEvents[Button(button)] = true
w.tempInp.buttons[Button(button)] = false
}
})
Expand All @@ -374,8 +376,10 @@ func (w *Window) initInput() {
}
switch action {
case glfw.Press:
w.tempPressEvents[Button(key)] = true
w.tempInp.buttons[Button(key)] = true
case glfw.Release:
w.tempReleaseEvents[Button(key)] = true
w.tempInp.buttons[Button(key)] = false
case glfw.Repeat:
w.tempInp.repeat[Button(key)] = true
Expand Down Expand Up @@ -431,6 +435,12 @@ func (w *Window) doUpdateInput() {
w.prevInp = w.currInp
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.scroll = pixel.ZV
w.tempInp.typed = ""
Expand Down
3 changes: 3 additions & 0 deletions pixelgl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type Window struct {
typed string
}

pressEvents, tempPressEvents [KeyLast + 1]bool
releaseEvents, tempReleaseEvents [KeyLast + 1]bool

prevJoy, currJoy, tempJoy joystickState
}

Expand Down

0 comments on commit 98f3e74

Please sign in to comment.