mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-14 13:02:08 +01:00
Compare commits
No commits in common. "01dc4ed9b1f5520c9a90c7efa2362419f56e04ae" and "ecc42d4042228987fbd006605c38a6193aa71cff" have entirely different histories.
01dc4ed9b1
...
ecc42d4042
@ -16,7 +16,7 @@
|
|||||||
package inpututil
|
package inpututil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"slices"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
@ -28,20 +28,21 @@ type pos struct {
|
|||||||
y int
|
y int
|
||||||
}
|
}
|
||||||
|
|
||||||
type gamepadState struct {
|
|
||||||
buttonDurations [ebiten.GamepadButtonMax + 1]int
|
|
||||||
standardButtonDurations [ebiten.StandardGamepadButtonMax + 1]int
|
|
||||||
}
|
|
||||||
|
|
||||||
type inputState struct {
|
type inputState struct {
|
||||||
keyDurations [ebiten.KeyMax + 1]int
|
keyDurations []int
|
||||||
prevKeyDurations [ebiten.KeyMax + 1]int
|
prevKeyDurations []int
|
||||||
|
|
||||||
mouseButtonDurations [ebiten.MouseButtonMax + 1]int
|
mouseButtonDurations map[ebiten.MouseButton]int
|
||||||
prevMouseButtonDurations [ebiten.MouseButtonMax + 1]int
|
prevMouseButtonDurations map[ebiten.MouseButton]int
|
||||||
|
|
||||||
gamepadStates map[ebiten.GamepadID]gamepadState
|
gamepadIDs map[ebiten.GamepadID]struct{}
|
||||||
prevGamepadStates map[ebiten.GamepadID]gamepadState
|
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{}
|
touchIDs map[ebiten.TouchID]struct{}
|
||||||
touchDurations map[ebiten.TouchID]int
|
touchDurations map[ebiten.TouchID]int
|
||||||
@ -56,8 +57,20 @@ type inputState struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var theInputState = &inputState{
|
var theInputState = &inputState{
|
||||||
gamepadStates: map[ebiten.GamepadID]gamepadState{},
|
keyDurations: make([]int, ebiten.KeyMax+1),
|
||||||
prevGamepadStates: map[ebiten.GamepadID]gamepadState{},
|
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{}{},
|
touchIDs: map[ebiten.TouchID]struct{}{},
|
||||||
touchDurations: map[ebiten.TouchID]int{},
|
touchDurations: map[ebiten.TouchID]int{},
|
||||||
@ -78,83 +91,109 @@ func (i *inputState) update() {
|
|||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
|
|
||||||
// Keyboard
|
// Keyboard
|
||||||
copy(i.prevKeyDurations[:], i.keyDurations[:])
|
copy(i.prevKeyDurations, i.keyDurations)
|
||||||
for idx := range i.keyDurations {
|
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
|
||||||
if ebiten.IsKeyPressed(ebiten.Key(idx)) {
|
if ebiten.IsKeyPressed(k) {
|
||||||
i.keyDurations[idx]++
|
i.keyDurations[k]++
|
||||||
} else {
|
} else {
|
||||||
i.keyDurations[idx] = 0
|
i.keyDurations[k] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mouse
|
// Mouse
|
||||||
copy(i.prevMouseButtonDurations[:], i.mouseButtonDurations[:])
|
for b := ebiten.MouseButton(0); b <= ebiten.MouseButtonMax; b++ {
|
||||||
for idx := range i.mouseButtonDurations {
|
i.prevMouseButtonDurations[b] = i.mouseButtonDurations[b]
|
||||||
if ebiten.IsMouseButtonPressed(ebiten.MouseButton(idx)) {
|
if ebiten.IsMouseButtonPressed(b) {
|
||||||
i.mouseButtonDurations[idx]++
|
i.mouseButtonDurations[b]++
|
||||||
} else {
|
} else {
|
||||||
i.mouseButtonDurations[idx] = 0
|
i.mouseButtonDurations[b] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gamepads
|
// Gamepads
|
||||||
|
|
||||||
// Copy the gamepad states.
|
// Copy the gamepad IDs.
|
||||||
clear(i.prevGamepadStates)
|
for id := range i.prevGamepadIDs {
|
||||||
for id, s := range i.gamepadStates {
|
delete(i.prevGamepadIDs, id)
|
||||||
i.prevGamepadStates[id] = s
|
}
|
||||||
|
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])
|
i.gamepadIDsBuf = ebiten.AppendGamepadIDs(i.gamepadIDsBuf[:0])
|
||||||
for _, id := range i.gamepadIDsBuf {
|
for _, id := range i.gamepadIDsBuf {
|
||||||
state := i.gamepadStates[id]
|
i.gamepadIDs[id] = struct{}{}
|
||||||
|
|
||||||
for b := range i.gamepadStates[id].buttonDurations {
|
if _, ok := i.gamepadButtonDurations[id]; !ok {
|
||||||
if ebiten.IsGamepadButtonPressed(id, ebiten.GamepadButton(b)) {
|
i.gamepadButtonDurations[id] = make([]int, ebiten.GamepadButtonMax+1)
|
||||||
state.buttonDurations[b]++
|
}
|
||||||
|
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
|
||||||
|
if ebiten.IsGamepadButtonPressed(id, b) {
|
||||||
|
i.gamepadButtonDurations[id][b]++
|
||||||
} else {
|
} else {
|
||||||
state.buttonDurations[b] = 0
|
i.gamepadButtonDurations[id][b] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for b := range i.gamepadStates[id].standardButtonDurations {
|
if _, ok := i.standardGamepadButtonDurations[id]; !ok {
|
||||||
if ebiten.IsStandardGamepadButtonPressed(id, ebiten.StandardGamepadButton(b)) {
|
i.standardGamepadButtonDurations[id] = make([]int, ebiten.StandardGamepadButtonMax+1)
|
||||||
state.standardButtonDurations[b]++
|
}
|
||||||
|
for b := ebiten.StandardGamepadButton(0); b <= ebiten.StandardGamepadButtonMax; b++ {
|
||||||
|
if ebiten.IsStandardGamepadButtonPressed(id, b) {
|
||||||
|
i.standardGamepadButtonDurations[id][b]++
|
||||||
} else {
|
} else {
|
||||||
state.standardButtonDurations[b] = 0
|
i.standardGamepadButtonDurations[id][b] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
i.gamepadStates[id] = state
|
|
||||||
}
|
}
|
||||||
|
for id := range i.gamepadButtonDurations {
|
||||||
// Remove disconnected gamepads.
|
if _, ok := i.gamepadIDs[id]; !ok {
|
||||||
for id := range i.gamepadStates {
|
delete(i.gamepadButtonDurations, id)
|
||||||
var found bool
|
|
||||||
for _, id2 := range i.gamepadIDsBuf {
|
|
||||||
if id == id2 {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if !found {
|
}
|
||||||
delete(i.gamepadStates, id)
|
for id := range i.standardGamepadButtonDurations {
|
||||||
|
if _, ok := i.gamepadIDs[id]; !ok {
|
||||||
|
delete(i.standardGamepadButtonDurations, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Touches
|
// Touches
|
||||||
|
|
||||||
// Copy the touch durations and positions.
|
// Copy the touch durations and positions.
|
||||||
clear(i.prevTouchPositions)
|
for id := range i.prevTouchDurations {
|
||||||
|
delete(i.prevTouchDurations, id)
|
||||||
|
}
|
||||||
for id := range i.touchDurations {
|
for id := range i.touchDurations {
|
||||||
i.prevTouchDurations[id] = i.touchDurations[id]
|
i.prevTouchDurations[id] = i.touchDurations[id]
|
||||||
}
|
}
|
||||||
clear(i.prevTouchPositions)
|
for id := range i.prevTouchPositions {
|
||||||
|
delete(i.prevTouchPositions, id)
|
||||||
|
}
|
||||||
for id := range i.touchPositions {
|
for id := range i.touchPositions {
|
||||||
i.prevTouchPositions[id] = i.touchPositions[id]
|
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])
|
i.touchIDsBuf = ebiten.AppendTouchIDs(i.touchIDsBuf[:0])
|
||||||
for _, id := range i.touchIDsBuf {
|
for _, id := range i.touchIDsBuf {
|
||||||
i.touchIDs[id] = struct{}{}
|
i.touchIDs[id] = struct{}{}
|
||||||
@ -227,14 +266,14 @@ func AppendJustReleasedKeys(keys []ebiten.Key) []ebiten.Key {
|
|||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
defer theInputState.m.RUnlock()
|
||||||
|
|
||||||
for i := range theInputState.keyDurations {
|
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
|
||||||
if theInputState.keyDurations[i] != 0 {
|
if theInputState.keyDurations[k] != 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if theInputState.prevKeyDurations[i] == 0 {
|
if theInputState.prevKeyDurations[k] == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
keys = append(keys, ebiten.Key(i))
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
@ -257,8 +296,9 @@ func IsKeyJustPressed(key ebiten.Key) bool {
|
|||||||
// IsKeyJustReleased is concurrent safe.
|
// IsKeyJustReleased is concurrent safe.
|
||||||
func IsKeyJustReleased(key ebiten.Key) bool {
|
func IsKeyJustReleased(key ebiten.Key) bool {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
r := theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
|
||||||
return theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
|
theInputState.m.RUnlock()
|
||||||
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// KeyPressDuration returns how long the key is pressed in ticks (Update).
|
// 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.
|
// KeyPressDuration is concurrent safe.
|
||||||
func KeyPressDuration(key ebiten.Key) int {
|
func KeyPressDuration(key ebiten.Key) int {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
s := theInputState.keyDurations[key]
|
||||||
return theInputState.keyDurations[key]
|
theInputState.m.RUnlock()
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsMouseButtonJustPressed returns a boolean value indicating
|
// IsMouseButtonJustPressed returns a boolean value indicating
|
||||||
@ -290,8 +331,10 @@ func IsMouseButtonJustPressed(button ebiten.MouseButton) bool {
|
|||||||
// IsMouseButtonJustReleased is concurrent safe.
|
// IsMouseButtonJustReleased is concurrent safe.
|
||||||
func IsMouseButtonJustReleased(button ebiten.MouseButton) bool {
|
func IsMouseButtonJustReleased(button ebiten.MouseButton) bool {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
r := theInputState.mouseButtonDurations[button] == 0 &&
|
||||||
return theInputState.mouseButtonDurations[button] == 0 && theInputState.prevMouseButtonDurations[button] > 0
|
theInputState.prevMouseButtonDurations[button] > 0
|
||||||
|
theInputState.m.RUnlock()
|
||||||
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// MouseButtonPressDuration returns how long the mouse button is pressed in ticks (Update).
|
// 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.
|
// MouseButtonPressDuration is concurrent safe.
|
||||||
func MouseButtonPressDuration(button ebiten.MouseButton) int {
|
func MouseButtonPressDuration(button ebiten.MouseButton) int {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
s := theInputState.mouseButtonDurations[button]
|
||||||
return theInputState.mouseButtonDurations[button]
|
theInputState.m.RUnlock()
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendJustConnectedGamepadIDs appends gamepad IDs that are connected just in the current tick to gamepadIDs,
|
// 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.
|
// AppendJustConnectedGamepadIDs is concurrent safe.
|
||||||
func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID {
|
func AppendJustConnectedGamepadIDs(gamepadIDs []ebiten.GamepadID) []ebiten.GamepadID {
|
||||||
theInputState.m.RLock()
|
|
||||||
defer theInputState.m.RUnlock()
|
|
||||||
|
|
||||||
origLen := len(gamepadIDs)
|
origLen := len(gamepadIDs)
|
||||||
for id := range theInputState.gamepadStates {
|
theInputState.m.RLock()
|
||||||
if _, ok := theInputState.prevGamepadStates[id]; !ok {
|
for id := range theInputState.gamepadIDs {
|
||||||
|
if _, ok := theInputState.prevGamepadIDs[id]; !ok {
|
||||||
gamepadIDs = append(gamepadIDs, id)
|
gamepadIDs = append(gamepadIDs, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
theInputState.m.RUnlock()
|
||||||
slices.Sort(gamepadIDs[origLen:])
|
s := gamepadIDs[origLen:]
|
||||||
|
sort.Slice(s, func(a, b int) bool {
|
||||||
|
return s[a] < s[b]
|
||||||
|
})
|
||||||
return gamepadIDs
|
return gamepadIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,11 +389,10 @@ func JustConnectedGamepadIDs() []ebiten.GamepadID {
|
|||||||
// IsGamepadJustDisconnected is concurrent safe.
|
// IsGamepadJustDisconnected is concurrent safe.
|
||||||
func IsGamepadJustDisconnected(id ebiten.GamepadID) bool {
|
func IsGamepadJustDisconnected(id ebiten.GamepadID) bool {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
_, prev := theInputState.prevGamepadIDs[id]
|
||||||
|
_, current := theInputState.gamepadIDs[id]
|
||||||
_, current := theInputState.gamepadStates[id]
|
theInputState.m.RUnlock()
|
||||||
_, prev := theInputState.prevGamepadStates[id]
|
return prev && !current
|
||||||
return !current && prev
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendPressedGamepadButtons append currently pressed gamepad buttons to buttons and returns the extended buffer.
|
// 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()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
defer theInputState.m.RUnlock()
|
||||||
|
|
||||||
state, ok := theInputState.gamepadStates[id]
|
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||||
if !ok {
|
|
||||||
return buttons
|
return buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
for b, d := range state.buttonDurations {
|
for b, d := range theInputState.gamepadButtonDurations[id] {
|
||||||
if d == 0 {
|
if d == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -386,12 +429,11 @@ func AppendJustPressedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.Gamep
|
|||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
defer theInputState.m.RUnlock()
|
||||||
|
|
||||||
state, ok := theInputState.gamepadStates[id]
|
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||||
if !ok {
|
|
||||||
return buttons
|
return buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
for b, d := range state.buttonDurations {
|
for b, d := range theInputState.gamepadButtonDurations[id] {
|
||||||
if d != 1 {
|
if d != 1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -411,23 +453,23 @@ func AppendJustReleasedGamepadButtons(id ebiten.GamepadID, buttons []ebiten.Game
|
|||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
defer theInputState.m.RUnlock()
|
||||||
|
|
||||||
state, ok := theInputState.gamepadStates[id]
|
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||||
if !ok {
|
|
||||||
return buttons
|
return buttons
|
||||||
}
|
}
|
||||||
prevState, ok := theInputState.prevGamepadStates[id]
|
if _, ok := theInputState.prevGamepadButtonDurations[id]; !ok {
|
||||||
if !ok {
|
|
||||||
return buttons
|
return buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
for b := range state.buttonDurations {
|
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
|
||||||
if state.buttonDurations[b] != 0 {
|
if theInputState.gamepadButtonDurations[id][b] == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if prevState.buttonDurations[b] == 0 {
|
|
||||||
|
if theInputState.prevGamepadButtonDurations[id][b] > 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
buttons = append(buttons, ebiten.GamepadButton(b))
|
|
||||||
|
buttons = append(buttons, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
return buttons
|
return buttons
|
||||||
@ -451,18 +493,16 @@ func IsGamepadButtonJustPressed(id ebiten.GamepadID, button ebiten.GamepadButton
|
|||||||
// IsGamepadButtonJustReleased is concurrent safe.
|
// IsGamepadButtonJustReleased is concurrent safe.
|
||||||
func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButton) bool {
|
func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButton) bool {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
prev := 0
|
||||||
|
if _, ok := theInputState.prevGamepadButtonDurations[id]; ok {
|
||||||
state, ok := theInputState.gamepadStates[id]
|
prev = theInputState.prevGamepadButtonDurations[id][button]
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
prevState, ok := theInputState.prevGamepadStates[id]
|
current := 0
|
||||||
if !ok {
|
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
|
||||||
return false
|
current = theInputState.gamepadButtonDurations[id][button]
|
||||||
}
|
}
|
||||||
|
theInputState.m.RUnlock()
|
||||||
return state.buttonDurations[button] == 0 && prevState.buttonDurations[button] > 0
|
return current == 0 && prev > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// GamepadButtonPressDuration returns how long the gamepad button of the gamepad id is pressed in ticks (Update).
|
// 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.
|
// GamepadButtonPressDuration is concurrent safe.
|
||||||
func GamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.GamepadButton) int {
|
func GamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.GamepadButton) int {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
s := 0
|
||||||
|
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
|
||||||
state, ok := theInputState.gamepadStates[id]
|
s = theInputState.gamepadButtonDurations[id][button]
|
||||||
if !ok {
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
theInputState.m.RUnlock()
|
||||||
return state.buttonDurations[button]
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendPressedStandardGamepadButtons append currently pressed standard gamepad buttons to buttons and returns the extended buffer.
|
// 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()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
defer theInputState.m.RUnlock()
|
||||||
|
|
||||||
state, ok := theInputState.gamepadStates[id]
|
if _, ok := theInputState.standardGamepadButtonDurations[id]; !ok {
|
||||||
if !ok {
|
|
||||||
return buttons
|
return buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, d := range state.standardButtonDurations {
|
for b, d := range theInputState.standardGamepadButtonDurations[id] {
|
||||||
if d == 0 {
|
if d == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
buttons = append(buttons, ebiten.StandardGamepadButton(i))
|
buttons = append(buttons, ebiten.StandardGamepadButton(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
return buttons
|
return buttons
|
||||||
@ -517,12 +554,11 @@ func AppendJustPressedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebit
|
|||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
defer theInputState.m.RUnlock()
|
||||||
|
|
||||||
state, ok := theInputState.gamepadStates[id]
|
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||||
if !ok {
|
|
||||||
return buttons
|
return buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
for b, d := range state.standardButtonDurations {
|
for b, d := range theInputState.standardGamepadButtonDurations[id] {
|
||||||
if d != 1 {
|
if d != 1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -542,23 +578,23 @@ func AppendJustReleasedStandardGamepadButtons(id ebiten.GamepadID, buttons []ebi
|
|||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
defer theInputState.m.RUnlock()
|
||||||
|
|
||||||
state, ok := theInputState.gamepadStates[id]
|
if _, ok := theInputState.gamepadButtonDurations[id]; !ok {
|
||||||
if !ok {
|
|
||||||
return buttons
|
return buttons
|
||||||
}
|
}
|
||||||
prevState, ok := theInputState.prevGamepadStates[id]
|
if _, ok := theInputState.prevGamepadButtonDurations[id]; !ok {
|
||||||
if !ok {
|
|
||||||
return buttons
|
return buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
for b := range state.standardButtonDurations {
|
for b := ebiten.StandardGamepadButton(0); b <= ebiten.StandardGamepadButtonMax; b++ {
|
||||||
if state.standardButtonDurations[b] != 0 {
|
if theInputState.standardGamepadButtonDurations[id][b] == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if prevState.standardButtonDurations[b] == 0 {
|
|
||||||
|
if theInputState.prevStandardGamepadButtonDurations[id][b] > 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
buttons = append(buttons, ebiten.StandardGamepadButton(b))
|
|
||||||
|
buttons = append(buttons, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
return buttons
|
return buttons
|
||||||
@ -584,16 +620,15 @@ func IsStandardGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.Stan
|
|||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
defer theInputState.m.RUnlock()
|
||||||
|
|
||||||
state, ok := theInputState.gamepadStates[id]
|
var prev int
|
||||||
if !ok {
|
if _, ok := theInputState.prevStandardGamepadButtonDurations[id]; ok {
|
||||||
return false
|
prev = theInputState.prevStandardGamepadButtonDurations[id][button]
|
||||||
}
|
}
|
||||||
prevState, ok := theInputState.prevGamepadStates[id]
|
var current int
|
||||||
if !ok {
|
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok {
|
||||||
return false
|
current = theInputState.standardGamepadButtonDurations[id][button]
|
||||||
}
|
}
|
||||||
|
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).
|
// 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()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
defer theInputState.m.RUnlock()
|
||||||
|
|
||||||
state, ok := theInputState.gamepadStates[id]
|
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok {
|
||||||
if !ok {
|
return theInputState.standardGamepadButtonDurations[id][button]
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
return state.standardButtonDurations[button]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendJustPressedTouchIDs append touch IDs that are created just in the current tick to touchIDs,
|
// 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
|
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
|
return touchIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -686,8 +727,9 @@ func IsTouchJustReleased(id ebiten.TouchID) bool {
|
|||||||
// TouchPressDuration is concurrent safe.
|
// TouchPressDuration is concurrent safe.
|
||||||
func TouchPressDuration(id ebiten.TouchID) int {
|
func TouchPressDuration(id ebiten.TouchID) int {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
defer theInputState.m.RUnlock()
|
s := theInputState.touchDurations[id]
|
||||||
return theInputState.touchDurations[id]
|
theInputState.m.RUnlock()
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// TouchPositionInPreviousTick returns the position in the previous tick.
|
// TouchPositionInPreviousTick returns the position in the previous tick.
|
||||||
|
@ -421,7 +421,6 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var imgs [graphics.ShaderSrcImageCount]*restorable.Image
|
|
||||||
for i, src := range srcs {
|
for i, src := range srcs {
|
||||||
if src == nil {
|
if src == nil {
|
||||||
continue
|
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
|
// A source region can be deliberately empty when this is not needed in order to avoid unexpected
|
||||||
// performance issue (#1293).
|
// performance issue (#1293).
|
||||||
// TODO: This should no longer be needed but is kept just in case. Remove this later.
|
// TODO: This should no longer be needed but is kept just in case. Remove this later.
|
||||||
if !srcRegions[i].Empty() {
|
if srcRegions[i].Empty() {
|
||||||
r := src.regionWithPadding()
|
continue
|
||||||
srcRegions[i] = srcRegions[i].Add(r.Min)
|
}
|
||||||
|
|
||||||
|
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
|
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() {
|
if !src.isOnSourceBackend() && src.canBePutOnAtlas() {
|
||||||
// src might already registered, but assigning it again is not harmful.
|
// src might already registered, but assigning it again is not harmful.
|
||||||
imagesToPutOnSourceBackend.add(src)
|
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.
|
// WritePixels replaces the pixels on the image.
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
// Code generated by gen.go using 'go generate'. DO NOT EDIT.
|
// Code generated by gen.go using 'go generate'. DO NOT EDIT.
|
||||||
|
|
||||||
//go:build darwin && !ios
|
//go:build darwing && !ios
|
||||||
|
|
||||||
package gamepaddb
|
package gamepaddb
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ func run() error {
|
|||||||
},
|
},
|
||||||
"Mac OS X": {
|
"Mac OS X": {
|
||||||
filenameSuffix: "macos",
|
filenameSuffix: "macos",
|
||||||
buildConstraints: "//go:build darwin && !ios",
|
buildConstraints: "//go:build darwing && !ios",
|
||||||
},
|
},
|
||||||
"Linux": {
|
"Linux": {
|
||||||
filenameSuffix: "linbsd",
|
filenameSuffix: "linbsd",
|
||||||
|
Loading…
Reference in New Issue
Block a user