gim: add terminal.Buffer.DrawBufferCentered

This commit is contained in:
Brian Picciano 2018-06-08 00:17:49 +00:00
parent 0b36e4ec37
commit 0a6526d2c3
4 changed files with 14 additions and 10 deletions

View File

@ -61,10 +61,7 @@ func (b box) rect() geo.Rect {
func (b box) draw(buf *terminal.Buffer) { func (b box) draw(buf *terminal.Buffer) {
rect := b.rect() rect := b.rect()
buf.DrawRect(rect, terminal.SingleLine) buf.DrawRect(rect, terminal.SingleLine)
if b.bodyBuf != nil { if b.bodyBuf != nil {
center := rect.Center() buf.DrawBufferCentered(rect.Center(), b.bodyBuf)
bodyBufRect := geo.Rect{Size: b.bodyBuf.Size()}
buf.DrawBuffer(bodyBufRect.Centered(center).TopLeft, b.bodyBuf)
} }
} }

View File

@ -26,8 +26,6 @@ func (l line) draw(buf *terminal.Buffer, flowDir, secFlowDir geo.XY) {
// draw the body // draw the body
if l.bodyBuf != nil { if l.bodyBuf != nil {
mid := start.Midpoint(end) buf.DrawBufferCentered(start.Midpoint(end), l.bodyBuf)
bodyBufRect := geo.Rect{Size: l.bodyBuf.Size()}
buf.DrawBuffer(bodyBufRect.Centered(mid).TopLeft, l.bodyBuf)
} }
} }

View File

@ -90,12 +90,14 @@ func main() {
start: start, start: start,
} }
viewBuf := terminal.NewBuffer()
v.draw(viewBuf)
buf := terminal.NewBuffer() buf := terminal.NewBuffer()
v.draw(buf) buf.DrawBufferCentered(center, viewBuf)
bufRect := geo.Rect{Size: buf.Size()}.Centered(center)
term.Clear() term.Clear()
term.WriteBuffer(bufRect.TopLeft, buf) term.WriteBuffer(geo.Zero, buf)
term.SetPos(wSize.Add(geo.XY{0, -1})) term.SetPos(wSize.Add(geo.XY{0, -1}))
term.Draw() term.Draw()
} }

View File

@ -203,6 +203,13 @@ func (b *Buffer) DrawBuffer(at geo.XY, b2 *Buffer) {
}) })
} }
// DrawBufferCentered is like DrawBuffer, but centered around the given point
// instead of translated by it.
func (b *Buffer) DrawBufferCentered(around geo.XY, b2 *Buffer) {
b2rect := geo.Rect{Size: b2.Size()}
b.DrawBuffer(b2rect.Centered(around).TopLeft, b2)
}
// Size returns the dimensions of the Buffer's current area which has been // Size returns the dimensions of the Buffer's current area which has been
// written to. // written to.
func (b *Buffer) Size() geo.XY { func (b *Buffer) Size() geo.XY {