termui/render.go

39 lines
770 B
Go
Raw Normal View History

2017-01-13 23:07:43 -07:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2022-08-28 10:46:41 -06:00
// Use of this source code is governed by a GPLv3 license that can
2015-03-20 14:21:50 -06:00
// be found in the LICENSE file.
2015-02-03 07:07:31 -07:00
package termui
2015-09-18 09:41:44 -06:00
import (
"image"
"sync"
2015-09-18 09:41:44 -06:00
2018-09-06 15:48:09 -06:00
tb "github.com/nsf/termbox-go"
2015-09-18 09:41:44 -06:00
)
2015-02-03 07:07:31 -07:00
2019-01-23 21:12:10 -07:00
type Drawable interface {
GetRect() image.Rectangle
SetRect(int, int, int, int)
Draw(*Buffer)
sync.Locker
2019-01-23 21:12:10 -07:00
}
func Render(items ...Drawable) {
for _, item := range items {
buf := NewBuffer(item.GetRect())
item.Lock()
2019-01-23 21:12:10 -07:00
item.Draw(buf)
item.Unlock()
2019-01-23 21:12:10 -07: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-03 18:56:49 -07:00
}
}
2018-09-06 15:48:09 -06:00
tb.Flush()
2015-02-03 07:07:31 -07:00
}