move menu logic to menu.go

This commit is contained in:
2023-07-03 16:09:43 +02:00
parent f5902947f8
commit f2bb6c3430
2 changed files with 53 additions and 33 deletions

View File

@@ -1,14 +1,9 @@
package konstructor package konstructor
import ( import (
"image/color"
"os" "os"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/text"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
) )
type Engine struct { type Engine struct {
@@ -26,40 +21,17 @@ func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) {
func (e *Engine) Update(screen *ebiten.Image) error { func (e *Engine) Update(screen *ebiten.Image) error {
e.Controller.Watch() e.Controller.Watch()
if e.KContext.Screen.Type == "menu" { if e.KContext.Screen.Type == "menu" {
e.MenuUpdate()
} }
return nil return nil
} }
func (e *Engine) Draw(screen *ebiten.Image) { func (e *Engine) Draw(screen *ebiten.Image) {
if e.KContext.Screen.Type == "menu" { if e.KContext.Screen.Type == "menu" {
menu := e.MenuMap[e.KContext.Screen.Value] e.MenuDraw(screen)
if e.Controller.DownPressed() && menu.CurrentSelected != len(menu.MenuItems)-1 {
menu.CurrentSelected++
}
if e.Controller.UpPressed() && menu.CurrentSelected != 0 {
menu.CurrentSelected--
}
e.MenuMap[e.KContext.Screen.Value] = menu
tt, _ := opentype.Parse(fonts.MPlus1pRegular_ttf)
menu_face, _ := opentype.NewFace(tt, &opentype.FaceOptions{
Size: menu.MenuLayout.DPI,
DPI: menu.MenuLayout.Size,
Hinting: font.HintingVertical,
})
for i, menu_item := range menu.MenuItems {
if menu.CurrentSelected == i {
text.Draw(screen, menu_item.Label+"\n", menu_face, 8, 24*(i+1), color.RGBA{R: 0, G: 255, B: 0, A: 100})
} else {
text.Draw(screen, menu_item.Label+"\n", menu_face, 8, 24*(i+1), color.White)
}
} }
if e.Controller.Action3Pressed() { if e.Controller.Action3Pressed() {
os.Exit(1) os.Exit(1)
} }
}
e.Controller.Clear() e.Controller.Clear()
} }

View File

@@ -1,5 +1,15 @@
package konstructor package konstructor
import (
"image/color"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/text"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)
type MenuMap map[string]Menu type MenuMap map[string]Menu
type MenuLayout struct { type MenuLayout struct {
@@ -19,3 +29,41 @@ type Menu struct {
MenuLayout MenuLayout MenuLayout MenuLayout
MenuItems []MenuItem MenuItems []MenuItem
} }
func (e *Engine) MenuDraw(screen *ebiten.Image) {
menu := e.MenuMap[e.KContext.Screen.Value]
face := e.menuGetFontFace(menu)
for i, menu_item := range menu.MenuItems {
var c color.Color
if menu.CurrentSelected == i {
c = color.RGBA{R: 0, G: 255, B: 0, A: 100}
} else {
c = color.White
}
text.Draw(screen, menu_item.Label+"\n", face, 8, 24*(i+1), c)
}
}
func (e *Engine) MenuUpdate() {
menu := e.MenuMap[e.KContext.Screen.Value]
if e.Controller.DownPressed() && menu.CurrentSelected != len(menu.MenuItems)-1 {
menu.CurrentSelected++
}
if e.Controller.UpPressed() && menu.CurrentSelected != 0 {
menu.CurrentSelected--
}
e.MenuMap[e.KContext.Screen.Value] = menu
}
func (e *Engine) menuGetFontFace(menu Menu) font.Face {
tt, _ := opentype.Parse(fonts.MPlus1pRegular_ttf)
face, _ := opentype.NewFace(tt, &opentype.FaceOptions{
Size: menu.MenuLayout.DPI,
DPI: menu.MenuLayout.Size,
Hinting: font.HintingVertical,
})
return face
}