mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
examples: Use inpututil (#415)
This commit is contained in:
parent
e86972b849
commit
658c7cf452
@ -31,6 +31,7 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/audio/mp3"
|
"github.com/hajimehoshi/ebiten/audio/mp3"
|
||||||
"github.com/hajimehoshi/ebiten/audio/wav"
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
"github.com/hajimehoshi/ebiten/inpututil"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -47,46 +48,8 @@ var (
|
|||||||
playerCurrentColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
|
playerCurrentColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Input manages the current input state to detect keys 'just pressed'.
|
|
||||||
//
|
|
||||||
// Note: 'just pressed' is a very common idiom.
|
|
||||||
// There is a plan to create a new package for input utility.
|
|
||||||
// See https://github.com/hajimehoshi/ebiten/issues/415.
|
|
||||||
type Input struct {
|
|
||||||
mouseButtonStates map[ebiten.MouseButton]int
|
|
||||||
keyStates map[ebiten.Key]int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Input) update() {
|
|
||||||
for _, key := range []ebiten.Key{ebiten.KeyP, ebiten.KeyS, ebiten.KeyX, ebiten.KeyZ, ebiten.KeyB} {
|
|
||||||
if !ebiten.IsKeyPressed(key) {
|
|
||||||
i.keyStates[key] = 0
|
|
||||||
} else {
|
|
||||||
i.keyStates[key]++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
|
||||||
i.mouseButtonStates[ebiten.MouseButtonLeft] = 0
|
|
||||||
} else {
|
|
||||||
i.mouseButtonStates[ebiten.MouseButtonLeft]++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Input) isKeyJustPressed(key ebiten.Key) bool {
|
|
||||||
return i.keyStates[key] == 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Input) isKeyPressed(key ebiten.Key) bool {
|
|
||||||
return i.keyStates[key] > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Input) isMouseButtonJustPressed(mouseButton ebiten.MouseButton) bool {
|
|
||||||
return i.mouseButtonStates[mouseButton] == 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Player represents the current audio state.
|
// Player represents the current audio state.
|
||||||
type Player struct {
|
type Player struct {
|
||||||
input *Input
|
|
||||||
audioContext *audio.Context
|
audioContext *audio.Context
|
||||||
audioPlayer *audio.Player
|
audioPlayer *audio.Player
|
||||||
current time.Duration
|
current time.Duration
|
||||||
@ -122,10 +85,6 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
player := &Player{
|
player := &Player{
|
||||||
input: &Input{
|
|
||||||
mouseButtonStates: map[ebiten.MouseButton]int{},
|
|
||||||
keyStates: map[ebiten.Key]int{},
|
|
||||||
},
|
|
||||||
audioContext: audioContext,
|
audioContext: audioContext,
|
||||||
audioPlayer: p,
|
audioPlayer: p,
|
||||||
total: time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
|
total: time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
|
||||||
@ -153,7 +112,6 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) update() error {
|
func (p *Player) update() error {
|
||||||
p.input.update()
|
|
||||||
select {
|
select {
|
||||||
case p.seBytes = <-p.seCh:
|
case p.seBytes = <-p.seCh:
|
||||||
close(p.seCh)
|
close(p.seCh)
|
||||||
@ -169,7 +127,7 @@ func (p *Player) update() error {
|
|||||||
p.playSEIfNeeded()
|
p.playSEIfNeeded()
|
||||||
p.updateVolumeIfNeeded()
|
p.updateVolumeIfNeeded()
|
||||||
|
|
||||||
if p.input.isKeyJustPressed(ebiten.KeyB) {
|
if inpututil.IsKeyJustPressed(ebiten.KeyB) {
|
||||||
b := ebiten.IsRunnableInBackground()
|
b := ebiten.IsRunnableInBackground()
|
||||||
ebiten.SetRunnableInBackground(!b)
|
ebiten.SetRunnableInBackground(!b)
|
||||||
}
|
}
|
||||||
@ -182,7 +140,7 @@ func (p *Player) playSEIfNeeded() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !p.input.isKeyJustPressed(ebiten.KeyP) {
|
if !inpututil.IsKeyJustPressed(ebiten.KeyP) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, p.seBytes)
|
sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, p.seBytes)
|
||||||
@ -190,10 +148,10 @@ func (p *Player) playSEIfNeeded() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) updateVolumeIfNeeded() {
|
func (p *Player) updateVolumeIfNeeded() {
|
||||||
if p.input.isKeyPressed(ebiten.KeyZ) {
|
if ebiten.IsKeyPressed(ebiten.KeyZ) {
|
||||||
p.volume128--
|
p.volume128--
|
||||||
}
|
}
|
||||||
if p.input.isKeyPressed(ebiten.KeyX) {
|
if ebiten.IsKeyPressed(ebiten.KeyX) {
|
||||||
p.volume128++
|
p.volume128++
|
||||||
}
|
}
|
||||||
if p.volume128 < 0 {
|
if p.volume128 < 0 {
|
||||||
@ -206,7 +164,7 @@ func (p *Player) updateVolumeIfNeeded() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) switchPlayStateIfNeeded() {
|
func (p *Player) switchPlayStateIfNeeded() {
|
||||||
if !p.input.isKeyJustPressed(ebiten.KeyS) {
|
if !inpututil.IsKeyJustPressed(ebiten.KeyS) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if p.audioPlayer.IsPlaying() {
|
if p.audioPlayer.IsPlaying() {
|
||||||
@ -217,7 +175,7 @@ func (p *Player) switchPlayStateIfNeeded() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) seekBarIfNeeded() {
|
func (p *Player) seekBarIfNeeded() {
|
||||||
if !p.input.isMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
if !inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/inpututil"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -31,13 +32,6 @@ const (
|
|||||||
initScreenScale = 1
|
initScreenScale = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
keyStates = map[ebiten.Key]int{
|
|
||||||
ebiten.KeyS: 0,
|
|
||||||
ebiten.KeyF: 0,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dots []uint8
|
dots []uint8
|
||||||
dotsWidth int
|
dotsWidth int
|
||||||
@ -65,17 +59,10 @@ func getDots(width, height int) []uint8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func update(screen *ebiten.Image) error {
|
func update(screen *ebiten.Image) error {
|
||||||
for key := range keyStates {
|
|
||||||
if !ebiten.IsKeyPressed(key) {
|
|
||||||
keyStates[key] = 0
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
keyStates[key]++
|
|
||||||
}
|
|
||||||
screenScale := ebiten.ScreenScale()
|
screenScale := ebiten.ScreenScale()
|
||||||
fullscreen := ebiten.IsFullscreen()
|
fullscreen := ebiten.IsFullscreen()
|
||||||
|
|
||||||
if keyStates[ebiten.KeyS] == 1 {
|
if inpututil.IsKeyJustPressed(ebiten.KeyS) {
|
||||||
switch screenScale {
|
switch screenScale {
|
||||||
case 1:
|
case 1:
|
||||||
screenScale = 1.5
|
screenScale = 1.5
|
||||||
@ -87,7 +74,7 @@ func update(screen *ebiten.Image) error {
|
|||||||
panic("not reached")
|
panic("not reached")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if keyStates[ebiten.KeyF] == 1 {
|
if inpututil.IsKeyJustPressed(ebiten.KeyF) {
|
||||||
fullscreen = !fullscreen
|
fullscreen = !fullscreen
|
||||||
}
|
}
|
||||||
ebiten.SetScreenScale(screenScale)
|
ebiten.SetScreenScale(screenScale)
|
||||||
|
Loading…
Reference in New Issue
Block a user