forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlignment.go
More file actions
106 lines (88 loc) · 2.11 KB
/
Alignment.go
File metadata and controls
106 lines (88 loc) · 2.11 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
package giu
import (
"fmt"
"image"
"github.com/AllenDang/imgui-go"
)
type AlignmentType byte
const (
AlignLeft AlignmentType = iota
AlignCenter
AlignRight
)
type AlignmentSetter struct {
alignType AlignmentType
layout Layout
id string
}
// Align sets widgets alignment.
// usage: see examples/align
//
// FIXME: all widgets will be build twice
// it means, that if you have e.g. CustomWidget it could do unexpected things.
// Example:
// Align(AlignToCenter).To(
// Custom(func() { fmt.Println("running custom widget") }),
// )
// will print the message two times per frame.
//
// BUG: DatePickerWidget doesn't work properly
func Align(at AlignmentType) *AlignmentSetter {
return &AlignmentSetter{
alignType: at,
id: GenAutoID("alignSetter"),
}
}
func (a *AlignmentSetter) To(widgets ...Widget) *AlignmentSetter {
a.layout = Layout(widgets)
return a
}
func (a *AlignmentSetter) ID(id string) *AlignmentSetter {
a.id = id
return a
}
func (a *AlignmentSetter) Build() {
if a.layout == nil {
return
}
// WORKAROUND: get widgets widths rendering them with 100% transparency
// first save start cursor position
startPos := GetCursorPos()
widgetsWidths := make([]float32, 0)
// render widgets with 0 alpha and store thems widths
imgui.PushStyleVarFloat(imgui.StyleVarID(StyleVarAlpha), 0)
a.layout.Range(func(item Widget) {
var width float32
if item != nil {
item.Build()
size := imgui.GetItemRectSize()
width = size.X
}
widgetsWidths = append(widgetsWidths, width)
})
imgui.PopStyleVar()
// reset cursor pos
SetCursorPos(startPos)
// ALIGN WIDGETS
idx := 0o0
a.layout.Range(func(item Widget) {
if item == nil {
return
}
w := widgetsWidths[idx]
currentPos := GetCursorPos()
availableW, _ := GetAvailableRegion()
switch a.alignType {
case AlignLeft:
// noop
case AlignCenter:
SetCursorPos(image.Pt(int(availableW/2-w/2), int(currentPos.Y)))
case AlignRight:
SetCursorPos(image.Pt(int(availableW-w), int(currentPos.Y)))
default:
panic(fmt.Sprintf("giu: (*AlignSetter).Build: unknown align type %d", a.alignType))
}
item.Build()
idx++
})
}