-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack_container.go
More file actions
230 lines (196 loc) · 5.25 KB
/
stack_container.go
File metadata and controls
230 lines (196 loc) · 5.25 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package ebui
import (
"image"
"github.com/hajimehoshi/ebiten/v2"
)
// StackContainer manages a stack of views/screens with transitions
type StackContainer struct {
*BaseContainer
stack []Component
transitioning bool
transitionPos float64
pushing bool
transitionSpeed float64
}
func WithTransitionSpeed(speed float64) ComponentOpt {
return func(c Component) {
if sc, ok := c.(*StackContainer); ok {
sc.transitionSpeed = speed
}
}
}
func NewStackContainer(opts ...ComponentOpt) *StackContainer {
sc := &StackContainer{
BaseContainer: NewBaseContainer(opts...),
stack: make([]Component, 0),
transitionSpeed: 0.05, // Default transition speed
}
for _, opt := range opts {
opt(sc)
}
return sc
}
// Push adds a new view to the stack with a transition
func (sc *StackContainer) Push(view Container) {
if sc.transitioning {
return
}
// For the very first view, just add it directly without animation
if len(sc.stack) == 0 {
view.SetSize(sc.GetSize())
view.SetPosition(Position{
X: 0, // Start in normal position
Y: 0,
Relative: true,
})
sc.AddChild(view)
sc.stack = append(sc.stack, view)
return
}
// For subsequent views, do the sliding animation
sc.transitioning = true
sc.pushing = true
sc.transitionPos = 0
viewSize := sc.GetSize()
view.SetSize(viewSize)
view.SetPosition(Position{
X: viewSize.Width, // Start offscreen to the right
Y: 0,
Relative: true,
})
sc.AddChild(view)
sc.stack = append(sc.stack, view)
}
// Update handles the transition animation
func (sc *StackContainer) Update() error {
if len(sc.stack) == 0 {
return nil
}
if sc.transitioning {
// Update transition position
sc.transitionPos += sc.transitionSpeed
if sc.transitionPos >= 1 {
sc.finishTransition()
}
// Calculate positions for views
width := sc.GetSize().Width
if sc.pushing && len(sc.stack) >= 2 {
// Move previous view left and new view in from right
prevView := sc.stack[len(sc.stack)-2]
currentView := sc.stack[len(sc.stack)-1]
prevView.SetPosition(Position{
X: -width * sc.transitionPos,
Y: 0,
Relative: true,
})
currentView.SetPosition(Position{
X: width * (1 - sc.transitionPos),
Y: 0,
Relative: true,
})
} else if !sc.pushing && len(sc.stack) >= 2 {
// Move current view right and previous view in from left
currentView := sc.stack[len(sc.stack)-1]
previousView := sc.stack[len(sc.stack)-2]
currentView.SetPosition(Position{
X: width * sc.transitionPos,
Y: 0,
Relative: true,
})
previousView.SetPosition(Position{
X: -width * (1 - sc.transitionPos),
Y: 0,
Relative: true,
})
}
}
return sc.BaseContainer.Update()
}
// Pop removes the top view from the stack with a transition
func (sc *StackContainer) Pop() {
if len(sc.stack) <= 1 || sc.transitioning {
return
}
sc.transitioning = true
sc.pushing = false
sc.transitionPos = 0
// Previous view should already be in the visual hierarchy
// Just need to ensure it's positioned correctly
previousView := sc.stack[len(sc.stack)-2]
previousView.SetPosition(Position{
X: -sc.GetSize().Width,
Y: 0,
Relative: true,
})
}
// finishTransition completes the current transition
func (sc *StackContainer) finishTransition() {
sc.transitionPos = 1
if !sc.pushing {
// When popping, remove the top view after transition
currentView := sc.stack[len(sc.stack)-1]
sc.RemoveChild(currentView)
sc.stack = sc.stack[:len(sc.stack)-1]
// Ensure the new top view is positioned correctly
if len(sc.stack) > 0 {
topView := sc.stack[len(sc.stack)-1]
topView.SetPosition(Position{
X: 0,
Y: 0,
Relative: true,
})
}
}
sc.transitioning = false
}
// Draw overrides the BaseContainer.Draw method to implement clipping
func (sc *StackContainer) Draw(screen *ebiten.Image) {
// Draw the container's background and debug info
sc.BaseComponent.Draw(screen)
// Create a sub-image for clipping to the container's bounds
bounds := sc.getVisibleBounds()
subScreen := screen.SubImage(bounds).(*ebiten.Image)
// Draw all children to the clipped sub-image
for _, child := range sc.children {
child.Draw(subScreen)
}
}
// getVisibleBounds returns the visible rectangle of the container
func (sc *StackContainer) getVisibleBounds() image.Rectangle {
pos := sc.GetAbsolutePosition()
size := sc.GetSize()
padding := sc.GetPadding()
return image.Rectangle{
Min: image.Point{
X: int(pos.X + padding.Left),
Y: int(pos.Y + padding.Top),
},
Max: image.Point{
X: int(pos.X + size.Width - padding.Right),
Y: int(pos.Y + size.Height - padding.Bottom),
},
}
}
// GetActiveView returns the currently active view
func (sc *StackContainer) GetActiveView() Component {
if len(sc.stack) == 0 {
return nil
}
return sc.stack[len(sc.stack)-1]
}
// Clear removes all views from the stack except the root view
func (sc *StackContainer) Clear() {
if len(sc.stack) <= 1 {
return
}
rootView := sc.stack[0]
sc.stack = []Component{rootView}
// Remove all children except root
sc.children = []Component{rootView}
// Ensure root view is positioned correctly
rootView.SetPosition(Position{
X: 0,
Y: 0,
Relative: true,
})
}