From 53a7e11936a7e5a7bae85392c9e0ba58cd3a0ee9 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 9 Nov 2020 20:21:33 +0000 Subject: [PATCH] focussing works better now --- internal/app/border_view.go | 31 ++++++++++++++++++++++--------- internal/app/chat_view.go | 6 ++---- internal/app/container_view.go | 28 +++++++++++++++++++++++++++- internal/app/ui.go | 8 ++++---- 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/internal/app/border_view.go b/internal/app/border_view.go index a85bc7c..f44fece 100644 --- a/internal/app/border_view.go +++ b/internal/app/border_view.go @@ -6,7 +6,8 @@ import ( ) type borderedView struct { - inner view + inner view + hasFocus bool } 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) { + s := tcell.StyleDefault + if !b.hasFocus { + s = s.Foreground(tcell.NewRGBColor(128, 128, 128)) + } + bounds := img.bounds() - img.setTile(0, 0, tile{r: '┌'}) - img.setTile(bounds.Width-1, 0, tile{r: '┐'}) - img.setTile(0, bounds.Height-1, tile{r: '└'}) - img.setTile(bounds.Width-1, bounds.Height-1, tile{r: '┘'}) + img.setTile(0, 0, tile{r: '┌', style: s}) + img.setTile(bounds.Width-1, 0, tile{r: '┐', style: s}) + img.setTile(0, bounds.Height-1, tile{r: '└', style: s}) + img.setTile(bounds.Width-1, bounds.Height-1, tile{r: '┘', style: s}) for x := 1; x < bounds.Width-1; x++ { - img.setTile(x, 0, tile{r: '─'}) - img.setTile(x, bounds.Height-1, tile{r: '─'}) + img.setTile(x, 0, tile{r: '─', style: s}) + img.setTile(x, bounds.Height-1, tile{r: '─', style: s}) } for y := 1; y < bounds.Height-1; y++ { - img.setTile(0, y, tile{r: '│'}) - img.setTile(bounds.Width-1, y, tile{r: '│'}) + img.setTile(0, y, tile{r: '│', style: s}) + 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) } + +func (b *borderedView) setFocus(enabled bool) { + b.hasFocus = enabled + if v, ok := b.inner.(focusable); ok { + v.setFocus(enabled) + } +} diff --git a/internal/app/chat_view.go b/internal/app/chat_view.go index 4d76d81..ee7def4 100644 --- a/internal/app/chat_view.go +++ b/internal/app/chat_view.go @@ -6,11 +6,9 @@ import ( "github.com/gdamore/tcell/v2" "github.com/jordanorelli/astro-domu/internal/math" "github.com/jordanorelli/astro-domu/internal/sim" - "github.com/jordanorelli/blammo" ) type chatView struct { - *blammo.Log composing string inFocus bool history []sim.ChatMessage @@ -31,16 +29,16 @@ func (c *chatView) handleEvent(e tcell.Event) change { } if key == tcell.KeyEnter { + msg := c.composing c.composing = "" return changeFn(func(ui *UI) { // ugh lol - go ui.client.Send(sim.SendChatMessage{Text: c.composing}) + go ui.client.Send(sim.SendChatMessage{Text: msg}) }) } if key == tcell.KeyRune { c.composing = fmt.Sprintf("%s%c", c.composing, t.Rune()) - c.Info("composing: %v", c.composing) } default: diff --git a/internal/app/container_view.go b/internal/app/container_view.go index 3626f4b..8d69ca9 100644 --- a/internal/app/container_view.go +++ b/internal/app/container_view.go @@ -13,8 +13,12 @@ type containerView struct { } func (c *containerView) handleEvent(e tcell.Event) change { - switch e.(type) { + switch v := e.(type) { case *tcell.EventKey: + if v.Key() == tcell.KeyTab { + c.nextFocus() + return nil + } return c.children[c.focussed].handleEvent(e) default: @@ -24,6 +28,28 @@ func (c *containerView) handleEvent(e tcell.Event) change { 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) { bounds := img.bounds() diff --git a/internal/app/ui.go b/internal/app/ui.go index ad126d7..45a9558 100644 --- a/internal/app/ui.go +++ b/internal/app/ui.go @@ -201,12 +201,12 @@ var inGameView = &containerView{ children: []*node{ { 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}, - view: borderedView{ - &menuList{ + view: &borderedView{ + inner: &menuList{ choices: []menuItem{ menuItem{name: "chocolate"}, menuItem{name: "vanilla"}, @@ -218,7 +218,7 @@ var inGameView = &containerView{ }, { frame: math.Rect{math.Vec{0, 4}, 8, 4}, - view: borderedView{inner: &chatView{}}, + view: &borderedView{inner: &chatView{}}, }, }, }