From bf129d9bf69dff263b146c40f019fca84e1a7d4d Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 4 Nov 2020 01:16:03 +0000 Subject: [PATCH] can now focus between views --- internal/app/chat_view.go | 40 ++++++++++++++++++++++++++++++++++++ internal/app/game_view.go | 7 +++++-- internal/app/ui.go | 43 ++++++++++++++++++++++++++++++++------- internal/app/view.go | 1 + internal/math/abs.go | 15 ++++++++++++++ 5 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 internal/app/chat_view.go diff --git a/internal/app/chat_view.go b/internal/app/chat_view.go new file mode 100644 index 0000000..5dd4159 --- /dev/null +++ b/internal/app/chat_view.go @@ -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 } diff --git a/internal/app/game_view.go b/internal/app/game_view.go index 726e983..d4d605e 100644 --- a/internal/app/game_view.go +++ b/internal/app/game_view.go @@ -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 } diff --git a/internal/app/ui.go b/internal/app/ui.go index f145f51..5cbfcd8 100644 --- a/internal/app/ui.go +++ b/internal/app/ui.go @@ -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() } diff --git a/internal/app/view.go b/internal/app/view.go index c5fb45e..0a662a3 100644 --- a/internal/app/view.go +++ b/internal/app/view.go @@ -7,4 +7,5 @@ import ( type view interface { handleEvent(*UI, tcell.Event) bool draw(*buffer) + setFocus(bool) } diff --git a/internal/math/abs.go b/internal/math/abs.go index dcb3aa9..4472870 100644 --- a/internal/math/abs.go +++ b/internal/math/abs.go @@ -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 +}