forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextWidgets.go
529 lines (441 loc) · 12.3 KB
/
TextWidgets.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
package giu
import (
"fmt"
"math"
"golang.org/x/image/colornames"
"github.com/AllenDang/imgui-go"
"github.com/sahilm/fuzzy"
)
var _ Widget = &InputTextMultilineWidget{}
// InputTextMultilineWidget is a large (multiline) text input
// see examples/widgets/.
type InputTextMultilineWidget struct {
label string
text *string
width, height float32
flags InputTextFlags
cb imgui.InputTextCallback
scrollToBottom bool
onChange func()
}
// InputTextMultiline creates InputTextMultilineWidget.
func InputTextMultiline(text *string) *InputTextMultilineWidget {
return &InputTextMultilineWidget{
text: text,
width: 0,
height: 0,
flags: 0,
cb: nil,
onChange: nil,
label: GenAutoID("##InputTextMultiline"),
}
}
// Label sets input field label.
func (i *InputTextMultilineWidget) Label(label string) *InputTextMultilineWidget {
i.label = label
return i
}
// Labelf is formatting version of Label.
func (i *InputTextMultilineWidget) Labelf(format string, args ...any) *InputTextMultilineWidget {
return i.Label(fmt.Sprintf(format, args...))
}
// Flags sets InputTextFlags (see Flags.go).
func (i *InputTextMultilineWidget) Flags(flags InputTextFlags) *InputTextMultilineWidget {
i.flags = flags
return i
}
// Callback sets imgui.InputTextCallback.
func (i *InputTextMultilineWidget) Callback(cb imgui.InputTextCallback) *InputTextMultilineWidget {
i.cb = cb
return i
}
// OnChange set callback called when user action taken on input text field (when text was changed).
func (i *InputTextMultilineWidget) OnChange(onChange func()) *InputTextMultilineWidget {
i.onChange = onChange
return i
}
// Size sets input field size.
func (i *InputTextMultilineWidget) Size(width, height float32) *InputTextMultilineWidget {
i.width, i.height = width, height
return i
}
// AutoScrollToBottom Enables/Disables auto scroll to bottom.
func (i *InputTextMultilineWidget) AutoScrollToBottom(b bool) *InputTextMultilineWidget {
i.scrollToBottom = b
return i
}
// Build implements Widget interface.
func (i *InputTextMultilineWidget) Build() {
if imgui.InputTextMultilineV(
Context.FontAtlas.RegisterString(i.label),
Context.FontAtlas.RegisterStringPointer(i.text),
imgui.Vec2{
X: i.width,
Y: i.height,
},
int(i.flags), i.cb,
) && i.onChange != nil {
i.onChange()
}
if i.scrollToBottom {
imgui.BeginChild(i.label)
imgui.SetScrollHereY(1.0)
imgui.EndChild()
}
}
var _ Widget = &BulletWidget{}
// BulletWidget adds a small, white dot (bullet).
// useful in enumerations.
type BulletWidget struct{}
// Bullet creates a bullet widget.
func Bullet() *BulletWidget {
return &BulletWidget{}
}
// Build implements Widget interface.
func (b *BulletWidget) Build() {
imgui.Bullet()
}
var _ Widget = &BulletTextWidget{}
// BulletTextWidget does similar to BulletWidget, but allows
// to add a text after a bullet. Very useful to create lists.
type BulletTextWidget struct {
text string
}
// BulletText creates bulletTextWidget.
func BulletText(text string) *BulletTextWidget {
return &BulletTextWidget{
text: Context.FontAtlas.RegisterString(text),
}
}
// BulletTextf is a formatting version of BulletText.
func BulletTextf(format string, args ...any) *BulletTextWidget {
return BulletText(fmt.Sprintf(format, args...))
}
// Build implements Widget interface.
func (bt *BulletTextWidget) Build() {
imgui.BulletText(bt.text)
}
var _ Disposable = &inputTextState{}
type inputTextState struct {
autoCompleteCandidates fuzzy.Matches
currentIdx int
}
// Dispose implements disposable interface.
func (s *inputTextState) Dispose() {
s.autoCompleteCandidates = nil
s.currentIdx = 0
}
var _ Widget = &InputTextWidget{}
// InputTextWidget is a single-line text input.
type InputTextWidget struct {
label string
hint string
value *string
width float32
candidates []string
flags InputTextFlags
cb imgui.InputTextCallback
onChange func()
}
// InputText creates new input text widget.
func InputText(value *string) *InputTextWidget {
return &InputTextWidget{
label: GenAutoID("##InputText"),
hint: "",
value: value,
width: 0,
flags: 0,
cb: nil,
onChange: nil,
}
}
// Label adds label (alternatively you can use it to set widget's id).
func (i *InputTextWidget) Label(label string) *InputTextWidget {
i.label = Context.FontAtlas.RegisterString(label)
return i
}
// Labelf adds formatted label.
func (i *InputTextWidget) Labelf(format string, args ...any) *InputTextWidget {
return i.Label(fmt.Sprintf(format, args...))
}
// AutoComplete enables auto complete popup by using fuzzy search of current value against candidates
// Press enter to confirm the first candidate.
func (i *InputTextWidget) AutoComplete(candidates []string) *InputTextWidget {
i.candidates = candidates
return i
}
// Hint sets hint text.
func (i *InputTextWidget) Hint(hint string) *InputTextWidget {
i.hint = Context.FontAtlas.RegisterString(hint)
return i
}
// Size sets field's width.
func (i *InputTextWidget) Size(width float32) *InputTextWidget {
i.width = width
return i
}
// Flags sets flags.
func (i *InputTextWidget) Flags(flags InputTextFlags) *InputTextWidget {
i.flags = flags
return i
}
// Callback sets input text callback.
func (i *InputTextWidget) Callback(cb imgui.InputTextCallback) *InputTextWidget {
i.cb = cb
return i
}
// OnChange sets callback when text was changed.
func (i *InputTextWidget) OnChange(onChange func()) *InputTextWidget {
i.onChange = onChange
return i
}
// Build implements Widget interface.
func (i *InputTextWidget) Build() {
// Get state
var state *inputTextState
if state = GetState[inputTextState](Context, i.label); state == nil {
state = &inputTextState{}
SetState(Context, i.label, state)
}
if i.width != 0 {
PushItemWidth(i.width)
defer PopItemWidth()
}
isChanged := imgui.InputTextWithHint(i.label, i.hint, Context.FontAtlas.RegisterStringPointer(i.value), int(i.flags), i.cb)
if isChanged && i.onChange != nil {
i.onChange()
}
if isChanged {
// Enable auto complete
if len(i.candidates) > 0 {
matches := fuzzy.Find(*i.value, i.candidates)
size := int(math.Min(5, float64(matches.Len())))
matches = matches[:size]
state.autoCompleteCandidates = matches
}
}
// Draw autocomplete list
if len(state.autoCompleteCandidates) > 0 && imgui.IsItemFocused() {
i.handleAutoComplete(state)
}
}
func (i *InputTextWidget) handleAutoComplete(state *inputTextState) {
if state.currentIdx >= len(state.autoCompleteCandidates) {
state.currentIdx = 0
}
labels := make(Layout, len(state.autoCompleteCandidates))
for i, m := range state.autoCompleteCandidates {
labels[i] = Label(m.Str)
if i == state.currentIdx {
labels[i] = Layout{
Custom(func() { PushStyleColor(StyleColorText, colornames.Blue) }),
labels[i],
Custom(func() { PopStyleColor() }),
}
}
}
SetNextWindowPos(imgui.GetItemRectMin().X, imgui.GetItemRectMax().Y)
imgui.BeginTooltip()
labels.Build()
imgui.EndTooltip()
// Press enter will replace value string with first match candidate
switch {
case IsKeyPressed(KeyEnter) || IsKeyPressed(KeyTab):
*i.value = state.autoCompleteCandidates[state.currentIdx].Str
state.autoCompleteCandidates = nil
if i.onChange != nil {
i.onChange()
}
case IsKeyPressed(KeyDown):
state.currentIdx++
if state.currentIdx >= state.autoCompleteCandidates.Len() {
state.currentIdx = 0
}
case IsKeyPressed(KeyUp):
state.currentIdx--
if state.currentIdx < 0 {
state.currentIdx = len(state.autoCompleteCandidates) - 1
}
}
}
var _ Widget = &InputIntWidget{}
// InputIntWidget is an input text field accepting integer values only.
type InputIntWidget struct {
label string
value *int32
width float32
flags InputTextFlags
onChange func()
step int
stepFast int
}
// InputInt creates input int widget
// NOTE: value is int32, so its size is up to 10^32-1.
// to process greater values, you need to use InputTextWidget
// with InputTextFlagsCharsDecimal and strconv.ParseInt in OnChange callback.
func InputInt(value *int32) *InputIntWidget {
return &InputIntWidget{
label: GenAutoID("##InputInt"),
value: value,
width: 0,
flags: 0,
onChange: nil,
}
}
// Label sets label (id).
func (i *InputIntWidget) Label(label string) *InputIntWidget {
i.label = Context.FontAtlas.RegisterString(label)
return i
}
// Labelf sets formatted label.
func (i *InputIntWidget) Labelf(format string, args ...any) *InputIntWidget {
return i.Label(fmt.Sprintf(format, args...))
}
// Size sets input's width.
func (i *InputIntWidget) Size(width float32) *InputIntWidget {
i.width = width
return i
}
// Flags sets flags.
func (i *InputIntWidget) Flags(flags InputTextFlags) *InputIntWidget {
i.flags = flags
return i
}
// StepSize sets the step size.
func (i *InputIntWidget) StepSize(step int) *InputIntWidget {
i.step = step
return i
}
// StepSizeFast sets the fast step size.
func (i *InputIntWidget) StepSizeFast(stepFast int) *InputIntWidget {
i.stepFast = stepFast
return i
}
// OnChange adds on change callback.
func (i *InputIntWidget) OnChange(onChange func()) *InputIntWidget {
i.onChange = onChange
return i
}
// Build implements Widget interface.
func (i *InputIntWidget) Build() {
if i.width != 0 {
PushItemWidth(i.width)
defer PopItemWidth()
}
if imgui.InputIntV(i.label, i.value, i.step, i.stepFast, int(i.flags)) && i.onChange != nil {
i.onChange()
}
}
var _ Widget = &InputFloatWidget{}
// InputFloatWidget does similar to InputIntWIdget, but accepts float numbers.
type InputFloatWidget struct {
label string
value *float32
width float32
flags InputTextFlags
format string
onChange func()
step float32
stepFast float32
}
// InputFloat constructs InputFloatWidget.
func InputFloat(value *float32) *InputFloatWidget {
return &InputFloatWidget{
label: GenAutoID("##InputFloatWidget"),
width: 0,
value: value,
format: "%.3f",
flags: 0,
onChange: nil,
}
}
// Label sets label of input field.
func (i *InputFloatWidget) Label(label string) *InputFloatWidget {
i.label = Context.FontAtlas.RegisterString(label)
return i
}
// Labelf sets formatted label.
func (i *InputFloatWidget) Labelf(format string, args ...any) *InputFloatWidget {
return i.Label(fmt.Sprintf(format, args...))
}
// Size sets input field's width.
func (i *InputFloatWidget) Size(width float32) *InputFloatWidget {
i.width = width
return i
}
// Flags sets flags.
func (i *InputFloatWidget) Flags(flags InputTextFlags) *InputFloatWidget {
i.flags = flags
return i
}
// Format sets data format (e.g. %.3f).
func (i *InputFloatWidget) Format(format string) *InputFloatWidget {
i.format = format
return i
}
// OnChange sets callback called when text is changed.
func (i *InputFloatWidget) OnChange(onChange func()) *InputFloatWidget {
i.onChange = onChange
return i
}
// StepSize sets the step size.
func (i *InputFloatWidget) StepSize(step float32) *InputFloatWidget {
i.step = step
return i
}
// StepSizeFast sets the fast step size.
func (i *InputFloatWidget) StepSizeFast(stepFast float32) *InputFloatWidget {
i.stepFast = stepFast
return i
}
// Build implements Widget interface.
func (i *InputFloatWidget) Build() {
if i.width != 0 {
PushItemWidth(i.width)
defer PopItemWidth()
}
if imgui.InputFloatV(i.label, i.value, i.step, i.stepFast, i.format, int(i.flags)) && i.onChange != nil {
i.onChange()
}
}
var _ Widget = &LabelWidget{}
// LabelWidget is a plain text label.
type LabelWidget struct {
label string
fontInfo *FontInfo
wrapped bool
}
// Label constructs label widget.
func Label(label string) *LabelWidget {
return &LabelWidget{
label: Context.FontAtlas.RegisterString(label),
wrapped: false,
}
}
// Labelf allows to add formatted label.
func Labelf(format string, args ...any) *LabelWidget {
return Label(fmt.Sprintf(format, args...))
}
// Wrapped determines if label is wrapped.
func (l *LabelWidget) Wrapped(wrapped bool) *LabelWidget {
l.wrapped = wrapped
return l
}
// Font sets specific font (does like Style().SetFont).
func (l *LabelWidget) Font(font *FontInfo) *LabelWidget {
l.fontInfo = font
return l
}
// Build implements Widget interface.
func (l *LabelWidget) Build() {
if l.wrapped {
PushTextWrapPos()
defer PopTextWrapPos()
}
if l.fontInfo != nil {
if PushFont(l.fontInfo) {
defer PopFont()
}
}
imgui.Text(l.label)
}