Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Created February 4, 2018 07:38
Show Gist options
  • Save hajimehoshi/25e97956da5c8e39221882201be37cd9 to your computer and use it in GitHub Desktop.
Save hajimehoshi/25e97956da5c8e39221882201be37cd9 to your computer and use it in GitHub Desktop.
// 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
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/sync"
)
type inputState struct {
keyStates map[ebiten.Key]int
mouseButtonStates map[ebiten.MouseButton]int
gamepadButtonStates map[int]map[ebiten.GamepadButton]int
touchStates map[int]int
m sync.RWMutex
}
var theInputState = &inputState{
keyStates: map[ebiten.Key]int{},
mouseButtonStates: map[ebiten.MouseButton]int{},
gamepadButtonStates: map[int]map[ebiten.GamepadButton]int{},
touchStates: map[int]int{},
}
func Update() {
theInputState.update()
}
func (i *inputState) update() {
theInputState.m.Lock()
defer theInputState.m.Unlock()
for k := ebiten.Key(0); k < ebiten.KeyMax; k++ {
if ebiten.IsKeyPressed(k) {
i.keyStates[k]++
} else {
i.keyStates[k] = 0
}
}
for _, b := range []ebiten.MouseButton{
ebiten.MouseButtonLeft,
ebiten.MouseButtonRight,
ebiten.MouseButtonMiddle,
} {
if ebiten.IsMouseButtonPressed(b) {
i.mouseButtonStates[b]++
} else {
i.mouseButtonStates[b] = 0
}
}
for _, id := range ebiten.GamepadIDs() {
if _, ok := i.gamepadButtonStates[id]; !ok {
i.gamepadButtonStates[id] = map[ebiten.GamepadButton]int{}
}
n := ebiten.GamepadButtonNum(id)
for b := ebiten.GamepadButton(0); b < ebiten.GamepadButton(n); b++ {
if ebiten.IsGamepadButtonPressed(id, b) {
i.gamepadButtonStates[id][b]++
} else {
i.gamepadButtonStates[id][b] = 0
}
}
}
ids := map[int]struct{}{}
for _, t := range ebiten.Touches() {
ids[t.ID()] = struct{}{}
i.touchStates[t.ID()]++
}
idsToDelete := []int{}
for id := range i.touchStates {
if _, ok := ids[id]; !ok {
idsToDelete = append(idsToDelete, id)
}
}
for _, id := range idsToDelete {
delete(i.touchStates, id)
}
}
func IsKeyJustPressed(key ebiten.Key) bool {
return KeyPressingFrames(key) == 1
}
func KeyPressingFrames(key ebiten.Key) int {
theInputState.m.RLock()
s := theInputState.keyStates[key]
theInputState.m.RUnlock()
return s
}
func IsMouseButtonJustPressed(button ebiten.MouseButton) bool {
return MouseButtonPressingFrames(button) == 1
}
func MouseButtonPressingFrames(button ebiten.MouseButton) int {
theInputState.m.RLock()
s := theInputState.mouseButtonStates[button]
theInputState.m.RUnlock()
return s
}
func IsGamepadButtonJustPressed(id int, button ebiten.GamepadButton) bool {
return GamepadButtonPressingFrames(id, button) == 1
}
func GamepadButtonPressingFrames(id int, button ebiten.GamepadButton) int {
theInputState.m.RLock()
s := 0
if _, ok := theInputState.gamepadButtonStates[id]; ok {
s = theInputState.gamepadButtonStates[id][button]
}
theInputState.m.RUnlock()
return s
}
func IsJustTouched(id int) bool {
return TouchPressingFrames(id) == 1
}
func TouchPressingFrames(id int) int {
theInputState.m.RLock()
s := theInputState.touchStates[id]
theInputState.m.RUnlock()
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment