Compare commits

...

8 Commits

Author SHA1 Message Date
Hajime Hoshi
01dc4ed9b1 inpututil: refactoring: clean up gamepad implementations 2024-10-26 19:28:53 +09:00
Hajime Hoshi
e6b8ebe8a2 internal/gamepaddb: bug fix: typo
Closes #3148
2024-10-26 19:05:51 +09:00
Hajime Hoshi
9127c30223 inpututil: bug fix: AppendJustReleasedGamepadButtons and AppendJustReleasedStandardGamepadButtons returned just-pressed buttons
Closes #3147
2024-10-26 17:31:24 +09:00
Hajime Hoshi
812feec1d5 inpututil: reland: refactoring: use clear 2024-10-26 16:54:31 +09:00
Hajime Hoshi
7fe50478a7 Revert "inpututil: refactoring: use clear"
This reverts commit d6427d63ca.

Reason: wrong clear usages
2024-10-26 16:52:17 +09:00
Hajime Hoshi
d6427d63ca inpututil: refactoring: use clear 2024-10-26 16:47:43 +09:00
Hajime Hoshi
a6b278d0e5 inpututil: refactoring 2024-10-26 16:26:04 +09:00
Hajime Hoshi
c7880a5ad6 internal/atlas: reduce for-loops 2024-10-26 16:02:25 +09:00
4 changed files with 148 additions and 204 deletions

View File

@ -16,7 +16,7 @@
package inpututil
import (
"sort"
"slices"
"sync"
"github.com/hajimehoshi/ebiten/v2"
@ -28,21 +28,20 @@ type pos struct {
y int
}
type gamepadState struct {
buttonDurations [ebiten.GamepadButtonMax + 1]int
standardButtonDurations [ebiten.StandardGamepadButtonMax + 1]int
}
type inputState struct {
keyDurations []int
prevKeyDurations []int
keyDurations [ebiten.KeyMax + 1]int
prevKeyDurations [ebiten.KeyMax + 1]int
mouseButtonDurations map[ebiten.MouseButton]int
prevMouseButtonDurations map[ebiten.MouseButton]int
mouseButtonDurations [ebiten.MouseButtonMax + 1]int
prevMouseButtonDurations [ebiten.MouseButtonMax + 1]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
gamepadStates map[ebiten.GamepadID]gamepadState
prevGamepadStates map[ebiten.GamepadID]gamepadState
touchIDs map[ebiten.TouchID]struct{}
touchDurations map[ebiten.TouchID]int
@ -57,20 +56,8 @@ 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{},
gamepadStates: map[ebiten.GamepadID]gamepadState{},
prevGamepadStates: map[ebiten.GamepadID]gamepadState{},
touchIDs: map[ebiten.TouchID]struct{}{},
touchDurations: map[ebiten.TouchID]int{},
@ -91,109 +78,83 @@ func (i *inputState) update() {
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]++
copy(i.prevKeyDurations[:], i.keyDurations[:])
for idx := range i.keyDurations {
if ebiten.IsKeyPressed(ebiten.Key(idx)) {
i.keyDurations[idx]++
} else {
i.keyDurations[k] = 0
i.keyDurations[idx] = 0
}
}
// Mouse
for b := ebiten.MouseButton(0); b <= ebiten.MouseButtonMax; b++ {
i.prevMouseButtonDurations[b] = i.mouseButtonDurations[b]
if ebiten.IsMouseButtonPressed(b) {
i.mouseButtonDurations[b]++
copy(i.prevMouseButtonDurations[:], i.mouseButtonDurations[:])
for idx := range i.mouseButtonDurations {
if ebiten.IsMouseButtonPressed(ebiten.MouseButton(idx)) {
i.mouseButtonDurations[idx]++
} else {
i.mouseButtonDurations[b] = 0
i.mouseButtonDurations[idx] = 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)
// Remove disconnected gamepads.
for id := range i.gamepadStates {
var found bool
for _, id2 := range i.gamepadIDsBuf {
if id == id2 {
found = true
break
}
}
for id := range i.standardGamepadButtonDurations {
if _, ok := i.gamepadIDs[id]; !ok {
delete(i.standardGamepadButtonDurations, id)
if !found {
delete(i.gamepadStates, id)
}
}
// Touches
// Copy the touch durations and positions.
for id := range i.prevTouchDurations {
delete(i.prevTouchDurations, id)
}
clear(i.prevTouchPositions)
for id := range i.touchDurations {
i.prevTouchDurations[id] = i.touchDurations[id]
}
for id := range i.prevTouchPositions {
delete(i.prevTouchPositions, id)
}
clear(i.prevTouchPositions)
for id := range i.touchPositions {
i.prevTouchPositions[id] = i.touchPositions[id]
}
for id := range i.touchIDs {
delete(i.touchIDs, id)
}
clear(i.touchIDs)
i.touchIDsBuf = ebiten.AppendTouchIDs(i.touchIDsBuf[:0])
for _, id := range i.touchIDsBuf {
i.touchIDs[id] = struct{}{}
@ -266,14 +227,14 @@ 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 {
for i := range theInputState.keyDurations {
if theInputState.keyDurations[i] != 0 {
continue
}
if theInputState.prevKeyDurations[k] == 0 {
if theInputState.prevKeyDurations[i] == 0 {
continue
}
keys = append(keys, k)
keys = append(keys, ebiten.Key(i))
}
return keys
}
@ -296,9 +257,8 @@ 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
defer theInputState.m.RUnlock()
return theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
}
// KeyPressDuration returns how long the key is pressed in ticks (Update).
@ -308,9 +268,8 @@ 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
defer theInputState.m.RUnlock()
return theInputState.keyDurations[key]
}
// IsMouseButtonJustPressed returns a boolean value indicating
@ -331,10 +290,8 @@ 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
defer theInputState.m.RUnlock()
return theInputState.mouseButtonDurations[button] == 0 && theInputState.prevMouseButtonDurations[button] > 0
}
// MouseButtonPressDuration returns how long the mouse button is pressed in ticks (Update).
@ -344,9 +301,8 @@ 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
defer theInputState.m.RUnlock()
return theInputState.mouseButtonDurations[button]
}
// AppendJustConnectedGamepadIDs appends gamepad IDs that are connected just in the current tick to gamepadIDs,
@ -357,18 +313,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 +344,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 +361,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 +386,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 +411,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 +451,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 +472,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 +492,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 +517,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 +542,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 +584,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 +605,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 state.standardButtonDurations[button]
}
// AppendJustPressedTouchIDs append touch IDs that are created just in the current tick to touchIDs,
@ -664,11 +631,7 @@ func AppendJustPressedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
}
}
s := touchIDs[origLen:]
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
slices.Sort(touchIDs[origLen:])
return touchIDs
}
@ -699,11 +662,7 @@ func AppendJustReleasedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
}
}
s := touchIDs[origLen:]
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
slices.Sort(touchIDs[origLen:])
return touchIDs
}
@ -727,9 +686,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.touchDurations[id]
}
// TouchPositionInPreviousTick returns the position in the previous tick.

View File

@ -421,6 +421,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice
}
}
var imgs [graphics.ShaderSrcImageCount]*restorable.Image
for i, src := range srcs {
if src == nil {
continue
@ -429,33 +430,18 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice
// A source region can be deliberately empty when this is not needed in order to avoid unexpected
// performance issue (#1293).
// TODO: This should no longer be needed but is kept just in case. Remove this later.
if srcRegions[i].Empty() {
continue
}
if !srcRegions[i].Empty() {
r := src.regionWithPadding()
srcRegions[i] = srcRegions[i].Add(r.Min)
}
var imgs [graphics.ShaderSrcImageCount]*restorable.Image
for i, src := range srcs {
if src == nil {
continue
}
imgs[i] = src.backend.restorable
}
i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule, hint)
for _, src := range srcs {
if src == nil {
continue
}
if !src.isOnSourceBackend() && src.canBePutOnAtlas() {
// src might already registered, but assigning it again is not harmful.
imagesToPutOnSourceBackend.add(src)
}
}
i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule, hint)
}
// WritePixels replaces the pixels on the image.

View File

@ -14,7 +14,7 @@
// Code generated by gen.go using 'go generate'. DO NOT EDIT.
//go:build darwing && !ios
//go:build darwin && !ios
package gamepaddb

View File

@ -113,7 +113,7 @@ func run() error {
},
"Mac OS X": {
filenameSuffix: "macos",
buildConstraints: "//go:build darwing && !ios",
buildConstraints: "//go:build darwin && !ios",
},
"Linux": {
filenameSuffix: "linbsd",