-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Alignment.go
151 lines (130 loc) · 4 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
package giu
import (
"fmt"
"image"
"github.com/AllenDang/imgui-go"
)
// AlignmentType represents a bype of alignment to use with AlignSetter.
type AlignmentType byte
const (
// AlignLeft is here just for clearity.
// if set, no action is taken so don't use it.
AlignLeft AlignmentType = iota
// AlignCenter centers widget.
AlignCenter
// AlignRight aligns a widget to right side of window.
AlignRight
)
var _ Widget = &AlignmentSetter{}
// AlignmentSetter allows to align to right / center a widget or widgets group.
// NOTE: Because of AlignSetter uses experimental GetWidgetWidth,
// it is experimental too.
// usage: see examples/align
//
// list of known bugs:
// - BUG: DatePickerWidget doesn't work properly
// - BUG: there is some bug with SelectableWidget
// - BUG: ComboWidget and ComboCustomWidgets doesn't work properly.
type AlignmentSetter struct {
alignType AlignmentType
layout Layout
id string
}
// Align sets widgets alignment.
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
// NOTE: there isn't any known reason to use this method, however
// it is here for some random cases. YOU DON'T NEED TO USE IT
// in normal conditions.
func (a *AlignmentSetter) ID(id string) *AlignmentSetter {
a.id = id
return a
}
// Build implements Widget interface.
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
// 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: user can interact with invisible widget (created by GetWidgetWidth)
// - https://github.com/AllenDang/giu/issues/341
// - https://github.com/ocornut/imgui/issues/4588
//
// if you find anything else, please report it on
// https://github.com/AllenDang/giu Any contribution is appreciated!
func GetWidgetWidth(w Widget) (result float32) {
imgui.PushID(GenAutoID("GetWIdgetWidthMeasurement"))
defer imgui.PopID()
// save cursor position before rendering
currentPos := GetCursorPos()
// render widget in `dry` mode
imgui.PushStyleVarFloat(imgui.StyleVarAlpha, 0)
w.Build()
imgui.PopStyleVar()
// save widget's width
// check cursor position
imgui.SameLine()
spacingW, _ := GetItemSpacing()
result = float32(GetCursorPos().X-currentPos.X) - spacingW
SetCursorPos(currentPos)
return result
}