updated ebiten version from 2.7.9 to 2.9.9
This commit is contained in:
+153
-266
@@ -16,39 +16,32 @@
|
||||
package inpututil
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/hook"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/inputstate"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
||||
)
|
||||
|
||||
type pos struct {
|
||||
x int
|
||||
y int
|
||||
type gamepadState struct {
|
||||
buttonDurations [ebiten.GamepadButtonMax + 1]int
|
||||
standardButtonDurations [ebiten.StandardGamepadButtonMax + 1]int
|
||||
}
|
||||
|
||||
type touchState struct {
|
||||
duration int
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
type inputState struct {
|
||||
keyDurations []int
|
||||
prevKeyDurations []int
|
||||
gamepadStates map[ebiten.GamepadID]gamepadState
|
||||
prevGamepadStates map[ebiten.GamepadID]gamepadState
|
||||
|
||||
mouseButtonDurations map[ebiten.MouseButton]int
|
||||
prevMouseButtonDurations map[ebiten.MouseButton]int
|
||||
|
||||
gamepadIDs map[ebiten.GamepadID]struct{}
|
||||
prevGamepadIDs map[ebiten.GamepadID]struct{}
|
||||
|
||||
gamepadButtonDurations map[ebiten.GamepadID][]int
|
||||
prevGamepadButtonDurations map[ebiten.GamepadID][]int
|
||||
|
||||
standardGamepadButtonDurations map[ebiten.GamepadID][]int
|
||||
prevStandardGamepadButtonDurations map[ebiten.GamepadID][]int
|
||||
|
||||
touchIDs map[ebiten.TouchID]struct{}
|
||||
touchDurations map[ebiten.TouchID]int
|
||||
touchPositions map[ebiten.TouchID]pos
|
||||
prevTouchDurations map[ebiten.TouchID]int
|
||||
prevTouchPositions map[ebiten.TouchID]pos
|
||||
touchStates map[ebiten.TouchID]touchState
|
||||
prevTouchStates map[ebiten.TouchID]touchState
|
||||
|
||||
gamepadIDsBuf []ebiten.GamepadID
|
||||
touchIDsBuf []ebiten.TouchID
|
||||
@@ -57,26 +50,10 @@ type inputState struct {
|
||||
}
|
||||
|
||||
var theInputState = &inputState{
|
||||
keyDurations: make([]int, ebiten.KeyMax+1),
|
||||
prevKeyDurations: make([]int, ebiten.KeyMax+1),
|
||||
|
||||
mouseButtonDurations: map[ebiten.MouseButton]int{},
|
||||
prevMouseButtonDurations: map[ebiten.MouseButton]int{},
|
||||
|
||||
gamepadIDs: map[ebiten.GamepadID]struct{}{},
|
||||
prevGamepadIDs: map[ebiten.GamepadID]struct{}{},
|
||||
|
||||
gamepadButtonDurations: map[ebiten.GamepadID][]int{},
|
||||
prevGamepadButtonDurations: map[ebiten.GamepadID][]int{},
|
||||
|
||||
standardGamepadButtonDurations: map[ebiten.GamepadID][]int{},
|
||||
prevStandardGamepadButtonDurations: map[ebiten.GamepadID][]int{},
|
||||
|
||||
touchIDs: map[ebiten.TouchID]struct{}{},
|
||||
touchDurations: map[ebiten.TouchID]int{},
|
||||
touchPositions: map[ebiten.TouchID]pos{},
|
||||
prevTouchDurations: map[ebiten.TouchID]int{},
|
||||
prevTouchPositions: map[ebiten.TouchID]pos{},
|
||||
gamepadStates: map[ebiten.GamepadID]gamepadState{},
|
||||
prevGamepadStates: map[ebiten.GamepadID]gamepadState{},
|
||||
touchStates: map[ebiten.TouchID]touchState{},
|
||||
prevTouchStates: map[ebiten.TouchID]touchState{},
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -90,121 +67,64 @@ func (i *inputState) update() {
|
||||
i.m.Lock()
|
||||
defer i.m.Unlock()
|
||||
|
||||
// Keyboard
|
||||
copy(i.prevKeyDurations, i.keyDurations)
|
||||
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
|
||||
if ebiten.IsKeyPressed(k) {
|
||||
i.keyDurations[k]++
|
||||
} else {
|
||||
i.keyDurations[k] = 0
|
||||
}
|
||||
}
|
||||
|
||||
// Mouse
|
||||
for b := ebiten.MouseButton(0); b <= ebiten.MouseButtonMax; b++ {
|
||||
i.prevMouseButtonDurations[b] = i.mouseButtonDurations[b]
|
||||
if ebiten.IsMouseButtonPressed(b) {
|
||||
i.mouseButtonDurations[b]++
|
||||
} else {
|
||||
i.mouseButtonDurations[b] = 0
|
||||
}
|
||||
}
|
||||
|
||||
// Gamepads
|
||||
|
||||
// Copy the gamepad IDs.
|
||||
for id := range i.prevGamepadIDs {
|
||||
delete(i.prevGamepadIDs, id)
|
||||
}
|
||||
for id := range i.gamepadIDs {
|
||||
i.prevGamepadIDs[id] = struct{}{}
|
||||
// Copy the gamepad states.
|
||||
clear(i.prevGamepadStates)
|
||||
for id, s := range i.gamepadStates {
|
||||
i.prevGamepadStates[id] = s
|
||||
}
|
||||
|
||||
// Copy the gamepad button durations.
|
||||
for id := range i.prevGamepadButtonDurations {
|
||||
delete(i.prevGamepadButtonDurations, id)
|
||||
}
|
||||
for id, ds := range i.gamepadButtonDurations {
|
||||
i.prevGamepadButtonDurations[id] = append([]int{}, ds...)
|
||||
}
|
||||
|
||||
for id := range i.prevStandardGamepadButtonDurations {
|
||||
delete(i.prevStandardGamepadButtonDurations, id)
|
||||
}
|
||||
for id, ds := range i.standardGamepadButtonDurations {
|
||||
i.prevStandardGamepadButtonDurations[id] = append([]int{}, ds...)
|
||||
}
|
||||
|
||||
for id := range i.gamepadIDs {
|
||||
delete(i.gamepadIDs, id)
|
||||
}
|
||||
i.gamepadIDsBuf = ebiten.AppendGamepadIDs(i.gamepadIDsBuf[:0])
|
||||
for _, id := range i.gamepadIDsBuf {
|
||||
i.gamepadIDs[id] = struct{}{}
|
||||
state := i.gamepadStates[id]
|
||||
|
||||
if _, ok := i.gamepadButtonDurations[id]; !ok {
|
||||
i.gamepadButtonDurations[id] = make([]int, ebiten.GamepadButtonMax+1)
|
||||
}
|
||||
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
|
||||
if ebiten.IsGamepadButtonPressed(id, b) {
|
||||
i.gamepadButtonDurations[id][b]++
|
||||
for b := range i.gamepadStates[id].buttonDurations {
|
||||
if ebiten.IsGamepadButtonPressed(id, ebiten.GamepadButton(b)) {
|
||||
state.buttonDurations[b]++
|
||||
} else {
|
||||
i.gamepadButtonDurations[id][b] = 0
|
||||
state.buttonDurations[b] = 0
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := i.standardGamepadButtonDurations[id]; !ok {
|
||||
i.standardGamepadButtonDurations[id] = make([]int, ebiten.StandardGamepadButtonMax+1)
|
||||
}
|
||||
for b := ebiten.StandardGamepadButton(0); b <= ebiten.StandardGamepadButtonMax; b++ {
|
||||
if ebiten.IsStandardGamepadButtonPressed(id, b) {
|
||||
i.standardGamepadButtonDurations[id][b]++
|
||||
for b := range i.gamepadStates[id].standardButtonDurations {
|
||||
if ebiten.IsStandardGamepadButtonPressed(id, ebiten.StandardGamepadButton(b)) {
|
||||
state.standardButtonDurations[b]++
|
||||
} else {
|
||||
i.standardGamepadButtonDurations[id][b] = 0
|
||||
state.standardButtonDurations[b] = 0
|
||||
}
|
||||
}
|
||||
|
||||
i.gamepadStates[id] = state
|
||||
}
|
||||
for id := range i.gamepadButtonDurations {
|
||||
if _, ok := i.gamepadIDs[id]; !ok {
|
||||
delete(i.gamepadButtonDurations, id)
|
||||
}
|
||||
}
|
||||
for id := range i.standardGamepadButtonDurations {
|
||||
if _, ok := i.gamepadIDs[id]; !ok {
|
||||
delete(i.standardGamepadButtonDurations, id)
|
||||
|
||||
// Remove disconnected gamepads.
|
||||
for id := range i.gamepadStates {
|
||||
if !slices.Contains(i.gamepadIDsBuf, id) {
|
||||
delete(i.gamepadStates, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Touches
|
||||
|
||||
// Copy the touch durations and positions.
|
||||
for id := range i.prevTouchDurations {
|
||||
delete(i.prevTouchDurations, id)
|
||||
}
|
||||
for id := range i.touchDurations {
|
||||
i.prevTouchDurations[id] = i.touchDurations[id]
|
||||
}
|
||||
for id := range i.prevTouchPositions {
|
||||
delete(i.prevTouchPositions, id)
|
||||
}
|
||||
for id := range i.touchPositions {
|
||||
i.prevTouchPositions[id] = i.touchPositions[id]
|
||||
clear(i.prevTouchStates)
|
||||
for id, state := range i.touchStates {
|
||||
i.prevTouchStates[id] = state
|
||||
}
|
||||
|
||||
for id := range i.touchIDs {
|
||||
delete(i.touchIDs, id)
|
||||
}
|
||||
i.touchIDsBuf = ebiten.AppendTouchIDs(i.touchIDsBuf[:0])
|
||||
for _, id := range i.touchIDsBuf {
|
||||
i.touchIDs[id] = struct{}{}
|
||||
i.touchDurations[id]++
|
||||
x, y := ebiten.TouchPosition(id)
|
||||
i.touchPositions[id] = pos{x: x, y: y}
|
||||
state := i.touchStates[id]
|
||||
state.duration++
|
||||
state.x, state.y = ebiten.TouchPosition(id)
|
||||
i.touchStates[id] = state
|
||||
}
|
||||
for id := range i.touchDurations {
|
||||
if _, ok := i.touchIDs[id]; !ok {
|
||||
delete(i.touchDurations, id)
|
||||
delete(i.touchPositions, id)
|
||||
|
||||
// Remove released touches.
|
||||
for id := range i.touchStates {
|
||||
if !slices.Contains(i.touchIDsBuf, id) {
|
||||
delete(i.touchStates, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -216,16 +136,7 @@ func (i *inputState) update() {
|
||||
//
|
||||
// AppendPressedKeys is concurrent safe.
|
||||
func AppendPressedKeys(keys []ebiten.Key) []ebiten.Key {
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
for i, d := range theInputState.keyDurations {
|
||||
if d == 0 {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, ebiten.Key(i))
|
||||
}
|
||||
return keys
|
||||
return inputstate.AppendPressedKeys(keys)
|
||||
}
|
||||
|
||||
// PressedKeys returns a set of currently pressed keyboard keys.
|
||||
@@ -244,16 +155,7 @@ func PressedKeys() []ebiten.Key {
|
||||
//
|
||||
// AppendJustPressedKeys is concurrent safe.
|
||||
func AppendJustPressedKeys(keys []ebiten.Key) []ebiten.Key {
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
for i, d := range theInputState.keyDurations {
|
||||
if d != 1 {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, ebiten.Key(i))
|
||||
}
|
||||
return keys
|
||||
return inputstate.AppendJustPressedKeys(keys)
|
||||
}
|
||||
|
||||
// AppendJustReleasedKeys append just released keyboard keys to keys and returns the extended buffer.
|
||||
@@ -263,19 +165,7 @@ func AppendJustPressedKeys(keys []ebiten.Key) []ebiten.Key {
|
||||
//
|
||||
// AppendJustReleasedKeys is concurrent safe.
|
||||
func AppendJustReleasedKeys(keys []ebiten.Key) []ebiten.Key {
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
|
||||
if theInputState.keyDurations[k] != 0 {
|
||||
continue
|
||||
}
|
||||
if theInputState.prevKeyDurations[k] == 0 {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
return inputstate.AppendJustReleasedKeys(keys)
|
||||
}
|
||||
|
||||
// IsKeyJustPressed returns a boolean value indicating
|
||||
@@ -285,7 +175,7 @@ func AppendJustReleasedKeys(keys []ebiten.Key) []ebiten.Key {
|
||||
//
|
||||
// IsKeyJustPressed is concurrent safe.
|
||||
func IsKeyJustPressed(key ebiten.Key) bool {
|
||||
return KeyPressDuration(key) == 1
|
||||
return inputstate.Get().IsKeyJustPressed(ui.Key(key))
|
||||
}
|
||||
|
||||
// IsKeyJustReleased returns a boolean value indicating
|
||||
@@ -295,10 +185,7 @@ func IsKeyJustPressed(key ebiten.Key) bool {
|
||||
//
|
||||
// IsKeyJustReleased is concurrent safe.
|
||||
func IsKeyJustReleased(key ebiten.Key) bool {
|
||||
theInputState.m.RLock()
|
||||
r := theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
|
||||
theInputState.m.RUnlock()
|
||||
return r
|
||||
return inputstate.Get().IsKeyJustReleased(ui.Key(key))
|
||||
}
|
||||
|
||||
// KeyPressDuration returns how long the key is pressed in ticks (Update).
|
||||
@@ -307,10 +194,7 @@ func IsKeyJustReleased(key ebiten.Key) bool {
|
||||
//
|
||||
// KeyPressDuration is concurrent safe.
|
||||
func KeyPressDuration(key ebiten.Key) int {
|
||||
theInputState.m.RLock()
|
||||
s := theInputState.keyDurations[key]
|
||||
theInputState.m.RUnlock()
|
||||
return s
|
||||
return int(inputstate.Get().KeyPressDuration(ui.Key(key)))
|
||||
}
|
||||
|
||||
// IsMouseButtonJustPressed returns a boolean value indicating
|
||||
@@ -320,7 +204,7 @@ func KeyPressDuration(key ebiten.Key) int {
|
||||
//
|
||||
// IsMouseButtonJustPressed is concurrent safe.
|
||||
func IsMouseButtonJustPressed(button ebiten.MouseButton) bool {
|
||||
return MouseButtonPressDuration(button) == 1
|
||||
return inputstate.Get().IsMouseButtonJustPressed(ui.MouseButton(button))
|
||||
}
|
||||
|
||||
// IsMouseButtonJustReleased returns a boolean value indicating
|
||||
@@ -330,11 +214,7 @@ func IsMouseButtonJustPressed(button ebiten.MouseButton) bool {
|
||||
//
|
||||
// IsMouseButtonJustReleased is concurrent safe.
|
||||
func IsMouseButtonJustReleased(button ebiten.MouseButton) bool {
|
||||
theInputState.m.RLock()
|
||||
r := theInputState.mouseButtonDurations[button] == 0 &&
|
||||
theInputState.prevMouseButtonDurations[button] > 0
|
||||
theInputState.m.RUnlock()
|
||||
return r
|
||||
return inputstate.Get().IsMouseButtonJustReleased(ui.MouseButton(button))
|
||||
}
|
||||
|
||||
// MouseButtonPressDuration returns how long the mouse button is pressed in ticks (Update).
|
||||
@@ -343,10 +223,7 @@ func IsMouseButtonJustReleased(button ebiten.MouseButton) bool {
|
||||
//
|
||||
// MouseButtonPressDuration is concurrent safe.
|
||||
func MouseButtonPressDuration(button ebiten.MouseButton) int {
|
||||
theInputState.m.RLock()
|
||||
s := theInputState.mouseButtonDurations[button]
|
||||
theInputState.m.RUnlock()
|
||||
return s
|
||||
return int(inputstate.Get().MouseButtonPressDuration(ui.MouseButton(button)))
|
||||
}
|
||||
|
||||
// AppendJustConnectedGamepadIDs appends gamepad IDs that are connected just in the current tick to gamepadIDs,
|
||||
@@ -357,18 +234,17 @@ func MouseButtonPressDuration(button ebiten.MouseButton) int {
|
||||
//
|
||||
// AppendJustConnectedGamepadIDs is concurrent safe.
|
||||
func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID {
|
||||
origLen := len(gamepadIDs)
|
||||
theInputState.m.RLock()
|
||||
for id := range theInputState.gamepadIDs {
|
||||
if _, ok := theInputState.prevGamepadIDs[id]; !ok {
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
origLen := len(gamepadIDs)
|
||||
for id := range theInputState.gamepadStates {
|
||||
if _, ok := theInputState.prevGamepadStates[id]; !ok {
|
||||
gamepadIDs = append(gamepadIDs, id)
|
||||
}
|
||||
}
|
||||
theInputState.m.RUnlock()
|
||||
s := gamepadIDs[origLen:]
|
||||
sort.Slice(s, func(a, b int) bool {
|
||||
return s[a] < s[b]
|
||||
})
|
||||
|
||||
slices.Sort(gamepadIDs[origLen:])
|
||||
return gamepadIDs
|
||||
}
|
||||
|
||||
@@ -389,10 +265,11 @@ func JustConnectedGamepadIDs() []ebiten.GamepadID {
|
||||
// IsGamepadJustDisconnected is concurrent safe.
|
||||
func IsGamepadJustDisconnected(id ebiten.GamepadID) bool {
|
||||
theInputState.m.RLock()
|
||||
_, prev := theInputState.prevGamepadIDs[id]
|
||||
_, current := theInputState.gamepadIDs[id]
|
||||
theInputState.m.RUnlock()
|
||||
return prev && !current
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
_, current := theInputState.gamepadStates[id]
|
||||
_, prev := theInputState.prevGamepadStates[id]
|
||||
return !current && prev
|
||||
}
|
||||
|
||||
// AppendPressedGamepadButtons append currently pressed gamepad buttons to buttons and returns the extended buffer.
|
||||
@@ -405,11 +282,12 @@ func AppendPressedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.GamepadBu
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return buttons
|
||||
}
|
||||
|
||||
for b, d := range theInputState.gamepadButtonDurations[id] {
|
||||
for b, d := range state.buttonDurations {
|
||||
if d == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -429,11 +307,12 @@ func AppendJustPressedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.Gamep
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return buttons
|
||||
}
|
||||
|
||||
for b, d := range theInputState.gamepadButtonDurations[id] {
|
||||
for b, d := range state.buttonDurations {
|
||||
if d != 1 {
|
||||
continue
|
||||
}
|
||||
@@ -453,23 +332,23 @@ func AppendJustReleasedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.Game
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return buttons
|
||||
}
|
||||
if _, ok := theInputState.prevGamepadButtonDurations[id]; !ok {
|
||||
prevState, ok := theInputState.prevGamepadStates[id]
|
||||
if !ok {
|
||||
return buttons
|
||||
}
|
||||
|
||||
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
|
||||
if theInputState.gamepadButtonDurations[id][b] == 0 {
|
||||
for b := range state.buttonDurations {
|
||||
if state.buttonDurations[b] != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if theInputState.prevGamepadButtonDurations[id][b] > 0 {
|
||||
if prevState.buttonDurations[b] == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
buttons = append(buttons, b)
|
||||
buttons = append(buttons, ebiten.GamepadButton(b))
|
||||
}
|
||||
|
||||
return buttons
|
||||
@@ -493,16 +372,18 @@ func IsGamepadButtonJustPressed(id ebiten.GamepadID, button ebiten.GamepadButton
|
||||
// IsGamepadButtonJustReleased is concurrent safe.
|
||||
func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButton) bool {
|
||||
theInputState.m.RLock()
|
||||
prev := 0
|
||||
if _, ok := theInputState.prevGamepadButtonDurations[id]; ok {
|
||||
prev = theInputState.prevGamepadButtonDurations[id][button]
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
current := 0
|
||||
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
|
||||
current = theInputState.gamepadButtonDurations[id][button]
|
||||
prevState, ok := theInputState.prevGamepadStates[id]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
theInputState.m.RUnlock()
|
||||
return current == 0 && prev > 0
|
||||
|
||||
return state.buttonDurations[button] == 0 && prevState.buttonDurations[button] > 0
|
||||
}
|
||||
|
||||
// GamepadButtonPressDuration returns how long the gamepad button of the gamepad id is pressed in ticks (Update).
|
||||
@@ -512,12 +393,14 @@ func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButto
|
||||
// GamepadButtonPressDuration is concurrent safe.
|
||||
func GamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.GamepadButton) int {
|
||||
theInputState.m.RLock()
|
||||
s := 0
|
||||
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
|
||||
s = theInputState.gamepadButtonDurations[id][button]
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
theInputState.m.RUnlock()
|
||||
return s
|
||||
|
||||
return state.buttonDurations[button]
|
||||
}
|
||||
|
||||
// AppendPressedStandardGamepadButtons append currently pressed standard gamepad buttons to buttons and returns the extended buffer.
|
||||
@@ -530,15 +413,16 @@ func AppendPressedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebiten.S
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
if _, ok := theInputState.standardGamepadButtonDurations[id]; !ok {
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return buttons
|
||||
}
|
||||
|
||||
for b, d := range theInputState.standardGamepadButtonDurations[id] {
|
||||
for i, d := range state.standardButtonDurations {
|
||||
if d == 0 {
|
||||
continue
|
||||
}
|
||||
buttons = append(buttons, ebiten.StandardGamepadButton(b))
|
||||
buttons = append(buttons, ebiten.StandardGamepadButton(i))
|
||||
}
|
||||
|
||||
return buttons
|
||||
@@ -554,11 +438,12 @@ func AppendJustPressedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebit
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return buttons
|
||||
}
|
||||
|
||||
for b, d := range theInputState.standardGamepadButtonDurations[id] {
|
||||
for b, d := range state.standardButtonDurations {
|
||||
if d != 1 {
|
||||
continue
|
||||
}
|
||||
@@ -578,23 +463,23 @@ func AppendJustReleasedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebi
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return buttons
|
||||
}
|
||||
if _, ok := theInputState.prevGamepadButtonDurations[id]; !ok {
|
||||
prevState, ok := theInputState.prevGamepadStates[id]
|
||||
if !ok {
|
||||
return buttons
|
||||
}
|
||||
|
||||
for b := ebiten.StandardGamepadButton(0); b <= ebiten.StandardGamepadButtonMax; b++ {
|
||||
if theInputState.standardGamepadButtonDurations[id][b] == 0 {
|
||||
for b := range state.standardButtonDurations {
|
||||
if state.standardButtonDurations[b] != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if theInputState.prevStandardGamepadButtonDurations[id][b] > 0 {
|
||||
if prevState.standardButtonDurations[b] == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
buttons = append(buttons, b)
|
||||
buttons = append(buttons, ebiten.StandardGamepadButton(b))
|
||||
}
|
||||
|
||||
return buttons
|
||||
@@ -620,15 +505,16 @@ func IsStandardGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.Stan
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
var prev int
|
||||
if _, ok := theInputState.prevStandardGamepadButtonDurations[id]; ok {
|
||||
prev = theInputState.prevStandardGamepadButtonDurations[id][button]
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
var current int
|
||||
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok {
|
||||
current = theInputState.standardGamepadButtonDurations[id][button]
|
||||
prevState, ok := theInputState.prevGamepadStates[id]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return current == 0 && prev > 0
|
||||
|
||||
return state.standardButtonDurations[button] == 0 && prevState.standardButtonDurations[button] > 0
|
||||
}
|
||||
|
||||
// StandardGamepadButtonPressDuration returns how long the standard gamepad button of the gamepad id is pressed in ticks (Update).
|
||||
@@ -640,10 +526,12 @@ func StandardGamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.Stand
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok {
|
||||
return theInputState.standardGamepadButtonDurations[id][button]
|
||||
state, ok := theInputState.gamepadStates[id]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return 0
|
||||
|
||||
return state.standardButtonDurations[button]
|
||||
}
|
||||
|
||||
// AppendJustPressedTouchIDs append touch IDs that are created just in the current tick to touchIDs,
|
||||
@@ -658,17 +546,14 @@ func AppendJustPressedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
origLen := len(touchIDs)
|
||||
for id, s := range theInputState.touchDurations {
|
||||
if s == 1 {
|
||||
touchIDs = append(touchIDs, id)
|
||||
for id, state := range theInputState.touchStates {
|
||||
if state.duration != 1 {
|
||||
continue
|
||||
}
|
||||
touchIDs = append(touchIDs, id)
|
||||
}
|
||||
|
||||
s := touchIDs[origLen:]
|
||||
sort.Slice(s, func(a, b int) bool {
|
||||
return s[a] < s[b]
|
||||
})
|
||||
|
||||
slices.Sort(touchIDs[origLen:])
|
||||
return touchIDs
|
||||
}
|
||||
|
||||
@@ -693,17 +578,18 @@ func AppendJustReleasedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
origLen := len(touchIDs)
|
||||
for id := range theInputState.prevTouchDurations {
|
||||
if theInputState.touchDurations[id] == 0 && theInputState.prevTouchDurations[id] > 0 {
|
||||
touchIDs = append(touchIDs, id)
|
||||
// Iterate prevTouchStates instead of touchStates since touchStates doesn't have released touches.
|
||||
for id, state := range theInputState.prevTouchStates {
|
||||
if state.duration == 0 {
|
||||
continue
|
||||
}
|
||||
if theInputState.touchStates[id].duration != 0 {
|
||||
continue
|
||||
}
|
||||
touchIDs = append(touchIDs, id)
|
||||
}
|
||||
|
||||
s := touchIDs[origLen:]
|
||||
sort.Slice(s, func(a, b int) bool {
|
||||
return s[a] < s[b]
|
||||
})
|
||||
|
||||
slices.Sort(touchIDs[origLen:])
|
||||
return touchIDs
|
||||
}
|
||||
|
||||
@@ -717,7 +603,9 @@ func IsTouchJustReleased(id ebiten.TouchID) bool {
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
return theInputState.touchDurations[id] == 0 && theInputState.prevTouchDurations[id] > 0
|
||||
current := theInputState.touchStates[id]
|
||||
prev := theInputState.prevTouchStates[id]
|
||||
return current.duration == 0 && prev.duration > 0
|
||||
}
|
||||
|
||||
// TouchPressDuration returns how long the touch remains in ticks (Update).
|
||||
@@ -727,9 +615,8 @@ func IsTouchJustReleased(id ebiten.TouchID) bool {
|
||||
// TouchPressDuration is concurrent safe.
|
||||
func TouchPressDuration(id ebiten.TouchID) int {
|
||||
theInputState.m.RLock()
|
||||
s := theInputState.touchDurations[id]
|
||||
theInputState.m.RUnlock()
|
||||
return s
|
||||
defer theInputState.m.RUnlock()
|
||||
return theInputState.touchStates[id].duration
|
||||
}
|
||||
|
||||
// TouchPositionInPreviousTick returns the position in the previous tick.
|
||||
@@ -742,6 +629,6 @@ func TouchPositionInPreviousTick(id ebiten.TouchID) (int, int) {
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
p := theInputState.prevTouchPositions[id]
|
||||
return p.x, p.y
|
||||
state := theInputState.prevTouchStates[id]
|
||||
return state.x, state.y
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user