add help screen, use keymap
This commit is contained in:
parent
ffbf8afca3
commit
a386ae1872
78
keymap.go
Normal file
78
keymap.go
Normal file
@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import "github.com/charmbracelet/bubbles/key"
|
||||
|
||||
type keyMap struct {
|
||||
Up key.Binding
|
||||
Down key.Binding
|
||||
Left key.Binding
|
||||
Right key.Binding
|
||||
Help key.Binding
|
||||
Quit key.Binding
|
||||
Gather key.Binding
|
||||
Pickup key.Binding
|
||||
Destroy key.Binding
|
||||
Place key.Binding
|
||||
Craft key.Binding
|
||||
}
|
||||
|
||||
// ShortHelp returns keybindings to be shown in the mini help view. It's part
|
||||
// of the key.Map interface.
|
||||
func (k keyMap) ShortHelp() []key.Binding {
|
||||
return []key.Binding{k.Help, k.Quit}
|
||||
}
|
||||
|
||||
// FullHelp returns keybindings for the expanded help view. It's part of the
|
||||
// key.Map interface.
|
||||
func (k keyMap) FullHelp() [][]key.Binding {
|
||||
return [][]key.Binding{
|
||||
{k.Up, k.Down, k.Left, k.Right, k.Gather, k.Place, k.Craft, k.Destroy, k.Pickup, k.Help, k.Quit}, //second column
|
||||
}
|
||||
}
|
||||
|
||||
var keys = keyMap{
|
||||
Up: key.NewBinding(
|
||||
key.WithKeys("up", "k"),
|
||||
key.WithHelp("↑/k", "move up"),
|
||||
),
|
||||
Down: key.NewBinding(
|
||||
key.WithKeys("down", "j"),
|
||||
key.WithHelp("↓/j", "move down"),
|
||||
),
|
||||
Left: key.NewBinding(
|
||||
key.WithKeys("left", "h"),
|
||||
key.WithHelp("←/h", "move left"),
|
||||
),
|
||||
Right: key.NewBinding(
|
||||
key.WithKeys("right", "l"),
|
||||
key.WithHelp("→/l", "move right"),
|
||||
),
|
||||
Help: key.NewBinding(
|
||||
key.WithKeys("?"),
|
||||
key.WithHelp("?", "toggle help"),
|
||||
),
|
||||
Quit: key.NewBinding(
|
||||
key.WithKeys("esc", "ctrl+c"),
|
||||
key.WithHelp("ctrl+c", "quit"),
|
||||
),
|
||||
Gather: key.NewBinding(
|
||||
key.WithKeys("g"),
|
||||
key.WithHelp("g", "gather tile resource"),
|
||||
),
|
||||
Pickup: key.NewBinding(
|
||||
key.WithKeys(","),
|
||||
key.WithHelp(",", "pickup object"),
|
||||
),
|
||||
Destroy: key.NewBinding(
|
||||
key.WithKeys("x"),
|
||||
key.WithHelp("x", "destroy tile object "),
|
||||
),
|
||||
Place: key.NewBinding(
|
||||
key.WithKeys("p"),
|
||||
key.WithHelp("p", "place object"),
|
||||
),
|
||||
Craft: key.NewBinding(
|
||||
key.WithKeys("c"),
|
||||
key.WithHelp("c", "craft object"),
|
||||
),
|
||||
}
|
@ -7,20 +7,25 @@ import (
|
||||
"time"
|
||||
|
||||
sim "git.saintnet.tech/stryan/spacetea/simulator"
|
||||
"github.com/charmbracelet/bubbles/help"
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
var style = lipgloss.NewStyle().
|
||||
Width(24).
|
||||
Width(32).
|
||||
Align(lipgloss.Center).
|
||||
Foreground(lipgloss.Color("#F0FFFF")).
|
||||
BorderStyle(lipgloss.NormalBorder()).
|
||||
BorderForeground(lipgloss.Color("63"))
|
||||
|
||||
type model struct {
|
||||
s *sim.Simulator
|
||||
input textinput.Model
|
||||
help help.Model
|
||||
keys keyMap
|
||||
next string
|
||||
}
|
||||
|
||||
@ -50,6 +55,8 @@ func initMainscreen() model {
|
||||
return model{
|
||||
s: simulator,
|
||||
input: ti,
|
||||
help: help.New(),
|
||||
keys: keys,
|
||||
}
|
||||
}
|
||||
func (m model) Init() tea.Cmd {
|
||||
@ -62,6 +69,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
case beat:
|
||||
//heartbeat
|
||||
return m, heartbeat()
|
||||
case tea.WindowSizeMsg:
|
||||
m.help.Width = msg.Width
|
||||
case placeMsg:
|
||||
simc := fmt.Sprintf("place %v", string(msg))
|
||||
m.s.Input(simc)
|
||||
@ -71,39 +80,28 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.s.Input(simc)
|
||||
return m, nil
|
||||
case tea.KeyMsg:
|
||||
switch msg.Type {
|
||||
case tea.KeyCtrlC:
|
||||
switch {
|
||||
case key.Matches(msg, m.keys.Help):
|
||||
m.help.ShowAll = !m.help.ShowAll
|
||||
return m, nil
|
||||
case key.Matches(msg, m.keys.Quit):
|
||||
m.s.Stop()
|
||||
return m, tea.Quit
|
||||
case tea.KeyEnter:
|
||||
m.s.Input(m.input.Value())
|
||||
m.input.Reset()
|
||||
return m, nil
|
||||
case tea.KeyRight:
|
||||
case key.Matches(msg, m.keys.Right):
|
||||
m.s.Input("right")
|
||||
return m, nil
|
||||
case tea.KeyLeft:
|
||||
case key.Matches(msg, m.keys.Left):
|
||||
m.s.Input("left")
|
||||
return m, nil
|
||||
case tea.KeyUp:
|
||||
case key.Matches(msg, m.keys.Up):
|
||||
m.s.Input("up")
|
||||
return m, nil
|
||||
case tea.KeyDown:
|
||||
case key.Matches(msg, m.keys.Down):
|
||||
m.s.Input("down")
|
||||
return m, nil
|
||||
case tea.KeyCtrlF:
|
||||
if m.input.Focused() {
|
||||
m.input.Blur()
|
||||
} else {
|
||||
m.input.Focus()
|
||||
}
|
||||
return m, nil
|
||||
case tea.KeyRunes:
|
||||
if !m.input.Focused() {
|
||||
switch msg.String() {
|
||||
case "g":
|
||||
case key.Matches(msg, m.keys.Gather):
|
||||
m.s.Input("get")
|
||||
case "p":
|
||||
case key.Matches(msg, m.keys.Place):
|
||||
var res entrylist
|
||||
for _, k := range sim.GlobalItems {
|
||||
if m.s.Player.Resources[k.ID()] != 0 {
|
||||
@ -112,11 +110,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
sort.Sort(res)
|
||||
return newMenuModel(res, placeMenu), nil
|
||||
case ",":
|
||||
case key.Matches(msg, m.keys.Pickup):
|
||||
m.s.Input("pickup")
|
||||
case "x":
|
||||
case key.Matches(msg, m.keys.Destroy):
|
||||
m.s.Input("destroy")
|
||||
case "c":
|
||||
case key.Matches(msg, m.keys.Craft):
|
||||
var res entrylist
|
||||
for k := range m.s.Player.Craftables {
|
||||
res = append(res, sim.GlobalItems[k].(sim.ItemEntry))
|
||||
@ -124,9 +122,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
sort.Sort(res)
|
||||
return newMenuModel(res, craftMenu), nil
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
m.input, cmd = m.input.Update(msg)
|
||||
return m, cmd
|
||||
@ -135,9 +130,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
func (m model) View() string {
|
||||
var render string
|
||||
if m.help.ShowAll != true {
|
||||
playerAndLog := lipgloss.JoinVertical(0, style.Render(fmt.Sprintf("%v", m.s.Player.String())), style.Render(fmt.Sprintf("%v", m.s.Player.Log())))
|
||||
placeAndInput := lipgloss.JoinVertical(0, style.Render(m.s.Place.String()), style.Render(m.input.View()))
|
||||
placeAndInput := lipgloss.JoinVertical(0, style.Render(m.s.Place.String()), style.Render(m.help.View(m.keys)))
|
||||
display := lipgloss.JoinHorizontal(0, placeAndInput, playerAndLog)
|
||||
render = fmt.Sprintf("%v\n%v\n", style.Render(fmt.Sprintf("Current Time: %v", strconv.Itoa(m.s.Time))), display)
|
||||
} else {
|
||||
render = style.Render(m.help.View(m.keys))
|
||||
}
|
||||
return render
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user