Skip to content

Commit

Permalink
Command Help Hints (#15)
Browse files Browse the repository at this point in the history
* command hints on bottom of screen
  • Loading branch information
silverbucket authored Jan 27, 2025
1 parent d3c37f9 commit 440d056
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 86 deletions.
2 changes: 2 additions & 0 deletions cmd/dialog/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ var ListHeader = base.
PaddingBottom(1).
Width(100).
Render

var HelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render
254 changes: 175 additions & 79 deletions input.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"github.com/charmbracelet/bubbletea"
"pirate-wars/cmd/common"
"pirate-wars/cmd/npc"
Expand All @@ -11,56 +10,95 @@ import (

var ExamineData = user_action.Examine()

func (m model) miniMapInput(msg tea.Msg) (tea.Model, tea.Cmd) {
const KeyCatAdmin = 0
const KeyCatNav = 1
const KeyCatAction = 3
const KeyCatAux = 4

type keyItem struct {
key []string
cat int
help string
exec func(m model) (tea.Model, tea.Cmd)
}

type KeyMap []keyItem

func (m model) getInput(msg tea.Msg, km KeyMap) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
// These keys should exit the program.
case "ctrl+c", "ctrl+q":
return m, tea.Quit

// The "m" key toggles the minimap
case "m", "enter":
m.viewType = world.ViewTypeMainMap
input := msg.String()
for _, e := range km {
for _, k := range e.key {
if input == k {
return e.exec(m)
}
}
}
}
return m, nil
}

func (m model) sailingInput(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
func keyQuit(m model) (tea.Model, tea.Cmd) {
return m, tea.Quit
}

// These keys should exit the program.
case "ctrl+c", "ctrl+q":
return m, tea.Quit
var miniMapKeyMap = KeyMap{
{
key: []string{"ctrl+q"},
cat: KeyCatAdmin,
help: "quit",
exec: keyQuit,
},
{
key: []string{"m", "enter"},
cat: KeyCatAux,
help: "exit minimap",
exec: func(m model) (tea.Model, tea.Cmd) {
m.viewType = world.ViewTypeMainMap
return m, nil
},
},
}

// The "m" key displays the minimap
case "m":
var sailingKeyMap = KeyMap{
{
key: []string{"?"},
cat: KeyCatAdmin,
help: "help",
exec: func(m model) (tea.Model, tea.Cmd) {
m.action = user_action.UserActionIdHelp
return m, nil
},
},
{
key: []string{"m"},
help: "minimap",
cat: KeyCatAux,
exec: func(m model) (tea.Model, tea.Cmd) {
m.viewType = world.ViewTypeMiniMap

//case "p":
// if m.viewType == world.ViewTypeHeatMap {
// m.viewType = world.ViewTypeMainMap
// } else {
// m.viewType = world.ViewTypeHeatMap
// }

// examine something on the map
case "x":
return m, nil
},
},
{
key: []string{"x"},
help: "examine",
cat: KeyCatAction,
exec: func(m model) (tea.Model, tea.Cmd) {
m.action = user_action.UserActionIdExamine
npcs := m.npcs.GetVisible(m.player.GetPos())
ExamineData = user_action.Examine()
npcs.ForEach(func(n npc.Npc) {
ExamineData.AddItem(&n)
})

case "left", "h", "a":
return m, nil
},
},
{
key: []string{"left", "h", "a"},
help: "left",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
c := m.player.GetPos()
if c.X > 0 {
t := common.Coordinates{
Expand All @@ -71,8 +109,14 @@ func (m model) sailingInput(msg tea.Msg) (tea.Model, tea.Cmd) {
m.player.SetPos(t)
}
}

case "right", "l", "d":
return m, nil
},
},
{
key: []string{"right", "l", "d"},
help: "right",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
c := m.player.GetPos()
if c.X < m.world.GetWidth()-1 {
t := common.Coordinates{
Expand All @@ -83,9 +127,14 @@ func (m model) sailingInput(msg tea.Msg) (tea.Model, tea.Cmd) {
m.player.SetPos(t)
}
}

// The "up" and "k" keys move the cursor up
case "up", "k", "w":
return m, nil
},
},
{
key: []string{"up", "k", "w"},
help: "up",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
c := m.player.GetPos()
if c.Y > 0 {
t := common.Coordinates{
Expand All @@ -96,9 +145,14 @@ func (m model) sailingInput(msg tea.Msg) (tea.Model, tea.Cmd) {
m.player.SetPos(t)
}
}

// The "down" and "j" keys move the cursor down
case "down", "j", "s":
return m, nil
},
},
{
key: []string{"down", "j", "s"},
help: "down",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
c := m.player.GetPos()
if c.Y < m.world.GetHeight()-1 {
t := common.Coordinates{
Expand All @@ -109,9 +163,14 @@ func (m model) sailingInput(msg tea.Msg) (tea.Model, tea.Cmd) {
m.player.SetPos(t)
}
}

// The "up+left" and "y" keys move the cursor diagonal up+left
case "up+left", "y", "q":
return m, nil
},
},
{
key: []string{"q", "y"},
help: "up & left",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
c := m.player.GetPos()
if c.Y > 0 && c.X > 0 {
t := common.Coordinates{
Expand All @@ -122,9 +181,14 @@ func (m model) sailingInput(msg tea.Msg) (tea.Model, tea.Cmd) {
m.player.SetPos(t)
}
}

// The "down+left" and "b" keys move the cursor diagonal down+left
case "down+left", "b", "z":
return m, nil
},
},
{
key: []string{"b", "z"},
help: "down & left",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
c := m.player.GetPos()
if c.Y < m.world.GetHeight()-1 && c.X > 0 {
t := common.Coordinates{
Expand All @@ -135,9 +199,14 @@ func (m model) sailingInput(msg tea.Msg) (tea.Model, tea.Cmd) {
m.player.SetPos(t)
}
}

// The "upright" and "u" keys move the cursor diagonal up+left
case "up+right", "u", "e":
return m, nil
},
},
{
key: []string{"u", "e"},
help: "up & right",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
c := m.player.GetPos()
if c.Y > 0 && c.X < m.world.GetWidth()-1 {
t := common.Coordinates{
Expand All @@ -148,9 +217,14 @@ func (m model) sailingInput(msg tea.Msg) (tea.Model, tea.Cmd) {
m.player.SetPos(t)
}
}

// The "downright" and "n" keys move the cursor diagonal down+left
case "down+right", "n", "c":
return m, nil
},
},
{
key: []string{"n", "c"},
help: "down & right",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
c := m.player.GetPos()
if c.Y < m.world.GetHeight()-1 && c.X < m.world.GetWidth()-1 {
t := common.Coordinates{
Expand All @@ -161,35 +235,57 @@ func (m model) sailingInput(msg tea.Msg) (tea.Model, tea.Cmd) {
m.player.SetPos(t)
}
}
}
}
m.logger.Debug(fmt.Sprintf("Player position %v", m.player.GetPos()))
return m, nil
return m, nil
},
},
{
key: []string{"ctrl+q"},
help: "quit",
cat: KeyCatAdmin,
exec: keyQuit,
},
}

func (m model) actionInput(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {

// These keys should exit the program.
case "ctrl+c", "ctrl+q":
return m, tea.Quit
//case "p":
// if m.viewType == world.ViewTypeHeatMap {
// m.viewType = world.ViewTypeMainMap
// } else {
// m.viewType = world.ViewTypeHeatMap
// }

// exit examine mode
case "x", "return":
var examineKeyMap = KeyMap{
{
key: []string{"ctrl+q"},
help: "quit",
cat: KeyCatAdmin,
exec: keyQuit,
},
{
key: []string{"x", "enter"},
help: "exit examine mode",
cat: KeyCatAux,
exec: func(m model) (tea.Model, tea.Cmd) {
m.action = user_action.UserActionIdNone
ExamineData = user_action.Examine()

case "left", "h", "a":
return m, nil
},
},
{
key: []string{"left", "h", "a"},
help: "examine item left",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
ExamineData.FocusLeft()

case "right", "l", "d":
return m, nil
},
},
{
key: []string{"right", "l", "d"},
help: "examine item right",
cat: KeyCatNav,
exec: func(m model) (tea.Model, tea.Cmd) {
ExamineData.FocusRight()

}
}
return m, nil
return m, nil
},
},
}
Loading

0 comments on commit 440d056

Please sign in to comment.