mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-04 23:14:28 +01:00
inpututil: Rename states -> durations
This commit is contained in:
parent
5dc1df3260
commit
8c258cce7d
@ -24,33 +24,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type inputState struct {
|
type inputState struct {
|
||||||
keyStates map[ebiten.Key]int
|
keyDurations map[ebiten.Key]int
|
||||||
prevKeyStates map[ebiten.Key]int
|
prevKeyDurations map[ebiten.Key]int
|
||||||
|
|
||||||
mouseButtonStates map[ebiten.MouseButton]int
|
mouseButtonDurations map[ebiten.MouseButton]int
|
||||||
prevMouseButtonStates map[ebiten.MouseButton]int
|
prevMouseButtonDurations map[ebiten.MouseButton]int
|
||||||
|
|
||||||
gamepadButtonStates map[int]map[ebiten.GamepadButton]int
|
gamepadButtonDurations map[int]map[ebiten.GamepadButton]int
|
||||||
prevGamepadButtonStates map[int]map[ebiten.GamepadButton]int
|
prevGamepadButtonDurations map[int]map[ebiten.GamepadButton]int
|
||||||
|
|
||||||
touchStates map[int]int
|
touchDurations map[int]int
|
||||||
prevTouchStates map[int]int
|
prevTouchDurations map[int]int
|
||||||
|
|
||||||
m sync.RWMutex
|
m sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var theInputState = &inputState{
|
var theInputState = &inputState{
|
||||||
keyStates: map[ebiten.Key]int{},
|
keyDurations: map[ebiten.Key]int{},
|
||||||
prevKeyStates: map[ebiten.Key]int{},
|
prevKeyDurations: map[ebiten.Key]int{},
|
||||||
|
|
||||||
mouseButtonStates: map[ebiten.MouseButton]int{},
|
mouseButtonDurations: map[ebiten.MouseButton]int{},
|
||||||
prevMouseButtonStates: map[ebiten.MouseButton]int{},
|
prevMouseButtonDurations: map[ebiten.MouseButton]int{},
|
||||||
|
|
||||||
gamepadButtonStates: map[int]map[ebiten.GamepadButton]int{},
|
gamepadButtonDurations: map[int]map[ebiten.GamepadButton]int{},
|
||||||
prevGamepadButtonStates: map[int]map[ebiten.GamepadButton]int{},
|
prevGamepadButtonDurations: map[int]map[ebiten.GamepadButton]int{},
|
||||||
|
|
||||||
touchStates: map[int]int{},
|
touchDurations: map[int]int{},
|
||||||
prevTouchStates: map[int]int{},
|
prevTouchDurations: map[int]int{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -66,11 +66,11 @@ func (i *inputState) update() {
|
|||||||
|
|
||||||
// Keyboard
|
// Keyboard
|
||||||
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
|
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
|
||||||
i.prevKeyStates[k] = i.keyStates[k]
|
i.prevKeyDurations[k] = i.keyDurations[k]
|
||||||
if ebiten.IsKeyPressed(k) {
|
if ebiten.IsKeyPressed(k) {
|
||||||
i.keyStates[k]++
|
i.keyDurations[k]++
|
||||||
} else {
|
} else {
|
||||||
i.keyStates[k] = 0
|
i.keyDurations[k] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,74 +80,74 @@ func (i *inputState) update() {
|
|||||||
ebiten.MouseButtonRight,
|
ebiten.MouseButtonRight,
|
||||||
ebiten.MouseButtonMiddle,
|
ebiten.MouseButtonMiddle,
|
||||||
} {
|
} {
|
||||||
i.prevMouseButtonStates[b] = i.mouseButtonStates[b]
|
i.prevMouseButtonDurations[b] = i.mouseButtonDurations[b]
|
||||||
if ebiten.IsMouseButtonPressed(b) {
|
if ebiten.IsMouseButtonPressed(b) {
|
||||||
i.mouseButtonStates[b]++
|
i.mouseButtonDurations[b]++
|
||||||
} else {
|
} else {
|
||||||
i.mouseButtonStates[b] = 0
|
i.mouseButtonDurations[b] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gamepads
|
// Gamepads
|
||||||
|
|
||||||
// Reset the previous states first since some gamepad IDs might be already gone.
|
// Reset the previous states first since some gamepad IDs might be already gone.
|
||||||
for id := range i.prevGamepadButtonStates {
|
for id := range i.prevGamepadButtonDurations {
|
||||||
for b := range i.prevGamepadButtonStates[id] {
|
for b := range i.prevGamepadButtonDurations[id] {
|
||||||
i.prevGamepadButtonStates[id][b] = 0
|
i.prevGamepadButtonDurations[id][b] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ids := map[int]struct{}{}
|
ids := map[int]struct{}{}
|
||||||
for _, id := range ebiten.GamepadIDs() {
|
for _, id := range ebiten.GamepadIDs() {
|
||||||
ids[id] = struct{}{}
|
ids[id] = struct{}{}
|
||||||
|
|
||||||
if _, ok := i.prevGamepadButtonStates[id]; !ok {
|
if _, ok := i.prevGamepadButtonDurations[id]; !ok {
|
||||||
i.prevGamepadButtonStates[id] = map[ebiten.GamepadButton]int{}
|
i.prevGamepadButtonDurations[id] = map[ebiten.GamepadButton]int{}
|
||||||
}
|
}
|
||||||
if _, ok := i.gamepadButtonStates[id]; !ok {
|
if _, ok := i.gamepadButtonDurations[id]; !ok {
|
||||||
i.gamepadButtonStates[id] = map[ebiten.GamepadButton]int{}
|
i.gamepadButtonDurations[id] = map[ebiten.GamepadButton]int{}
|
||||||
}
|
}
|
||||||
|
|
||||||
n := ebiten.GamepadButtonNum(id)
|
n := ebiten.GamepadButtonNum(id)
|
||||||
for b := ebiten.GamepadButton(0); b < ebiten.GamepadButton(n); b++ {
|
for b := ebiten.GamepadButton(0); b < ebiten.GamepadButton(n); b++ {
|
||||||
i.prevGamepadButtonStates[id][b] = i.gamepadButtonStates[id][b]
|
i.prevGamepadButtonDurations[id][b] = i.gamepadButtonDurations[id][b]
|
||||||
if ebiten.IsGamepadButtonPressed(id, b) {
|
if ebiten.IsGamepadButtonPressed(id, b) {
|
||||||
i.gamepadButtonStates[id][b]++
|
i.gamepadButtonDurations[id][b]++
|
||||||
} else {
|
} else {
|
||||||
i.gamepadButtonStates[id][b] = 0
|
i.gamepadButtonDurations[id][b] = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
idsToDelete := []int{}
|
idsToDelete := []int{}
|
||||||
for id := range i.gamepadButtonStates {
|
for id := range i.gamepadButtonDurations {
|
||||||
if _, ok := ids[id]; !ok {
|
if _, ok := ids[id]; !ok {
|
||||||
idsToDelete = append(idsToDelete, id)
|
idsToDelete = append(idsToDelete, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, id := range idsToDelete {
|
for _, id := range idsToDelete {
|
||||||
delete(i.gamepadButtonStates, id)
|
delete(i.gamepadButtonDurations, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Touches
|
// Touches
|
||||||
ids = map[int]struct{}{}
|
ids = map[int]struct{}{}
|
||||||
|
|
||||||
// Reset the previous states first since some gamepad IDs might be already gone.
|
// Reset the previous states first since some gamepad IDs might be already gone.
|
||||||
for id := range i.prevTouchStates {
|
for id := range i.prevTouchDurations {
|
||||||
i.prevTouchStates[id] = 0
|
i.prevTouchDurations[id] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range ebiten.Touches() {
|
for _, t := range ebiten.Touches() {
|
||||||
ids[t.ID()] = struct{}{}
|
ids[t.ID()] = struct{}{}
|
||||||
i.prevTouchStates[t.ID()] = i.touchStates[t.ID()]
|
i.prevTouchDurations[t.ID()] = i.touchDurations[t.ID()]
|
||||||
i.touchStates[t.ID()]++
|
i.touchDurations[t.ID()]++
|
||||||
}
|
}
|
||||||
idsToDelete = []int{}
|
idsToDelete = []int{}
|
||||||
for id := range i.touchStates {
|
for id := range i.touchDurations {
|
||||||
if _, ok := ids[id]; !ok {
|
if _, ok := ids[id]; !ok {
|
||||||
idsToDelete = append(idsToDelete, id)
|
idsToDelete = append(idsToDelete, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, id := range idsToDelete {
|
for _, id := range idsToDelete {
|
||||||
delete(i.touchStates, id)
|
delete(i.touchDurations, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,7 +165,7 @@ 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()
|
||||||
r := theInputState.keyStates[key] == 0 && theInputState.prevKeyStates[key] > 0
|
r := theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@ -175,7 +175,7 @@ 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()
|
||||||
s := theInputState.keyStates[key]
|
s := theInputState.keyDurations[key]
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@ -194,8 +194,8 @@ 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()
|
||||||
r := theInputState.mouseButtonStates[button] == 0 &&
|
r := theInputState.mouseButtonDurations[button] == 0 &&
|
||||||
theInputState.prevMouseButtonStates[button] > 0
|
theInputState.prevMouseButtonDurations[button] > 0
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@ -205,7 +205,7 @@ 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()
|
||||||
s := theInputState.mouseButtonStates[button]
|
s := theInputState.mouseButtonDurations[button]
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@ -225,12 +225,12 @@ func IsGamepadButtonJustPressed(id int, button ebiten.GamepadButton) bool {
|
|||||||
func IsGamepadButtonJustReleased(id int, button ebiten.GamepadButton) bool {
|
func IsGamepadButtonJustReleased(id int, button ebiten.GamepadButton) bool {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
prev := 0
|
prev := 0
|
||||||
if _, ok := theInputState.prevGamepadButtonStates[id]; ok {
|
if _, ok := theInputState.prevGamepadButtonDurations[id]; ok {
|
||||||
prev = theInputState.prevGamepadButtonStates[id][button]
|
prev = theInputState.prevGamepadButtonDurations[id][button]
|
||||||
}
|
}
|
||||||
current := 0
|
current := 0
|
||||||
if _, ok := theInputState.gamepadButtonStates[id]; ok {
|
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
|
||||||
current = theInputState.gamepadButtonStates[id][button]
|
current = theInputState.gamepadButtonDurations[id][button]
|
||||||
}
|
}
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
return current == 0 && prev > 0
|
return current == 0 && prev > 0
|
||||||
@ -242,8 +242,8 @@ func IsGamepadButtonJustReleased(id int, button ebiten.GamepadButton) bool {
|
|||||||
func GamepadButtonPressDuration(id int, button ebiten.GamepadButton) int {
|
func GamepadButtonPressDuration(id int, button ebiten.GamepadButton) int {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
s := 0
|
s := 0
|
||||||
if _, ok := theInputState.gamepadButtonStates[id]; ok {
|
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
|
||||||
s = theInputState.gamepadButtonStates[id][button]
|
s = theInputState.gamepadButtonDurations[id][button]
|
||||||
}
|
}
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
return s
|
return s
|
||||||
@ -255,7 +255,7 @@ func GamepadButtonPressDuration(id int, button ebiten.GamepadButton) int {
|
|||||||
func JustPressedTouches() []int {
|
func JustPressedTouches() []int {
|
||||||
ids := []int{}
|
ids := []int{}
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
for id, s := range theInputState.touchStates {
|
for id, s := range theInputState.touchDurations {
|
||||||
if s == 1 {
|
if s == 1 {
|
||||||
ids = append(ids, id)
|
ids = append(ids, id)
|
||||||
}
|
}
|
||||||
@ -270,7 +270,7 @@ func JustPressedTouches() []int {
|
|||||||
// IsTouchJustReleased is concurrent safe.
|
// IsTouchJustReleased is concurrent safe.
|
||||||
func IsTouchJustReleased(id int) bool {
|
func IsTouchJustReleased(id int) bool {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
r := theInputState.touchStates[id] == 0 && theInputState.prevTouchStates[id] > 0
|
r := theInputState.touchDurations[id] == 0 && theInputState.prevTouchDurations[id] > 0
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@ -280,7 +280,7 @@ func IsTouchJustReleased(id int) bool {
|
|||||||
// TouchDuration is concurrent safe.
|
// TouchDuration is concurrent safe.
|
||||||
func TouchDuration(id int) int {
|
func TouchDuration(id int) int {
|
||||||
theInputState.m.RLock()
|
theInputState.m.RLock()
|
||||||
s := theInputState.touchStates[id]
|
s := theInputState.touchDurations[id]
|
||||||
theInputState.m.RUnlock()
|
theInputState.m.RUnlock()
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user