This commit is contained in:
2025-06-19 06:07:07 -05:00
parent 74dd092a99
commit 5988d048c1
14 changed files with 391 additions and 42 deletions

View File

@@ -35,6 +35,9 @@ type AbsoluteLayout struct {
w, h int
widgets []Widget
wCoords map[Widget]Coord
wAnchor map[Widget]AbsoluteAnchor
defAnchor AbsoluteAnchor
active bool
visible bool
@@ -42,22 +45,31 @@ type AbsoluteLayout struct {
logger func(string)
}
func (w *AbsoluteLayout) SetLogger(l func(string)) { w.logger = l }
func (w *AbsoluteLayout) Log(txt string) {
if w.logger != nil {
w.logger(txt)
}
}
type AbsoluteAnchor int
const (
AnchorTL = AbsoluteAnchor(iota) // x,y starts at <startX> <startY>
AnchorT // x,y starts at <middleX>, <startY>
AnchorTR // x,y starts at <endX>, <startY>
AnchorR // x,y starts at <endX>, <middleY>
AnchorBR // x,y starts at <endX>, <endY>
AnchorB // x,y starts at <middleX>, <endY>
AnchorBL // x,y starts at <endX>, <startY>
AnchorL // x,y starts at <startX>, <middleY>
AnchorErr
)
func NewAbsoluteLayout(id string, s tcell.Style) *AbsoluteLayout {
ret := &AbsoluteLayout{style: s}
ret.Init(id)
ret := &AbsoluteLayout{}
ret.Init(id, s)
return ret
}
func (w *AbsoluteLayout) Init(id string) {
func (w *AbsoluteLayout) Init(id string, s tcell.Style) {
w.id = id
w.style = s
w.visible = true
w.defAnchor = AnchorTL
w.wCoords = make(map[Widget]Coord)
}
@@ -90,12 +102,43 @@ func (w *AbsoluteLayout) Draw(screen tcell.Screen) {
}
for i := len(w.widgets) - 1; i >= 0; i-- {
var p Coord
var a AbsoluteAnchor
var ok bool
if p, ok = w.wCoords[w.widgets[i]]; !ok {
// Don't know where to put this widget
continue
}
w.widgets[i].SetPos(p.Add(Coord{X: w.x, Y: w.y}))
if a, ok = w.wAnchor[w.widgets[i]]; !ok {
a = w.defAnchor
}
midX := (w.x + (w.x + w.w)) / 2
midY := (w.y + (w.y + w.h)) / 2
switch a {
case AnchorTL:
w.widgets[i].SetPos(p.Add(Coord{X: w.x, Y: w.y}))
case AnchorT:
wrk := w.widgets[i].GetW() / 2
w.widgets[i].SetPos(p.Add(Coord{X: (midX - wrk), Y: w.y}))
case AnchorTR:
wrk := w.widgets[i].GetW()
w.widgets[i].SetPos(p.Add(Coord{X: w.x + w.w - wrk, Y: w.y}))
case AnchorR:
wrkYmid := w.widgets[i].GetH() / 2
wrkX := w.widgets[i].GetW()
w.widgets[i].SetPos(p.Add(Coord{X: w.x + w.w - wrkX, Y: w.y - (w.h / 2) - wrkYmid}))
case AnchorBR:
// TODO
w.widgets[i].SetPos(p.Add(Coord{X: w.x, Y: w.y}))
case AnchorB:
// TODO
w.widgets[i].SetPos(p.Add(Coord{X: w.x, Y: w.y}))
case AnchorBL:
wrkX, wrkY := (w.x+w.h)-w.widgets[i].GetH(), 0
w.widgets[i].SetPos(p.Add(Coord{X: wrkX, Y: wrkY}))
case AnchorL:
// TODO
w.widgets[i].SetPos(p.Add(Coord{X: w.x, Y: w.y}))
}
w.widgets[i].Draw(screen)
}
}
@@ -118,12 +161,29 @@ func (w *AbsoluteLayout) WantW() int { return w.w }
func (w *AbsoluteLayout) WantH() int { return w.h }
// Add a widget at x/y
func (w *AbsoluteLayout) Add(n Widget, pos Coord) {
func (w *AbsoluteLayout) Add(n Widget, pos Coord) { w.AddAnchored(n, pos, w.defAnchor) }
func (w *AbsoluteLayout) AddAnchored(n Widget, pos Coord, anchor AbsoluteAnchor) {
w.widgets = append(w.widgets, n)
w.wCoords[n] = pos
w.wAnchor[n] = anchor
}
func (w *AbsoluteLayout) Clear() {
w.widgets = []Widget{}
w.wCoords = make(map[Widget]Coord)
}
func (w *AbsoluteLayout) SetDefaultAnchor(d AbsoluteAnchor) {
if d < 0 || d > AnchorErr {
d = AnchorTL
}
w.defAnchor = d
}
func (w *AbsoluteLayout) SetLogger(l func(string)) { w.logger = l }
func (w *AbsoluteLayout) Log(txt string) {
if w.logger != nil {
w.logger(txt)
}
}