forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SliderWidgets.go
243 lines (205 loc) · 5.64 KB
/
SliderWidgets.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
package giu
import (
"fmt"
imgui "github.com/AllenDang/cimgui-go"
)
var _ Widget = &SliderIntWidget{}
// SliderIntWidget is a slider around int32 values.
type SliderIntWidget struct {
label ID
value *int32
minValue int32
maxValue int32
format string
width float32
onChange func()
}
// SliderInt constructs new SliderIntWidget.
func SliderInt(value *int32, minValue, maxValue int32) *SliderIntWidget {
return &SliderIntWidget{
label: GenAutoID("##SliderInt"),
value: value,
minValue: minValue,
maxValue: maxValue,
format: "%d",
width: 0,
onChange: nil,
}
}
// Format sets data format displayed on the slider
// NOTE: on C side of imgui, it will be processed like:
// fmt.Sprintf(format, currentValue) so you can do e.g.
// SLiderInt(...).Format("My age is %d") and %d will be replaced with current value.
func (s *SliderIntWidget) Format(format string) *SliderIntWidget {
s.format = format
return s
}
// Size sets slider's width.
func (s *SliderIntWidget) Size(width float32) *SliderIntWidget {
s.width = width
return s
}
// OnChange sets callback when slider's position gets changed.
func (s *SliderIntWidget) OnChange(onChange func()) *SliderIntWidget {
s.onChange = onChange
return s
}
// Label sets slider label (id).
func (s *SliderIntWidget) Label(label string) *SliderIntWidget {
s.label = GenAutoID(label)
return s
}
// Labelf sets formatted label.
func (s *SliderIntWidget) Labelf(format string, args ...any) *SliderIntWidget {
return s.Label(fmt.Sprintf(format, args...))
}
func (s *SliderIntWidget) ID(id ID) *SliderIntWidget {
s.label = id
return s
}
// Build implements Widget interface.
func (s *SliderIntWidget) Build() {
if s.width != 0 {
PushItemWidth(s.width)
defer PopItemWidth()
}
if imgui.SliderIntV(Context.FontAtlas.RegisterString(s.label.String()), s.value, s.minValue, s.maxValue, s.format, 0) && s.onChange != nil {
s.onChange()
}
}
var _ Widget = &VSliderIntWidget{}
// VSliderIntWidget stands from Vertical SliderIntWidget.
type VSliderIntWidget struct {
label ID
width float32
height float32
value *int32
minValue int32
maxValue int32
format string
flags SliderFlags
onChange func()
}
// VSliderInt creates new vslider int.
func VSliderInt(value *int32, minValue, maxValue int32) *VSliderIntWidget {
return &VSliderIntWidget{
label: GenAutoID("##VSliderInt"),
width: 18,
height: 60,
value: value,
minValue: minValue,
maxValue: maxValue,
format: "%d",
flags: SliderFlagsNone,
}
}
// Size sets slider's size.
func (vs *VSliderIntWidget) Size(width, height float32) *VSliderIntWidget {
vs.width, vs.height = width, height
return vs
}
// Flags sets flags.
func (vs *VSliderIntWidget) Flags(flags SliderFlags) *VSliderIntWidget {
vs.flags = flags
return vs
}
// Format sets format (see comment on (*SliderIntWidget).Format).
func (vs *VSliderIntWidget) Format(format string) *VSliderIntWidget {
vs.format = format
return vs
}
// OnChange sets callback called when slider's position gets changed.
func (vs *VSliderIntWidget) OnChange(onChange func()) *VSliderIntWidget {
vs.onChange = onChange
return vs
}
// Label sets slider's label (id).
func (vs *VSliderIntWidget) Label(label string) *VSliderIntWidget {
vs.label = GenAutoID(label)
return vs
}
// Labelf sets formatted label.
func (vs *VSliderIntWidget) Labelf(format string, args ...any) *VSliderIntWidget {
return vs.Label(fmt.Sprintf(format, args...))
}
func (vs *VSliderIntWidget) ID(id ID) *VSliderIntWidget {
vs.label = id
return vs
}
// Build implements Widget interface.
func (vs *VSliderIntWidget) Build() {
if imgui.VSliderIntV(
Context.FontAtlas.RegisterString(vs.label.String()),
imgui.Vec2{X: vs.width, Y: vs.height},
vs.value,
vs.minValue,
vs.maxValue,
vs.format,
imgui.SliderFlags(vs.flags),
) && vs.onChange != nil {
vs.onChange()
}
}
var _ Widget = &SliderFloatWidget{}
// SliderFloatWidget does similar to SliderIntWidget but slides around
// float32 values.
type SliderFloatWidget struct {
label ID
value *float32
minValue float32
maxValue float32
format string
width float32
onChange func()
}
// SliderFloat creates new slider float widget.
func SliderFloat(value *float32, minValue, maxValue float32) *SliderFloatWidget {
return &SliderFloatWidget{
label: GenAutoID("##SliderFloat"),
value: value,
minValue: minValue,
maxValue: maxValue,
format: "%.3f",
width: 0,
onChange: nil,
}
}
// Format sets format of text displayed on the slider.
// default is %.3f.
func (sf *SliderFloatWidget) Format(format string) *SliderFloatWidget {
sf.format = format
return sf
}
// OnChange is callback called when slider's position gets changed.
func (sf *SliderFloatWidget) OnChange(onChange func()) *SliderFloatWidget {
sf.onChange = onChange
return sf
}
// Size sets slider's width.
func (sf *SliderFloatWidget) Size(width float32) *SliderFloatWidget {
sf.width = width
return sf
}
// Label sets slider's label (id).
func (sf *SliderFloatWidget) Label(label string) *SliderFloatWidget {
sf.label = GenAutoID(Context.FontAtlas.RegisterString(label))
return sf
}
// Labelf sets formatted label.
func (sf *SliderFloatWidget) Labelf(format string, args ...any) *SliderFloatWidget {
return sf.Label(fmt.Sprintf(format, args...))
}
func (sf *SliderFloatWidget) ID(id ID) *SliderFloatWidget {
sf.label = id
return sf
}
// Build implements Widget interface.
func (sf *SliderFloatWidget) Build() {
if sf.width != 0 {
PushItemWidth(sf.width)
defer PopItemWidth()
}
if imgui.SliderFloatV(Context.FontAtlas.RegisterString(sf.label.String()), sf.value, sf.minValue, sf.maxValue, sf.format, 1.0) && sf.onChange != nil {
sf.onChange()
}
}