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) {
rect := b.rect()
buf.DrawRect(rect, terminal.SingleLine)
if b.bodyBuf != nil {
center := rect.Center()
bodyBufRect := geo.Rect{Size: b.bodyBuf.Size()}
buf.DrawBuffer(bodyBufRect.Centered(center).TopLeft, b.bodyBuf)
buf.DrawBufferCentered(rect.Center(), b.bodyBuf)
}
}

View File

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

View File

@ -90,12 +90,14 @@ func main() {
start: start,
}
viewBuf := terminal.NewBuffer()
v.draw(viewBuf)
buf := terminal.NewBuffer()
v.draw(buf)
bufRect := geo.Rect{Size: buf.Size()}.Centered(center)
buf.DrawBufferCentered(center, viewBuf)
term.Clear()
term.WriteBuffer(bufRect.TopLeft, buf)
term.WriteBuffer(geo.Zero, buf)
term.SetPos(wSize.Add(geo.XY{0, -1}))
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
// written to.
func (b *Buffer) Size() geo.XY {