Working on Layout Stuff
This commit is contained in:
@@ -30,15 +30,16 @@ type AbsoluteLayout struct {
|
||||
id string
|
||||
style tcell.Style
|
||||
|
||||
x, y int
|
||||
w, h int
|
||||
bordered bool
|
||||
widgets []Widget
|
||||
wCoords map[Widget]Coord
|
||||
wAnchor map[Widget]AbsoluteAnchor
|
||||
wManualSizes map[Widget]Coord
|
||||
x, y int
|
||||
w, h int
|
||||
bordered bool
|
||||
widgets []Widget
|
||||
wCoords map[Widget]Coord
|
||||
wAnchor map[Widget]LayoutFlag
|
||||
wManualSizes map[Widget]Coord
|
||||
wDynamicSizes map[Widget]LayoutFlag
|
||||
|
||||
defAnchor AbsoluteAnchor
|
||||
defAnchor LayoutFlag
|
||||
|
||||
active bool
|
||||
visible bool
|
||||
@@ -52,21 +53,6 @@ type AbsoluteLayout struct {
|
||||
logger func(string)
|
||||
}
|
||||
|
||||
type AbsoluteAnchor int
|
||||
|
||||
const (
|
||||
AnchorTL = AbsoluteAnchor(iota) // widget starts at <startX>, <startY>
|
||||
AnchorT // widget starts at <middleX>-<widget width/2>, <startY>
|
||||
AnchorTR // widget starts at <endX>, <startY>
|
||||
AnchorL // widget starts at <startX>, <middleY>
|
||||
AnchorC // widget starts at <middleX>-<widget width/2>, <middleY>-<widget height/2>
|
||||
AnchorR // widget starts at <endX>-<widget width>, <middleY>-<widget height/2>
|
||||
AnchorBL // widget starts at <startX>, <endY>-<widget height>
|
||||
AnchorB // widget starts at <middleX>-<widget width/2>, <endY>-<widget height>
|
||||
AnchorBR // widget starts at <endX>-<widget width>, <endY>-<widget height>
|
||||
AnchorErr
|
||||
)
|
||||
|
||||
var _ Widget = (*AbsoluteLayout)(nil)
|
||||
|
||||
func NewAbsoluteLayout(id string, s tcell.Style) *AbsoluteLayout {
|
||||
@@ -79,11 +65,12 @@ func (w *AbsoluteLayout) Init(id string, s tcell.Style) {
|
||||
w.id = id
|
||||
w.style = s
|
||||
w.visible = true
|
||||
w.defAnchor = AnchorTL
|
||||
w.defAnchor = LFAlignCenter
|
||||
w.keyMap = BlankKeyMap()
|
||||
w.wCoords = make(map[Widget]Coord)
|
||||
w.wAnchor = make(map[Widget]AbsoluteAnchor)
|
||||
w.wAnchor = make(map[Widget]LayoutFlag)
|
||||
w.wManualSizes = make(map[Widget]Coord)
|
||||
w.wDynamicSizes = make(map[Widget]LayoutFlag)
|
||||
w.focusable = true
|
||||
}
|
||||
|
||||
@@ -207,7 +194,7 @@ func (w *AbsoluteLayout) MinH() int {
|
||||
// Add a widget at x/y
|
||||
func (w *AbsoluteLayout) Add(n Widget, pos Coord) { w.AddAnchored(n, pos, w.defAnchor) }
|
||||
|
||||
func (w *AbsoluteLayout) AddAnchored(n Widget, pos Coord, anchor AbsoluteAnchor) {
|
||||
func (w *AbsoluteLayout) AddAnchored(n Widget, pos Coord, anchor LayoutFlag) {
|
||||
w.widgets = append(w.widgets, n)
|
||||
w.wCoords[n] = pos
|
||||
w.wAnchor[n] = anchor
|
||||
@@ -220,12 +207,7 @@ func (w *AbsoluteLayout) Clear() {
|
||||
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) SetDefaultAnchor(d LayoutFlag) { w.defAnchor = d }
|
||||
|
||||
func (w *AbsoluteLayout) SetLogger(l func(string)) { w.logger = l }
|
||||
func (w *AbsoluteLayout) Log(txt string) {
|
||||
@@ -238,11 +220,26 @@ func (w *AbsoluteLayout) SetBordered(b bool) { w.bordered = b }
|
||||
|
||||
func (w *AbsoluteLayout) updateWidgetLayouts() {
|
||||
// In an Absolute Layout, widgets are given a definite position and anchor.
|
||||
// The anchor is a side of the layout (see AbsoluteAnchor type)
|
||||
// The anchor is a side of the layout (see LayoutFlag type)
|
||||
|
||||
// Process:
|
||||
// 1. Update pos of all widgets
|
||||
// 2. Update manually sized widgets
|
||||
// 3. Update dynamically sized widgets
|
||||
for _, wd := range w.widgets {
|
||||
w.updateWidgetPos(wd)
|
||||
}
|
||||
for wd := range w.wManualSizes {
|
||||
w.updateWidgetSize(wd)
|
||||
}
|
||||
for wd := range w.wDynamicSizes {
|
||||
w.updateWidgetSize(wd)
|
||||
}
|
||||
}
|
||||
|
||||
// Set a widgets position relative to the layout
|
||||
func (w *AbsoluteLayout) updateWidgetPos(wd Widget) {
|
||||
wd.SetPos(w.getRelPos(wd))
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) updateWidgetSize(wd Widget) {
|
||||
@@ -274,21 +271,13 @@ func (w *AbsoluteLayout) updateWidgetSize(wd Widget) {
|
||||
}
|
||||
}
|
||||
|
||||
// Set a widgets position relative to the layout
|
||||
func (w *AbsoluteLayout) updateWidgetPos(wd Widget) {
|
||||
wd.SetPos(w.getRelPos(wd))
|
||||
}
|
||||
|
||||
// Manually set the size of a widget, the Layout won't override it
|
||||
func (w *AbsoluteLayout) SetWidgetSize(wd Widget, sz Coord) { w.wManualSizes[wd] = sz }
|
||||
|
||||
func (w *AbsoluteLayout) ShrinkWrap(wd Widget) {
|
||||
w.SetWidgetSize(wd, Coord{X: wd.MinW(), Y: wd.MinH()})
|
||||
}
|
||||
func (w *AbsoluteLayout) SetWidgetSize(wd Widget, sz Coord) { w.wManualSizes[wd] = sz }
|
||||
func (w *AbsoluteLayout) SetDynamicWidgetSize(wd Widget, flg LayoutFlag) { w.wDynamicSizes[wd] = flg }
|
||||
|
||||
func (w *AbsoluteLayout) getRelPos(wd Widget) Coord {
|
||||
var p Coord
|
||||
var a AbsoluteAnchor
|
||||
var a LayoutFlag
|
||||
var ok bool
|
||||
if p, ok = w.wCoords[wd]; !ok {
|
||||
// Default to top-left corner
|
||||
@@ -306,32 +295,32 @@ func (w *AbsoluteLayout) getRelPos(wd Widget) Coord {
|
||||
}
|
||||
midX, midY := (w.w / 2), (w.h / 2)
|
||||
switch a {
|
||||
case AnchorTL:
|
||||
case LFAlignTopLeft:
|
||||
return p.Add(Coord{X: leftX, Y: topY})
|
||||
|
||||
case AnchorT:
|
||||
case LFAlignTop:
|
||||
return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: topY})
|
||||
|
||||
case AnchorTR:
|
||||
case LFAlignTopRight:
|
||||
return p.Add(Coord{X: rightX - wd.GetW(), Y: topY})
|
||||
|
||||
case AnchorL:
|
||||
case LFAlignLeft:
|
||||
return p.Add(Coord{X: leftX, Y: midY - (wd.GetH() / 2)})
|
||||
|
||||
case AnchorC:
|
||||
case LFAlignCenter:
|
||||
return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: midY - (wd.GetH() / 2)})
|
||||
|
||||
case AnchorR:
|
||||
case LFAlignRight:
|
||||
return p.Add(Coord{X: rightX - wd.GetW(), Y: midY - (wd.GetH() / 2)})
|
||||
|
||||
case AnchorBR:
|
||||
return p.Add(Coord{X: rightX - wd.GetW(), Y: bottomY - wd.GetH()})
|
||||
case LFAlignBottomLeft:
|
||||
return p.Add(Coord{X: leftX, Y: bottomY - wd.GetH()})
|
||||
|
||||
case AnchorB:
|
||||
case LFAlignBottom:
|
||||
return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: bottomY - wd.GetH()})
|
||||
|
||||
case AnchorBL:
|
||||
return p.Add(Coord{X: leftX, Y: bottomY - wd.GetH()})
|
||||
case LFAlignBottomRight:
|
||||
return p.Add(Coord{X: rightX - wd.GetW(), Y: bottomY - wd.GetH()})
|
||||
|
||||
}
|
||||
return p
|
||||
@@ -341,3 +330,55 @@ func (w *AbsoluteLayout) getAbsPos(wd Widget) Coord {
|
||||
rel := w.getRelPos(wd)
|
||||
return rel.Add(Coord{X: w.x, Y: w.y})
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) DeleteIndex(idx int) {
|
||||
if idx < len(w.widgets) {
|
||||
p := w.widgets[idx]
|
||||
w.widgets = append(w.widgets[:idx], w.widgets[idx+1:]...)
|
||||
delete(w.wCoords, p)
|
||||
delete(w.wAnchor, p)
|
||||
}
|
||||
}
|
||||
func (w *AbsoluteLayout) Delete(n Widget) {
|
||||
for i := 0; i < len(w.widgets); i++ {
|
||||
if w.widgets[i] == n {
|
||||
w.DeleteIndex(i)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
func (w *AbsoluteLayout) IndexOf(n Widget) int {
|
||||
for i := range w.widgets {
|
||||
if w.widgets[i] == n {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) ActivateWidget(n Widget) {
|
||||
for i := range w.widgets {
|
||||
w.widgets[i].SetActive(w.widgets[i] == n)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) FindById(id string) Widget {
|
||||
for i := range w.widgets {
|
||||
if w.widgets[i].Id() == id {
|
||||
return w.widgets[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) Replace(n, with Widget) {
|
||||
idx := w.IndexOf(n)
|
||||
if idx == -1 {
|
||||
// 'n' isn't in layout. Bail out.
|
||||
return
|
||||
}
|
||||
coords := w.wCoords[n]
|
||||
anchor := w.wAnchor[n]
|
||||
w.Delete(n)
|
||||
w.AddAnchored(with, coords, anchor)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user