forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableWidgets.go
214 lines (175 loc) · 4.35 KB
/
TableWidgets.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
package giu
import (
"image/color"
"github.com/AllenDang/imgui-go"
)
type TableRowWidget struct {
flags TableRowFlags
minRowHeight float64
layout Layout
bgColor color.Color
}
func TableRow(widgets ...Widget) *TableRowWidget {
return &TableRowWidget{
flags: 0,
minRowHeight: 0,
layout: widgets,
bgColor: nil,
}
}
func (r *TableRowWidget) BgColor(c color.Color) *TableRowWidget {
r.bgColor = c
return r
}
func (r *TableRowWidget) Flags(flags TableRowFlags) *TableRowWidget {
r.flags = flags
return r
}
func (r *TableRowWidget) MinHeight(height float64) *TableRowWidget {
r.minRowHeight = height
return r
}
// BuildTableRow executes table row build steps.
func (r *TableRowWidget) BuildTableRow() {
imgui.TableNextRow(imgui.TableRowFlags(r.flags), r.minRowHeight)
for _, w := range r.layout {
switch w.(type) {
case *TooltipWidget,
*ContextMenuWidget, *PopupModalWidget:
// noop
default:
imgui.TableNextColumn()
}
w.Build()
}
if r.bgColor != nil {
imgui.TableSetBgColor(imgui.TableBgTarget_RowBg0, uint32(imgui.GetColorU32(ToVec4Color(r.bgColor))), -1)
}
}
type TableColumnWidget struct {
label string
flags TableColumnFlags
innerWidthOrWeight float32
userID uint32
}
func TableColumn(label string) *TableColumnWidget {
return &TableColumnWidget{
label: Context.FontAtlas.RegisterString(label),
flags: 0,
innerWidthOrWeight: 0,
userID: 0,
}
}
func (c *TableColumnWidget) Flags(flags TableColumnFlags) *TableColumnWidget {
c.flags = flags
return c
}
func (c *TableColumnWidget) InnerWidthOrWeight(w float32) *TableColumnWidget {
c.innerWidthOrWeight = w
return c
}
func (c *TableColumnWidget) UserID(id uint32) *TableColumnWidget {
c.userID = id
return c
}
// BuildTableColumn executes table column build steps.
func (c *TableColumnWidget) BuildTableColumn() {
imgui.TableSetupColumn(c.label, imgui.TableColumnFlags(c.flags), c.innerWidthOrWeight, c.userID)
}
var _ Widget = &TableWidget{}
type TableWidget struct {
id string
flags TableFlags
size imgui.Vec2
innerWidth float64
rows []*TableRowWidget
columns []*TableColumnWidget
fastMode bool
freezeRow int
freezeColumn int
}
func Table() *TableWidget {
return &TableWidget{
id: GenAutoID("Table"),
flags: TableFlagsResizable | TableFlagsBorders | TableFlagsScrollY,
rows: nil,
columns: nil,
fastMode: false,
freezeRow: -1,
freezeColumn: -1,
}
}
// ID sets the internal id of table widget.
func (t *TableWidget) ID(id string) *TableWidget {
t.id = id
return t
}
// FastMode Displays visible rows only to boost performance.
func (t *TableWidget) FastMode(b bool) *TableWidget {
t.fastMode = b
return t
}
// Freeze columns/rows so they stay visible when scrolled.
func (t *TableWidget) Freeze(col, row int) *TableWidget {
t.freezeColumn = col
t.freezeRow = row
return t
}
func (t *TableWidget) Columns(cols ...*TableColumnWidget) *TableWidget {
t.columns = cols
return t
}
func (t *TableWidget) Rows(rows ...*TableRowWidget) *TableWidget {
t.rows = rows
return t
}
func (t *TableWidget) Size(width, height float32) *TableWidget {
t.size = imgui.Vec2{X: width, Y: height}
return t
}
func (t *TableWidget) InnerWidth(width float64) *TableWidget {
t.innerWidth = width
return t
}
func (t *TableWidget) Flags(flags TableFlags) *TableWidget {
t.flags = flags
return t
}
// Build implements Widget interface.
func (t *TableWidget) Build() {
if len(t.rows) == 0 {
return
}
colCount := len(t.columns)
if colCount == 0 {
colCount = len(t.rows[0].layout)
}
if imgui.BeginTable(t.id, colCount, imgui.TableFlags(t.flags), t.size, t.innerWidth) {
if t.freezeColumn >= 0 && t.freezeRow >= 0 {
imgui.TableSetupScrollFreeze(t.freezeColumn, t.freezeRow)
}
if len(t.columns) > 0 {
for _, col := range t.columns {
col.BuildTableColumn()
}
imgui.TableHeadersRow()
}
if t.fastMode {
clipper := imgui.NewListClipper()
defer clipper.Delete()
clipper.Begin(len(t.rows))
for clipper.Step() {
for i := clipper.DisplayStart(); i < clipper.DisplayEnd(); i++ {
row := t.rows[i]
row.BuildTableRow()
}
}
clipper.End()
} else {
for _, row := range t.rows {
row.BuildTableRow()
}
}
imgui.EndTable()
}
}