unify craft and place menu

This commit is contained in:
stryan 2022-05-25 16:48:44 -04:00
parent 30cbd5559b
commit 53d42f1f3e
3 changed files with 47 additions and 90 deletions

View File

@ -1,67 +0,0 @@
package main
import (
sim "git.saintnet.tech/stryan/spacetea/simulator"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
type craftModel struct {
list list.Model
}
type craftMsg string
func (c craftModel) buildCraftMsg() tea.Msg {
i := c.list.SelectedItem().(item)
return craftMsg(i.ID())
}
func newCraftModel(entries []sim.ItemEntry, m tea.Model) craftModel {
var c craftModel
items := []list.Item{}
for _, v := range entries {
items = append(items, item{v.Name(), "no description", v.ID()})
}
//w,h
c.list = list.New(items, list.NewDefaultDelegate(), 32, 32)
c.list.Title = "What do you want to craft?"
c.list.DisableQuitKeybindings()
return c
}
// Init is the first function that will be called. It returns an optional
// initial command. To not perform an initial command return nil.
func (c craftModel) Init() tea.Cmd {
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.
func (c craftModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
c.list.SetSize(msg.Width-h, msg.Height-v)
return c, nil
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "ctrl-c":
return c, tea.Quit
case "esc":
return initMainscreen(), heartbeat()
case "enter":
return initMainscreen(), tea.Batch(c.buildCraftMsg, heartbeat())
}
}
var cmd tea.Cmd
c.list, cmd = c.list.Update(msg)
return c, cmd
}
// View renders the program's UI, which is just a string. The view is
// rendered after every Update.
func (c craftModel) View() string {
return c.list.View()
}

View File

@ -104,20 +104,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.s.Input("get")
case "p":
var res []sim.ItemEntry
for _, k := range sim.GlobalItemList {
if m.s.Player.Resources[k] != 0 {
res = append(res, sim.Lookup(k))
for _, k := range sim.GlobalItems {
if m.s.Player.Resources[k.ID()] != 0 {
res = append(res, k.(sim.ItemEntry))
}
}
return newPlaceModel(res, nil), nil
return newMenuModel(res, placeMenu), nil
case "c":
var res []sim.ItemEntry
for _, k := range sim.GlobalTechList {
if _, ok := m.s.Player.Techs[k]; ok {
res = append(res, sim.LookupTech(k))
}
for k := range m.s.Player.Craftables {
res = append(res, sim.GlobalItems[k].(sim.ItemEntry))
}
return newCraftModel(res, nil), nil
return newMenuModel(res, craftMenu), nil
}
return m, nil
}

View File

@ -9,8 +9,16 @@ import (
var docStyle = lipgloss.NewStyle().Margin(1, 2)
type menutype int
const (
placeMenu menutype = iota
craftMenu
)
type item struct {
title, desc, id string
entry sim.ItemEntry
}
func (i item) Title() string { return i.title }
@ -18,39 +26,56 @@ func (i item) Description() string { return i.desc }
func (i item) ID() string { return i.id }
func (i item) FilterValue() string { return i.title }
type placeModel struct {
type menuModel struct {
list list.Model
kind menutype
}
type placeMsg string
type craftMsg string
func (p placeModel) buildPlaceMsg() tea.Msg {
i := p.list.SelectedItem().(item)
return placeMsg(i.ID())
}
func newPlaceModel(entries []sim.ItemEntry, m tea.Model) placeModel {
var p placeModel
func newMenuModel(entries []sim.ItemEntry, i menutype) menuModel {
var p menuModel
p.kind = i
items := []list.Item{}
for _, v := range entries {
items = append(items, item{v.Name(), "no description", v.ID()})
items = append(items, item{v.String(), "no description", v.ID().String(), v})
}
//w,h
p.list = list.New(items, list.NewDefaultDelegate(), 32, 32)
p.list.Title = "What do you want to place?"
switch i {
case placeMenu:
p.list.Title = "What do you want to place?"
case craftMenu:
p.list.Title = "What do you want to craft?"
}
p.list.DisableQuitKeybindings()
return p
}
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 ""
}
// Init is the first function that will be called. It returns an optional
// initial command. To not perform an initial command return nil.
func (p placeModel) Init() tea.Cmd {
func (p menuModel) Init() tea.Cmd {
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.
func (p placeModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (p menuModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
@ -63,7 +88,7 @@ func (p placeModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "esc":
return initMainscreen(), heartbeat()
case "enter":
return initMainscreen(), tea.Batch(p.buildPlaceMsg, heartbeat())
return initMainscreen(), tea.Batch(p.buildMenuMsg, heartbeat())
}
}
@ -74,6 +99,6 @@ func (p placeModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// View renders the program's UI, which is just a string. The view is
// rendered after every Update.
func (p placeModel) View() string {
func (p menuModel) View() string {
return p.list.View()
}