termui/render.go

39 lines
768 B
Go
Raw Normal View History

2017-01-14 14:07:43 +08:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2015-03-21 04:21:50 +08:00
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
2015-02-03 22:07:31 +08:00
package termui
2015-09-18 23:41:44 +08:00
import (
"image"
"sync"
2015-09-18 23:41:44 +08:00
2018-09-07 05:48:09 +08:00
tb "github.com/nsf/termbox-go"
2015-09-18 23:41:44 +08:00
)
2015-02-03 22:07:31 +08:00
2019-01-24 12:12:10 +08:00
type Drawable interface {
GetRect() image.Rectangle
SetRect(int, int, int, int)
Draw(*Buffer)
sync.Locker
2019-01-24 12:12:10 +08:00
}
func Render(items ...Drawable) {
for _, item := range items {
buf := NewBuffer(item.GetRect())
item.Lock()
2019-01-24 12:12:10 +08:00
item.Draw(buf)
item.Unlock()
2019-01-24 12:12:10 +08:00
for point, cell := range buf.CellMap {
if point.In(buf.Rectangle) {
tb.SetCell(
point.X, point.Y,
cell.Rune,
tb.Attribute(cell.Style.Fg+1)|tb.Attribute(cell.Style.Modifier), tb.Attribute(cell.Style.Bg+1),
)
}
2015-02-04 09:56:49 +08:00
}
}
2018-09-07 05:48:09 +08:00
tb.Flush()
2015-02-03 22:07:31 +08:00
}