Implemented scene events and callback handlers for those events. Manager defaults also added.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package splashmenu
|
||||
|
||||
import (
|
||||
"cosmos/diego/groovy"
|
||||
"image/color"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
||||
@@ -13,7 +15,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
menuFont font.Face
|
||||
titleFont font.Face
|
||||
menuFont font.Face
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -23,8 +26,17 @@ func init() {
|
||||
}
|
||||
|
||||
const dpi = 72
|
||||
titleFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
||||
Size: 16,
|
||||
DPI: dpi,
|
||||
Hinting: font.HintingVertical,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
menuFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
||||
Size: 12,
|
||||
Size: 10,
|
||||
DPI: dpi,
|
||||
Hinting: font.HintingVertical,
|
||||
})
|
||||
@@ -34,35 +46,62 @@ func init() {
|
||||
}
|
||||
|
||||
type Menu struct {
|
||||
bgcolor color.RGBA
|
||||
completed bool
|
||||
bgcolor color.RGBA
|
||||
options map[int]MenuOption
|
||||
events map[groovy.SceneEvent]func()
|
||||
}
|
||||
|
||||
func NewMenu() Menu {
|
||||
return Menu{
|
||||
bgcolor: color.RGBA{0x33, 0x33, 0x99, 0xFF},
|
||||
completed: false,
|
||||
bgcolor: color.RGBA{0x33, 0x33, 0x99, 0xFF},
|
||||
events: make(map[groovy.SceneEvent]func()),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Menu) Draw(screen *ebiten.Image) {
|
||||
screen.Fill(m.bgcolor)
|
||||
text.Draw(screen, "menu", menuFont, 40, 40, color.White)
|
||||
text.Draw(screen, "menu", titleFont, 40, 40, color.White)
|
||||
|
||||
m.RenderOptions(screen)
|
||||
}
|
||||
|
||||
func (m Menu) SetEventHandler(event groovy.SceneEvent, f func()) {
|
||||
m.events[event] = f
|
||||
}
|
||||
|
||||
func (m *Menu) SetOptions(options map[int]MenuOption) {
|
||||
m.options = make(map[int]MenuOption)
|
||||
|
||||
for k, v := range options {
|
||||
m.options[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Menu) RenderOptions(screen *ebiten.Image) {
|
||||
|
||||
var offset int = 20
|
||||
|
||||
for k, v := range m.options {
|
||||
m.options[k] = v
|
||||
text.Draw(screen, strconv.Itoa(k)+": "+v.Description, menuFont, 40, 60+offset*k, color.White)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (m *Menu) Update() error {
|
||||
|
||||
var keysPressed []ebiten.Key
|
||||
keysPressed = inpututil.AppendPressedKeys(keysPressed[:0])
|
||||
for _, k := range keysPressed {
|
||||
if k == ebiten.KeyEnter {
|
||||
m.completed = true
|
||||
|
||||
for _, o := range m.options {
|
||||
if m.events[o.SelectionEvent] != nil {
|
||||
for _, k := range keysPressed {
|
||||
if k == o.Mapping {
|
||||
m.events[o.SelectionEvent]()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Menu) Completed() bool {
|
||||
return m.completed
|
||||
}
|
||||
|
||||
13
examples/splashmenu/scenes/menuoption.go
Normal file
13
examples/splashmenu/scenes/menuoption.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package splashmenu
|
||||
|
||||
import (
|
||||
"cosmos/diego/groovy"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type MenuOption struct {
|
||||
Description string
|
||||
SelectionEvent groovy.SceneEvent
|
||||
Mapping ebiten.Key
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package splashmenu
|
||||
|
||||
import (
|
||||
"cosmos/diego/groovy"
|
||||
"image/color"
|
||||
"log"
|
||||
|
||||
@@ -34,14 +35,19 @@ func init() {
|
||||
|
||||
type Splash struct {
|
||||
bgcolor color.RGBA
|
||||
completed bool
|
||||
increment int
|
||||
events map[groovy.SceneEvent]func()
|
||||
}
|
||||
|
||||
// GetSceneEvents implements groovy.Scene.
|
||||
func (*Splash) GetSceneEvents() []groovy.SceneEvent {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewSplash() Splash {
|
||||
return Splash{
|
||||
bgcolor: color.RGBA{0xFF, 0xFF, 0xFF, 0xFF},
|
||||
completed: false,
|
||||
bgcolor: color.RGBA{0xFF, 0xFF, 0xFF, 0xFF},
|
||||
events: make(map[groovy.SceneEvent]func()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,12 +64,12 @@ func (s *Splash) Update() error {
|
||||
s.bgcolor.B = (s.bgcolor.B - 2) % 0xFF
|
||||
|
||||
if s.bgcolor.R == 0x00 {
|
||||
s.completed = true
|
||||
s.events[groovy.COMPLETED]()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Splash) Completed() bool {
|
||||
return s.completed
|
||||
func (s Splash) SetEventHandler(event groovy.SceneEvent, f func()) {
|
||||
s.events[event] = f
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user