From e1d0b902e18d4e0d79069d27ad3e9a34f1429609 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 4 May 2021 01:27:57 +0900 Subject: [PATCH] examples/audio: Add buttons --- examples/audio/main.go | 103 +++++++++++++++++++++- examples/resources/generate.go | 3 + examples/resources/images/audio/alert.go | 5 ++ examples/resources/images/audio/alert.png | Bin 0 -> 368 bytes examples/resources/images/audio/pause.go | 5 ++ examples/resources/images/audio/pause.png | Bin 0 -> 90 bytes examples/resources/images/audio/play.go | 5 ++ examples/resources/images/audio/play.png | Bin 0 -> 210 bytes examples/resources/images/license.md | 18 +++- 9 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 examples/resources/images/audio/alert.go create mode 100644 examples/resources/images/audio/alert.png create mode 100644 examples/resources/images/audio/pause.go create mode 100644 examples/resources/images/audio/pause.png create mode 100644 examples/resources/images/audio/play.go create mode 100644 examples/resources/images/audio/play.png diff --git a/examples/audio/main.go b/examples/audio/main.go index e16208eef..91402e526 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -22,7 +22,9 @@ package main import ( "bytes" "fmt" + "image" "image/color" + _ "image/png" "io" "io/ioutil" "log" @@ -35,6 +37,7 @@ import ( "github.com/hajimehoshi/ebiten/v2/audio/wav" "github.com/hajimehoshi/ebiten/v2/ebitenutil" raudio "github.com/hajimehoshi/ebiten/v2/examples/resources/audio" + riaudio "github.com/hajimehoshi/ebiten/v2/examples/resources/images/audio" "github.com/hajimehoshi/ebiten/v2/inpututil" ) @@ -50,6 +53,44 @@ var ( playerCurrentColor = color.RGBA{0xff, 0xff, 0xff, 0xff} ) +var ( + playButtonPosition image.Point + alertButtonPosition image.Point + + playButtonImage *ebiten.Image + pauseButtonImage *ebiten.Image + alertButtonImage *ebiten.Image +) + +func init() { + img, _, err := image.Decode(bytes.NewReader(riaudio.Play_png)) + if err != nil { + panic(err) + } + playButtonImage = ebiten.NewImageFromImage(img) + + img, _, err = image.Decode(bytes.NewReader(riaudio.Pause_png)) + if err != nil { + panic(err) + } + pauseButtonImage = ebiten.NewImageFromImage(img) + + img, _, err = image.Decode(bytes.NewReader(riaudio.Alert_png)) + if err != nil { + panic(err) + } + alertButtonImage = ebiten.NewImageFromImage(img) + + const buttonPadding = 16 + + w, _ := playButtonImage.Size() + playButtonPosition.X = (screenWidth - w*2 + buttonPadding*1) / 2 + playButtonPosition.Y = screenHeight - 160 + + alertButtonPosition.X = playButtonPosition.X + w + buttonPadding + alertButtonPosition.Y = playButtonPosition.Y +} + type musicType int const ( @@ -172,13 +213,34 @@ func (p *Player) update() error { return nil } -func (p *Player) playSEIfNeeded() { +func (p *Player) shouldPlaySE() bool { if p.seBytes == nil { // Bytes for the SE is not loaded yet. - return + return false } - if !inpututil.IsKeyJustPressed(ebiten.KeyP) { + if inpututil.IsKeyJustPressed(ebiten.KeyP) { + return true + } + r := image.Rectangle{ + Min: alertButtonPosition, + Max: alertButtonPosition.Add(image.Pt(alertButtonImage.Size())), + } + if image.Pt(ebiten.CursorPosition()).In(r) { + if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) { + return true + } + } + for _, id := range inpututil.JustPressedTouchIDs() { + if image.Pt(ebiten.TouchPosition(id)).In(r) { + return true + } + } + return false +} + +func (p *Player) playSEIfNeeded() { + if !p.shouldPlaySE() { return } sePlayer := audio.NewPlayerFromBytes(p.audioContext, p.seBytes) @@ -201,8 +263,29 @@ func (p *Player) updateVolumeIfNeeded() { p.audioPlayer.SetVolume(float64(p.volume128) / 128) } +func (p *Player) shouldSwitchPlayStateIfNeeded() bool { + if inpututil.IsKeyJustPressed(ebiten.KeyS) { + return true + } + r := image.Rectangle{ + Min: playButtonPosition, + Max: playButtonPosition.Add(image.Pt(playButtonImage.Size())), + } + if image.Pt(ebiten.CursorPosition()).In(r) { + if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) { + return true + } + } + for _, id := range inpututil.JustPressedTouchIDs() { + if image.Pt(ebiten.TouchPosition(id)).In(r) { + return true + } + } + return false +} + func (p *Player) switchPlayStateIfNeeded() { - if !inpututil.IsKeyJustPressed(ebiten.KeyS) { + if !p.shouldSwitchPlayStateIfNeeded() { return } if p.audioPlayer.IsPlaying() { @@ -262,6 +345,18 @@ func (p *Player) draw(screen *ebiten.Image) { s := (c / time.Second) % 60 currentTimeStr := fmt.Sprintf("%02d:%02d", m, s) + // Draw buttons + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(playButtonPosition.X), float64(playButtonPosition.Y)) + if p.audioPlayer.IsPlaying() { + screen.DrawImage(pauseButtonImage, op) + } else { + screen.DrawImage(playButtonImage, op) + } + op.GeoM.Reset() + op.GeoM.Translate(float64(alertButtonPosition.X), float64(alertButtonPosition.Y)) + screen.DrawImage(alertButtonImage, op) + // Draw the debug message. msg := fmt.Sprintf(`TPS: %0.2f Press S to toggle Play/Pause diff --git a/examples/resources/generate.go b/examples/resources/generate.go index a1472824d..7a251ea6a 100644 --- a/examples/resources/generate.go +++ b/examples/resources/generate.go @@ -26,6 +26,9 @@ //go:generate file2byteslice -package=images -input=./images/runner.png -output=./images/runner.go -var=Runner_png //go:generate file2byteslice -package=images -input=./images/tiles.png -output=./images/tiles.go -var=Tiles_png //go:generate file2byteslice -package=images -input=./images/ui.png -output=./images/ui.go -var=UI_png +//go:generate file2byteslice -package=audio -input=./images/audio/alert.png -output=./images/audio/alert.go -var=Alert_png +//go:generate file2byteslice -package=audio -input=./images/audio/pause.png -output=./images/audio/pause.go -var=Pause_png +//go:generate file2byteslice -package=audio -input=./images/audio/play.png -output=./images/audio/play.go -var=Play_png //go:generate file2byteslice -package=blocks -input=./images/blocks/background.png -output=./images/blocks/background.go -var=Background_png //go:generate file2byteslice -package=blocks -input=./images/blocks/blocks.png -output=./images/blocks/blocks.go -var=Blocks_png //go:generate file2byteslice -package=flappy -input=./images/flappy/gopher.png -output=./images/flappy/gopher.go -var=Gopher_png diff --git a/examples/resources/images/audio/alert.go b/examples/resources/images/audio/alert.go new file mode 100644 index 000000000..ebc1ffe49 --- /dev/null +++ b/examples/resources/images/audio/alert.go @@ -0,0 +1,5 @@ +// Code generated by file2byteslice. DO NOT EDIT. + +package audio + +var Alert_png = []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\x00\x000\b\x04\x00\x00\x00\xfd\v1\f\x00\x00\x017IDATX\xc3c`\x18\x05\xa3`\x14P\x11\xfc\x0f\xfe\xbf\xf9?3\xad\fg\xfb?\xf9?\b\xb4\xd2\xc6x\xa5\xffg\xfeC\xc0\xdf\xff\xee\xb4\b\x9a\x0f\xffa\xe0\xe7\xff$\xca]\xbb\t\x85\xdf\xff\x1f\x01\xee\xfd7\xa1\xccp\x8e\xff\xf5\xff\xbf\xff\xff\x8f\"V\t7~\xed\u007f~ʌ\xf7\xf9\u007f\x17b\x12\x8a(\xe3\xff\xed\xe0\xa0ɥ8`\x10!\x81&'\xfa\xff\xd0\u007fc*\x04\f.\v(\x8dTx\xc0\xa0Z\xf0߂t\xa3\xa4\xff/\xfd\xff\xec?\x11\x00\xac\xfa\xcd\u007f5R\x8d\u007f\xf3\x9fH\x00V\xff\xff\xff\x1e\xd2,X\xfa\xff?\x89\x16\xfc\xff\xefD\x8a\x05Oɰ`&)\x16\xfc'Â\xab\xb8\xf5Sǂ\x8fC\xde\x02\x92\x82\x88\xe6\x91LN2u\xa4mF\xdbMۢ\xe2\xf5\u007fU\xf2\v8d\x83p\x15vf\x94\x94\xa0()\x81\x06\xc55\xa6Ax+\x1c\x91\xff\aI\xacpp\x18\x84\xab\xca\xdc\n\x14\xfa\xf1?\x87b\vpT\xfaep\x8f\xad&\xba\xd2\xc7\x17\xd6\x18͖IH\x91s\x97Ƞ\"-2\xff\x87\"5\xbc~\x10\xd5\xf0\"5\xb5\xfcW\xa6iӑ\x0e\x8d_:4\xdfG\xc1(\x18\xd2\x00\x00-\xdc\xe2\x06\xe3I\x8d\x19\x00\x00\x00\x00IEND\xaeB`\x82") diff --git a/examples/resources/images/audio/alert.png b/examples/resources/images/audio/alert.png new file mode 100644 index 0000000000000000000000000000000000000000..d23387ea3842d565d6750f89f4c2a4c526397df3 GIT binary patch literal 368 zcmV-$0gwKPP) zV=4p;E(<7)RCvIkTJYb3bfcvIFa7`QKM??|`ptheQjLG}pGW{!|Ns9U(kxi||38Tq z{QvJniUm)}uwW)B7W^l}f~&aoQ^$gjL*4?Cf{5mkOinU&q-{pqP1~a4^?z0J3piwh za20lDl$4+-DS-yXHKsv$D;$yk2T81Oa2SNE%G?SR`td)9VheCo`eksBttPtC#~0QZ z&6Y_>k#m>Gpdu|Y|A!(qynYbX@FF#}{8y%F(~%C1Uph43N5Lo<(f|N0+~Nk~NsSo* O0000zopr0AgQu&X J%Q~loCIA2|Qf2@E literal 0 HcmV?d00001 diff --git a/examples/resources/images/license.md b/examples/resources/images/license.md index 979b23070..17dc1cf34 100644 --- a/examples/resources/images/license.md +++ b/examples/resources/images/license.md @@ -55,6 +55,22 @@ https://opengameart.org/content/orthographic-outdoor-tiles CC0 1.0 ``` +## audio/play.png + +``` +https://fonts.google.com/icons + +Apache License 2.0 +``` + +## audio/pause.png + +``` +https://fonts.google.com/icons + +Apache License 2.0 +``` + ## flappy/gopher.png ``` @@ -124,7 +140,7 @@ Generated by Magnus Wahlstrand (https://github.com/kyeett) MIT License ``` -## Other image files +## The other image files ``` Copyright 2014 Hajime Hoshi