2019-02-01 05:07:25 +00:00
|
|
|
package widgets
|
2019-01-31 00:02:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 04:39:34 +00:00
|
|
|
func (self *Layout) AddComponent(drawable Drawable, position Position, size Size, Type ComponentType) {
|
|
|
|
self.components = append(self.components, Component{drawable, position, size, Type})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Layout) GetComponents(Type ComponentType) []Drawable {
|
|
|
|
|
|
|
|
var components []Drawable
|
|
|
|
|
|
|
|
for _, component := range self.components {
|
|
|
|
if component.Type == Type {
|
|
|
|
components = append(components, component.Drawable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return components
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Layout) ChangeDimensions(width, height int) {
|
|
|
|
self.SetRect(0, 0, width, height)
|
|
|
|
}
|
|
|
|
|
2019-02-03 03:30:45 +00:00
|
|
|
func (self *Layout) Draw(buffer *Buffer) {
|
2019-01-31 00:02:38 +00:00
|
|
|
|
|
|
|
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))
|
2019-02-03 03:30:45 +00:00
|
|
|
component.Drawable.Draw(buffer)
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
}
|