mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
examples/audio: Refactoring: Add Input struct (#302)
This commit is contained in:
parent
d340a50188
commit
250afe97a5
@ -52,14 +52,45 @@ func init() {
|
|||||||
playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff})
|
playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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} {
|
||||||
|
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) isKeyTriggered(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) isMouseButtonTriggered(mouseButton ebiten.MouseButton) bool {
|
||||||
|
return i.mouseButtonStates[mouseButton] == 1
|
||||||
|
}
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
audioContext *audio.Context
|
input *Input
|
||||||
audioPlayer *audio.Player
|
audioContext *audio.Context
|
||||||
total time.Duration
|
audioPlayer *audio.Player
|
||||||
seekedCh chan error
|
total time.Duration
|
||||||
mouseButtonState map[ebiten.MouseButton]int
|
seekedCh chan error
|
||||||
keyState map[ebiten.Key]int
|
volume128 int
|
||||||
volume128 int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -90,18 +121,21 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
player := &Player{
|
player := &Player{
|
||||||
audioContext: audioContext,
|
input: &Input{
|
||||||
audioPlayer: p,
|
mouseButtonStates: map[ebiten.MouseButton]int{},
|
||||||
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
|
keyStates: map[ebiten.Key]int{},
|
||||||
mouseButtonState: map[ebiten.MouseButton]int{},
|
},
|
||||||
keyState: map[ebiten.Key]int{},
|
audioContext: audioContext,
|
||||||
volume128: 128,
|
audioPlayer: p,
|
||||||
|
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
|
||||||
|
volume128: 128,
|
||||||
}
|
}
|
||||||
player.audioPlayer.Play()
|
player.audioPlayer.Play()
|
||||||
return player, nil
|
return player, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) update() error {
|
func (p *Player) update() error {
|
||||||
|
p.input.update()
|
||||||
p.updateBar()
|
p.updateBar()
|
||||||
p.updatePlayPause()
|
p.updatePlayPause()
|
||||||
p.updateSE()
|
p.updateSE()
|
||||||
@ -125,12 +159,7 @@ func (p *Player) updateSE() {
|
|||||||
if seBytes == nil {
|
if seBytes == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !ebiten.IsKeyPressed(ebiten.KeyP) {
|
if !p.input.isKeyTriggered(ebiten.KeyP) {
|
||||||
p.keyState[ebiten.KeyP] = 0
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p.keyState[ebiten.KeyP]++
|
|
||||||
if p.keyState[ebiten.KeyP] != 1 {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, seBytes)
|
sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, seBytes)
|
||||||
@ -138,10 +167,10 @@ func (p *Player) updateSE() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) updateVolume() {
|
func (p *Player) updateVolume() {
|
||||||
if ebiten.IsKeyPressed(ebiten.KeyZ) {
|
if p.input.isKeyPressed(ebiten.KeyZ) {
|
||||||
p.volume128--
|
p.volume128--
|
||||||
}
|
}
|
||||||
if ebiten.IsKeyPressed(ebiten.KeyX) {
|
if p.input.isKeyPressed(ebiten.KeyX) {
|
||||||
p.volume128++
|
p.volume128++
|
||||||
}
|
}
|
||||||
if p.volume128 < 0 {
|
if p.volume128 < 0 {
|
||||||
@ -154,12 +183,7 @@ func (p *Player) updateVolume() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) updatePlayPause() {
|
func (p *Player) updatePlayPause() {
|
||||||
if !ebiten.IsKeyPressed(ebiten.KeyS) {
|
if !p.input.isKeyTriggered(ebiten.KeyS) {
|
||||||
p.keyState[ebiten.KeyS] = 0
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p.keyState[ebiten.KeyS]++
|
|
||||||
if p.keyState[ebiten.KeyS] != 1 {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if p.audioPlayer.IsPlaying() {
|
if p.audioPlayer.IsPlaying() {
|
||||||
@ -173,12 +197,7 @@ func (p *Player) updateBar() {
|
|||||||
if p.seekedCh != nil {
|
if p.seekedCh != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
if !p.input.isMouseButtonTriggered(ebiten.MouseButtonLeft) {
|
||||||
p.mouseButtonState[ebiten.MouseButtonLeft] = 0
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p.mouseButtonState[ebiten.MouseButtonLeft]++
|
|
||||||
if p.mouseButtonState[ebiten.MouseButtonLeft] != 1 {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
x, y := ebiten.CursorPosition()
|
x, y := ebiten.CursorPosition()
|
||||||
@ -193,7 +212,6 @@ func (p *Player) updateBar() {
|
|||||||
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
|
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
|
||||||
p.seekedCh = make(chan error, 1)
|
p.seekedCh = make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
// This can't be done parallely! !?!?
|
|
||||||
p.seekedCh <- p.audioPlayer.Seek(pos)
|
p.seekedCh <- p.audioPlayer.Seek(pos)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user