2023-03-03 14:41:38 -06:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2024-01-03 18:15:54 -06:00
|
|
|
"go.wit.com/log"
|
2024-01-05 13:18:44 -06:00
|
|
|
"go.wit.com/gui/widget"
|
2023-03-03 14:41:38 -06:00
|
|
|
)
|
|
|
|
|
2023-04-04 06:31:30 -05:00
|
|
|
// Grid numbering examples (H) or (W,H)
|
2023-04-27 20:41:53 -05:00
|
|
|
// -----------------------
|
|
|
|
// -- (1) -- (2) -- (3) -- (X)
|
|
|
|
// -----------------------
|
|
|
|
//
|
|
|
|
// (Y)
|
2023-03-23 12:35:12 -05:00
|
|
|
// ---------
|
|
|
|
// -- (1) --
|
|
|
|
// -- (2) --
|
|
|
|
// ---------
|
|
|
|
//
|
2023-04-27 20:41:53 -05:00
|
|
|
// (X,Y)
|
2023-03-23 12:35:12 -05:00
|
|
|
// -----------------------------
|
2023-04-04 06:31:30 -05:00
|
|
|
// -- (1,1) -- (2,1) -- (3,1) --
|
|
|
|
// -- (1,2) -- (2,2) -- (3,2) --
|
|
|
|
// -- (1,3) -- -- (3,3) --
|
2023-03-23 12:35:12 -05:00
|
|
|
// -----------------------------
|
|
|
|
|
2024-01-11 19:32:40 -06:00
|
|
|
type Grid struct {
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
}
|
|
|
|
|
|
|
|
type GridOffset struct {
|
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
}
|
|
|
|
|
2023-04-04 06:31:30 -05:00
|
|
|
func (n *Node) NewGrid(name string, w int, h int) *Node {
|
2024-01-05 13:18:44 -06:00
|
|
|
newNode := n.newNode(name, widget.Grid)
|
2023-03-03 14:41:38 -06:00
|
|
|
|
2023-05-10 14:28:30 -05:00
|
|
|
newNode.W = w
|
|
|
|
newNode.H = h
|
2023-05-09 17:48:21 -05:00
|
|
|
newNode.NextW = 1
|
|
|
|
newNode.NextH = 1
|
2023-04-28 15:34:55 -05:00
|
|
|
|
2024-01-06 13:53:15 -06:00
|
|
|
if ! newNode.hidden {
|
|
|
|
a := newAction(newNode, widget.Add)
|
|
|
|
sendAction(a)
|
|
|
|
}
|
2023-12-31 01:48:39 -06:00
|
|
|
|
|
|
|
// by default, always pad grids
|
|
|
|
newNode.Pad()
|
2023-05-09 08:25:10 -05:00
|
|
|
return newNode
|
|
|
|
}
|
2023-03-23 12:35:12 -05:00
|
|
|
|
2023-05-09 17:48:21 -05:00
|
|
|
// true if the grid already have a child at W,H
|
2023-05-09 18:34:09 -05:00
|
|
|
func (n *Node) gridCollision() bool {
|
2023-05-09 17:48:21 -05:00
|
|
|
for _, child := range n.children {
|
2023-05-09 18:34:09 -05:00
|
|
|
if ((child.AtW == n.NextW) && (child.AtH == n.NextH)) {
|
2023-05-09 17:48:21 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:34:09 -05:00
|
|
|
// keeps incrementing NextW & NextH until there is not a widget
|
|
|
|
func (n *Node) gridIncrement() {
|
|
|
|
if ! n.gridCollision() {
|
|
|
|
return
|
2023-05-09 17:48:21 -05:00
|
|
|
}
|
2023-05-09 18:34:09 -05:00
|
|
|
|
2023-05-10 14:28:30 -05:00
|
|
|
n.NextW += 1
|
|
|
|
if (n.NextW > n.W) {
|
|
|
|
n.NextW = 1
|
|
|
|
n.NextH += 1
|
2023-05-09 18:34:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
n.gridIncrement()
|
2023-05-09 17:48:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) At(w int, h int) *Node {
|
|
|
|
if (n == nil) {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
n.NextW = w
|
|
|
|
n.NextH = h
|
|
|
|
|
2023-05-09 18:34:09 -05:00
|
|
|
n.gridIncrement()
|
|
|
|
if (n.NextW != w) || (n.NextH != h) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("At() (W,H)", w, h, " was moved to avoid a collision (W,H) =", n.NextW, n.NextH)
|
2023-05-09 17:48:21 -05:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|