focussing works better now

master
Jordan Orelli 4 years ago
parent 4b58ed886f
commit 53a7e11936

@ -7,6 +7,7 @@ import (
type borderedView struct { type borderedView struct {
inner view inner view
hasFocus bool
} }
func (b borderedView) handleEvent(e tcell.Event) change { func (b borderedView) handleEvent(e tcell.Event) change {
@ -14,19 +15,31 @@ func (b borderedView) handleEvent(e tcell.Event) change {
} }
func (b borderedView) draw(img canvas, st *state) { func (b borderedView) draw(img canvas, st *state) {
s := tcell.StyleDefault
if !b.hasFocus {
s = s.Foreground(tcell.NewRGBColor(128, 128, 128))
}
bounds := img.bounds() bounds := img.bounds()
img.setTile(0, 0, tile{r: '┌'}) img.setTile(0, 0, tile{r: '┌', style: s})
img.setTile(bounds.Width-1, 0, tile{r: '┐'}) img.setTile(bounds.Width-1, 0, tile{r: '┐', style: s})
img.setTile(0, bounds.Height-1, tile{r: '└'}) img.setTile(0, bounds.Height-1, tile{r: '└', style: s})
img.setTile(bounds.Width-1, bounds.Height-1, tile{r: '┘'}) img.setTile(bounds.Width-1, bounds.Height-1, tile{r: '┘', style: s})
for x := 1; x < bounds.Width-1; x++ { for x := 1; x < bounds.Width-1; x++ {
img.setTile(x, 0, tile{r: '─'}) img.setTile(x, 0, tile{r: '─', style: s})
img.setTile(x, bounds.Height-1, tile{r: '─'}) img.setTile(x, bounds.Height-1, tile{r: '─', style: s})
} }
for y := 1; y < bounds.Height-1; y++ { for y := 1; y < bounds.Height-1; y++ {
img.setTile(0, y, tile{r: '│'}) img.setTile(0, y, tile{r: '│', style: s})
img.setTile(bounds.Width-1, y, tile{r: '│'}) img.setTile(bounds.Width-1, y, tile{r: '│', style: s})
} }
b.inner.draw(cut(img, math.Rect{math.Vec{1, 1}, bounds.Width - 2, bounds.Height - 2}), st) b.inner.draw(cut(img, math.Rect{math.Vec{1, 1}, bounds.Width - 2, bounds.Height - 2}), st)
} }
func (b *borderedView) setFocus(enabled bool) {
b.hasFocus = enabled
if v, ok := b.inner.(focusable); ok {
v.setFocus(enabled)
}
}

@ -6,11 +6,9 @@ import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/math" "github.com/jordanorelli/astro-domu/internal/math"
"github.com/jordanorelli/astro-domu/internal/sim" "github.com/jordanorelli/astro-domu/internal/sim"
"github.com/jordanorelli/blammo"
) )
type chatView struct { type chatView struct {
*blammo.Log
composing string composing string
inFocus bool inFocus bool
history []sim.ChatMessage history []sim.ChatMessage
@ -31,16 +29,16 @@ func (c *chatView) handleEvent(e tcell.Event) change {
} }
if key == tcell.KeyEnter { if key == tcell.KeyEnter {
msg := c.composing
c.composing = "" c.composing = ""
return changeFn(func(ui *UI) { return changeFn(func(ui *UI) {
// ugh lol // ugh lol
go ui.client.Send(sim.SendChatMessage{Text: c.composing}) go ui.client.Send(sim.SendChatMessage{Text: msg})
}) })
} }
if key == tcell.KeyRune { if key == tcell.KeyRune {
c.composing = fmt.Sprintf("%s%c", c.composing, t.Rune()) c.composing = fmt.Sprintf("%s%c", c.composing, t.Rune())
c.Info("composing: %v", c.composing)
} }
default: default:

@ -13,8 +13,12 @@ type containerView struct {
} }
func (c *containerView) handleEvent(e tcell.Event) change { func (c *containerView) handleEvent(e tcell.Event) change {
switch e.(type) { switch v := e.(type) {
case *tcell.EventKey: case *tcell.EventKey:
if v.Key() == tcell.KeyTab {
c.nextFocus()
return nil
}
return c.children[c.focussed].handleEvent(e) return c.children[c.focussed].handleEvent(e)
default: default:
@ -24,6 +28,28 @@ func (c *containerView) handleEvent(e tcell.Event) change {
return nil return nil
} }
func (c *containerView) nextFocus() {
setFocus := func(i int, enabled bool) bool {
n := c.children[i]
if v, ok := n.view.(focusable); ok {
v.setFocus(enabled)
return true
}
return false
}
for start, next := c.focussed, c.focussed+1; next != start; next++ {
if next >= len(c.children) {
next = 0
}
if setFocus(next, true) {
c.focussed = next
setFocus(start, false)
return
}
}
}
func (c *containerView) draw(img canvas, st *state) { func (c *containerView) draw(img canvas, st *state) {
bounds := img.bounds() bounds := img.bounds()

@ -201,12 +201,12 @@ var inGameView = &containerView{
children: []*node{ children: []*node{
{ {
frame: math.Rect{math.Vec{0, 0}, 4, 4}, frame: math.Rect{math.Vec{0, 0}, 4, 4},
view: borderedView{&gameView{}}, view: &borderedView{inner: &gameView{}},
}, },
{ {
frame: math.Rect{math.Vec{4, 0}, 4, 4}, frame: math.Rect{math.Vec{4, 0}, 4, 4},
view: borderedView{ view: &borderedView{
&menuList{ inner: &menuList{
choices: []menuItem{ choices: []menuItem{
menuItem{name: "chocolate"}, menuItem{name: "chocolate"},
menuItem{name: "vanilla"}, menuItem{name: "vanilla"},
@ -218,7 +218,7 @@ var inGameView = &containerView{
}, },
{ {
frame: math.Rect{math.Vec{0, 4}, 8, 4}, frame: math.Rect{math.Vec{0, 4}, 8, 4},
view: borderedView{inner: &chatView{}}, view: &borderedView{inner: &chatView{}},
}, },
}, },
} }

Loading…
Cancel
Save