tui/layouts/layout_coord.go

57 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-04-24 11:41:38 +02:00
package layouts
2022-04-02 13:01:41 +02:00
2023-04-24 11:55:04 +02:00
import "git.milar.in/milarin/tui"
2022-04-02 13:01:41 +02:00
2022-04-02 15:21:17 +02:00
// CoordLayout is a tui.Layout which places its children on predefined coordinates
type CoordLayout struct {
2022-04-02 13:01:41 +02:00
tui.ViewTmpl
views map[tui.View]tui.Dimension
}
2022-04-02 15:21:17 +02:00
var _ tui.Layout = &CoordLayout{}
2022-04-02 13:01:41 +02:00
2022-04-02 15:21:17 +02:00
func NewCoordLayout() *CoordLayout {
return &CoordLayout{
2022-04-02 13:01:41 +02:00
views: map[tui.View]tui.Dimension{},
}
}
2022-04-02 15:21:17 +02:00
func (g *CoordLayout) Views() []tui.View {
2022-04-02 13:01:41 +02:00
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
2022-04-02 15:21:17 +02:00
func (g *CoordLayout) SetView(v tui.View, x, y, width, height int) {
2022-04-02 13:01:41 +02:00
g.views[v] = tui.Dimension{Point: tui.Point{X: x, Y: y}, Size: tui.Size{Width: width, Height: height}}
}
2022-04-03 17:07:15 +02:00
2022-04-02 15:21:17 +02:00
func (g *CoordLayout) Draw(buf *tui.ViewBuffer) {
2022-04-02 13:01:41 +02:00
for v, d := range g.views {
v.Draw(buf.Sub(d.X, d.Y, d.Width, d.Height))
}
}
2022-04-02 15:21:17 +02:00
func (v *CoordLayout) Layout() (prefWidth, prefHeight int) {
2022-04-02 13:01:41 +02:00
return -1, -1
}
2022-04-02 15:21:17 +02:00
func (g *CoordLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
if g.KeyPressed != nil {
return g.KeyPressed(event)
}
2022-04-02 13:01:41 +02:00
for _, view := range g.Views() {
if view.OnKeyPressed(event) {
return true
}
}
return false
}
// TODO OnMouseEvent