forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyleSetter.go
197 lines (166 loc) · 4.39 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
package giu
import (
"image/color"
"github.com/AllenDang/imgui-go"
)
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
font *FontInfo
disabled bool
layout Layout
// 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.styles = make(map[StyleVarID]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
}
// 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
}
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 ss.layout == nil || len(ss.layout) == -1 {
return
}
ss.Push()
ss.layout.Build()
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.PushStyleColor(imgui.StyleColorID(k), ToVec4Color(v))
}
// push style vars
for k, v := range ss.styles {
if k.IsVec2() {
var value imgui.Vec2
switch typed := v.(type) {
case imgui.Vec2:
value = typed
case float32:
value = imgui.Vec2{X: typed, Y: typed}
}
imgui.PushStyleVarVec2(imgui.StyleVarID(k), value)
} else {
var value float32
switch typed := v.(type) {
case float32:
value = typed
case imgui.Vec2:
value = typed.X
}
imgui.PushStyleVarFloat(imgui.StyleVarID(k), value)
}
}
// push font
if ss.font != nil {
ss.isFontPushed = PushFont(ss.font)
}
imgui.BeginDisabled(ss.disabled)
}
// Pop allows to manually pop the whole StyleSetter (use after Push!)
func (ss *StyleSetter) Pop() {
if ss.isFontPushed {
imgui.PopFont()
}
imgui.EndDisabled()
imgui.PopStyleColorV(len(ss.colors))
imgui.PopStyleVarV(len(ss.styles))
}