-
Notifications
You must be signed in to change notification settings - Fork 5
/
tables.columns.go
453 lines (387 loc) · 15.8 KB
/
tables.columns.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package imgui
const COLUMNS_HIT_RECT_HALF_WIDTH float = 4
// Get into the columns background draw command (which is generally the same draw command as before we called BeginColumns)
func PushColumnsBackground() {
var window = GetCurrentWindowRead()
var columns = window.DC.CurrentColumns
if columns.Count == 1 {
return
}
// Optimization: avoid SetCurrentChannel() + PushClipRect()
columns.HostBackupClipRect = window.ClipRect
SetWindowClipRectBeforeSetChannel(window, &columns.HostInitialClipRect)
columns.Splitter.SetCurrentChannel(window.DrawList, 0)
}
// Internal Columns API (this is not exposed because we will encourage transitioning to the Tables API)
// [Internal] Small optimization to avoid calls to PopClipRect/SetCurrentChannel/PushClipRect in sequences,
// they would meddle many times with the underlying ImDrawCmd.
// Instead, we do a preemptive overwrite of clipping rectangle _without_ altering the command-buffer and let
// the subsequent single call to SetCurrentChannel() does it things once.
func SetWindowClipRectBeforeSetChannel(window *ImGuiWindow, clip_rect *ImRect) {
var clip_rect_vec4 = clip_rect.ToVec4()
window.ClipRect = *clip_rect
window.DrawList._CmdHeader.ClipRect = clip_rect_vec4
window.DrawList._ClipRectStack[len(window.DrawList._ClipRectStack)-1] = clip_rect_vec4
}
// setup number of columns. use an identifier to distinguish multiple column sets. close with EndColumns().
func BeginColumns(str_id string, columns_count int, flags ImGuiOldColumnFlags) {
var g = GImGui
var window = GetCurrentWindow()
IM_ASSERT(columns_count >= 1)
IM_ASSERT(window.DC.CurrentColumns == nil) // Nested columns are currently not supported
// Acquire storage for the columns set
var id = GetColumnsID(str_id, columns_count)
var columns = FindOrCreateColumns(window, id)
IM_ASSERT(columns.ID == id)
columns.Current = 0
columns.Count = columns_count
columns.Flags = flags
window.DC.CurrentColumns = columns
columns.HostCursorPosY = window.DC.CursorPos.y
columns.HostCursorMaxPosX = window.DC.CursorMaxPos.x
columns.HostInitialClipRect = window.ClipRect
columns.HostBackupParentWorkRect = window.ParentWorkRect
window.ParentWorkRect = window.WorkRect
// Set state for first column
// We aim so that the right-most column will have the same clipping width as other after being clipped by parent ClipRect
var column_padding = g.Style.ItemSpacing.x
var half_clip_extend_x = ImFloor(ImMax(window.WindowPadding.x*0.5, window.WindowBorderSize))
var max_1 = window.WorkRect.Max.x + column_padding - ImMax(column_padding-window.WindowPadding.x, 0.0)
var max_2 = window.WorkRect.Max.x + half_clip_extend_x
columns.OffMinX = window.DC.Indent.x - column_padding + ImMax(column_padding-window.WindowPadding.x, 0.0)
columns.OffMaxX = ImMax(ImMin(max_1, max_2)-window.Pos.x, columns.OffMinX+1.0)
columns.LineMinY = window.DC.CursorPos.y
columns.LineMaxY = window.DC.CursorPos.y
// Clear data if columns count changed
if len(columns.Columns) != 0 && int(len(columns.Columns)) != columns_count+1 {
columns.Columns = columns.Columns[:0]
}
// Initialize default widths
columns.IsFirstFrame = (len(columns.Columns) == 0)
if len(columns.Columns) == 0 {
for n := int(0); n < columns_count+1; n++ {
var column ImGuiOldColumnData
column.OffsetNorm = float(n) / (float)(columns_count)
columns.Columns = append(columns.Columns, column)
}
}
for n := int(0); n < int(columns_count); n++ {
// Compute clipping rectangle
var column = &columns.Columns[n]
var clip_x1 = IM_ROUND(window.Pos.x + GetColumnOffset(n))
var clip_x2 = IM_ROUND(window.Pos.x + GetColumnOffset(n+1) - 1.0)
column.ClipRect = ImRect{ImVec2{clip_x1, -FLT_MAX}, ImVec2{clip_x2, +FLT_MAX}}
column.ClipRect.ClipWithFull(window.ClipRect)
}
if columns.Count > 1 {
columns.Splitter.Split(window.DrawList, 1+columns.Count)
columns.Splitter.SetCurrentChannel(window.DrawList, 1)
PushColumnClipRect(0)
}
// We don't generally store Indent.x inside ColumnsOffset because it may be manipulated by the user.
var offset_0 = GetColumnOffset(columns.Current)
var offset_1 = GetColumnOffset(columns.Current + 1)
var width = offset_1 - offset_0
PushItemWidth(width * 0.65)
window.DC.ColumnsOffset.x = ImMax(column_padding-window.WindowPadding.x, 0.0)
window.DC.CursorPos.x = IM_FLOOR(window.Pos.x + window.DC.Indent.x + window.DC.ColumnsOffset.x)
window.WorkRect.Max.x = window.Pos.x + offset_1 - column_padding
}
// close columns
func EndColumns() {
var g = GImGui
var window = GetCurrentWindow()
var columns = window.DC.CurrentColumns
IM_ASSERT(columns != nil)
PopItemWidth()
if columns.Count > 1 {
PopClipRect()
columns.Splitter.Merge(window.DrawList)
}
var flags = columns.Flags
columns.LineMaxY = ImMax(columns.LineMaxY, window.DC.CursorPos.y)
window.DC.CursorPos.y = columns.LineMaxY
if (flags & ImGuiOldColumnFlags_GrowParentContentsSize) == 0 {
window.DC.CursorMaxPos.x = columns.HostCursorMaxPosX // Restore cursor max pos, as columns don't grow parent
}
// Draw columns borders and handle resize
// The IsBeingResized flag ensure we preserve pre-resize columns width so back-and-forth are not lossy
var is_being_resized = false
if (flags&ImGuiOldColumnFlags_NoBorder) == 0 && !window.SkipItems {
// We clip Y boundaries CPU side because very long triangles are mishandled by some GPU drivers.
var y1 = ImMax(columns.HostCursorPosY, window.ClipRect.Min.y)
var y2 = ImMin(window.DC.CursorPos.y, window.ClipRect.Max.y)
var dragging_column int = -1
for n := int(1); n < columns.Count; n++ {
var column = &columns.Columns[n]
var x = window.Pos.x + GetColumnOffset(n)
var column_id = columns.ID + ImGuiID(n)
var column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH
var column_hit_rect = ImRect{ImVec2{x - column_hit_hw, y1}, ImVec2{x + column_hit_hw, y2}}
KeepAliveID(column_id)
if IsClippedEx(&column_hit_rect, column_id, false) {
continue
}
var hovered, held = false, false
if (flags & ImGuiOldColumnFlags_NoResize) == 0 {
ButtonBehavior(&column_hit_rect, column_id, &hovered, &held, 0)
if hovered || held {
g.MouseCursor = ImGuiMouseCursor_ResizeEW
}
if held && (column.Flags&ImGuiOldColumnFlags_NoResize) == 0 {
dragging_column = n
}
}
// Draw column
var col ImU32
if held {
col = GetColorU32FromID(ImGuiCol_SeparatorActive, 1)
} else if hovered {
col = GetColorU32FromID(ImGuiCol_SeparatorHovered, 1)
} else {
col = GetColorU32FromID(ImGuiCol_Separator, 1)
}
var xi = IM_FLOOR(x)
window.DrawList.AddLine(&ImVec2{xi, y1 + 1.0}, &ImVec2{xi, y2}, col, 1)
}
// Apply dragging after drawing the column lines, so our rendered lines are in sync with how items were displayed during the frame.
if dragging_column != -1 {
if !columns.IsBeingResized {
for n := int(0); n < columns.Count+1; n++ {
columns.Columns[n].OffsetNormBeforeResize = columns.Columns[n].OffsetNorm
}
}
columns.IsBeingResized = true
is_being_resized = true
var x = GetDraggedColumnOffset(columns, dragging_column)
SetColumnOffset(dragging_column, x)
}
}
columns.IsBeingResized = is_being_resized
window.WorkRect = window.ParentWorkRect
window.ParentWorkRect = columns.HostBackupParentWorkRect
window.DC.CurrentColumns = nil
window.DC.ColumnsOffset.x = 0.0
window.DC.CursorPos.x = IM_FLOOR(window.Pos.x + window.DC.Indent.x + window.DC.ColumnsOffset.x)
}
func PushColumnClipRect(column_index int) {
var window = GetCurrentWindowRead()
var columns = window.DC.CurrentColumns
if column_index < 0 {
column_index = columns.Current
}
var column = &columns.Columns[column_index]
PushClipRect(column.ClipRect.Min, column.ClipRect.Max, false)
}
func PopColumnsBackground() {
var window = GetCurrentWindowRead()
var columns = window.DC.CurrentColumns
if columns.Count == 1 {
return
}
// Optimization: avoid PopClipRect() + SetCurrentChannel()
SetWindowClipRectBeforeSetChannel(window, &columns.HostBackupClipRect)
columns.Splitter.SetCurrentChannel(window.DrawList, columns.Current+1)
}
func GetColumnsID(str_id string, columns_count int) ImGuiID {
var window = GetCurrentWindow()
var id ImGuiID
// Differentiate column ID with an arbitrary prefix for cases where users name their columns set the same as another widget.
// In addition, when an identifier isn't explicitly provided we include the number of columns in the hash to make it uniquer.
if str_id != "" {
PushID(0x11223347)
id = window.GetIDs(str_id)
} else {
PushID(0x11223347 + columns_count)
id = window.GetIDs("columns")
}
PopID()
return id
}
func FindOrCreateColumns(window *ImGuiWindow, id ImGuiID) *ImGuiOldColumns {
// We have few columns per window so for now we don't need bother much with turning this into a faster lookup.
for n := range window.ColumnsStorage {
if window.ColumnsStorage[n].ID == id {
return &window.ColumnsStorage[n]
}
}
window.ColumnsStorage = append(window.ColumnsStorage, ImGuiOldColumns{})
var columns = &window.ColumnsStorage[len(window.ColumnsStorage)-1]
columns.ID = id
return columns
}
func GetColumnOffsetFromNorm(columns *ImGuiOldColumns, offset_norm float) float {
return offset_norm * (columns.OffMaxX - columns.OffMinX)
}
func GetColumnNormFromOffset(columns *ImGuiOldColumns, offset float) float {
return offset / (columns.OffMaxX - columns.OffMinX)
}
// Legacy Columns API (prefer using Tables!)
// - You can also use SameLine(pos_x) to mimic simplified columns.
func Columns(columns_count int /*= 1*/, id string /*= L*/, border bool /*= true*/) {
var window = GetCurrentWindow()
IM_ASSERT(columns_count >= 1)
var flags = ImGuiOldColumnFlags_NoBorder
if border {
flags = 0
}
//flags |= ImGuiOldColumnFlags_NoPreserveWidths; // NB: Legacy behavior
var columns = window.DC.CurrentColumns
if columns != nil && columns.Count == columns_count && columns.Flags == flags {
return
}
if columns != nil {
EndColumns()
}
if columns_count != 1 {
BeginColumns(id, columns_count, flags)
}
}
// next column, defaults to current row or next row if the current row is finished
func NextColumn() {
var window = GetCurrentWindow()
if window.SkipItems || window.DC.CurrentColumns == nil {
return
}
var g = GImGui
var columns = window.DC.CurrentColumns
if columns.Count == 1 {
window.DC.CursorPos.x = IM_FLOOR(window.Pos.x + window.DC.Indent.x + window.DC.ColumnsOffset.x)
IM_ASSERT(columns.Current == 0)
return
}
// Next column
columns.Current++
if columns.Current == columns.Count {
columns.Current = 0
}
PopItemWidth()
// Optimization: avoid PopClipRect() + SetCurrentChannel() + PushClipRect()
// (which would needlessly attempt to update commands in the wrong channel, then pop or overwrite them),
var column = &columns.Columns[columns.Current]
SetWindowClipRectBeforeSetChannel(window, &column.ClipRect)
columns.Splitter.SetCurrentChannel(window.DrawList, columns.Current+1)
var column_padding = g.Style.ItemSpacing.x
columns.LineMaxY = ImMax(columns.LineMaxY, window.DC.CursorPos.y)
if columns.Current > 0 {
// Columns 1+ ignore IndentX (by canceling it out)
// FIXME-COLUMNS: Unnecessary, could be locked?
window.DC.ColumnsOffset.x = GetColumnOffset(columns.Current) - window.DC.Indent.x + column_padding
} else {
// New row/line: column 0 honor IndentX.
window.DC.ColumnsOffset.x = ImMax(column_padding-window.WindowPadding.x, 0.0)
columns.LineMinY = columns.LineMaxY
}
window.DC.CursorPos.x = IM_FLOOR(window.Pos.x + window.DC.Indent.x + window.DC.ColumnsOffset.x)
window.DC.CursorPos.y = columns.LineMinY
window.DC.CurrLineSize = ImVec2{0.0, 0.0}
window.DC.CurrLineTextBaseOffset = 0.0
// FIXME-COLUMNS: Share code with BeginColumns() - move code on columns setup.
var offset_0 = GetColumnOffset(columns.Current)
var offset_1 = GetColumnOffset(columns.Current + 1)
var width = offset_1 - offset_0
PushItemWidth(width * 0.65)
window.WorkRect.Max.x = window.Pos.x + offset_1 - column_padding
}
// get current column index
func GetColumnIndex() int {
var window = GetCurrentWindowRead()
if window.DC.CurrentColumns != nil {
return window.DC.CurrentColumns.Current
}
return 0
}
// get column width (in pixels). pass -1 to use current column
func GetColumnWidth(column_index int /*= -1*/) float {
var g = GImGui
var window = g.CurrentWindow
var columns = window.DC.CurrentColumns
if columns == nil {
return GetContentRegionAvail().x
}
if column_index < 0 {
column_index = columns.Current
}
return GetColumnOffsetFromNorm(columns, columns.Columns[column_index+1].OffsetNorm-columns.Columns[column_index].OffsetNorm)
}
// set column width (in pixels). pass -1 to use current column
func SetColumnWidth(column_index int, width float) {
var window = GetCurrentWindowRead()
var columns = window.DC.CurrentColumns
IM_ASSERT(columns != nil)
if column_index < 0 {
column_index = columns.Current
}
SetColumnOffset(column_index+1, GetColumnOffset(column_index)+width)
}
// get position of column line (in pixels, from the left side of the contents region). pass -1 to use current column, otherwise 0..GetColumnsCount() inclusive. column 0 is typically 0.0
func GetColumnOffset(column_index int /*= -1*/) float {
var window = GetCurrentWindowRead()
var columns = window.DC.CurrentColumns
if columns == nil {
return 0.0
}
if column_index < 0 {
column_index = columns.Current
}
IM_ASSERT(column_index < int(len(columns.Columns)))
var t = columns.Columns[column_index].OffsetNorm
var x_offset = ImLerp(columns.OffMinX, columns.OffMaxX, t)
return x_offset
}
// set position of column line (in pixels, from the left side of the contents region). pass -1 to use current column
func SetColumnOffset(column_index int, offset float) {
var g = GImGui
var window = g.CurrentWindow
var columns = window.DC.CurrentColumns
IM_ASSERT(columns != nil)
if column_index < 0 {
column_index = columns.Current
}
IM_ASSERT(column_index < int(len(columns.Columns)))
var preserve_width = (columns.Flags&ImGuiOldColumnFlags_NoPreserveWidths) == 0 && (column_index < columns.Count-1)
var width float = 0.0
if preserve_width {
width = GetColumnWidthEx(columns, column_index, columns.IsBeingResized)
}
if (columns.Flags & ImGuiOldColumnFlags_NoForceWithinWindow) == 0 {
offset = ImMin(offset, columns.OffMaxX-g.Style.ColumnsMinSpacing*float(columns.Count-column_index))
}
columns.Columns[column_index].OffsetNorm = GetColumnNormFromOffset(columns, offset-columns.OffMinX)
if preserve_width {
SetColumnOffset(column_index+1, offset+ImMax(g.Style.ColumnsMinSpacing, width))
}
}
func GetColumnsCount() int {
var window = GetCurrentWindowRead()
if window.DC.CurrentColumns != nil {
return window.DC.CurrentColumns.Count
}
return 1
}
func GetDraggedColumnOffset(columns *ImGuiOldColumns, column_index int) float {
// Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing
// window creates a feedback loop because we store normalized positions. So while dragging we enforce absolute positioning.
var g = GImGui
var window = g.CurrentWindow
IM_ASSERT(column_index > 0) // We are not supposed to drag column 0.
IM_ASSERT(g.ActiveId == columns.ID+ImGuiID(column_index))
var x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window.Pos.x
x = ImMax(x, GetColumnOffset(column_index-1)+g.Style.ColumnsMinSpacing)
if (columns.Flags & ImGuiOldColumnFlags_NoPreserveWidths) != 0 {
x = ImMin(x, GetColumnOffset(column_index+1)-g.Style.ColumnsMinSpacing)
}
return x
}
func GetColumnWidthEx(columns *ImGuiOldColumns, column_index int, before_resize bool) float {
if column_index < 0 {
column_index = columns.Current
}
var offset_norm float
if before_resize {
offset_norm = columns.Columns[column_index+1].OffsetNormBeforeResize - columns.Columns[column_index].OffsetNormBeforeResize
} else {
offset_norm = columns.Columns[column_index+1].OffsetNorm - columns.Columns[column_index].OffsetNorm
}
return GetColumnOffsetFromNorm(columns, offset_norm)
}