108 lines
2.0 KiB
Go
108 lines
2.0 KiB
Go
package splashmenu
|
|
|
|
import (
|
|
"cosmos/diego/groovy"
|
|
"image/color"
|
|
"log"
|
|
"strconv"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
"github.com/hajimehoshi/ebiten/v2/text"
|
|
"golang.org/x/image/font"
|
|
"golang.org/x/image/font/opentype"
|
|
)
|
|
|
|
var (
|
|
titleFont font.Face
|
|
menuFont font.Face
|
|
)
|
|
|
|
func init() {
|
|
tt, err := opentype.Parse(fonts.PressStart2P_ttf)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
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: 10,
|
|
DPI: dpi,
|
|
Hinting: font.HintingVertical,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
type Menu struct {
|
|
bgcolor color.RGBA
|
|
options map[int]MenuOption
|
|
events map[groovy.SceneEvent]func()
|
|
}
|
|
|
|
func NewMenu() Menu {
|
|
return Menu{
|
|
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", 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 _, o := range m.options {
|
|
if m.events[o.SelectionEvent] != nil {
|
|
for _, k := range keysPressed {
|
|
if k == o.Mapping {
|
|
m.events[o.SelectionEvent]()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|