Menu and save system

This commit is contained in:
2025-11-19 13:13:22 -07:00
parent a7e6f4e0bf
commit cd6c1a78b0
9 changed files with 1231 additions and 286 deletions

View File

@@ -8,6 +8,8 @@ import (
"github.com/hajimehoshi/ebiten/v2/text/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
"golang.org/x/image/font/basicfont"
"github.com/atridad/BigFeelings/internal/screens"
)
var (
@@ -37,7 +39,9 @@ type MenuOption int
const (
OptionResume MenuOption = iota
OptionSave
OptionSettings
OptionMainMenu
OptionQuit
optionCount
)
@@ -50,10 +54,9 @@ const (
)
type PauseMenu struct {
selectedIndex int
currentScreen menuScreen
fpsMonitorValue *bool
fpsCapValue FPSCapSetting
selectedIndex int
currentScreen menuScreen
settingsScreen *screens.SettingsScreen
}
type FPSCapSetting interface {
@@ -63,17 +66,18 @@ type FPSCapSetting interface {
func NewPauseMenu() *PauseMenu {
return &PauseMenu{
selectedIndex: 0,
currentScreen: screenMain,
selectedIndex: 0,
currentScreen: screenMain,
settingsScreen: screens.NewSettingsScreen(),
}
}
func (m *PauseMenu) SetFPSMonitor(enabled *bool) {
m.fpsMonitorValue = enabled
m.settingsScreen.SetFPSMonitor(enabled)
}
func (m *PauseMenu) SetFPSCap(cap FPSCapSetting) {
m.fpsCapValue = cap
m.settingsScreen.SetFPSCap(cap)
}
// Returns the selected option if one was chosen, nil otherwise
@@ -112,34 +116,10 @@ func (m *PauseMenu) updateMain() *MenuOption {
}
func (m *PauseMenu) updateSettings() *MenuOption {
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
if m.settingsScreen.Update() {
m.currentScreen = screenMain
m.selectedIndex = 0
return nil
}
settingsCount := 2
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) || inpututil.IsKeyJustPressed(ebiten.KeyW) {
m.selectedIndex--
if m.selectedIndex < 0 {
m.selectedIndex = 0
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) || inpututil.IsKeyJustPressed(ebiten.KeyS) {
m.selectedIndex++
if m.selectedIndex >= settingsCount {
m.selectedIndex = settingsCount - 1
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) || inpututil.IsKeyJustPressed(ebiten.KeySpace) {
if m.selectedIndex == 0 && m.fpsMonitorValue != nil {
*m.fpsMonitorValue = !*m.fpsMonitorValue
} else if m.selectedIndex == 1 && m.fpsCapValue != nil {
m.fpsCapValue.Cycle()
}
}
return nil
}
@@ -149,7 +129,7 @@ func (m *PauseMenu) Draw(screen *ebiten.Image, screenWidth, screenHeight int) {
}
menuWidth := 400
menuHeight := 300
menuHeight := 380
menuX := (screenWidth - menuWidth) / 2
menuY := (screenHeight - menuHeight) / 2
@@ -181,11 +161,11 @@ func (m *PauseMenu) drawMain(screen *ebiten.Image, menuX, menuY, menuWidth, menu
titleY := menuY + 50
m.drawText(screen, titleText, color.White, titleX, titleY)
options := []string{"Resume", "Settings", "Quit"}
startY := menuY + 110
options := []string{"Resume", "Save", "Settings", "Main Menu", "Quit"}
startY := menuY + 90
for i, option := range options {
optionY := startY + (i * 40)
optionY := startY + (i * 45)
optionX := menuX + (menuWidth / 2) - (len(option) * 7 / 2)
if i == m.selectedIndex {
@@ -204,49 +184,35 @@ func (m *PauseMenu) drawMain(screen *ebiten.Image, menuX, menuY, menuWidth, menu
}
func (m *PauseMenu) drawSettings(screen *ebiten.Image, menuX, menuY, menuWidth, menuHeight int) {
titleText := "SETTINGS"
titleX := menuX + (menuWidth / 2) - (len(titleText) * 7 / 2)
titleY := menuY + 50
m.drawText(screen, titleText, color.White, titleX, titleY)
// Draw menu background and border
vector.DrawFilledRect(screen,
float32(menuX), float32(menuY),
float32(menuWidth), float32(menuHeight),
color.RGBA{R: 40, G: 40, B: 50, A: 255},
false,
)
startY := menuY + 110
leftMargin := menuX + 40
vector.StrokeRect(screen,
float32(menuX), float32(menuY),
float32(menuWidth), float32(menuHeight),
2,
color.RGBA{R: 100, G: 100, B: 120, A: 255},
false,
)
fpsMonitorText := "FPS Monitor: "
if m.fpsMonitorValue != nil && *m.fpsMonitorValue {
fpsMonitorText += "ON"
} else {
fpsMonitorText += "OFF"
}
// Create a sub-image for the settings screen to draw within the menu bounds
// We'll draw to the full screen and the settings screen will handle positioning
screenWidth := menuWidth
screenHeight := menuHeight
if m.selectedIndex == 0 {
indicatorX := leftMargin - 20
m.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, startY)
m.drawText(screen, fpsMonitorText, color.RGBA{R: 255, G: 255, B: 100, A: 255}, leftMargin, startY)
} else {
m.drawText(screen, fpsMonitorText, color.RGBA{R: 180, G: 180, B: 180, A: 255}, leftMargin, startY)
}
// Temporarily adjust the drawing to center within the menu
subScreen := ebiten.NewImage(screenWidth, screenHeight)
m.settingsScreen.Draw(subScreen, screenWidth, screenHeight, "SETTINGS")
fpsCapText := "FPS Cap: "
if m.fpsCapValue != nil {
fpsCapText += m.fpsCapValue.String()
} else {
fpsCapText += "60 FPS"
}
capY := startY + 30
if m.selectedIndex == 1 {
indicatorX := leftMargin - 20
m.drawText(screen, ">", color.RGBA{R: 255, G: 200, B: 0, A: 255}, indicatorX, capY)
m.drawText(screen, fpsCapText, color.RGBA{R: 255, G: 255, B: 100, A: 255}, leftMargin, capY)
} else {
m.drawText(screen, fpsCapText, color.RGBA{R: 180, G: 180, B: 180, A: 255}, leftMargin, capY)
}
hintText := "Enter/Space to toggle, ESC to go back"
hintX := menuX + (menuWidth / 2) - (len(hintText) * 7 / 2)
hintY := menuY + menuHeight - 30
m.drawText(screen, hintText, color.RGBA{R: 150, G: 150, B: 150, A: 255}, hintX, hintY)
// Draw the settings content in the menu area
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(menuX), float64(menuY))
screen.DrawImage(subScreen, op)
}
func (m *PauseMenu) drawText(screen *ebiten.Image, txt string, clr color.Color, x, y int) {
@@ -259,4 +225,5 @@ func (m *PauseMenu) drawText(screen *ebiten.Image, txt string, clr color.Color,
func (m *PauseMenu) Reset() {
m.selectedIndex = 0
m.currentScreen = screenMain
m.settingsScreen.Reset()
}