tui/layouts/layout_coord.go
2023-04-24 11:41:38 +02:00

53 lines
1.2 KiB
Go

package layouts
import "git.tordarus.net/Tordarus/tui"
// CoordLayout is a tui.Layout which places its children on predefined coordinates
type CoordLayout struct {
tui.ViewTmpl
views map[tui.View]tui.Dimension
}
var _ tui.Layout = &CoordLayout{}
func NewCoordLayout() *CoordLayout {
return &CoordLayout{
views: map[tui.View]tui.Dimension{},
}
}
func (g *CoordLayout) Views() []tui.View {
s := make([]tui.View, 0, len(g.views))
for v := range g.views {
s = append(s, v)
}
return s
}
// SetView places v at the given coordinates with the given dimensions.
// v will be added to g's children if not already added before
func (g *CoordLayout) SetView(v tui.View, x, y, width, height int) {
g.views[v] = tui.Dimension{Point: tui.Point{X: x, Y: y}, Size: tui.Size{Width: width, Height: height}}
}
func (g *CoordLayout) Draw(buf *tui.ViewBuffer) {
for v, d := range g.views {
v.Draw(buf.Sub(d.X, d.Y, d.Width, d.Height))
}
}
func (v *CoordLayout) Layout() (prefWidth, prefHeight int) {
return -1, -1
}
func (g *CoordLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
for _, view := range g.Views() {
if view.OnKeyPressed(event) {
return true
}
}
return false
}
// TODO OnMouseEvent