52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package layout
|
|
|
|
import (
|
|
. "github.com/sqshq/termui"
|
|
)
|
|
|
|
type Layout struct {
|
|
Block
|
|
components []Component
|
|
}
|
|
|
|
const (
|
|
columnsCount = 30
|
|
rowsCount = 30
|
|
)
|
|
|
|
func NewLayout(width, height int) *Layout {
|
|
|
|
block := *NewBlock()
|
|
block.SetRect(0, 0, width, height)
|
|
|
|
return &Layout{
|
|
Block: block,
|
|
components: make([]Component, 0),
|
|
}
|
|
}
|
|
|
|
func (self *Layout) AddItem(drawable Drawable, position Position, size Size) {
|
|
self.components = append(self.components, Component{drawable, position, size})
|
|
}
|
|
|
|
func (self *Layout) ChangeDimensions(width, height int) {
|
|
self.SetRect(0, 0, width, height)
|
|
}
|
|
|
|
func (self *Layout) Draw(buf *Buffer) {
|
|
|
|
columnWidth := float64(self.GetRect().Dx()) / columnsCount
|
|
rowHeight := float64(self.GetRect().Dy()) / rowsCount
|
|
|
|
for _, component := range self.components {
|
|
|
|
x1 := float64(component.Position.X) * columnWidth
|
|
y1 := float64(component.Position.Y) * rowHeight
|
|
x2 := x1 + float64(component.Size.X)*columnWidth
|
|
y2 := y1 + float64(component.Size.Y)*rowHeight
|
|
|
|
component.Drawable.SetRect(int(x1), int(y1), int(x2), int(y2))
|
|
component.Drawable.Draw(buf)
|
|
}
|
|
}
|