forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
StackWidget.go
47 lines (39 loc) · 1.25 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
package giu
import "github.com/HACKERALERT/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,
}
}
func (s *StackWidget) Build() {
// save visible cursor position
visiblePos := GetCursorScreenPos()
// build visible layout
// NOTE: it is important to build the visiblely showed layout before
// building another 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()
}