summaryrefslogtreecommitdiff
path: root/grid.go
blob: 6b2df82f05f1e01915a156b42ad91bfcc8c2d29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gui

import (
	"git.wit.org/wit/gui/toolkit"
)

// Grid numbering examples (H) or (W,H)
// -----------------------
// -- (1) -- (2) -- (3) -- (X)
// -----------------------
//
//    (Y)
// ---------
// -- (1) --
// -- (2) --
// ---------
//
//    (X,Y)
// -----------------------------
// -- (1,1) -- (2,1) -- (3,1) --
// -- (1,2) -- (2,2) -- (3,2) --
// -- (1,3) --       -- (3,3) --
// -----------------------------

func (n *Node) NewGrid(name string, w int, h int) *Node {
	newNode := n.newNode(name, toolkit.Grid)

	newNode.X = w
	newNode.Y = h
	newNode.NextW = 1
	newNode.NextH = 1

	a := newAction(newNode, toolkit.Add)
	sendAction(a)
	return newNode
}

// true if the grid already have a child at W,H
func (n *Node) gridCollision() bool {
	for _, child := range n.children {
		if ((child.AtW == n.NextW) && (child.AtH == n.NextH)) {
			return true
		}
	}
	return false
}

// keeps incrementing NextW & NextH until there is not a widget
func (n *Node) gridIncrement() {
	if ! n.gridCollision() {
		return
	}

	n.NextH += 1
	if (n.NextH > n.Y) {
		n.NextW += 1
		n.NextH = 1
	}

	n.gridIncrement()
}

func (n *Node) At(w int, h int) *Node {
	if (n == nil) {
		return n
	}

	n.NextW = w
	n.NextH = h

	n.gridIncrement()
	if (n.NextW != w) || (n.NextH != h) {
		log(logError, "At() (W,H)", w, h, " was moved to avoid a collision (W,H) =", n.NextW, n.NextH)
	}
	return n
}

// finds the next place on the grid to place the new node 'n'
func placeGrid(a *toolkit.Action, n *Node, where *Node) {
	where.NextH += 1
	if (where.NextH > where.Y) {
		where.NextW += 1
		where.NextH = 1
	}

	a.X = where.NextW
	a.Y = where.NextH
	log(logNow, "placeGrid() (X,Y)", where.X, where.Y, " next(X,Y) =", where.NextW, where.NextH)
}