So much work
This commit is contained in:
@@ -22,8 +22,6 @@ THE SOFTWARE.
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
@@ -31,16 +29,21 @@ type AbsoluteLayout struct {
|
||||
id string
|
||||
style tcell.Style
|
||||
|
||||
x, y int
|
||||
w, h int
|
||||
widgets []Widget
|
||||
wCoords map[Widget]Coord
|
||||
wAnchor map[Widget]AbsoluteAnchor
|
||||
x, y int
|
||||
w, h int
|
||||
widgets []Widget
|
||||
wCoords map[Widget]Coord
|
||||
wAnchor map[Widget]AbsoluteAnchor
|
||||
wManualSizes map[Widget]Coord
|
||||
|
||||
defAnchor AbsoluteAnchor
|
||||
|
||||
active bool
|
||||
visible bool
|
||||
active bool
|
||||
visible bool
|
||||
tabbable bool
|
||||
|
||||
cursor int
|
||||
disableTab bool
|
||||
|
||||
logger func(string)
|
||||
}
|
||||
@@ -48,14 +51,15 @@ type AbsoluteLayout struct {
|
||||
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>
|
||||
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
|
||||
)
|
||||
|
||||
@@ -72,18 +76,46 @@ func (w *AbsoluteLayout) Init(id string, s tcell.Style) {
|
||||
w.defAnchor = AnchorTL
|
||||
w.wCoords = make(map[Widget]Coord)
|
||||
w.wAnchor = make(map[Widget]AbsoluteAnchor)
|
||||
w.wManualSizes = make(map[Widget]Coord)
|
||||
w.tabbable = true
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) Id() string { return w.id }
|
||||
func (w *AbsoluteLayout) HandleResize(ev *tcell.EventResize) {
|
||||
for _, wi := range w.widgets {
|
||||
wi.HandleResize(ev)
|
||||
}
|
||||
w.w, w.h = ev.Size()
|
||||
w.updateWidgetLayouts()
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.disableTab && ev.Key() == tcell.KeyTab {
|
||||
fndP := -1
|
||||
for i := w.cursor; i < len(w.widgets); i++ {
|
||||
if fndP == -1 {
|
||||
if w.widgets[i].Active() {
|
||||
fndP = i
|
||||
w.widgets[i].SetActive(false)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if w.widgets[i].Focusable() && w.widgets[i].Tabbable() {
|
||||
w.widgets[i].SetActive(true)
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we're here, we hit the end.
|
||||
if fndP == -1 { // But didn't even find the start
|
||||
return false
|
||||
}
|
||||
for i := 0; i < fndP; i++ {
|
||||
if w.widgets[i].Focusable() && w.widgets[i].Tabbable() {
|
||||
w.widgets[i].SetActive(true)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
for _, wi := range w.widgets {
|
||||
w.Log(fmt.Sprintf("Passing key (%s) to %s", ev.Name(), wi.Id()))
|
||||
if wi.HandleKey(ev) {
|
||||
return true
|
||||
}
|
||||
@@ -101,65 +133,57 @@ func (w *AbsoluteLayout) Draw(screen tcell.Screen) {
|
||||
if !w.visible {
|
||||
return
|
||||
}
|
||||
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
|
||||
}
|
||||
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)
|
||||
p := w.GetPos()
|
||||
for _, wd := range w.widgets {
|
||||
o := wd.GetPos()
|
||||
wd.SetPos(p.Add(o))
|
||||
wd.Draw(screen)
|
||||
wd.SetPos(o)
|
||||
}
|
||||
}
|
||||
func (w *AbsoluteLayout) Active() bool { return w.active }
|
||||
func (w *AbsoluteLayout) SetActive(a bool) { w.active = a }
|
||||
func (w *AbsoluteLayout) Visible() bool { return w.visible }
|
||||
func (w *AbsoluteLayout) SetVisible(a bool) { w.visible = a }
|
||||
func (w *AbsoluteLayout) Focusable() bool { return true }
|
||||
func (w *AbsoluteLayout) SetX(x int) { w.x = x }
|
||||
func (w *AbsoluteLayout) SetY(y int) { w.y = y }
|
||||
func (w *AbsoluteLayout) GetX() int { return w.x }
|
||||
func (w *AbsoluteLayout) GetY() int { return w.y }
|
||||
func (w *AbsoluteLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *AbsoluteLayout) GetW() int { return w.w }
|
||||
func (w *AbsoluteLayout) GetH() int { return w.h }
|
||||
func (w *AbsoluteLayout) SetW(wd int) { w.w = wd }
|
||||
func (w *AbsoluteLayout) SetH(h int) { w.h = h }
|
||||
func (w *AbsoluteLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *AbsoluteLayout) WantW() int { return w.w }
|
||||
func (w *AbsoluteLayout) WantH() int { return w.h }
|
||||
func (w *AbsoluteLayout) Active() bool { return w.active }
|
||||
func (w *AbsoluteLayout) SetActive(a bool) { w.active = a }
|
||||
func (w *AbsoluteLayout) Visible() bool { return w.visible }
|
||||
func (w *AbsoluteLayout) SetVisible(a bool) { w.visible = a }
|
||||
func (w *AbsoluteLayout) Focusable() bool { return true }
|
||||
func (w *AbsoluteLayout) SetTabbable(b bool) { w.tabbable = b }
|
||||
func (w *AbsoluteLayout) Tabbable() bool { return w.tabbable }
|
||||
func (w *AbsoluteLayout) SetX(x int) { w.x = x }
|
||||
func (w *AbsoluteLayout) SetY(y int) { w.y = y }
|
||||
func (w *AbsoluteLayout) GetX() int { return w.x }
|
||||
func (w *AbsoluteLayout) GetY() int { return w.y }
|
||||
func (w *AbsoluteLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
||||
func (w *AbsoluteLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *AbsoluteLayout) GetW() int { return w.w }
|
||||
func (w *AbsoluteLayout) GetH() int { return w.h }
|
||||
func (w *AbsoluteLayout) SetW(wd int) { w.w = wd }
|
||||
func (w *AbsoluteLayout) SetH(h int) { w.h = h }
|
||||
func (w *AbsoluteLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *AbsoluteLayout) WantW() int { return w.w }
|
||||
func (w *AbsoluteLayout) WantH() int { return w.h }
|
||||
func (w *AbsoluteLayout) MinW() int {
|
||||
// Find the highest value for x in all widgets GetX() + MinW()
|
||||
var minW int
|
||||
for _, wd := range w.widgets {
|
||||
wrk := wd.GetX() + wd.MinW()
|
||||
if wrk > minW {
|
||||
minW = wrk
|
||||
}
|
||||
}
|
||||
return minW
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) MinH() int {
|
||||
// Find the highest value for y in all widgets GetY() + MinH()
|
||||
var minH int
|
||||
for _, wd := range w.widgets {
|
||||
wrk := wd.GetY() + wd.MinH()
|
||||
if wrk > minH {
|
||||
minH = wrk
|
||||
}
|
||||
}
|
||||
return minH
|
||||
}
|
||||
|
||||
// Add a widget at x/y
|
||||
func (w *AbsoluteLayout) Add(n Widget, pos Coord) { w.AddAnchored(n, pos, w.defAnchor) }
|
||||
@@ -168,6 +192,8 @@ func (w *AbsoluteLayout) AddAnchored(n Widget, pos Coord, anchor AbsoluteAnchor)
|
||||
w.widgets = append(w.widgets, n)
|
||||
w.wCoords[n] = pos
|
||||
w.wAnchor[n] = anchor
|
||||
|
||||
w.updateWidgetLayouts()
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) Clear() {
|
||||
@@ -188,3 +214,100 @@ func (w *AbsoluteLayout) Log(txt string) {
|
||||
w.logger(txt)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
for _, wd := range w.widgets {
|
||||
w.updateWidgetPos(wd)
|
||||
w.updateWidgetSize(wd)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) updateWidgetSize(wd Widget) {
|
||||
if sz, ok := w.wManualSizes[wd]; ok {
|
||||
wd.SetW(sz.X)
|
||||
wd.SetH(sz.Y)
|
||||
return
|
||||
}
|
||||
// The available space is:
|
||||
// X: Layout Width - Widget X
|
||||
// Y: Layout Height - Widget Y
|
||||
available := Coord{X: w.GetW() - wd.GetX(), Y: w.GetH() - wd.GetY()}
|
||||
ww := wd.WantW()
|
||||
if ww < available.X {
|
||||
wd.SetW(ww)
|
||||
} else if wd.MinW() < available.X {
|
||||
wd.SetW(available.X)
|
||||
} else {
|
||||
wd.SetW(wd.MinW())
|
||||
}
|
||||
|
||||
wh := wd.WantH()
|
||||
if wh < available.Y {
|
||||
wd.SetH(wh)
|
||||
} else if wd.MinH() < available.Y {
|
||||
wd.SetH(available.Y)
|
||||
} else {
|
||||
wd.SetH(wd.MinH())
|
||||
}
|
||||
}
|
||||
|
||||
// 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) getRelPos(wd Widget) Coord {
|
||||
var p Coord
|
||||
var a AbsoluteAnchor
|
||||
var ok bool
|
||||
if p, ok = w.wCoords[wd]; !ok {
|
||||
// Default to top-left corner
|
||||
p = Coord{X: 0, Y: 0}
|
||||
}
|
||||
if a, ok = w.wAnchor[wd]; !ok {
|
||||
a = w.defAnchor
|
||||
}
|
||||
midX, midY := (w.w / 2), (w.h / 2)
|
||||
switch a {
|
||||
case AnchorTL:
|
||||
return p
|
||||
|
||||
case AnchorT:
|
||||
return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: 0})
|
||||
|
||||
case AnchorTR:
|
||||
return p.Add(Coord{X: w.w - wd.GetW(), Y: 0})
|
||||
|
||||
case AnchorL:
|
||||
return p.Add(Coord{X: 0, Y: midY - (wd.GetH() / 2)})
|
||||
|
||||
case AnchorC:
|
||||
return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: midY - (wd.GetH() / 2)})
|
||||
|
||||
case AnchorR:
|
||||
return p.Add(Coord{X: w.w - wd.GetW(), Y: midY - (wd.GetH() / 2)})
|
||||
|
||||
case AnchorBR:
|
||||
return p.Add(Coord{X: w.w - wd.GetW(), Y: w.h - wd.GetH()})
|
||||
|
||||
case AnchorB:
|
||||
return p.Add(Coord{X: midX - (wd.GetW() / 2), Y: w.h - wd.GetH()})
|
||||
|
||||
case AnchorBL:
|
||||
return p.Add(Coord{X: 0, Y: w.h - wd.GetH()})
|
||||
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (w *AbsoluteLayout) getAbsPos(wd Widget) Coord {
|
||||
rel := w.getRelPos(wd)
|
||||
return rel.Add(Coord{X: w.x, Y: w.y})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user