-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
StyleSetter.go
275 lines (231 loc) · 6.33 KB
/
StyleSetter.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
package giu
import (
"image/color"
"github.com/AllenDang/cimgui-go/imgui"
"github.com/AllenDang/cimgui-go/implot"
)
var _ Widget = &StyleSetter{}
// StyleSetter is a user-friendly way to manage imgui styles.
// For style IDs see StyleIDs.go, for detailed instruction of using styles, see Styles.go.
type StyleSetter struct {
colors map[StyleColorID]color.Color
styles map[StyleVarID]any
plotColors map[StylePlotColorID]color.Color
plotStyles map[StylePlotVarID]any
font *FontInfo
disabled bool
layout Layout
plots []PlotWidget
// set by imgui.PushFont inside ss.Push() method
isFontPushed bool
}
// Style initializes a style setter (see examples/setstyle).
func Style() *StyleSetter {
var ss StyleSetter
ss.colors = make(map[StyleColorID]color.Color)
ss.plotColors = make(map[StylePlotColorID]color.Color)
ss.styles = make(map[StyleVarID]any)
ss.plotStyles = make(map[StylePlotVarID]any)
return &ss
}
// SetColor sets colorID's color.
func (ss *StyleSetter) SetColor(colorID StyleColorID, col color.Color) *StyleSetter {
ss.colors[colorID] = col
return ss
}
// SetStyle sets styleVarID to width and height.
func (ss *StyleSetter) SetStyle(varID StyleVarID, width, height float32) *StyleSetter {
ss.styles[varID] = imgui.Vec2{X: width, Y: height}
return ss
}
// SetStyleFloat sets styleVarID to float value.
// NOTE: for float typed values see above in comments over
// StyleVarID's comments.
func (ss *StyleSetter) SetStyleFloat(varID StyleVarID, value float32) *StyleSetter {
ss.styles[varID] = value
return ss
}
// SetPlotColor sets colorID's color.
func (ss *StyleSetter) SetPlotColor(colorID StylePlotColorID, col color.Color) *StyleSetter {
ss.plotColors[colorID] = col
return ss
}
// SetPlotStyle sets stylePlotVarID to width and height.
func (ss *StyleSetter) SetPlotStyle(varID StylePlotVarID, width, height float32) *StyleSetter {
ss.plotStyles[varID] = imgui.Vec2{X: width, Y: height}
return ss
}
// SetPlotStyleFloat sets StylePlotVarID to float value.
// NOTE: for float typed values see above in comments over
// StyleVarID's comments.
func (ss *StyleSetter) SetPlotStyleFloat(varID StylePlotVarID, value float32) *StyleSetter {
ss.plotStyles[varID] = value
return ss
}
// SetFont sets font.
func (ss *StyleSetter) SetFont(font *FontInfo) *StyleSetter {
ss.font = font
return ss
}
// SetFontSize sets size of the font.
// NOTE: Be aware, that StyleSetter needs to add a new font to font atlas for
// each font's size.
func (ss *StyleSetter) SetFontSize(size float32) *StyleSetter {
var font FontInfo
if ss.font != nil {
font = *ss.font
} else {
font = Context.FontAtlas.defaultFonts[0]
}
ss.font = font.SetSize(size)
return ss
}
// SetDisabled sets if items are disabled.
func (ss *StyleSetter) SetDisabled(d bool) *StyleSetter {
ss.disabled = d
return ss
}
// To allows to specify a layout, StyleSetter should apply style for.
func (ss *StyleSetter) To(widgets ...Widget) *StyleSetter {
ss.layout = widgets
return ss
}
// Plots allows to set plots to apply style for.
func (ss *StyleSetter) Plots(widgets ...PlotWidget) *StyleSetter {
ss.plots = widgets
return ss
}
// Range implements Splitable interface.
func (ss *StyleSetter) Range(rangeFunc func(w Widget)) {
if ss.layout != nil {
var result Layout
// need to consider the following cases:
// if 0 - return
// if 1 - joing push/render/pop in one step
// else - join push with first widget, than render another
// widgets and in the end render last widget with pop func
// it is, because Push/Pop don't move cursor so
// they doesn't exist for imgui in fact.
// It leads to problemms with RowWidget
//
// see: https://github.com/AllenDang/giu/issues/619
layoutLen := len(ss.layout)
switch layoutLen {
case 0:
return
case 1:
result = Layout{
Custom(func() {
ss.Push()
ss.layout.Build()
ss.Pop()
}),
}
default:
result = Layout{
Custom(func() {
ss.Push()
ss.layout[0].Build()
}),
ss.layout[1 : len(ss.layout)-1],
Custom(func() {
ss.layout[layoutLen-1].Build()
ss.Pop()
}),
}
}
result.Range(rangeFunc)
}
}
// Build implements Widget.
func (ss *StyleSetter) Build() {
if len(ss.layout) == 0 {
return
}
ss.Push()
ss.layout.Build()
ss.Pop()
}
// Plot implements PlotWidget.
func (ss *StyleSetter) Plot() {
if len(ss.plots) == 0 {
return
}
ss.Push()
for _, plot := range ss.plots {
plot.Plot()
}
ss.Pop()
}
// Push allows to manually activate Styles written inside of StyleSetter
// it works like imgui.PushXXX() stuff, but for group of style variables,
// just like StyleSetter.
// NOTE: DO NOT ORGET to call ss.Pop() at the end of styled layout, because
// else you'll get ImGui exception!
func (ss *StyleSetter) Push() {
// Push colors
for k, v := range ss.colors {
imgui.PushStyleColorVec4(imgui.Col(k), ToVec4Color(v))
}
// Push plot colors
for k, v := range ss.plotColors {
implot.PushStyleColorVec4(implot.Col(k), ToVec4Color(v))
}
// push style vars
for k, v := range ss.styles {
pushVarID(k.IsVec2(), v, func(value float32) {
imgui.PushStyleVarFloat(imgui.StyleVar(k), value)
}, func(value imgui.Vec2) {
imgui.PushStyleVarVec2(imgui.StyleVar(k), value)
})
}
// Push plot colors
for k, v := range ss.plotStyles {
pushVarID(k.IsVec2(), v, func(value float32) {
implot.PushStyleVarFloat(implot.StyleVar(k), value)
}, func(value imgui.Vec2) {
implot.PushStyleVarVec2(implot.StyleVar(k), value)
})
}
// push font
if ss.font != nil {
ss.isFontPushed = PushFont(ss.font)
}
if ss.disabled {
imgui.BeginDisabled()
}
}
// Pop allows to manually pop the whole StyleSetter (use after Push!)
func (ss *StyleSetter) Pop() {
if ss.isFontPushed {
imgui.PopFont()
}
if ss.disabled {
imgui.EndDisabled()
}
imgui.PopStyleColorV(int32(len(ss.colors)))
implot.PopStyleColorV(int32(len(ss.plotColors)))
imgui.PopStyleVarV(int32(len(ss.styles)))
implot.PopStyleVarV(int32(len(ss.plotStyles)))
}
func pushVarID(isVec2 bool, v any, pushFloat func(float32), pushVec2 func(imgui.Vec2)) {
if isVec2 {
var value imgui.Vec2
switch typed := v.(type) {
case imgui.Vec2:
value = typed
case float32:
value = imgui.Vec2{X: typed, Y: typed}
}
pushVec2(value)
} else {
var value float32
switch typed := v.(type) {
case float32:
value = typed
case imgui.Vec2:
value = typed.X
}
pushFloat(value)
}
}