ebiten/inpututil/inpututil.go

452 lines
14 KiB
Go
Raw Normal View History

2018-02-04 09:51:03 +01:00
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package inpututil provides utility functions of input like keyboard or mouse.
package inpututil
import (
"sort"
"sync"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
2018-02-04 09:51:03 +01:00
)
type inputState struct {
keyDurations []int
prevKeyDurations []int
2018-04-29 19:19:53 +02:00
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
prevTouchDurations map[ebiten.TouchID]int
2018-02-04 09:51:03 +01:00
gamepadIDsBuf []ebiten.GamepadID
touchIDsBuf []ebiten.TouchID
2018-02-04 09:51:03 +01:00
m sync.RWMutex
}
var theInputState = &inputState{
keyDurations: make([]int, ebiten.KeyMax+1),
prevKeyDurations: make([]int, ebiten.KeyMax+1),
2018-04-29 19:19:53 +02:00
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{},
prevTouchDurations: map[ebiten.TouchID]int{},
2018-02-04 09:51:03 +01:00
}
func init() {
hooks.AppendHookOnBeforeUpdate(func() error {
2018-02-04 09:51:03 +01:00
theInputState.update()
return nil
})
}
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++ {
2018-02-04 09:51:03 +01:00
if ebiten.IsKeyPressed(k) {
2018-04-29 19:19:53 +02:00
i.keyDurations[k]++
2018-02-04 09:51:03 +01:00
} else {
2018-04-29 19:19:53 +02:00
i.keyDurations[k] = 0
2018-02-04 09:51:03 +01:00
}
}
// Mouse
2018-02-04 09:51:03 +01:00
for _, b := range []ebiten.MouseButton{
ebiten.MouseButtonLeft,
ebiten.MouseButtonRight,
ebiten.MouseButtonMiddle,
} {
2018-04-29 19:19:53 +02:00
i.prevMouseButtonDurations[b] = i.mouseButtonDurations[b]
2018-02-04 09:51:03 +01:00
if ebiten.IsMouseButtonPressed(b) {
2018-04-29 19:19:53 +02:00
i.mouseButtonDurations[b]++
2018-02-04 09:51:03 +01:00
} else {
2018-04-29 19:19:53 +02:00
i.mouseButtonDurations[b] = 0
2018-02-04 09:51:03 +01:00
}
}
// Gamepads
// Copy the gamepad IDs.
2021-01-24 16:31:32 +01:00
for id := range i.prevGamepadIDs {
delete(i.prevGamepadIDs, id)
}
for id := range i.gamepadIDs {
i.prevGamepadIDs[id] = struct{}{}
}
// Copy the gamepad button durations.
2021-01-24 16:31:32 +01:00
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...)
}
2021-01-24 16:31:32 +01:00
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{}{}
2018-04-29 19:19:53 +02:00
if _, ok := i.gamepadButtonDurations[id]; !ok {
i.gamepadButtonDurations[id] = make([]int, ebiten.GamepadButtonMax+1)
2018-02-04 09:51:03 +01:00
}
n := ebiten.GamepadButtonNum(id)
if n > ebiten.GamepadButtonMax+1 {
n = ebiten.GamepadButtonMax + 1
}
2018-02-04 09:51:03 +01:00
for b := ebiten.GamepadButton(0); b < ebiten.GamepadButton(n); b++ {
if ebiten.IsGamepadButtonPressed(id, b) {
2018-04-29 19:19:53 +02:00
i.gamepadButtonDurations[id][b]++
2018-02-04 09:51:03 +01:00
} else {
2018-04-29 19:19:53 +02:00
i.gamepadButtonDurations[id][b] = 0
2018-02-04 09:51:03 +01:00
}
}
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 {
i.standardGamepadButtonDurations[id][b] = 0
}
}
2018-02-04 09:51:03 +01:00
}
2018-04-29 19:19:53 +02:00
for id := range i.gamepadButtonDurations {
if _, ok := i.gamepadIDs[id]; !ok {
2021-01-24 16:31:32 +01:00
delete(i.gamepadButtonDurations, id)
}
}
for id := range i.standardGamepadButtonDurations {
if _, ok := i.gamepadIDs[id]; !ok {
delete(i.standardGamepadButtonDurations, id)
}
}
2018-02-04 09:51:03 +01:00
// Touches
// Copy the touch durations.
for id := range i.prevTouchDurations {
delete(i.prevTouchDurations, id)
}
for id := range i.touchDurations {
i.prevTouchDurations[id] = i.touchDurations[id]
}
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]++
2018-02-04 09:51:03 +01:00
}
2018-04-29 19:19:53 +02:00
for id := range i.touchDurations {
if _, ok := i.touchIDs[id]; !ok {
delete(i.touchDurations, id)
2018-02-04 09:51:03 +01:00
}
}
}
// AppendPressedKeys append currently pressed keyboard keys to keys and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently.
2021-04-06 18:48:13 +02:00
//
// AppendPressedKeys is concurrent safe.
func AppendPressedKeys(keys []ebiten.Key) []ebiten.Key {
2021-04-06 18:48:13 +02:00
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
}
// PressedKeys returns a set of currently pressed keyboard keys.
//
// Deprecated: as of v2.2. Use AppendPressedKeys instead.
func PressedKeys() []ebiten.Key {
return AppendPressedKeys(nil)
}
2018-02-04 09:51:03 +01:00
// IsKeyJustPressed returns a boolean value indicating
// whether the given key is pressed just in the current frame.
//
// IsKeyJustPressed is concurrent safe.
2018-02-04 09:51:03 +01:00
func IsKeyJustPressed(key ebiten.Key) bool {
return KeyPressDuration(key) == 1
}
// IsKeyJustReleased returns a boolean value indicating
// whether the given key is released just in the current frame.
//
// IsKeyJustReleased is concurrent safe.
func IsKeyJustReleased(key ebiten.Key) bool {
theInputState.m.RLock()
2018-04-29 19:19:53 +02:00
r := theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
theInputState.m.RUnlock()
return r
}
2018-02-04 09:51:03 +01:00
// KeyPressDuration returns how long the key is pressed in frames.
//
// KeyPressDuration is concurrent safe.
2018-02-04 09:51:03 +01:00
func KeyPressDuration(key ebiten.Key) int {
theInputState.m.RLock()
2018-04-29 19:19:53 +02:00
s := theInputState.keyDurations[key]
2018-02-04 09:51:03 +01:00
theInputState.m.RUnlock()
return s
}
// IsMouseButtonJustPressed returns a boolean value indicating
// whether the given mouse button is pressed just in the current frame.
//
// IsMouseButtonJustPressed is concurrent safe.
2018-02-04 09:51:03 +01:00
func IsMouseButtonJustPressed(button ebiten.MouseButton) bool {
return MouseButtonPressDuration(button) == 1
}
// IsMouseButtonJustReleased returns a boolean value indicating
// whether the given mouse button is released just in the current frame.
//
// IsMouseButtonJustReleased is concurrent safe.
func IsMouseButtonJustReleased(button ebiten.MouseButton) bool {
theInputState.m.RLock()
2018-04-29 19:19:53 +02:00
r := theInputState.mouseButtonDurations[button] == 0 &&
theInputState.prevMouseButtonDurations[button] > 0
theInputState.m.RUnlock()
return r
}
2018-02-04 09:51:03 +01:00
// MouseButtonPressDuration returns how long the mouse button is pressed in frames.
//
// MouseButtonPressDuration is concurrent safe.
2018-02-04 09:51:03 +01:00
func MouseButtonPressDuration(button ebiten.MouseButton) int {
theInputState.m.RLock()
2018-04-29 19:19:53 +02:00
s := theInputState.mouseButtonDurations[button]
2018-02-04 09:51:03 +01:00
theInputState.m.RUnlock()
return s
}
// AppendJustConnectedGamepadIDs appends gamepad IDs that are connected just in the current frame to gamepadIDs,
// and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently.
2018-04-29 19:52:56 +02:00
//
// 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 {
gamepadIDs = append(gamepadIDs, id)
}
}
theInputState.m.RUnlock()
s := gamepadIDs[origLen:]
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
return gamepadIDs
}
// JustConnectedGamepadIDs returns gamepad IDs that are connected just in the current frame.
//
// Deprecated: as of v2.2. Use AppendJustConnectedGamepadIDs instead.
func JustConnectedGamepadIDs() []ebiten.GamepadID {
return AppendJustConnectedGamepadIDs(nil)
}
// IsGamepadJustDisconnected returns a boolean value indicating
// whether the gamepad of the given id is released just in the current frame.
2018-04-29 19:52:56 +02:00
//
// 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
}
2018-02-04 09:51:03 +01:00
// IsGamepadButtonJustPressed returns a boolean value indicating
// whether the given gamepad button of the gamepad id is pressed just in the current frame.
//
// IsGamepadButtonJustPressed is concurrent safe.
func IsGamepadButtonJustPressed(id ebiten.GamepadID, button ebiten.GamepadButton) bool {
2018-02-04 09:51:03 +01:00
return GamepadButtonPressDuration(id, button) == 1
}
// IsGamepadButtonJustReleased returns a boolean value indicating
// whether the given gamepad button of the gamepad id is released just in the current frame.
//
// IsGamepadButtonJustReleased is concurrent safe.
func IsGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.GamepadButton) bool {
theInputState.m.RLock()
prev := 0
2018-04-29 19:19:53 +02:00
if _, ok := theInputState.prevGamepadButtonDurations[id]; ok {
prev = theInputState.prevGamepadButtonDurations[id][button]
}
current := 0
2018-04-29 19:19:53 +02:00
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
current = theInputState.gamepadButtonDurations[id][button]
}
theInputState.m.RUnlock()
return current == 0 && prev > 0
}
2018-02-04 09:51:03 +01:00
// GamepadButtonPressDuration returns how long the gamepad button of the gamepad id is pressed in frames.
//
// GamepadButtonPressDuration is concurrent safe.
func GamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.GamepadButton) int {
2018-02-04 09:51:03 +01:00
theInputState.m.RLock()
s := 0
2018-04-29 19:19:53 +02:00
if _, ok := theInputState.gamepadButtonDurations[id]; ok {
s = theInputState.gamepadButtonDurations[id][button]
2018-02-04 09:51:03 +01:00
}
theInputState.m.RUnlock()
return s
}
// IsStandardGamepadButtonJustPressed returns a boolean value indicating
// whether the given standard gamepad button of the gamepad id is pressed just in the current frame.
//
// IsStandardGamepadButtonJustPressed is concurrent safe.
func IsStandardGamepadButtonJustPressed(id ebiten.GamepadID, button ebiten.StandardGamepadButton) bool {
return StandardGamepadButtonPressDuration(id, button) == 1
}
// IsStandardGamepadButtonJustReleased returns a boolean value indicating
// whether the given standard gamepad button of the gamepad id is released just in the current frame.
//
// IsStandardGamepadButtonJustReleased is concurrent safe.
func IsStandardGamepadButtonJustReleased(id ebiten.GamepadID, button ebiten.StandardGamepadButton) bool {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
var prev int
if _, ok := theInputState.prevStandardGamepadButtonDurations[id]; ok {
prev = theInputState.prevStandardGamepadButtonDurations[id][button]
}
var current int
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok {
current = theInputState.standardGamepadButtonDurations[id][button]
}
return current == 0 && prev > 0
}
// StandardGamepadButtonPressDuration returns how long the standard gamepad button of the gamepad id is pressed in frames.
//
// StandardGamepadButtonPressDuration is concurrent safe.
func StandardGamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.StandardGamepadButton) int {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
if _, ok := theInputState.standardGamepadButtonDurations[id]; ok {
return theInputState.standardGamepadButtonDurations[id][button]
}
return 0
}
// AppendJustPressedTouchIDs append touch IDs that are created just in the current frame to touchIDs,
// and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently.
//
// AppendJustPressedTouchIDs is concurrent safe.
func AppendJustPressedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
origLen := len(touchIDs)
theInputState.m.RLock()
2018-04-29 19:19:53 +02:00
for id, s := range theInputState.touchDurations {
if s == 1 {
touchIDs = append(touchIDs, id)
}
}
theInputState.m.RUnlock()
s := touchIDs[origLen:]
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
return touchIDs
}
// JustPressedTouchIDs returns touch IDs that are created just in the current frame.
//
// Deprecated: as of v2.2. Use AppendJustPressedTouchIDs instead.
func JustPressedTouchIDs() []ebiten.TouchID {
return AppendJustPressedTouchIDs(nil)
2018-02-04 09:51:03 +01:00
}
// IsTouchJustReleased returns a boolean value indicating
// whether the given touch is released just in the current frame.
//
// IsTouchJustReleased is concurrent safe.
func IsTouchJustReleased(id ebiten.TouchID) bool {
theInputState.m.RLock()
2018-04-29 19:19:53 +02:00
r := theInputState.touchDurations[id] == 0 && theInputState.prevTouchDurations[id] > 0
theInputState.m.RUnlock()
return r
}
// TouchPressDuration returns how long the touch remains in frames.
//
// TouchPressDuration is concurrent safe.
func TouchPressDuration(id ebiten.TouchID) int {
2018-02-04 09:51:03 +01:00
theInputState.m.RLock()
2018-04-29 19:19:53 +02:00
s := theInputState.touchDurations[id]
2018-02-04 09:51:03 +01:00
theInputState.m.RUnlock()
return s
}