make it possible to switch between panels

This commit is contained in:
2021-07-04 14:49:36 -04:00
parent a6f958ccfb
commit 3bff06aad7
2 changed files with 64 additions and 14 deletions

View File

@@ -10,6 +10,11 @@ import (
"github.com/kelseyhightower/envconfig"
)
const (
IN_MENU = iota
IN_BOOK
)
func main() {
var c config.Config
err := envconfig.Process("library", &c)
@@ -28,21 +33,28 @@ func main() {
l := ui.NewList([]string{"foo", "bar", "baz"}, 0)
menu := ui.NewBox(
"library",
[]string{"(e)dit", "(q)uit"},
[]string{"(q)uit"},
ui.Contents{{
Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2},
Container: l,
}},
)
book := ui.NewBookDetails()
activeBook := ui.NewBox(
"book",
[]string{"test"},
ui.Contents{},
ui.Contents{{
Offsets: ui.Offsets{Top: 1, Left: 2, Bottom: -2, Right: -2},
Container: book,
}},
)
container := ui.NewContainer(
ui.Contents{{Container: menu}, {Container: activeBook}},
ui.LayoutHorizontalEven,
ui.Contents{
{Container: menu, Offsets: ui.Offsets{Percent: 1}},
{Container: activeBook, Offsets: ui.Offsets{Percent: 2}},
},
ui.LayoutHorizontalPercent,
)
// init
@@ -52,6 +64,8 @@ func main() {
container.Draw(screen)
screen.Sync()
state := IN_MENU
// UI loop
for {
e := screen.PollEvent()
@@ -59,16 +73,25 @@ func main() {
case *tcell.EventError:
screen.Beep()
case *tcell.EventKey: // input handling
if v.Key() == tcell.KeyUp && l.Selected() > 0 {
l.SetSelected(l.Selected() - 1)
}
if v.Key() == tcell.KeyDown && l.Selected() < len(l.ListMembers())-1 {
l.SetSelected(l.Selected() + 1)
}
if v.Rune() == 'q' {
screen.Fini()
fmt.Printf("Thank you for playing Wing Commander!\n\n")
return
if state == IN_MENU {
if v.Key() == tcell.KeyUp && l.Selected() > 0 {
l.SetSelected(l.Selected() - 1)
}
if v.Key() == tcell.KeyDown && l.Selected() < len(l.ListMembers())-1 {
l.SetSelected(l.Selected() + 1)
}
if v.Rune() == 'q' {
screen.Fini()
fmt.Printf("Thank you for playing Wing Commander!\n\n")
return
}
if v.Key() == tcell.KeyRight {
state = IN_BOOK
}
} else if state == IN_BOOK {
if v.Key() == tcell.KeyLeft {
state = IN_MENU
}
}
screen.Clear()
container.Draw(screen)