spacetea/menu.go

112 lines
2.6 KiB
Go
Raw Normal View History

2022-05-18 22:47:48 -04:00
package main
import (
2022-05-19 17:09:46 -04:00
sim "git.saintnet.tech/stryan/spacetea/simulator"
2022-05-18 22:47:48 -04:00
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
2022-05-19 17:09:46 -04:00
"github.com/charmbracelet/lipgloss"
2022-05-18 22:47:48 -04:00
)
2022-05-19 17:09:46 -04:00
var docStyle = lipgloss.NewStyle().Margin(1, 2)
2022-05-25 16:48:44 -04:00
type menutype int
const (
placeMenu menutype = iota
craftMenu
journalMenu
2022-05-25 16:48:44 -04:00
)
2022-05-18 22:47:48 -04:00
type item struct {
2022-05-19 17:09:46 -04:00
title, desc, id string
2022-05-25 16:48:44 -04:00
entry sim.ItemEntry
2022-05-18 22:47:48 -04:00
}
func (i item) Title() string { return i.title }
func (i item) Description() string { return i.desc }
2022-05-19 17:09:46 -04:00
func (i item) ID() string { return i.id }
2022-05-18 22:47:48 -04:00
func (i item) FilterValue() string { return i.title }
2022-05-25 16:48:44 -04:00
type menuModel struct {
list list.Model
kind menutype
lastSize tea.WindowSizeMsg
2022-05-18 22:47:48 -04:00
}
2022-05-19 12:58:37 -04:00
type placeMsg string
2022-05-25 16:48:44 -04:00
type craftMsg string
2022-05-19 12:58:37 -04:00
2022-05-25 16:48:44 -04:00
func newMenuModel(entries []sim.ItemEntry, i menutype) menuModel {
var p menuModel
p.kind = i
2022-05-18 22:47:48 -04:00
items := []list.Item{}
for _, v := range entries {
2022-05-25 18:07:07 -04:00
items = append(items, item{v.Describe(), v.Description(), v.ID().String(), v})
2022-05-18 22:47:48 -04:00
}
//w,h
2022-05-25 18:07:07 -04:00
p.list = list.New(items, list.NewDefaultDelegate(), 80, 32)
2022-05-25 16:48:44 -04:00
switch i {
case placeMenu:
p.list.Title = "What do you want to place?"
case craftMenu:
p.list.Title = "What do you want to craft?"
}
2022-05-18 22:47:48 -04:00
p.list.DisableQuitKeybindings()
return p
}
2022-05-25 16:48:44 -04:00
func (p menuModel) buildMenuMsg() tea.Msg {
if p.list.SelectedItem() == nil {
return ""
}
i := p.list.SelectedItem().(item)
if p.kind == placeMenu {
return placeMsg(i.entry.String())
}
if p.kind == craftMenu {
return craftMsg(i.entry.String())
}
return ""
}
2022-05-18 22:47:48 -04:00
// Init is the first function that will be called. It returns an optional
// initial command. To not perform an initial command return nil.
2022-05-25 16:48:44 -04:00
func (p menuModel) Init() tea.Cmd {
2022-05-18 22:47:48 -04:00
return tea.EnterAltScreen
}
// Update is called when a message is received. Use it to inspect messages
// and, in response, update the model and/or send a command.
2022-05-25 16:48:44 -04:00
func (p menuModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2022-05-18 22:47:48 -04:00
switch msg := msg.(type) {
case tea.WindowSizeMsg:
2022-05-19 17:09:46 -04:00
h, v := docStyle.GetFrameSize()
p.list.SetSize(msg.Width-h, msg.Height-v)
p.lastSize = msg
2022-05-18 22:47:48 -04:00
return p, nil
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "ctrl-c":
return p, tea.Quit
case "esc":
return initMainscreen(), tea.Batch(p.GetSize, heartbeat())
2022-05-18 22:47:48 -04:00
case "enter":
return initMainscreen(), tea.Batch(p.GetSize, p.buildMenuMsg, heartbeat())
2022-05-18 22:47:48 -04:00
}
}
var cmd tea.Cmd
p.list, cmd = p.list.Update(msg)
return p, cmd
}
// View renders the program's UI, which is just a string. The view is
// rendered after every Update.
2022-05-25 16:48:44 -04:00
func (p menuModel) View() string {
2022-05-18 22:47:48 -04:00
return p.list.View()
}
func (p menuModel) GetSize() tea.Msg {
return p.lastSize
}