forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackWidget.go
50 lines (40 loc) · 1.28 KB
/
StackWidget.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
package giu
import "github.com/AllenDang/imgui-go"
var _ Widget = &StackWidget{}
// StackWidget is used to ensure, that the build methods of all
// the widgets (layouts field) was called, but only the selected
// (visible field) layout is rendered (visible) in app.
type StackWidget struct {
visible int32
layouts []Widget
}
// Stack creates a new StackWidget.
func Stack(visible int32, layouts ...Widget) *StackWidget {
return &StackWidget{
visible: visible,
layouts: layouts,
}
}
// Build implements widget interface.
func (s *StackWidget) Build() {
// save visible cursor position
visiblePos := GetCursorScreenPos()
// build visible layout
// NOTE: it is important to build the visible layout before
// building other ones, because the interactive layout widgets
// (e.g. buttons) should be rendered on top of `stack`
layouts := s.layouts
if s.visible >= 0 && s.visible < int32(len(s.layouts)) {
s.layouts[s.visible].Build()
// remove visible layout from layouts list
//nolint:gocritic // remove visible widget
layouts = append(s.layouts[:s.visible], s.layouts[:s.visible+1]...)
}
// build invisible layouts with 0 alpha
imgui.PushStyleVarFloat(imgui.StyleVarAlpha, 0)
for _, l := range layouts {
SetCursorScreenPos(visiblePos)
l.Build()
}
imgui.PopStyleVar()
}