forked from HACKERALERT/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alignment.go
156 lines (138 loc) · 3.82 KB
/
Alignment.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
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
package giu
import (
"fmt"
"image"
"github.com/Picocrypt/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
//
// - BUG: DatePickerWidget doesn't work properly
// - BUG: there is some bug with SelectableWidget
// - BUG: ComboWidget and ComboCustomWidgets doesn't work properly
func Align(at AlignmentType) *AlignmentSetter {
return &AlignmentSetter{
alignType: at,
id: GenAutoID("alignSetter"),
}
}
// To sets a layout, alignment should be applied to
func (a *AlignmentSetter) To(widgets ...Widget) *AlignmentSetter {
a.layout = Layout(widgets)
return a
}
// ID allows to manually set AlignmentSetter ID (it shouldn't be used
// in a normal conditions)
func (a *AlignmentSetter) ID(id string) *AlignmentSetter {
a.id = id
return a
}
func (a *AlignmentSetter) Build() {
if a.layout == nil {
return
}
a.layout.Range(func(item Widget) {
// if item is inil, just skip it
if item == nil {
return
}
switch item.(type) {
// ok, it doesn't make sense to align again :-)
case *AlignmentSetter:
item.Build()
return
case *CustomWidget:
item.Build()
return
// there is a bug with selectables and combos, so skip them for now
case *SelectableWidget, *ComboWidget, *ComboCustomWidget:
item.Build()
return
}
currentPos := GetCursorPos()
w := GetWidgetWidth(item)
availableW, _ := GetAvailableRegion()
// we need to increase available region by 2 * window padding (X),
// because GetCursorPos considers it
paddingW, _ := GetWindowPadding()
availableW += 2 * paddingW
// set cursor position to align the widget
switch a.alignType {
case AlignLeft:
SetCursorPos(currentPos)
case AlignCenter:
SetCursorPos(image.Pt(int(availableW/2-w/2), currentPos.Y))
case AlignRight:
SetCursorPos(image.Pt(int(availableW-w), currentPos.Y))
default:
panic(fmt.Sprintf("giu: (*AlignSetter).Build: unknown align type %d", a.alignType))
}
// build aligned widget
item.Build()
})
}
// GetWidgetWidth returns a width of widget
// NOTE: THIS IS A BETA SOLUTION and may contain bugs
// in most cases, you may want to use supported by imgui GetItemRectSize.
// There is an upstream issue for this problem:
// https://github.com/ocornut/imgui/issues/3714
//
// This function is just a workaround used in giu.
//
// NOTE: user-definied widgets, which contains more than one
// giu widget will be processed incorrectly (only width of the last built
// widget will be processed)
//
// here is a list of known bugs:
// - BUG: Custom widgets are skipped, so if user put some widgets
// inside of CustomWidget, its sizes will not be returned
//
// if you find anything else, please report it on
// https://github.com/HACKERALERT/giu Any contribution is appreciated!
func GetWidgetWidth(w Widget) (result float32) {
// save cursor position before rendering
currentPos := GetCursorPos()
// enumerate some special cases
switch typed := w.(type) {
// Don't process custom widgets
case *CustomWidget:
return 0
// when row, sum all widget's sizes (adding spacing)
case *RowWidget:
isFirst := true
typed.widgets.Range(func(r Widget) {
result += GetWidgetWidth(r)
if !isFirst {
spacing, _ := GetItemSpacing()
result += spacing
} else {
isFirst = false
}
})
return result
// panic if layout - cannot calculate width of multiple widgets
case Layout, *Layout:
panic("GetWidgetWidth: requires Widget argument, but []Widget (Layout) got")
default:
// render widget in `dry` mode
imgui.PushStyleVarFloat(imgui.StyleVarAlpha, 0)
w.Build()
imgui.PopStyleVar()
// save widget's width
size := imgui.GetItemRectSize()
result = size.X
}
SetCursorPos(currentPos)
return result
}