-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
153 lines (132 loc) · 3.38 KB
/
stack.go
File metadata and controls
153 lines (132 loc) · 3.38 KB
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
package ui
// import "github.com/hajimehoshi/ebiten/v2"
const stackLayerOffset = 1000
// A Stack is a container with widgets layed out stacked over each other.
// Unlike most widgest stack does not move the widgets, so exceptionally
// any child widgets can position themselves if needed.
// A Stack also stretches to the size given in LayoutWidget.
// Normally Window has a Stack for dialogs of the same size of Window.
type Stack struct {
BasicContainer
padded bool
}
func (b *Stack) Destroy() {
// free all controls
for i := 0; i < len(b.controls); i++ {
bc := b.controls[i]
bc.SetParent(nil)
bc.Destroy()
}
}
func (b *Stack) Enable() {
for _, child := range b.controls {
bw := child.(*BasicWidget)
bw.Enable()
}
}
func (b *Stack) Disable() {
for _, child := range b.controls {
bw := child.(*BasicWidget)
bw.Disable()
}
}
func (b *Stack) Append(c Control) {
b.BasicContainer.AppendWithParent(c, b)
c.RaiseWidget(stackLayerOffset * b.NumChildren())
if b.width > 0 && b.height > 0 {
b.LayoutWidget(b.width, b.height)
}
}
func (b *Stack) deleteNoLayout(index int) {
dprintln("Stack.delete")
if index < 0 || index >= len(b.controls) {
return
}
bc := b.controls[index]
bc.SetParent(nil)
b.controls = append(b.controls[:index-1], b.controls[:index+1]...)
}
func (b Stack) Padded() bool {
return b.padded
}
func (b *Stack) SetPadded(padded bool) {
b.padded = padded
}
func newStack() *Stack {
b := &Stack{}
b.controls = []Control{}
return b
}
// NewStack creates a new stack. Stacks always stretch to the size
// passed to them in LayoutWidget.
func NewStack() *Stack {
return newStack()
}
func (s *Stack) LayoutWidget(width, height int) {
if width < 1 || height < 1 {
panic("Stack: cannot lay out without space.")
}
// The size of a stack is simply the available space.
// The largest of the least width and least height of the stacked items.
w := 0
h := 0
for i, child := range s.controls {
if child.Hidden() {
continue
}
child.LayoutWidget(width, height)
realWidth, realHeight := child.WidgetSize()
if realWidth > w {
w = realWidth
}
if realHeight > h {
h = realHeight
}
if pane, ok := child.(*Pane); ok {
if pane.needLayout {
// The stacked items should stay where they are, but
// still position new panes.
// Center the stacked items, but if i > 0 shift it down a bit
// to prevent overlaps.
// Also wrap around to prevent invisible widgets.
x := (width - realWidth) / 2
remaining := (height - realHeight)
if remaining < realHeight {
remaining = realHeight + 1
}
y := (i * paneHeaderHeight) % (remaining)
y += paneHeaderHeight * 2
x += (i * paneHeaderHeight) / (remaining)
child.MoveWidget(x, y)
pane.needLayout = false
}
}
}
s.width, s.height = width, height
s.ClipTo(width, height)
}
func (b Stack) DrawWidget(g *Graphic) {
b.BasicContainer.DrawWidget(g)
b.DrawDebug(g, "STA")
}
func (c *Stack) CleanupClosePanes() {
closed := []int{}
// Handle any closed panes.
for i := len(c.controls) - 1; i >= 0; i-- {
child := c.controls[i]
if pane, ok := child.(*Pane); ok && pane.closed {
closed = append(closed, i)
break
}
}
for _, toClose := range closed {
c.Delete(toClose)
}
// sort the children in drawing order again
c.UpdateOrdered()
}
func (c *Stack) HandleWidget(ev Event) {
c.BasicContainer.HandleWidget(ev)
c.CleanupClosePanes()
}
var _ Control = &Stack{}