-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid.go
212 lines (191 loc) · 4.61 KB
/
grid.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package ui
import (
"image"
"math"
"slices"
"github.com/Zyko0/Ebiary/ui/internal/graphics"
)
type Grid struct {
*itemImpl
width int
height int
grid [][]Item
items []Item
}
func NewGrid(columns, rows int) *Grid {
grid := make([][]Item, rows)
for y := range grid {
grid[y] = make([]Item, columns)
}
g := &Grid{
width: columns,
height: rows,
grid: grid,
items: make([]Item, 0),
}
g.itemImpl = newItem(g)
return g
}
func (g *Grid) update(c *context, area image.Rectangle, z int) {
if g.Skipped() {
return
}
// Grid
clamped, inner := g.adjustInnerArea(area)
g.itemImpl.geom.lastFullRegion = inner
g.itemImpl.geom.lastRegion = clamped
g.itemImpl.geom.lastCursor = c.Cursor
g.itemImpl.update(c, g, clamped, z)
// Decorations
for _, d := range g.itemImpl.decorations {
if d.Visible() {
d.update(c, clamped, z)
}
}
// Children
unitX, unitY := clamped.Dx()/g.width, clamped.Dy()/g.height
/*remX := float64(inner.Dx()%g.width) / float64(g.width)
remY := float64(inner.Dy()%g.height) / float64(g.height)*/
itemAreas := map[Item]image.Rectangle{}
for y, row := range g.grid {
for x, i := range row {
// Skip empty cells
if i == nil {
continue
}
// Process cell's item area
if _, ok := itemAreas[i]; !ok {
itemAreas[i] = image.Rectangle{
Min: image.Pt(math.MaxInt32, math.MaxInt32),
Max: image.Pt(math.MinInt32, math.MinInt32),
}
}
// Padding offsets
/*xoff := int(float64(x) * remX)
yoff := int(float64(y) * remY)*/
r := itemAreas[i]
r.Min.X = min(r.Min.X, clamped.Min.X+x*unitX)
r.Min.Y = min(r.Min.Y, clamped.Min.Y+y*unitY)
r.Max.X = max(r.Max.X, clamped.Min.X+(x+1)*unitX)
r.Max.Y = max(r.Max.Y, clamped.Min.Y+(y+1)*unitY)
itemAreas[i] = r
}
}
// Update children base items and their own children
for i, r := range itemAreas {
i.update(c, r, z+1)
}
itemAreas = nil
}
func (g *Grid) addGFX(pp *graphics.Pipeline, area image.Rectangle, z int) {
if g.Skipped() {
return
}
area, _ = g.itemImpl.adjustInnerArea(area)
g.itemImpl.addGFX(pp, area, z)
// Decorations
for _, d := range g.itemImpl.decorations {
if d.Visible() {
// Note: mark them as z+1 so that they're drawn on top
// of the grid's content
d.addGFX(pp, area, z+1)
}
}
// Compute children items areas
unitX, unitY := area.Dx()/g.width, area.Dy()/g.height
//remX := float64(inner.Dx()%g.width) / float64(g.width)
//remY := float64(inner.Dy()%g.height) / float64(g.height)
itemAreas := map[Item]image.Rectangle{}
for y, row := range g.grid {
for x, i := range row {
// Skip empty cells
if i == nil {
continue
}
// Process cell's item area
if _, ok := itemAreas[i]; !ok {
itemAreas[i] = image.Rectangle{
Min: image.Pt(math.MaxInt32, math.MaxInt32),
Max: image.Pt(math.MinInt32, math.MinInt32),
}
}
// Padding offsets
//xoff := int(float64(x) * remX)
//yoff := int(float64(y) * remY)
r := itemAreas[i]
r.Min.X = min(r.Min.X, area.Min.X+x*unitX)
r.Min.Y = min(r.Min.Y, area.Min.Y+y*unitY)
r.Max.X = max(r.Max.X, area.Min.X+(x+1)*unitX)
r.Max.Y = max(r.Max.Y, area.Min.Y+(y+1)*unitY)
itemAreas[i] = r
}
}
// Draw children base items and their own children
for i, r := range itemAreas {
i.addGFX(pp, r, z+1)
}
itemAreas = nil
}
func (g *Grid) GridBase() *Grid {
return g
}
func (g *Grid) Columns() int {
return g.width
}
func (g *Grid) Rows() int {
return g.height
}
func (g *Grid) Dimensions() (int, int) {
return g.width, g.height
}
func (g *Grid) Add(x, y, columns, rows int, item Item) {
if x < 0 || y < 0 || x+columns > g.width || y+rows > g.height {
panic("ui: cannot add an item outside of the grid")
}
if columns == 0 || rows == 0 {
panic("ui: columns and rows must be > 0")
}
// Hack: need to set the address once
item.setAddr(item)
g.items = append(g.items, item)
deletions := map[Item]struct{}{}
// Add the item
for ty := y; ty < y+rows; ty++ {
for tx := x; tx < x+columns; tx++ {
if idel := g.grid[ty][tx]; idel != nil {
deletions[idel] = struct{}{}
}
g.grid[ty][tx] = item
}
}
// Delete items which space got compromised
for idel := range deletions {
for i, curr := range g.items {
if curr == idel {
g.items[i] = nil
g.items = slices.Delete(g.items, i, i+1)
}
}
for ty := range g.grid {
for tx, curr := range g.grid[ty] {
if curr == idel {
g.grid[ty][tx] = nil
}
}
}
}
deletions = nil
}
func (g *Grid) ForEach(fn func(i Item)) {
for _, item := range g.items {
fn(item)
}
}
// Options
type GridOption = func(*Grid)
func (g *Grid) WithOptions(opts ...GridOption) *Grid {
for _, o := range opts {
o(g)
}
return g
}