forked from HACKERALERT/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Style.go
415 lines (360 loc) · 13.2 KB
/
Style.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
package giu
import (
"image/color"
"github.com/Picocrypt/imgui-go"
)
// PushFont sets font to "font"
// NOTE: PopFont has to be called
// NOTE: Don't use PushFont. use StyleSetter instead
func PushFont(font *FontInfo) bool {
if f, ok := extraFontMap[font.String()]; ok {
imgui.PushFont(*f)
return true
}
return false
}
// PopFont pops the font (should be called after PushFont)
func PopFont() {
imgui.PopFont()
}
// PushStyleColor wrapps imgui.PushStyleColor
// NOTE: don't forget to call PopStyleColor()!
func PushStyleColor(id StyleColorID, col color.RGBA) {
imgui.PushStyleColor(imgui.StyleColorID(id), ToVec4Color(col))
}
// PushColorText calls PushStyleColor(StyleColorText,...)
// NOTE: don't forget to call PopStyleColor()!
func PushColorText(col color.RGBA) {
imgui.PushStyleColor(imgui.StyleColorText, ToVec4Color(col))
}
// PushColorTextDisabled calls PushStyleColor(StyleColorTextDisabled,...)
// NOTE: don't forget to call PopStyleColor()!
func PushColorTextDisabled(col color.RGBA) {
imgui.PushStyleColor(imgui.StyleColorTextDisabled, ToVec4Color(col))
}
// PushColorWindowBg calls PushStyleColor(StyleColorWindowBg,...)
// NOTE: don't forget to call PopStyleColor()!
func PushColorWindowBg(col color.RGBA) {
imgui.PushStyleColor(imgui.StyleColorWindowBg, ToVec4Color(col))
}
// PushColorFrameBg calls PushStyleColor(StyleColorFrameBg,...)
// NOTE: don't forget to call PopStyleColor()!
func PushColorFrameBg(col color.RGBA) {
imgui.PushStyleColor(imgui.StyleColorFrameBg, ToVec4Color(col))
}
// PushColorButton calls PushStyleColor(StyleColorButton,...)
// NOTE: don't forget to call PopStyleColor()!
func PushColorButton(col color.RGBA) {
imgui.PushStyleColor(imgui.StyleColorButton, ToVec4Color(col))
}
// PushColorButtonHovered calls PushStyleColor(StyleColorButtonHovered,...)
// NOTE: don't forget to call PopStyleColor()!
func PushColorButtonHovered(col color.RGBA) {
imgui.PushStyleColor(imgui.StyleColorButtonHovered, ToVec4Color(col))
}
// PushColorButtonActive calls PushStyleColor(StyleColorButtonActive,...)
// NOTE: don't forget to call PopStyleColor()!
func PushColorButtonActive(col color.RGBA) {
imgui.PushStyleColor(imgui.StyleColorButtonActive, ToVec4Color(col))
}
// PushWindowPadding calls PushStyleVar(StyleWindowPadding,...)
func PushWindowPadding(width, height float32) {
width *= Context.GetPlatform().GetContentScale()
height *= Context.GetPlatform().GetContentScale()
imgui.PushStyleVarVec2(imgui.StyleVarWindowPadding, imgui.Vec2{X: width, Y: height})
}
// PushFramePadding calls PushStyleVar(StyleFramePadding,...)
func PushFramePadding(width, height float32) {
width *= Context.GetPlatform().GetContentScale()
height *= Context.GetPlatform().GetContentScale()
imgui.PushStyleVarVec2(imgui.StyleVarFramePadding, imgui.Vec2{X: width, Y: height})
}
// PushItemSpacing calls PushStyleVar(StyleVarItemSpacing,...)
func PushItemSpacing(width, height float32) {
width *= Context.GetPlatform().GetContentScale()
height *= Context.GetPlatform().GetContentScale()
imgui.PushStyleVarVec2(imgui.StyleVarItemSpacing, imgui.Vec2{X: width, Y: height})
}
// PushButtonTextAlign sets alignment for button text. Defaults to (0.0f,0.5f) for left-aligned,vertically centered.
func PushButtonTextAlign(width, height float32) {
width *= Context.GetPlatform().GetContentScale()
height *= Context.GetPlatform().GetContentScale()
imgui.PushStyleVarVec2(imgui.StyleVarButtonTextAlign, imgui.Vec2{X: width, Y: height})
}
// PushSelectableTextAlign sets alignment for selectable text. Defaults to (0.0f,0.5f) for left-aligned,vertically centered.
func PushSelectableTextAlign(width, height float32) {
width *= Context.GetPlatform().GetContentScale()
height *= Context.GetPlatform().GetContentScale()
imgui.PushStyleVarVec2(imgui.StyleVarSelectableTextAlign, imgui.Vec2{X: width, Y: height})
}
// PopStyle should be called to stop applying style.
// It should be called as much times, as you Called PushStyle...
// NOTE: If you don't call PopStyle imgui will panic
func PopStyle() {
imgui.PopStyleVar()
}
// PopStyleV does similarly to PopStyle, but allows to specify number
// of styles you're going to pop
func PopStyleV(count int) {
imgui.PopStyleVarV(count)
}
// PopStyleColor is used to stop applying colors styles.
// It should be called after each PushStyleColor... (for each push)
// If PopStyleColor wasn't called after PushColor... or was called
// inproperly, imgui will panic
func PopStyleColor() {
imgui.PopStyleColor()
}
// PopStyleColorV does similar to PopStyleColor, but allows to specify
// how much style colors would you like to pop
func PopStyleColorV(count int) {
imgui.PopStyleColorV(count)
}
// AlignTextToFramePadding vertically aligns upcoming text baseline to
// FramePadding.y so that it will align properly to regularly framed
// items. Call if you have text on a line before a framed item.
func AlignTextToFramePadding() {
imgui.AlignTextToFramePadding()
}
// PushItemWidth sets following item's widths
// NOTE: don't forget to call PopItemWidth! If you don't do so, imgui
// will panic
func PushItemWidth(width float32) {
width *= Context.GetPlatform().GetContentScale()
imgui.PushItemWidth(width)
}
// PopItemWidth should be called to stop applying PushItemWidth effect
// If it isn't called imgui will panic
func PopItemWidth() {
imgui.PopItemWidth()
}
func PushTextWrapPos() {
imgui.PushTextWrapPos()
}
func PopTextWrapPos() {
imgui.PopTextWrapPos()
}
// MouseCursorType represents a type (layout) of mouse cursor
type MouseCursorType int
const (
// MouseCursorNone no mouse cursor
MouseCursorNone MouseCursorType = -1
// MouseCursorArrow standard arrow mouse cursor
MouseCursorArrow MouseCursorType = 0
// MouseCursorTextInput when hovering over InputText, etc.
MouseCursorTextInput MouseCursorType = 1
// MouseCursorResizeAll (Unused by imgui functions)
MouseCursorResizeAll MouseCursorType = 2
// MouseCursorResizeNS when hovering over an horizontal border
MouseCursorResizeNS MouseCursorType = 3
// MouseCursorResizeEW when hovering over a vertical border or a column
MouseCursorResizeEW MouseCursorType = 4
// MouseCursorResizeNESW when hovering over the bottom-left corner of a window
MouseCursorResizeNESW MouseCursorType = 5
// MouseCursorResizeNWSE when hovering over the bottom-right corner of a window
MouseCursorResizeNWSE MouseCursorType = 6
// MouseCursorHand (Unused by imgui functions. Use for e.g. hyperlinks)
MouseCursorHand MouseCursorType = 7
MouseCursorCount MouseCursorType = 8
)
// SetMouseCursor sets mouse cursor layout
func SetMouseCursor(cursor MouseCursorType) {
imgui.SetMouseCursor(int(cursor))
}
// GetWindowPadding returns window padding
func GetWindowPadding() (x, y float32) {
vec2 := imgui.CurrentStyle().WindowPadding()
return vec2.X, vec2.Y
}
// GetItemSpacing returns current item spacing
func GetItemSpacing() (w, h float32) {
vec2 := imgui.CurrentStyle().ItemSpacing()
return vec2.X, vec2.Y
}
// GetItemInnerSpacing returns current item inner spacing
func GetItemInnerSpacing() (w, h float32) {
vec2 := imgui.CurrentStyle().ItemInnerSpacing()
return vec2.X, vec2.Y
}
// GetFramePadding returns current frame padding
func GetFramePadding() (x, y float32) {
vec2 := imgui.CurrentStyle().FramePadding()
return vec2.X, vec2.Y
}
// StyleColorID identifies a color in the UI style.
type StyleColorID int
// StyleColor identifier
const (
StyleColorText StyleColorID = 0
StyleColorTextDisabled StyleColorID = 1
StyleColorWindowBg StyleColorID = 2
StyleColorChildBg StyleColorID = 3
StyleColorPopupBg StyleColorID = 4
StyleColorBorder StyleColorID = 5
StyleColorBorderShadow StyleColorID = 6
StyleColorFrameBg StyleColorID = 7
StyleColorFrameBgHovered StyleColorID = 8
StyleColorFrameBgActive StyleColorID = 9
StyleColorTitleBg StyleColorID = 10
StyleColorTitleBgActive StyleColorID = 11
StyleColorTitleBgCollapsed StyleColorID = 12
StyleColorMenuBarBg StyleColorID = 13
StyleColorScrollbarBg StyleColorID = 14
StyleColorScrollbarGrab StyleColorID = 15
StyleColorScrollbarGrabHovered StyleColorID = 16
StyleColorScrollbarGrabActive StyleColorID = 17
StyleColorCheckMark StyleColorID = 18
StyleColorSliderGrab StyleColorID = 19
StyleColorSliderGrabActive StyleColorID = 20
StyleColorButton StyleColorID = 21
StyleColorButtonHovered StyleColorID = 22
StyleColorButtonActive StyleColorID = 23
StyleColorHeader StyleColorID = 24
StyleColorHeaderHovered StyleColorID = 25
StyleColorHeaderActive StyleColorID = 26
StyleColorSeparator StyleColorID = 27
StyleColorSeparatorHovered StyleColorID = 28
StyleColorSeparatorActive StyleColorID = 29
StyleColorResizeGrip StyleColorID = 30
StyleColorResizeGripHovered StyleColorID = 31
StyleColorResizeGripActive StyleColorID = 32
StyleColorTab StyleColorID = 33
StyleColorTabHovered StyleColorID = 34
StyleColorTabActive StyleColorID = 35
StyleColorTabUnfocused StyleColorID = 36
StyleColorTabUnfocusedActive StyleColorID = 37
StyleColorPlotLines StyleColorID = 38
StyleColorPlotLinesHovered StyleColorID = 39
StyleColorProgressBarActive StyleColorID = 40
StyleColorPlotHistogram StyleColorID = 40
StyleColorPlotHistogramHovered StyleColorID = 41
StyleColorTableHeaderBg StyleColorID = 42
StyleColorTableBorderStrong StyleColorID = 43
StyleColorTableBorderLight StyleColorID = 44
StyleColorTableRowBg StyleColorID = 45
StyleColorTableRowBgAlt StyleColorID = 46
StyleColorTextSelectedBg StyleColorID = 47
StyleColorDragDropTarget StyleColorID = 48
StyleColorNavHighlight StyleColorID = 49
StyleColorNavWindowingHighlight StyleColorID = 50
StyleColorNavWindowingDimBg StyleColorID = 51
StyleColorModalWindowDimBg StyleColorID = 52
)
// StyleVarID identifies a style variable in the UI style.
type StyleVarID int
// Style IDs
const (
// StyleVarAlpha is a float
StyleVarAlpha StyleVarID = iota
// float DisabledAlpha
StyleVarDisabledAlpha
// StyleVarWindowPadding is a Vec2
StyleVarWindowPadding
// StyleVarWindowRounding is a float
StyleVarWindowRounding
// StyleVarWindowBorderSize is a float
StyleVarWindowBorderSize
// StyleVarWindowMinSize is a Vec2
StyleVarWindowMinSize
// StyleVarWindowTitleAlign is a Vec2
StyleVarWindowTitleAlign
// StyleVarChildRounding is a float
StyleVarChildRounding
// StyleVarChildBorderSize is a float
StyleVarChildBorderSize
// StyleVarPopupRounding is a float
StyleVarPopupRounding
// StyleVarPopupBorderSize is a float
StyleVarPopupBorderSize
// StyleVarFramePadding is a Vec2
StyleVarFramePadding
// StyleVarFrameRounding is a float
StyleVarFrameRounding
// StyleVarFrameBorderSize is a float
StyleVarFrameBorderSize
// StyleVarItemSpacing is a Vec2
StyleVarItemSpacing
// StyleVarItemInnerSpacing is a Vec2
StyleVarItemInnerSpacing
// StyleVarIndentSpacing is a float
StyleVarIndentSpacing
// StyleVarScrollbarSize is a float
StyleVarScrollbarSize
// StyleVarScrollbarRounding is a float
StyleVarScrollbarRounding
// StyleVarGrabMinSize is a float
StyleVarGrabMinSize
// StyleVarGrabRounding is a float
StyleVarGrabRounding
// StyleVarTabRounding is a float
StyleVarTabRounding
// StyleVarButtonTextAlign is a Vec2
StyleVarButtonTextAlign
// StyleVarSelectableTextAlign is a Vec2
StyleVarSelectableTextAlign
)
var _ Widget = &StyleSetter{}
// StyleSetter is a user-friendly way to manage imgui styles
type StyleSetter struct {
colors map[StyleColorID]color.RGBA
styles map[StyleVarID]imgui.Vec2
font *FontInfo
disabled bool
layout Layout
}
// Style initializes a style setter (see examples/setstyle)
func Style() *StyleSetter {
var ss StyleSetter
ss.colors = make(map[StyleColorID]color.RGBA)
ss.styles = make(map[StyleVarID]imgui.Vec2)
return &ss
}
// SetColor sets colorID's color
func (ss *StyleSetter) SetColor(colorID StyleColorID, col color.RGBA) *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
}
// SetFont sets font
func (ss *StyleSetter) SetFont(font *FontInfo) *StyleSetter {
ss.font = font
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
}
// Build implements Widget
func (ss *StyleSetter) Build() {
if ss.layout == nil || len(ss.layout) == 0 {
return
}
for k, v := range ss.colors {
imgui.PushStyleColor(imgui.StyleColorID(k), ToVec4Color(v))
}
for k, v := range ss.styles {
imgui.PushStyleVarVec2(imgui.StyleVarID(k), v)
}
isFontPushed := false
if ss.font != nil {
isFontPushed = PushFont(ss.font)
}
imgui.BeginDisabled(ss.disabled)
ss.layout.Build()
imgui.EndDisabled()
if isFontPushed {
PopFont()
}
imgui.PopStyleColorV(len(ss.colors))
imgui.PopStyleVarV(len(ss.styles))
}