From 5b153e076270e5729358e5cb28e8216f3ad302a0 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 25 Oct 2020 02:43:47 +0000 Subject: [PATCH] breaking up the structure of this thing a bit --- point.go | 3 +++ ui.go | 68 ++++++++---------------------------------------------- ui_menu.go | 5 ++++ ui_mode.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 59 deletions(-) create mode 100644 point.go create mode 100644 ui_menu.go diff --git a/point.go b/point.go new file mode 100644 index 0000000..0d82c46 --- /dev/null +++ b/point.go @@ -0,0 +1,3 @@ +package main + +type point struct { x, y int } diff --git a/ui.go b/ui.go index 1ac81b6..108453d 100644 --- a/ui.go +++ b/ui.go @@ -1,9 +1,6 @@ package main import ( - "fmt" - "time" - "github.com/gdamore/tcell/v2" "github.com/jordanorelli/belt-mud/internal/exit" "github.com/jordanorelli/blammo" @@ -13,6 +10,7 @@ import ( type ui struct { *blammo.Log screen tcell.Screen + mode uiMode } func (ui *ui) run() { @@ -38,12 +36,10 @@ func (ui *ui) run() { screen.Fini() }() + ui.mode = &boxWalker{} ui.menu() ui.Debug("clearing screen") screen.Clear() - - time.Sleep(1 * time.Second) - } // writeString writes a string in the given style from left to right beginning @@ -69,70 +65,24 @@ func (ui *ui) menu() { ui.writeString(0, height-1, "fart", tcell.StyleDefault) ui.screen.Sync() - type point struct{ x, y int } - position := point{10, 10} - - redraw := func() { - if position.x < 2 { - position.x = 2 - } - if position.x > 11 { - position.x = 11 - } - if position.y < 2 { - position.y = 2 - } - if position.y > 10 { - position.y = 10 - } - ui.screen.Clear() - ui.writeString(1, 1, `┌──────────┐`, tcell.StyleDefault) - ui.writeString(1, 2, `│··········│`, tcell.StyleDefault) - ui.writeString(1, 3, `│··········│`, tcell.StyleDefault) - ui.writeString(1, 4, `│··········│`, tcell.StyleDefault) - ui.writeString(1, 5, `│··········│`, tcell.StyleDefault) - ui.writeString(1, 6, `│··········│`, tcell.StyleDefault) - ui.writeString(1, 7, `│··········│`, tcell.StyleDefault) - ui.writeString(1, 8, `│··········│`, tcell.StyleDefault) - ui.writeString(1, 9, `│··········│`, tcell.StyleDefault) - ui.writeString(1, 10, `│··········│`, tcell.StyleDefault) - ui.writeString(1, 11, `└──────────┘`, tcell.StyleDefault) - ui.screen.SetContent(position.x, position.y, '+', nil, tcell.StyleDefault) - ui.writeString(0, 12, fmt.Sprintf(" (%02d, %02d)", position.x, position.y), tcell.StyleDefault) - ui.screen.Show() - } - redraw() - for { e := ui.screen.PollEvent() if e == nil { break } + switch v := e.(type) { case *tcell.EventKey: - ui.Debug("screen saw key event: %v", v.Key()) key := v.Key() if key == tcell.KeyCtrlC { return } - if key == tcell.KeyRune { - switch v.Rune() { - case 'w': - position.y-- - redraw() - case 'a': - position.x-- - redraw() - case 's': - position.y++ - redraw() - case 'd': - position.x++ - redraw() - } - } - default: - ui.Debug("screen saw unhandled event of type %T", e) } + + ui.mode.HandleEvent(e) + + ui.screen.Clear() + ui.mode.draw(ui) + ui.screen.Show() } } diff --git a/ui_menu.go b/ui_menu.go new file mode 100644 index 0000000..5a85f69 --- /dev/null +++ b/ui_menu.go @@ -0,0 +1,5 @@ +package main + +type uiMenu struct { + title string +} diff --git a/ui_mode.go b/ui_mode.go index fe65cd4..b0ecad8 100644 --- a/ui_mode.go +++ b/ui_mode.go @@ -1,4 +1,68 @@ package main +import ( + "fmt" + + "github.com/gdamore/tcell/v2" +) + type uiMode interface { + HandleEvent(tcell.Event) bool + draw(*ui) +} + +type boxWalker struct { + width int + height int + position point +} + +func (m *boxWalker) HandleEvent(e tcell.Event) bool { + switch v := e.(type) { + case *tcell.EventKey: + key := v.Key() + if key == tcell.KeyRune { + switch v.Rune() { + case 'w': + m.position.y-- + case 'a': + m.position.x-- + case 's': + m.position.y++ + case 'd': + m.position.x++ + } + } + default: + // ui.Debug("screen saw unhandled event of type %T", e) + } + return true +} + +func (m *boxWalker) draw(ui *ui) { + if m.position.x < 2 { + m.position.x = 2 + } + if m.position.x > 11 { + m.position.x = 11 + } + if m.position.y < 2 { + m.position.y = 2 + } + if m.position.y > 10 { + m.position.y = 10 + } + ui.writeString(1, 1, `┌──────────┐`, tcell.StyleDefault) + ui.writeString(1, 2, `│··········│`, tcell.StyleDefault) + ui.writeString(1, 3, `│··········│`, tcell.StyleDefault) + ui.writeString(1, 4, `│··········│`, tcell.StyleDefault) + ui.writeString(1, 5, `│··········│`, tcell.StyleDefault) + ui.writeString(1, 6, `│··········│`, tcell.StyleDefault) + ui.writeString(1, 7, `│··········│`, tcell.StyleDefault) + ui.writeString(1, 8, `│··········│`, tcell.StyleDefault) + ui.writeString(1, 9, `│··········│`, tcell.StyleDefault) + ui.writeString(1, 10, `│··········│`, tcell.StyleDefault) + ui.writeString(1, 11, `└──────────┘`, tcell.StyleDefault) + ui.screen.SetContent(m.position.x, m.position.y, '@', nil, tcell.StyleDefault) + ui.writeString(0, 12, fmt.Sprintf(" (%02d, %02d)", m.position.x, m.position.y), tcell.StyleDefault) }