tui/views/coordgroup.go
Timon Ringwald dfa00f5fe3 more views
2022-04-02 13:01:41 +02:00

50 lines
1.2 KiB
Go

package views
import "git.tordarus.net/Tordarus/tui"
// CoordGroup is a tui.Group which places its children on predefined coordinates
type CoordGroup struct {
tui.ViewTmpl
views map[tui.View]tui.Dimension
}
var _ tui.Group = &CoordGroup{}
func NewCoordGroup() *CoordGroup {
return &CoordGroup{
views: map[tui.View]tui.Dimension{},
}
}
func (g *CoordGroup) 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 *CoordGroup) 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 *CoordGroup) Draw(buf *tui.ViewBuffer) {
for v, d := range g.views {
v.Draw(buf.Sub(d.X, d.Y, d.Width, d.Height))
}
}
func (v *CoordGroup) Layout() (prefWidth, prefHeight int) {
return -1, -1
}
func (g *CoordGroup) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
for _, view := range g.Views() {
if view.OnKeyPressed(event) {
return true
}
}
return false
}