mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
inpututil: Add IsTouchJustReleased (#504)
This commit is contained in:
parent
a7fafc74e7
commit
d3f573523a
@ -33,7 +33,8 @@ type inputState struct {
|
|||||||
gamepadButtonStates map[int]map[ebiten.GamepadButton]int
|
gamepadButtonStates map[int]map[ebiten.GamepadButton]int
|
||||||
prevGamepadButtonStates map[int]map[ebiten.GamepadButton]int
|
prevGamepadButtonStates map[int]map[ebiten.GamepadButton]int
|
||||||
|
|
||||||
touchStates map[int]int
|
touchStates map[int]int
|
||||||
|
prevTouchStates map[int]int
|
||||||
|
|
||||||
m sync.RWMutex
|
m sync.RWMutex
|
||||||
}
|
}
|
||||||
@ -127,8 +128,15 @@ func (i *inputState) update() {
|
|||||||
|
|
||||||
// Touches
|
// Touches
|
||||||
ids = map[int]struct{}{}
|
ids = map[int]struct{}{}
|
||||||
|
|
||||||
|
// Reset the previous states first since some gamepad IDs might be already gone.
|
||||||
|
for id := range i.prevTouchStates {
|
||||||
|
i.prevTouchStates[id] = 0
|
||||||
|
}
|
||||||
|
|
||||||
for _, t := range ebiten.Touches() {
|
for _, t := range ebiten.Touches() {
|
||||||
ids[t.ID()] = struct{}{}
|
ids[t.ID()] = struct{}{}
|
||||||
|
i.prevTouchStates[t.ID()] = i.touchStates[t.ID()]
|
||||||
i.touchStates[t.ID()]++
|
i.touchStates[t.ID()]++
|
||||||
}
|
}
|
||||||
idsToDelete = []int{}
|
idsToDelete = []int{}
|
||||||
@ -248,6 +256,17 @@ func IsJustTouched(id int) bool {
|
|||||||
return TouchDuration(id) == 1
|
return TouchDuration(id) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsTouchJustReleased returns a boolean value indicating
|
||||||
|
// whether the given touch is released just in the current frame.
|
||||||
|
//
|
||||||
|
// IsTouchJustReleased is concurrent safe.
|
||||||
|
func IsTouchJustReleased(id int) bool {
|
||||||
|
theInputState.m.RLock()
|
||||||
|
r := theInputState.touchStates[id] == 0 && theInputState.prevTouchStates[id] > 0
|
||||||
|
theInputState.m.RUnlock()
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
// TouchDuration returns how long the touch remains in frames.
|
// TouchDuration returns how long the touch remains in frames.
|
||||||
//
|
//
|
||||||
// TouchDuration is concurrent safe.
|
// TouchDuration is concurrent safe.
|
||||||
|
Loading…
Reference in New Issue
Block a user