ok how do i program a UI though

master
Jordan Orelli 4 years ago
parent 8ab25870d0
commit f7b044d428

@ -0,0 +1,45 @@
package app
import (
"github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/math"
"github.com/jordanorelli/blammo"
)
type menuList struct {
*blammo.Log
choices []string
selected int
}
func (m *menuList) handleEvent(ui *UI, e tcell.Event) bool {
switch t := e.(type) {
case *tcell.EventKey:
key := t.Key()
switch key {
case tcell.KeyDown:
m.selected = (m.selected + 1) % len(m.choices)
case tcell.KeyUp:
if m.selected == 0 {
m.selected = len(m.choices) - 1
} else {
m.selected--
}
}
default:
// ui.Debug("screen saw unhandled event of type %T", e)
}
return true
}
func (m *menuList) draw(b *buffer) {
for i, choice := range m.choices {
if i == m.selected {
b.writeString("▷ "+choice, math.Vec{0, i}, tcell.StyleDefault)
} else {
b.writeString(" "+choice, math.Vec{0, i}, tcell.StyleDefault)
}
}
}
func (m *menuList) setFocus(bool) {}

@ -20,6 +20,7 @@ type UI struct {
statusBar *statusBar
gameView *gameView
testList *menuList
chatView *chatView
focussed view
}
@ -63,6 +64,10 @@ func (ui *UI) Run() {
history: make([]sim.ChatMessage, 0, 32),
}
ui.statusBar = &statusBar{}
ui.testList = &menuList{
Log: ui.Child("menu-list"),
choices: []string{"apple", "banana", "orange"},
}
ui.focussed = ui.gameView
ui.Info("running ui")
@ -217,9 +222,12 @@ func (ui *UI) handleUserInput() bool {
if key == tcell.KeyTab {
ui.Info("saw tab from keyboard input, switching focussed view")
ui.focussed.setFocus(false)
if ui.focussed == ui.gameView {
switch ui.focussed {
case ui.gameView:
ui.focussed = ui.chatView
} else {
case ui.chatView:
ui.focussed = ui.testList
case ui.testList:
ui.focussed = ui.gameView
}
ui.focussed.setFocus(true)
@ -252,7 +260,11 @@ func (ui *UI) render() {
ui.gameView.draw(b)
b.blit(ui.screen, math.Vec{0, 1})
}
{
b := newBuffer(width/2, gameViewHeight)
ui.testList.draw(b)
b.blit(ui.screen, math.Vec{width / 2, 1})
}
{
b := newBuffer(width, height-gameViewHeight-1)
ui.chatView.draw(b)

Loading…
Cancel
Save