can now focus between views

master
Jordan Orelli 4 years ago
parent 96537b98a9
commit bf129d9bf6

@ -0,0 +1,40 @@
package app
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/math"
"github.com/jordanorelli/blammo"
)
type chatView struct {
*blammo.Log
composing string
inFocus bool
}
func (c *chatView) handleEvent(ui *UI, e tcell.Event) bool {
switch t := e.(type) {
case *tcell.EventKey:
key := t.Key()
if key == tcell.KeyRune {
c.composing = fmt.Sprintf("%s%c", c.composing, t.Rune())
c.Info("composing: %v", c.composing)
}
default:
// ui.Debug("screen saw unhandled event of type %T", e)
}
return false
}
func (c *chatView) draw(b *buffer) {
b.writeString(c.composing, math.Vec{0, b.height - 1}, tcell.StyleDefault)
if c.inFocus {
b.set(len([]rune(c.composing)), b.height-1, tile{r: tcell.RuneBlock, style: tcell.StyleDefault.Blink(true).Foreground(tcell.NewRGBColor(255, 0, 0))})
}
}
func (c *chatView) setFocus(yes bool) { c.inFocus = yes }

@ -9,8 +9,9 @@ import (
type gameView struct {
*blammo.Log
room *wire.Room
me *wire.Entity
room *wire.Room
me *wire.Entity
inFocus bool
}
func (v *gameView) handleEvent(ui *UI, e tcell.Event) bool {
@ -84,3 +85,5 @@ func (v *gameView) drawHeader(b *buffer) {
}
}
}
func (v *gameView) setFocus(yes bool) { v.inFocus = yes }

@ -14,9 +14,12 @@ type UI struct {
*blammo.Log
PlayerName string
screen tcell.Screen
view view
room *wire.Room
client *wire.Client
gameView view
chatView view
focussed view
}
func (ui *UI) Run() {
@ -44,7 +47,7 @@ func (ui *UI) Run() {
meta := welcome.Players[ui.PlayerName]
room := welcome.Rooms[meta.Room]
ui.room = &room
ui.view = &gameView{
ui.gameView = &gameView{
Log: ui.Child("game-view"),
room: &room,
me: &wire.Entity{
@ -53,6 +56,10 @@ func (ui *UI) Run() {
Position: room.Entities[meta.Avatar].Position,
},
}
ui.chatView = &chatView{
Log: ui.Child("chat-view"),
}
ui.focussed = ui.gameView
ui.Info("running ui")
if ui.handleUserInput() {
@ -107,7 +114,7 @@ func (ui *UI) clearTerminal() {
func (ui *UI) handleNotifications(c <-chan wire.Response) {
for n := range c {
if ui.handleNotification(n.Body) {
if ui.view != nil {
if ui.gameView != nil {
ui.render()
}
}
@ -176,19 +183,41 @@ func (ui *UI) handleUserInput() bool {
// we want to shut things down
return true
}
if key == tcell.KeyTab {
ui.Info("saw tab from keyboard input, switching focussed view")
ui.focussed.setFocus(false)
if ui.focussed == ui.gameView {
ui.focussed = ui.chatView
} else {
ui.focussed = ui.gameView
}
ui.focussed.setFocus(true)
goto HANDLED
}
}
ui.view.handleEvent(ui, e)
ui.focussed.handleEvent(ui, e)
ui.screen.Clear()
ui.render()
ui.screen.Show()
HANDLED:
}
}
func (ui *UI) render() {
width, height := ui.screen.Size()
b := newBuffer(width, height)
ui.view.draw(b)
b.blit(ui.screen, math.Vec{1, 1})
{
b := newBuffer(width/2, height/2)
ui.gameView.draw(b)
b.blit(ui.screen, math.Vec{0, 0})
}
{
b := newBuffer(width, height/2)
ui.chatView.draw(b)
b.blit(ui.screen, math.Vec{0, height / 2})
}
ui.screen.Show()
}

@ -7,4 +7,5 @@ import (
type view interface {
handleEvent(*UI, tcell.Event) bool
draw(*buffer)
setFocus(bool)
}

@ -6,3 +6,18 @@ func Abs(n int) int {
}
return -n
}
func Max(a, b int, more ...int) int {
var v int
if a > b {
v = a
} else {
v = b
}
for _, next := range more {
if next > v {
v = next
}
}
return v
}

Loading…
Cancel
Save