Compare commits

..

No commits in common. "01dc4ed9b1f5520c9a90c7efa2362419f56e04ae" and "ecc42d4042228987fbd006605c38a6193aa71cff" have entirely different histories.

4 changed files with 204 additions and 148 deletions

View File

@ -16,7 +16,7 @@
package inpututil
import (
"slices"
"sort"
"sync"
"github.com/hajimehoshi/ebiten/v2"
@ -28,20 +28,21 @@ type pos struct {
y int
}
type gamepadState struct {
buttonDurations [ebiten.GamepadButtonMax + 1]int
standardButtonDurations [ebiten.StandardGamepadButtonMax + 1]int
}
type inputState struct {
keyDurations [ebiten.KeyMax + 1]int
prevKeyDurations [ebiten.KeyMax + 1]int
keyDurations []int
prevKeyDurations []int
mouseButtonDurations [ebiten.MouseButtonMax + 1]int
prevMouseButtonDurations [ebiten.MouseButtonMax + 1]int
mouseButtonDurations map[ebiten.MouseButton]int
prevMouseButtonDurations map[ebiten.MouseButton]int
gamepadStates map[ebiten.GamepadID]gamepadState
prevGamepadStates map[ebiten.GamepadID]gamepadState
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
@ -56,8 +57,20 @@ type inputState struct {
}
var theInputState = &inputState{
gamepadStates: map[ebiten.GamepadID]gamepadState{},
prevGamepadStates: map[ebiten.GamepadID]gamepadState{},
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{},
@ -78,83 +91,109 @@ func (i *inputState) update() {
defer i.m.Unlock()
// Keyboard
copy(i.prevKeyDurations[:], i.keyDurations[:])
for idx := range i.keyDurations {
if ebiten.IsKeyPressed(ebiten.Key(idx)) {
i.keyDurations[idx]++
copy(i.prevKeyDurations, i.keyDurations)
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
if ebiten.IsKeyPressed(k) {
i.keyDurations[k]++
} else {
i.keyDurations[idx] = 0
i.keyDurations[k] = 0
}
}
// Mouse
copy(i.prevMouseButtonDurations[:], i.mouseButtonDurations[:])
for idx := range i.mouseButtonDurations {
if ebiten.IsMouseButtonPressed(ebiten.MouseButton(idx)) {
i.mouseButtonDurations[idx]++
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[idx] = 0
i.mouseButtonDurations[b] = 0
}
}
// Gamepads
// Copy the gamepad states.
clear(i.prevGamepadStates)
for id, s := range i.gamepadStates {
i.prevGamepadStates[id] = s
// 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 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 {
state := i.gamepadStates[id]
i.gamepadIDs[id] = struct{}{}
for b := range i.gamepadStates[id].buttonDurations {
if ebiten.IsGamepadButtonPressed(id, ebiten.GamepadButton(b)) {
state.buttonDurations[b]++
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]++
} else {
state.buttonDurations[b] = 0
i.gamepadButtonDurations[id][b] = 0
}
}
for b := range i.gamepadStates[id].standardButtonDurations {
if ebiten.IsStandardGamepadButtonPressed(id, ebiten.StandardGamepadButton(b)) {
state.standardButtonDurations[b]++
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]++
} else {
state.standardButtonDurations[b] = 0
i.standardGamepadButtonDurations[id][b] = 0
}
}
i.gamepadStates[id] = state
}
// 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.gamepadButtonDurations {
if _, ok := i.gamepadIDs[id]; !ok {
delete(i.gamepadButtonDurations, id)
}
if !found {
delete(i.gamepadStates, id)
}
for id := range i.standardGamepadButtonDurations {
if _, ok := i.gamepadIDs[id]; !ok {
delete(i.standardGamepadButtonDurations, id)
}
}
// Touches
// Copy the touch durations and positions.
clear(i.prevTouchPositions)
for id := range i.prevTouchDurations {
delete(i.prevTouchDurations, id)
}
for id := range i.touchDurations {
i.prevTouchDurations[id] = i.touchDurations[id]
}
clear(i.prevTouchPositions)
for id := range i.prevTouchPositions {
delete(i.prevTouchPositions, id)
}
for id := range i.touchPositions {
i.prevTouchPositions[id] = i.touchPositions[id]
}
clear(i.touchIDs)
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{}{}
@ -227,14 +266,14 @@ func AppendJustReleasedKeys(keys []ebiten.Key) []ebiten.Key {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
for i := range theInputState.keyDurations {
if theInputState.keyDurations[i] != 0 {
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
if theInputState.keyDurations[k] != 0 {
continue
}
if theInputState.prevKeyDurations[i] == 0 {
if theInputState.prevKeyDurations[k] == 0 {
continue
}
keys = append(keys, ebiten.Key(i))
keys = append(keys, k)
}
return keys
}
@ -257,8 +296,9 @@ func IsKeyJustPressed(key ebiten.Key) bool {
// IsKeyJustReleased is concurrent safe.
func IsKeyJustReleased(key ebiten.Key) bool {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
return theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
r := theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
theInputState.m.RUnlock()
return r
}
// KeyPressDuration returns how long the key is pressed in ticks (Update).
@ -268,8 +308,9 @@ func IsKeyJustReleased(key ebiten.Key) bool {
// KeyPressDuration is concurrent safe.
func KeyPressDuration(key ebiten.Key) int {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
return theInputState.keyDurations[key]
s := theInputState.keyDurations[key]
theInputState.m.RUnlock()
return s
}
// IsMouseButtonJustPressed returns a boolean value indicating
@ -290,8 +331,10 @@ func IsMouseButtonJustPressed(button ebiten.MouseButton) bool {
// IsMouseButtonJustReleased is concurrent safe.
func IsMouseButtonJustReleased(button ebiten.MouseButton) bool {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
return theInputState.mouseButtonDurations[button] == 0 && theInputState.prevMouseButtonDurations[button] > 0
r := theInputState.mouseButtonDurations[button] == 0 &&
theInputState.prevMouseButtonDurations[button] > 0
theInputState.m.RUnlock()
return r
}
// MouseButtonPressDuration returns how long the mouse button is pressed in ticks (Update).
@ -301,8 +344,9 @@ func IsMouseButtonJustReleased(button ebiten.MouseButton) bool {
// MouseButtonPressDuration is concurrent safe.
func MouseButtonPressDuration(button ebiten.MouseButton) int {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
return theInputState.mouseButtonDurations[button]
s := theInputState.mouseButtonDurations[button]
theInputState.m.RUnlock()
return s
}
// AppendJustConnectedGamepadIDs appends gamepad IDs that are connected just in the current tick to gamepadIDs,
@ -313,17 +357,18 @@ func MouseButtonPressDuration(button ebiten.MouseButton) int {
//
// AppendJustConnectedGamepadIDs is concurrent safe.
func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
origLen := len(gamepadIDs)
for id := range theInputState.gamepadStates {
if _, ok := theInputState.prevGamepadStates[id]; !ok {
theInputState.m.RLock()
for id := range theInputState.gamepadIDs {
if _, ok := theInputState.prevGamepadIDs[id]; !ok {
gamepadIDs = append(gamepadIDs, id)
}
}
slices.Sort(gamepadIDs[origLen:])
theInputState.m.RUnlock()
s := gamepadIDs[origLen:]
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
return gamepadIDs
}
@ -344,11 +389,10 @@ func JustConnectedGamepadIDs() []ebiten.GamepadID {
// IsGamepadJustDisconnected is concurrent safe.
func IsGamepadJustDisconnected(id ebiten.GamepadID) bool {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
_, current := theInputState.gamepadStates[id]
_, prev := theInputState.prevGamepadStates[id]
return !current && prev
_, prev := theInputState.prevGamepadIDs[id]
_, current := theInputState.gamepadIDs[id]
theInputState.m.RUnlock()
return prev && !current
}
// AppendPressedGamepadButtons append currently pressed gamepad buttons to buttons and returns the extended buffer.
@ -361,12 +405,11 @@ func AppendPressedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.GamepadBu
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
return buttons
}
for b, d := range state.buttonDurations {
for b, d := range theInputState.gamepadButtonDurations[id] {
if d == 0 {
continue
}
@ -386,12 +429,11 @@ func AppendJustPressedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.Gamep
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
return buttons
}
for b, d := range state.buttonDurations {
for b, d := range theInputState.gamepadButtonDurations[id] {
if d != 1 {
continue
}
@ -411,23 +453,23 @@ func AppendJustReleasedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.Game
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
return buttons
}
prevState, ok := theInputState.prevGamepadStates[id]
if !ok {
if _, ok := theInputState.prevGamepadButtonDurations[id]; !ok {
return buttons
}
for b := range state.buttonDurations {
if state.buttonDurations[b] != 0 {
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
if theInputState.gamepadButtonDurations[id][b] == 0 {
continue
}
if prevState.buttonDurations[b] == 0 {
if theInputState.prevGamepadButtonDurations[id][b] > 0 {
continue
}
buttons = append(buttons, ebiten.GamepadButton(b))
buttons = append(buttons, b)
}
return buttons
@ -451,18 +493,16 @@ func IsGamepadButtonJustPressed(id ebiten.GamepadID, button ebiten.GamepadButton
// IsGamepadButtonJustReleased is concurrent safe.
func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButton) bool {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
return false
prev := 0
if _, ok := theInputState.prevGamepadButtonDurations[id]; ok {
prev = theInputState.prevGamepadButtonDurations[id][button]
}
prevState, ok := theInputState.prevGamepadStates[id]
if !ok {
return false
current := 0
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
current = theInputState.gamepadButtonDurations[id][button]
}
return state.buttonDurations[button] == 0 && prevState.buttonDurations[button] > 0
theInputState.m.RUnlock()
return current == 0 && prev > 0
}
// GamepadButtonPressDuration returns how long the gamepad button of the gamepad id is pressed in ticks (Update).
@ -472,14 +512,12 @@ func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButto
// GamepadButtonPressDuration is concurrent safe.
func GamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.GamepadButton) int {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
return 0
s := 0
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
s = theInputState.gamepadButtonDurations[id][button]
}
return state.buttonDurations[button]
theInputState.m.RUnlock()
return s
}
// AppendPressedStandardGamepadButtons append currently pressed standard gamepad buttons to buttons and returns the extended buffer.
@ -492,16 +530,15 @@ func AppendPressedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebiten.S
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
if _, ok := theInputState.standardGamepadButtonDurations[id]; !ok {
return buttons
}
for i, d := range state.standardButtonDurations {
for b, d := range theInputState.standardGamepadButtonDurations[id] {
if d == 0 {
continue
}
buttons = append(buttons, ebiten.StandardGamepadButton(i))
buttons = append(buttons, ebiten.StandardGamepadButton(b))
}
return buttons
@ -517,12 +554,11 @@ func AppendJustPressedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebit
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
return buttons
}
for b, d := range state.standardButtonDurations {
for b, d := range theInputState.standardGamepadButtonDurations[id] {
if d != 1 {
continue
}
@ -542,23 +578,23 @@ func AppendJustReleasedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebi
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
return buttons
}
prevState, ok := theInputState.prevGamepadStates[id]
if !ok {
if _, ok := theInputState.prevGamepadButtonDurations[id]; !ok {
return buttons
}
for b := range state.standardButtonDurations {
if state.standardButtonDurations[b] != 0 {
for b := ebiten.StandardGamepadButton(0); b <= ebiten.StandardGamepadButtonMax; b++ {
if theInputState.standardGamepadButtonDurations[id][b] == 0 {
continue
}
if prevState.standardButtonDurations[b] == 0 {
if theInputState.prevStandardGamepadButtonDurations[id][b] > 0 {
continue
}
buttons = append(buttons, ebiten.StandardGamepadButton(b))
buttons = append(buttons, b)
}
return buttons
@ -584,16 +620,15 @@ func IsStandardGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.Stan
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
return false
var prev int
if _, ok := theInputState.prevStandardGamepadButtonDurations[id]; ok {
prev = theInputState.prevStandardGamepadButtonDurations[id][button]
}
prevState, ok := theInputState.prevGamepadStates[id]
if !ok {
return false
var current int
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok {
current = theInputState.standardGamepadButtonDurations[id][button]
}
return state.standardButtonDurations[button] == 0 && prevState.standardButtonDurations[button] > 0
return current == 0 && prev > 0
}
// StandardGamepadButtonPressDuration returns how long the standard gamepad button of the gamepad id is pressed in ticks (Update).
@ -605,12 +640,10 @@ func StandardGamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.Stand
theInputState.m.RLock()
defer theInputState.m.RUnlock()
state, ok := theInputState.gamepadStates[id]
if !ok {
return 0
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok {
return theInputState.standardGamepadButtonDurations[id][button]
}
return state.standardButtonDurations[button]
return 0
}
// AppendJustPressedTouchIDs append touch IDs that are created just in the current tick to touchIDs,
@ -631,7 +664,11 @@ func AppendJustPressedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
}
}
slices.Sort(touchIDs[origLen:])
s := touchIDs[origLen:]
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
return touchIDs
}
@ -662,7 +699,11 @@ func AppendJustReleasedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
}
}
slices.Sort(touchIDs[origLen:])
s := touchIDs[origLen:]
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
return touchIDs
}
@ -686,8 +727,9 @@ func IsTouchJustReleased(id ebiten.TouchID) bool {
// TouchPressDuration is concurrent safe.
func TouchPressDuration(id ebiten.TouchID) int {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
return theInputState.touchDurations[id]
s := theInputState.touchDurations[id]
theInputState.m.RUnlock()
return s
}
// TouchPositionInPreviousTick returns the position in the previous tick.

View File

@ -421,7 +421,6 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice
}
}
var imgs [graphics.ShaderSrcImageCount]*restorable.Image
for i, src := range srcs {
if src == nil {
continue
@ -430,18 +429,33 @@ 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() {
r := src.regionWithPadding()
srcRegions[i] = srcRegions[i].Add(r.Min)
if srcRegions[i].Empty() {
continue
}
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 darwin && !ios
//go:build darwing && !ios
package gamepaddb

View File

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