breaking up the structure of this thing a bit

master
Jordan Orelli 4 years ago
parent 041b8ffd51
commit 5b153e0762

@ -0,0 +1,3 @@
package main
type point struct { x, y int }

68
ui.go

@ -1,9 +1,6 @@
package main package main
import ( import (
"fmt"
"time"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/belt-mud/internal/exit" "github.com/jordanorelli/belt-mud/internal/exit"
"github.com/jordanorelli/blammo" "github.com/jordanorelli/blammo"
@ -13,6 +10,7 @@ import (
type ui struct { type ui struct {
*blammo.Log *blammo.Log
screen tcell.Screen screen tcell.Screen
mode uiMode
} }
func (ui *ui) run() { func (ui *ui) run() {
@ -38,12 +36,10 @@ func (ui *ui) run() {
screen.Fini() screen.Fini()
}() }()
ui.mode = &boxWalker{}
ui.menu() ui.menu()
ui.Debug("clearing screen") ui.Debug("clearing screen")
screen.Clear() screen.Clear()
time.Sleep(1 * time.Second)
} }
// writeString writes a string in the given style from left to right beginning // 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.writeString(0, height-1, "fart", tcell.StyleDefault)
ui.screen.Sync() 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 { for {
e := ui.screen.PollEvent() e := ui.screen.PollEvent()
if e == nil { if e == nil {
break break
} }
switch v := e.(type) { switch v := e.(type) {
case *tcell.EventKey: case *tcell.EventKey:
ui.Debug("screen saw key event: %v", v.Key())
key := v.Key() key := v.Key()
if key == tcell.KeyCtrlC { if key == tcell.KeyCtrlC {
return 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()
} }
} }

@ -0,0 +1,5 @@
package main
type uiMenu struct {
title string
}

@ -1,4 +1,68 @@
package main package main
import (
"fmt"
"github.com/gdamore/tcell/v2"
)
type uiMode interface { 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)
} }

Loading…
Cancel
Save