termui/block.go

69 lines
1.0 KiB
Go
Raw Normal View History

2015-02-03 12:13:51 -07:00
package termui
2015-02-03 18:56:49 -07:00
type Block struct {
2015-02-03 12:13:51 -07:00
X int
Y int
2015-02-03 18:56:49 -07:00
Border labeledBorder
2015-02-03 12:13:51 -07:00
IsDisplay bool
HasBorder bool
2015-02-03 18:56:49 -07:00
BgColor Attribute
2015-02-03 12:13:51 -07:00
Width int
Height int
innerWidth int
innerHeight int
innerX int
innerY int
}
2015-02-03 18:56:49 -07:00
func NewBlock() *Block {
d := Block{}
2015-02-03 12:13:51 -07:00
d.IsDisplay = true
d.HasBorder = true
d.Width = 2
d.Height = 2
2015-02-03 18:56:49 -07:00
return &d
2015-02-03 12:13:51 -07:00
}
2015-02-03 18:56:49 -07:00
func (d *Block) align() {
2015-02-03 12:13:51 -07:00
d.innerWidth = d.Width
d.innerHeight = d.Height
d.innerX = d.X
d.innerY = d.Y
if d.HasBorder {
d.innerHeight -= 2
d.innerWidth -= 2
d.Border.X = d.X
d.Border.Y = d.Y
d.Border.Width = d.Width
d.Border.Height = d.Height
d.innerX += 1
d.innerY += 1
}
}
2015-02-03 18:56:49 -07:00
func (d *Block) Buffer() []Point {
d.align()
2015-02-03 12:13:51 -07:00
ps := []Point{}
if !d.IsDisplay {
return ps
}
if d.HasBorder {
ps = d.Border.Buffer()
}
for i := 0; i < d.innerWidth; i++ {
for j := 0; j < d.innerHeight; j++ {
p := Point{}
p.X = d.X + 1 + i
p.Y = d.Y + 1 + j
p.Code.Ch = ' '
2015-02-03 18:56:49 -07:00
p.Code.Bg = toTmAttr(d.BgColor)
2015-02-03 12:13:51 -07:00
ps = append(ps, p)
}
}
return ps
}