-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayout.go
78 lines (70 loc) · 1.95 KB
/
layout.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
// Copyright 2016 Josh Deprez
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package awakengine
import "github.com/DrJosh9000/vec"
type GridDelegate interface {
Columns() int
Item(i int, par *View)
ItemSize() vec.I2
ItemHandle(i int, e *Event) bool // item #i should handles event e
NumItems() int
}
type Grid struct {
*View
GridDelegate
items []*View
}
func (g *Grid) Handle(e *Event) bool {
if !g.Bounds().Contains(e.ScreenPos) {
return false
}
// Convert the coordinate back into an item index.
t := e.ScreenPos.Sub(g.Position()).EDiv(g.GridDelegate.ItemSize())
i := t.X + t.Y*g.GridDelegate.Columns()
if i < 0 && i >= g.GridDelegate.NumItems() {
return false
}
return g.GridDelegate.ItemHandle(i, e)
}
// Reload all the items.
func (g *Grid) Reload() {
n := g.GridDelegate.NumItems()
c := g.GridDelegate.Columns()
sz := g.GridDelegate.ItemSize()
gs := vec.I2{}
// Clear out all the existing views...
for _, v := range g.items {
v.SetRetire(true)
}
g.items = g.items[:0]
// Create fresh views.
for i := 0; i < n; i++ {
item := &View{}
item.SetParent(g.View)
p := vec.Div(i, c).EMul(sz)
item.SetPositionAndSize(p, sz)
item.SetZ(1)
g.GridDelegate.Item(i, item)
g.items = append(g.items, item)
// Ensure the grid itself is sized sufficiently. There's a mathsier way of
// doing it but this is simple.
if w := p.X + sz.X; w > gs.X {
gs.X = w
}
if h := p.Y + sz.Y; h > gs.Y {
gs.Y = h
}
}
g.View.SetSize(gs)
}