refactoring

master
Jordan Orelli 4 years ago
parent 5b153e0762
commit 8e27c1c6c8

@ -1,3 +1,13 @@
package main package main
type point struct { x, y int } type point struct{ x, y int }
func clamp(n, min, max int) int {
if n < min {
return min
}
if n > max {
return max
}
return n
}

@ -36,7 +36,10 @@ func (ui *ui) run() {
screen.Fini() screen.Fini()
}() }()
ui.mode = &boxWalker{} ui.mode = &boxWalker{
width: 10,
height: 6,
}
ui.menu() ui.menu()
ui.Debug("clearing screen") ui.Debug("clearing screen")
screen.Clear() screen.Clear()

@ -1,8 +1,6 @@
package main package main
import ( import (
"fmt"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
) )
@ -24,13 +22,13 @@ func (m *boxWalker) HandleEvent(e tcell.Event) bool {
if key == tcell.KeyRune { if key == tcell.KeyRune {
switch v.Rune() { switch v.Rune() {
case 'w': case 'w':
m.position.y-- m.move(0, -1)
case 'a': case 'a':
m.position.x-- m.move(-1, 0)
case 's': case 's':
m.position.y++ m.move(0, 1)
case 'd': case 'd':
m.position.x++ m.move(1, 0)
} }
} }
default: default:
@ -39,30 +37,35 @@ func (m *boxWalker) HandleEvent(e tcell.Event) bool {
return true return true
} }
func (m *boxWalker) move(dx, dy int) {
m.position.x = clamp(m.position.x+dx, 0, m.width-1)
m.position.y = clamp(m.position.y+dy, 0, m.height-1)
}
func (m *boxWalker) draw(ui *ui) { func (m *boxWalker) draw(ui *ui) {
if m.position.x < 2 { offset := point{1, 1}
m.position.x = 2
} // fill in background dots first
if m.position.x > 11 { for x := 0; x < m.width; x++ {
m.position.x = 11 for y := 0; y < m.height; y++ {
ui.screen.SetContent(x+offset.x, y+offset.y, '·', nil, tcell.StyleDefault)
}
} }
if m.position.y < 2 {
m.position.y = 2 // frame it
ui.screen.SetContent(offset.x-1, offset.y-1, '┌', nil, tcell.StyleDefault)
ui.screen.SetContent(offset.x+m.width, offset.y-1, '┐', nil, tcell.StyleDefault)
ui.screen.SetContent(offset.x-1, offset.y+m.height, '└', nil, tcell.StyleDefault)
ui.screen.SetContent(offset.x+m.width, offset.y+m.height, '┘', nil, tcell.StyleDefault)
for x := 0; x < m.width; x++ {
ui.screen.SetContent(x+offset.x, offset.y-1, '─', nil, tcell.StyleDefault)
ui.screen.SetContent(x+offset.x, offset.y+m.height, '─', nil, tcell.StyleDefault)
} }
if m.position.y > 10 { for y := 0; y < m.height; y++ {
m.position.y = 10 ui.screen.SetContent(offset.x-1, y+offset.y, '│', nil, tcell.StyleDefault)
ui.screen.SetContent(offset.x+m.width, y+offset.y, '│', nil, tcell.StyleDefault)
} }
ui.writeString(1, 1, `┌──────────┐`, tcell.StyleDefault)
ui.writeString(1, 2, `│··········│`, tcell.StyleDefault) // add all characters
ui.writeString(1, 3, `│··········│`, tcell.StyleDefault) ui.screen.SetContent(m.position.x+offset.x, m.position.y+offset.y, '@', nil, 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