-
Notifications
You must be signed in to change notification settings - Fork 5
/
widgets.combo.go
304 lines (261 loc) · 11.6 KB
/
widgets.combo.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package imgui
import "fmt"
// Widgets: Combo Box
// - The BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items.
// - The old Combo() api are helpers over BeginCombo()/EndCombo() which are kept available for convenience purpose. This is analogous to how ListBox are created.
func BeginCombo(label string, preview_value string, flags ImGuiComboFlags) bool {
var g = GImGui
var window = GetCurrentWindow()
var backup_next_window_data_flags = g.NextWindowData.Flags
g.NextWindowData.ClearFlags() // We behave like Begin() and need to consume those values
if window.SkipItems {
return false
}
var style = g.Style
var id = window.GetIDs(label)
IM_ASSERT((flags & (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) != (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) // Can't use both flags together
var arrow_size float = 0.0
if (flags & ImGuiComboFlags_NoArrowButton) == 0 {
arrow_size = GetFrameHeight()
}
var label_size = CalcTextSize(label, true, -1)
var w = arrow_size
if (flags & ImGuiComboFlags_NoPreview) == 0 {
w = CalcItemWidth()
}
var bb = ImRect{window.DC.CursorPos, window.DC.CursorPos.Add(ImVec2{w, label_size.y + style.FramePadding.y*2.0})}
var add float
if label_size.x > 0.0 {
add = style.ItemInnerSpacing.x + +label_size.x
}
var total_bb = ImRect{bb.Min, bb.Max.Add(ImVec2{add, 0.0})}
ItemSizeRect(&total_bb, style.FramePadding.y)
if !ItemAdd(&total_bb, id, &bb, 0) {
return false
}
// Open on click
var hovered, held bool
var pressed = ButtonBehavior(&bb, id, &hovered, &held, 0)
var popup_id = ImHashStr("##ComboPopup", 0, id)
var popup_open = isPopupOpen(popup_id, ImGuiPopupFlags_None)
if (pressed || g.NavActivateId == id) && !popup_open {
OpenPopupEx(popup_id, ImGuiPopupFlags_None)
popup_open = true
}
var c = ImGuiCol_FrameBg
if hovered {
c = ImGuiCol_FrameBgHovered
}
var rounding = ImDrawFlags_RoundCornersLeft
if flags&ImGuiComboFlags_NoArrowButton != 0 {
rounding = ImDrawFlags_RoundCornersAll
}
// Render shape
var frame_col = GetColorU32FromID(c, 1)
var value_x2 = ImMax(bb.Min.x, bb.Max.x-arrow_size)
RenderNavHighlight(&bb, id, 0)
if flags&ImGuiComboFlags_NoPreview == 0 {
window.DrawList.AddRectFilled(bb.Min, ImVec2{value_x2, bb.Max.y}, frame_col, style.FrameRounding, rounding)
}
if flags&ImGuiComboFlags_NoArrowButton == 0 {
var c = ImGuiCol_Button
if popup_open || hovered {
c = ImGuiCol_ButtonHovered
}
var bg_col = GetColorU32FromID(c, 1)
var text_col = GetColorU32FromID(ImGuiCol_Text, 1)
var rounding = ImDrawFlags_RoundCornersRight
if w <= arrow_size {
rounding = ImDrawFlags_RoundCornersAll
}
window.DrawList.AddRectFilled(ImVec2{value_x2, bb.Min.y}, bb.Max, bg_col, style.FrameRounding, rounding)
if value_x2+arrow_size-style.FramePadding.x <= bb.Max.x {
RenderArrow(window.DrawList, ImVec2{value_x2 + style.FramePadding.y, bb.Min.y + style.FramePadding.y}, text_col, ImGuiDir_Down, 1.0)
}
}
RenderFrameBorder(bb.Min, bb.Max, style.FrameRounding)
// Custom preview
if flags&ImGuiComboFlags_CustomPreview != 0 {
g.ComboPreviewData.PreviewRect = ImRect{ImVec2{bb.Min.x, bb.Min.y}, ImVec2{value_x2, bb.Max.y}}
IM_ASSERT(preview_value == "" || preview_value[0] == 0)
preview_value = ""
}
// Render preview and label
if preview_value != "" && (flags&ImGuiComboFlags_NoPreview == 0) {
if g.LogEnabled {
LogSetNextTextDecoration("{", "}")
}
min := bb.Min.Add(style.FramePadding)
RenderTextClipped(&min, &ImVec2{value_x2, bb.Max.y}, preview_value, nil, nil, nil)
}
if label_size.x > 0 {
RenderText(ImVec2{bb.Max.x + style.ItemInnerSpacing.x, bb.Min.y + style.FramePadding.y}, label, true)
}
if !popup_open {
return false
}
g.NextWindowData.Flags = backup_next_window_data_flags
return BeginComboPopup(popup_id, &bb, flags)
}
// Call directly after the BeginCombo/EndCombo block. The preview is designed to only host non-interactive elements
// (Experimental, see GitHub issues: #1658, #4168)
func BeginComboPreview() bool {
var g = GImGui
var window = g.CurrentWindow
var preview_data = &g.ComboPreviewData
if window.SkipItems || !window.ClipRect.Overlaps(g.LastItemData.Rect) { // FIXME: Because we don't have a ImGuiItemStatusFlags_Visible flag to test last ItemAdd() result
return false
}
IM_ASSERT(g.LastItemData.Rect.Min.x == preview_data.PreviewRect.Min.x && g.LastItemData.Rect.Min.y == preview_data.PreviewRect.Min.y) // Didn't call after BeginCombo/EndCombo block or forgot to pass ImGuiComboFlags_CustomPreview flag?
if !window.ClipRect.ContainsRect(preview_data.PreviewRect) { // Narrower test (optional)
return false
}
// FIXME: This could be contained in a PushWorkRect() api
preview_data.BackupCursorPos = window.DC.CursorPos
preview_data.BackupCursorMaxPos = window.DC.CursorMaxPos
preview_data.BackupCursorPosPrevLine = window.DC.CursorPosPrevLine
preview_data.BackupPrevLineTextBaseOffset = window.DC.PrevLineTextBaseOffset
preview_data.BackupLayout = window.DC.LayoutType
window.DC.CursorPos = preview_data.PreviewRect.Min.Add(g.Style.FramePadding)
window.DC.CursorMaxPos = window.DC.CursorPos
window.DC.LayoutType = ImGuiLayoutType_Horizontal
PushClipRect(preview_data.PreviewRect.Min, preview_data.PreviewRect.Max, true)
return true
}
func EndComboPreview() {
var g = GImGui
var window = g.CurrentWindow
var preview_data = &g.ComboPreviewData
// FIXME: Using CursorMaxPos approximation instead of correct AABB which we will store in ImDrawCmd in the future
var draw_list = window.DrawList
if window.DC.CursorMaxPos.x < preview_data.PreviewRect.Max.x && window.DC.CursorMaxPos.y < preview_data.PreviewRect.Max.y {
if len(draw_list.CmdBuffer) > 1 { // Unlikely case that the PushClipRect() didn't create a command
draw_list._CmdHeader.ClipRect = draw_list.CmdBuffer[len(draw_list.CmdBuffer)-2].ClipRect
draw_list.CmdBuffer[len(draw_list.CmdBuffer)-1].ClipRect = draw_list.CmdBuffer[len(draw_list.CmdBuffer)-2].ClipRect
draw_list._TryMergeDrawCmds()
}
}
PopClipRect()
window.DC.CursorPos = preview_data.BackupCursorPos
window.DC.CursorMaxPos = ImMaxVec2(&window.DC.CursorMaxPos, &preview_data.BackupCursorMaxPos)
window.DC.CursorPosPrevLine = preview_data.BackupCursorPosPrevLine
window.DC.PrevLineTextBaseOffset = preview_data.BackupPrevLineTextBaseOffset
window.DC.LayoutType = preview_data.BackupLayout
preview_data.PreviewRect = ImRect{}
}
func BeginComboPopup(popup_id ImGuiID, bb *ImRect, flags ImGuiComboFlags) bool {
var g = GImGui
if !isPopupOpen(popup_id, ImGuiPopupFlags_None) {
g.NextWindowData.ClearFlags()
return false
}
// Set popup size
var w = bb.GetWidth()
if g.NextWindowData.Flags&ImGuiNextWindowDataFlags_HasSizeConstraint != 0 {
g.NextWindowData.SizeConstraintRect.Min.x = ImMax(g.NextWindowData.SizeConstraintRect.Min.x, w)
} else {
if (flags & ImGuiComboFlags_HeightMask_) == 0 {
flags |= ImGuiComboFlags_HeightRegular
}
IM_ASSERT(ImIsPowerOfTwoInt(int(flags & ImGuiComboFlags_HeightMask_))) // Only one
var popup_max_height_in_items int = -1
if flags&ImGuiComboFlags_HeightRegular != 0 {
popup_max_height_in_items = 8
} else if flags&ImGuiComboFlags_HeightSmall != 0 {
popup_max_height_in_items = 4
} else if flags&ImGuiComboFlags_HeightLarge != 0 {
popup_max_height_in_items = 20
}
SetNextWindowSizeConstraints(ImVec2{w, 0.0}, ImVec2{FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items)}, nil, nil)
}
// This is essentially a specialized version of BeginPopupEx()
var name = fmt.Sprintf("##Combo_%d", len(g.BeginPopupStack)) // Recycle windows based on depth
// Set position given a custom constraint (peak into expected window size so we can position it)
// FIXME: This might be easier to express with an hypothetical SetNextWindowPosConstraints() function?
// FIXME: This might be moved to Begin() or at least around the same spot where Tooltips and other Popups are calling FindBestWindowPosForPopupEx()?
if popup_window := FindWindowByName(string(name[:])); popup_window != nil {
if popup_window.WasActive {
// Always override 'AutoPosLastDirection' to not leave a chance for a past value to affect us.
var size_expected = CalcWindowNextAutoFitSize(popup_window)
dir := ImGuiDir_Down
if (flags & ImGuiComboFlags_PopupAlignLeft) != 0 {
dir = ImGuiDir_Left
}
popup_window.AutoPosLastDirection = dir // Left = "Below, Toward Left", Down = "Below, Toward Right (default)"
var r_outer = GetPopupAllowedExtentRect(popup_window)
var bl = bb.GetBL()
var pos = FindBestWindowPosForPopupEx(&bl, &size_expected, &popup_window.AutoPosLastDirection, &r_outer, bb, ImGuiPopupPositionPolicy_ComboBox)
SetNextWindowPos(&pos, 0, ImVec2{})
}
}
// We don't use BeginPopupEx() solely because we have a custom name string, which we could make an argument to BeginPopupEx()
var window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMove
PushStyleVec(ImGuiStyleVar_WindowPadding, ImVec2{g.Style.FramePadding.x, g.Style.WindowPadding.y}) // Horizontally align ourselves with the framed text
var ret = Begin(string(name[:]), nil, window_flags)
PopStyleVar(1)
if !ret {
EndPopup()
IM_ASSERT(false) // This should never happen as we tested for IsPopupOpen() above
return false
}
return true
}
// only call EndCombo() if BeginCombo() returns true!
func EndCombo() {
EndPopup()
}
func CalcMaxPopupHeightFromItemCount(items_count int) float32 {
var g = GImGui
if items_count <= 0 {
return FLT_MAX
}
return (g.FontSize+g.Style.ItemSpacing.y)*float(items_count) - g.Style.ItemSpacing.y + (g.Style.WindowPadding.y * 2)
}
// Combo box helper allowing to pass an array of strings.
func Combo(label string, current_item *int, items []string, items_count int, popup_max_height_in_items int /*= -1*/) bool {
var value_changed = ComboFunc(label, current_item, func(slice any, idx int, val *string) bool {
*val = slice.([]string)[idx]
return true
}, items, items_count, popup_max_height_in_items)
return value_changed
}
// Old API, prefer using BeginCombo() nowadays if you can.
func ComboFunc(label string, current_item *int, items_getter func(data any, idx int, out_text *string) bool, data any, items_count, popup_max_height_in_items int /*= -1*/) bool {
var g = GImGui
// Call the getter to obtain the preview string which is a parameter to BeginCombo()
var preview_value string
if *current_item >= 0 && *current_item < items_count {
items_getter(data, *current_item, &preview_value)
}
// The old Combo() API exposed "popup_max_height_in_items". The new more general BeginCombo() API doesn't have/need it, but we emulate it here.
if popup_max_height_in_items != -1 && (g.NextWindowData.Flags&ImGuiNextWindowDataFlags_HasSizeConstraint == 0) {
SetNextWindowSizeConstraints(ImVec2{}, ImVec2{FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items)}, nil, nil)
}
if !BeginCombo(label, preview_value, ImGuiComboFlags_None) {
return false
}
// Display items
// FIXME-OPT: Use clipper (but we need to disable it on the appearing frame to make sure our call to SetItemDefaultFocus() is processed)
var value_changed = false
for i := int(0); i < items_count; i++ {
PushID(i)
var item_selected = (i == *current_item)
var item_text string
if !items_getter(data, i, &item_text) {
item_text = "*Unknown item*"
}
if Selectable(item_text, item_selected, 0, ImVec2{}) {
value_changed = true
*current_item = i
}
if item_selected {
SetItemDefaultFocus()
}
PopID()
}
EndCombo()
if value_changed {
MarkItemEdited(g.LastItemData.ID)
}
return value_changed
}