andlabs: now attempt grid placement

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-05-09 18:34:09 -05:00
parent d861dade77
commit 75954affce
5 changed files with 33 additions and 15 deletions

27
grid.go
View File

@ -42,23 +42,28 @@ func (n *Node) NewGrid(name string, w int, h int) *Node {
} }
// true if the grid already have a child at W,H // true if the grid already have a child at W,H
func (n *Node) gridCollision(w int, h int) bool { func (n *Node) gridCollision() bool {
for _, child := range n.children { for _, child := range n.children {
if ((child.AtW == w) && (child.AtH == h)) { if ((child.AtW == n.NextW) && (child.AtH == n.NextH)) {
return true return true
} }
} }
return false return false
} }
// increments NextW & NextH // keeps incrementing NextW & NextH until there is not a widget
func (n *Node) gridIncrement(w int, h int) bool { func (n *Node) gridIncrement() {
for _, child := range n.children { if ! n.gridCollision() {
if ((child.AtW == w) && (child.AtH == h)) { return
return true
} }
n.NextH += 1
if (n.NextH > n.Y) {
n.NextW += 1
n.NextH = 1
} }
return false
n.gridIncrement()
} }
func (n *Node) At(w int, h int) *Node { func (n *Node) At(w int, h int) *Node {
@ -69,9 +74,9 @@ func (n *Node) At(w int, h int) *Node {
n.NextW = w n.NextW = w
n.NextH = h n.NextH = h
// TODO: check for a collision here n.gridIncrement()
if n.gridCollision(w,h) { if (n.NextW != w) || (n.NextH != h) {
// TODO: find free next w,h log(logError, "At() (W,H)", w, h, " was moved to avoid a collision (W,H) =", n.NextW, n.NextH)
} }
return n return n
} }

View File

@ -12,6 +12,12 @@ func (n *Node) newNode(title string, t toolkit.WidgetType, custom func()) *Node
newN.WidgetType = t newN.WidgetType = t
newN.Custom = custom newN.Custom = custom
if n.WidgetType == toolkit.Grid {
n.gridIncrement()
}
newN.AtW = n.NextW
newN.AtH = n.NextH
n.children = append(n.children, newN) n.children = append(n.children, newN)
newN.parent = n newN.parent = n
return newN return newN

View File

@ -212,6 +212,9 @@ func newAction(n *Node, atype toolkit.ActionType) *toolkit.Action {
a.Name = n.Name a.Name = n.Name
a.Text = n.Text a.Text = n.Text
a.WidgetId = n.id a.WidgetId = n.id
if (n.parent != nil) {
a.ParentId = n.parent.id
}
a.WidgetType = n.WidgetType a.WidgetType = n.WidgetType
return &a return &a
@ -233,7 +236,7 @@ func newaction(a *toolkit.Action, n *Node, where *Node) {
if (where != nil) { if (where != nil) {
a.ParentId = where.id a.ParentId = where.id
if (where.WidgetType == toolkit.Grid) { if (where.WidgetType == toolkit.Grid) {
placeGrid(a, n, where) n.gridIncrement()
} }
} }

4
tab.go
View File

@ -22,8 +22,8 @@ func (n *Node) NewTab(text string) *Node {
log(logError, "NewTab() parent =", n.parent) log(logError, "NewTab() parent =", n.parent)
if (n.parent.WidgetType == toolkit.Root) { if (n.parent.WidgetType == toolkit.Root) {
// also broken // also broken
log(logError, "NewTab() TODO: make a window here", n) log(logError, "NewTab() TODO: make or find a window here", n)
panic("NewTab did not get passed a window") panic("NewTab() did not get passed a window")
} }
// go up the binary tree until we find a window widget to add a tab too // go up the binary tree until we find a window widget to add a tab too
return n.parent.NewTab(text) return n.parent.NewTab(text)

View File

@ -38,6 +38,10 @@ type Action struct {
X int X int
Y int Y int
// This is used for the widget's grid position
W int
H int
// Put space around elements to improve look & feel // Put space around elements to improve look & feel
Margin bool Margin bool