From 403a81d0fb7cd9756e6bbff3ee952a1cbbde0c5b Mon Sep 17 00:00:00 2001 From: iegod Date: Fri, 25 Aug 2023 11:59:14 -0400 Subject: [PATCH] Various menu improvements: dimension setter, menu rendering refactored, menu selection metadata and accessor added. --- examples/splashmenu/scenes/menu.go | 36 ++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/examples/splashmenu/scenes/menu.go b/examples/splashmenu/scenes/menu.go index efac856..c4e9ffc 100644 --- a/examples/splashmenu/scenes/menu.go +++ b/examples/splashmenu/scenes/menu.go @@ -51,10 +51,12 @@ func init() { } type Menu struct { - bgcolor color.RGBA - increment int - options map[int]MenuOption - events map[groovy.SceneEvent]func() + Dimensions groovy.Area + bgcolor color.RGBA + increment int + options map[int]MenuOption + events map[groovy.SceneEvent]func() + menuSelection uint } func NewMenu() Menu { @@ -68,10 +70,13 @@ func (m *Menu) Draw(screen *ebiten.Image) { screen.Fill(m.bgcolor) text.Draw(screen, "menu", titleFont, 40, 40, color.White) - bgcolor := " 0x" + strconv.FormatUint(uint64(m.bgcolor.R), 16) + " 0x" + strconv.FormatUint(uint64(m.bgcolor.G), 16) + " 0x" + strconv.FormatUint(uint64(m.bgcolor.B), 16) + m.RenderMetadata(screen) + m.RenderMenu(screen) +} +func (m *Menu) RenderMetadata(screen *ebiten.Image) { + bgcolor := " 0x" + strconv.FormatUint(uint64(m.bgcolor.R), 16) + " 0x" + strconv.FormatUint(uint64(m.bgcolor.G), 16) + " 0x" + strconv.FormatUint(uint64(m.bgcolor.B), 16) text.Draw(screen, bgcolor, menuFont, 40, 700, color.White) - m.RenderOptions(screen) } func (m Menu) SetEventHandler(event groovy.SceneEvent, f func()) { @@ -86,10 +91,17 @@ func (m *Menu) SetOptions(options map[int]MenuOption) { } } -func (m *Menu) RenderOptions(screen *ebiten.Image) { +// sets sene dimensions +func (m *Menu) SetDimensions(a groovy.Area) { + m.Dimensions = a +} + +func (m *Menu) RenderMenu(screen *ebiten.Image) { var offset int = 20 + screen.Set(m.Dimensions.Width/2, m.Dimensions.Height/2, color.RGBA{0xFF, 0x00, 0x00, 0xFF}) + 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) @@ -103,17 +115,19 @@ func (m *Menu) Update() error { var keysPressed []ebiten.Key keysPressed = inpututil.AppendPressedKeys(keysPressed[:0]) - for _, o := range m.options { + for idx, o := range m.options { if m.events[o.SelectionEvent] != nil { for _, k := range keysPressed { if k == o.Mapping { + m.menuSelection = uint(idx) m.events[o.SelectionEvent]() } } } } - colorOffset := uint8(int(math.Sin(float64(m.increment)/300) * 6)) + //m.UpdateNoise() + colorOffset := uint8(int(math.Sin(float64(m.increment)/60) * 25)) m.bgcolor.R = (colorOffset + backgroundBaseColor.R) % 0xFF m.bgcolor.G = (colorOffset + backgroundBaseColor.G) % 0xFF @@ -121,3 +135,7 @@ func (m *Menu) Update() error { return nil } + +func (m *Menu) GetMenuSelection() uint { + return m.menuSelection +}