sampler-fork/layout/layout.go

52 lines
1.1 KiB
Go
Raw Normal View History

2019-01-31 00:02:38 +00:00
package layout
import (
. "github.com/sqshq/termui"
)
type Layout struct {
Block
2019-01-31 01:41:51 +00:00
components []Component
2019-01-31 00:02:38 +00:00
}
const (
columnsCount = 30
rowsCount = 30
)
func NewLayout(width, height int) *Layout {
block := *NewBlock()
block.SetRect(0, 0, width, height)
return &Layout{
2019-01-31 01:41:51 +00:00
Block: block,
components: make([]Component, 0),
2019-01-31 00:02:38 +00:00
}
}
func (self *Layout) AddItem(drawable Drawable, position Position, size Size) {
2019-01-31 01:41:51 +00:00
self.components = append(self.components, Component{drawable, position, size})
2019-01-31 00:02:38 +00:00
}
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
2019-01-31 01:41:51 +00:00
for _, component := range self.components {
2019-01-31 00:02:38 +00:00
2019-01-31 01:41:51 +00:00
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
2019-01-31 00:02:38 +00:00
2019-01-31 01:41:51 +00:00
component.Drawable.SetRect(int(x1), int(y1), int(x2), int(y2))
component.Drawable.Draw(buf)
2019-01-31 00:02:38 +00:00
}
}