-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Widgets.go
860 lines (719 loc) · 18.9 KB
/
Widgets.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
package giu
import (
"fmt"
"image/color"
"github.com/AllenDang/cimgui-go/imgui"
)
var _ Widget = &RowWidget{}
// RowWidget joins a layout into one line
// calls imgui.SameLine().
type RowWidget struct {
widgets Layout
}
// Row creates RowWidget.
func Row(widgets ...Widget) *RowWidget {
return &RowWidget{
widgets: widgets,
}
}
// Build implements Widget interface.
func (l *RowWidget) Build() {
isFirst := true
l.widgets.Range(func(w Widget) {
switch w.(type) {
case *TooltipWidget,
*ContextMenuWidget, *PopupModalWidget,
*PopupWidget:
// noop
default:
if _, isLabel := w.(*LabelWidget); isLabel {
AlignTextToFramePadding()
}
if !isFirst {
imgui.SameLine()
} else {
isFirst = false
}
}
w.Build()
})
}
// SameLine wraps imgui.SomeLine
// Don't use if you don't have to (use RowWidget instead).
func SameLine() {
imgui.SameLine()
}
var _ Widget = &ChildWidget{}
// ChildWidget is a container widget. It will have a separated scroll bar.
// Use Child if you want to create a layout of e specific size.
type ChildWidget struct {
id ID
width float32
height float32
border bool
flags WindowFlags
layout Layout
}
// Child creates a new ChildWidget.
func Child() *ChildWidget {
return &ChildWidget{
id: GenAutoID("Child"),
width: 0,
height: 0,
border: true,
flags: 0,
layout: nil,
}
}
// Border sets whether child should have border
// You can use imgui.ChildFlagsBorders as well.
func (c *ChildWidget) Border(border bool) *ChildWidget {
c.border = border
return c
}
// Size sets child size.
func (c *ChildWidget) Size(width, height float32) *ChildWidget {
c.width, c.height = width, height
return c
}
// Flags allows to specify Child flags.
func (c *ChildWidget) Flags(flags WindowFlags) *ChildWidget {
c.flags = flags
return c
}
// Layout sets widgets that will be rendered inside of the Child.
func (c *ChildWidget) Layout(widgets ...Widget) *ChildWidget {
c.layout = Layout(widgets)
return c
}
// ID sets the interval id of child widgets.
func (c *ChildWidget) ID(id ID) *ChildWidget {
c.id = id
return c
}
// Build makes a Child.
func (c *ChildWidget) Build() {
if imgui.BeginChildStrV(c.id.String(), imgui.Vec2{X: c.width, Y: c.height}, func() imgui.ChildFlags {
if c.border {
return imgui.ChildFlagsBorders
}
return 0
}(), imgui.WindowFlags(c.flags)) {
c.layout.Build()
}
imgui.EndChild()
}
var _ Widget = &ComboCustomWidget{}
// ComboCustomWidget represents a combo with custom layout when opened.
type ComboCustomWidget struct {
label ID
previewValue string
width float32
flags ComboFlags
layout Layout
}
// ComboCustom creates a new combo custom widget.
func ComboCustom(label, previewValue string) *ComboCustomWidget {
return &ComboCustomWidget{
label: GenAutoID(label),
previewValue: Context.FontAtlas.RegisterString(previewValue),
width: 0,
flags: 0,
layout: nil,
}
}
// Layout add combo's layout.
func (cc *ComboCustomWidget) Layout(widgets ...Widget) *ComboCustomWidget {
cc.layout = Layout(widgets)
return cc
}
// Flags allows to set combo flags (see Flags.go).
func (cc *ComboCustomWidget) Flags(flags ComboFlags) *ComboCustomWidget {
cc.flags = flags
return cc
}
// Size sets combo preview width.
func (cc *ComboCustomWidget) Size(width float32) *ComboCustomWidget {
cc.width = width
return cc
}
// Build implements Widget interface.
func (cc *ComboCustomWidget) Build() {
if cc.width > 0 {
imgui.PushItemWidth(cc.width)
defer imgui.PopItemWidth()
}
if imgui.BeginComboV(Context.FontAtlas.RegisterString(cc.label.String()), cc.previewValue, imgui.ComboFlags(cc.flags)) {
cc.layout.Build()
imgui.EndCombo()
}
}
var _ Widget = &ComboWidget{}
// ComboWidget is a wrapper of ComboCustomWidget.
// It creates a combo of selectables. (it is the most frequently used).
type ComboWidget struct {
label ID
previewValue string
items []string
selected *int32
width float32
flags ComboFlags
onChange func()
}
// Combo creates a new ComboWidget.
func Combo(label, previewValue string, items []string, selected *int32) *ComboWidget {
return &ComboWidget{
label: GenAutoID(label),
previewValue: Context.FontAtlas.RegisterString(previewValue),
items: Context.FontAtlas.RegisterStringSlice(items),
selected: selected,
flags: 0,
width: 0,
onChange: nil,
}
}
// ID sets the interval id of combo. (overrides label).
func (c *ComboWidget) ID(id ID) *ComboWidget {
c.label = id
return c
}
// Flags allows to set combo flags (see Flags.go).
func (c *ComboWidget) Flags(flags ComboFlags) *ComboWidget {
c.flags = flags
return c
}
// Size sets combo's width.
func (c *ComboWidget) Size(width float32) *ComboWidget {
c.width = width
return c
}
// OnChange sets callback when combo value gets changed.
func (c *ComboWidget) OnChange(onChange func()) *ComboWidget {
c.onChange = onChange
return c
}
// Build implements Widget interface.
func (c *ComboWidget) Build() {
if c.width > 0 {
imgui.PushItemWidth(c.width)
defer imgui.PopItemWidth()
}
if imgui.BeginComboV(Context.FontAtlas.RegisterString(c.label.String()), c.previewValue, imgui.ComboFlags(c.flags)) {
for i, item := range c.items {
if imgui.SelectableBool(fmt.Sprintf("%s##%d", item, i)) {
*c.selected = int32(i)
if c.onChange != nil {
c.onChange()
}
}
}
imgui.EndCombo()
}
}
var _ Widget = &ContextMenuWidget{}
// ContextMenuWidget is a context menu on another widget. (e.g. right-click menu on button).
type ContextMenuWidget struct {
id ID
mouseButton MouseButton
layout Layout
}
// ContextMenu creates new ContextMenuWidget.
func ContextMenu() *ContextMenuWidget {
return &ContextMenuWidget{
mouseButton: MouseButtonRight,
layout: nil,
id: GenAutoID("ContextMenu"),
}
}
// Layout sets layout of the context menu.
func (c *ContextMenuWidget) Layout(widgets ...Widget) *ContextMenuWidget {
c.layout = Layout(widgets)
return c
}
// MouseButton sets mouse button that will trigger the context menu.
func (c *ContextMenuWidget) MouseButton(mouseButton MouseButton) *ContextMenuWidget {
c.mouseButton = mouseButton
return c
}
// ID sets the interval id of context menu.
func (c *ContextMenuWidget) ID(id ID) *ContextMenuWidget {
c.id = id
return c
}
// Build implements Widget interface.
func (c *ContextMenuWidget) Build() {
if imgui.BeginPopupContextItemV(c.id.String(), imgui.PopupFlags(c.mouseButton)) {
c.layout.Build()
imgui.EndPopup()
}
}
var _ Widget = &DragIntWidget{}
// DragIntWidget is a widget that allows to drag an integer value.
type DragIntWidget struct {
label ID
value *int32
speed float32
minValue int32
maxValue int32
format string
}
// DragInt creates new DragIntWidget.
func DragInt(label string, value *int32, minValue, maxValue int32) *DragIntWidget {
return &DragIntWidget{
label: GenAutoID(label),
value: value,
speed: 1.0,
minValue: minValue,
maxValue: maxValue,
format: "%d",
}
}
// Speed sets speed of the dragging.
func (d *DragIntWidget) Speed(speed float32) *DragIntWidget {
d.speed = speed
return d
}
// Format sets format of the value.
func (d *DragIntWidget) Format(format string) *DragIntWidget {
d.format = format
return d
}
// Build implements Widget interface.
func (d *DragIntWidget) Build() {
imgui.DragIntV(Context.FontAtlas.RegisterString(d.label.String()), d.value, d.speed, d.minValue, d.maxValue, d.format, 0)
}
var _ Widget = &ColumnWidget{}
// ColumnWidget will place all widgets one by one vertically.
type ColumnWidget struct {
widgets Layout
}
// Column creates a new ColumnWidget.
func Column(widgets ...Widget) *ColumnWidget {
return &ColumnWidget{
widgets: widgets,
}
}
// Build implements Widget interface.
func (g *ColumnWidget) Build() {
imgui.BeginGroup()
g.widgets.Build()
imgui.EndGroup()
}
var _ Widget = &MainMenuBarWidget{}
// MainMenuBarWidget is a widget that creates a main menu bar.
// Main means that it will be docked to the MasterWindow.
// Do NOT use with SingleWindow (see MenuBarWidget).
type MainMenuBarWidget struct {
layout Layout
}
// MainMenuBar creates new MainMenuBarWidget.
func MainMenuBar() *MainMenuBarWidget {
return &MainMenuBarWidget{
layout: nil,
}
}
// Layout sets layout of the menu bar. (See MenuWidget).
func (m *MainMenuBarWidget) Layout(widgets ...Widget) *MainMenuBarWidget {
m.layout = Layout(widgets)
return m
}
// Build implements Widget interface.
func (m *MainMenuBarWidget) Build() {
if imgui.BeginMainMenuBar() {
m.layout.Build()
imgui.EndMainMenuBar()
}
}
var _ Widget = &MenuBarWidget{}
// MenuBarWidget is a widget that creates a menu bar for a window.
// Use it e.g. with SingleWindowWithMenuBar.
type MenuBarWidget struct {
layout Layout
}
// MenuBar creates new MenuBarWidget.
func MenuBar() *MenuBarWidget {
return &MenuBarWidget{
layout: nil,
}
}
// Layout sets layout of the menu bar. (See MenuWidget).
func (m *MenuBarWidget) Layout(widgets ...Widget) *MenuBarWidget {
m.layout = Layout(widgets)
return m
}
// Build implements Widget interface.
func (m *MenuBarWidget) Build() {
if imgui.BeginMenuBar() {
m.layout.Build()
imgui.EndMenuBar()
}
}
var _ Widget = &MenuItemWidget{}
// MenuItemWidget is a menu node. Commonly used inside of MenuWidget.
type MenuItemWidget struct {
label ID
shortcut string
selected bool
enabled bool
onClick func()
}
// MenuItem creates new MenuItemWidget.
func MenuItem(label string) *MenuItemWidget {
return &MenuItemWidget{
label: GenAutoID(label),
shortcut: "",
selected: false,
enabled: true,
onClick: nil,
}
}
// MenuItemf creates MenuItem with formated label.
func MenuItemf(format string, args ...any) *MenuItemWidget {
return MenuItem(fmt.Sprintf(format, args...))
}
// Shortcut sets shortcut of the item (grayed, right-aligned text). Used for presenting e.g. keyboard shortcuts (e.g. "Ctrl+S")
// NOTE: this is only a visual effect. It has nothing to do with keyboard shortcuts.
func (m *MenuItemWidget) Shortcut(s string) *MenuItemWidget {
m.shortcut = s
return m
}
// Selected sets whether the item is selected.
func (m *MenuItemWidget) Selected(s bool) *MenuItemWidget {
m.selected = s
return m
}
// Enabled sets whether the item is enabled.
func (m *MenuItemWidget) Enabled(e bool) *MenuItemWidget {
m.enabled = e
return m
}
// OnClick sets callback that will be executed when item is clicked.
func (m *MenuItemWidget) OnClick(onClick func()) *MenuItemWidget {
m.onClick = onClick
return m
}
// Build implements Widget interface.
func (m *MenuItemWidget) Build() {
if imgui.MenuItemBoolV(Context.FontAtlas.RegisterString(m.label.String()), m.shortcut, m.selected, m.enabled) && m.onClick != nil {
m.onClick()
}
}
var _ Widget = &MenuWidget{}
// MenuWidget is a node of (Main)MenuBarWidget.
// See also: MenuItemWidget, MenuBarWidget, MainMenuBarWidget.
type MenuWidget struct {
label ID
enabled bool
layout Layout
}
// Menu creates new MenuWidget.
func Menu(label string) *MenuWidget {
return &MenuWidget{
label: GenAutoID(label),
enabled: true,
layout: nil,
}
}
// Menuf is alias to Menu(fmt.Sprintf(format, args...)).
func Menuf(format string, args ...any) *MenuWidget {
return Menu(fmt.Sprintf(format, args...))
}
// Enabled sets whether the menu is enabled.
func (m *MenuWidget) Enabled(e bool) *MenuWidget {
m.enabled = e
return m
}
// Layout sets layout of the menu. (See MenuItemWidget).
func (m *MenuWidget) Layout(widgets ...Widget) *MenuWidget {
m.layout = widgets
return m
}
// Build implements Widget interface.
func (m *MenuWidget) Build() {
if imgui.BeginMenuV(Context.FontAtlas.RegisterString(m.label.String()), m.enabled) {
m.layout.Build()
imgui.EndMenu()
}
}
var _ Widget = &ProgressBarWidget{}
// ProgressBarWidget is a progress bar (like in windows' copy-file dialog).
// It is a perfect solution to indicate percentage progress of some action.
type ProgressBarWidget struct {
fraction float32
width float32
height float32
overlay string
}
// ProgressBar creates new ProgressBar.
func ProgressBar(fraction float32) *ProgressBarWidget {
return &ProgressBarWidget{
fraction: fraction,
width: 0,
height: 0,
overlay: "",
}
}
// Size sets size of the bar.
func (p *ProgressBarWidget) Size(width, height float32) *ProgressBarWidget {
p.width, p.height = width, height
return p
}
// Overlay sets custom overlay displayed on the bar.
func (p *ProgressBarWidget) Overlay(overlay string) *ProgressBarWidget {
p.overlay = Context.FontAtlas.RegisterString(overlay)
return p
}
// Overlayf is alias to Overlay(fmt.Sprintf(format, args...)).
func (p *ProgressBarWidget) Overlayf(format string, args ...any) *ProgressBarWidget {
return p.Overlay(fmt.Sprintf(format, args...))
}
// Build implements Widget interface.
func (p *ProgressBarWidget) Build() {
imgui.ProgressBarV(p.fraction, imgui.Vec2{X: p.width, Y: p.height}, p.overlay)
}
var _ Widget = &SeparatorWidget{}
// SeparatorWidget is like <hr> in HTML.
// Creates a layout-wide line.
type SeparatorWidget struct{}
// Separator creates new SeparatorWidget.
func Separator() *SeparatorWidget {
return &SeparatorWidget{}
}
// Build implements Widget interface.
func (s *SeparatorWidget) Build() {
imgui.Separator()
}
var _ Widget = &DummyWidget{}
// DummyWidget creates an empty space (moves drawing cursor by width and height).
type DummyWidget struct {
width float32
height float32
}
// Dummy creates new DummyWidget.
func Dummy(width, height float32) *DummyWidget {
return &DummyWidget{
width: width,
height: height,
}
}
// Build implements Widget interface.
func (d *DummyWidget) Build() {
w, h := GetAvailableRegion()
if d.width < 0 {
d.width = w + d.width
}
if d.height < 0 {
d.height = h + d.height
}
imgui.Dummy(imgui.Vec2{X: d.width, Y: d.height})
}
// TabItemWidget is an item in TabBarWidget.
type TabItemWidget struct {
label string
open *bool
flags TabItemFlags
layout Layout
}
// TabItem creates new TabItem.
func TabItem(label string) *TabItemWidget {
return &TabItemWidget{
label: Context.FontAtlas.RegisterString(label),
open: nil,
flags: 0,
layout: nil,
}
}
// TabItemf creates tab item with formated label.
func TabItemf(format string, args ...any) *TabItemWidget {
return TabItem(fmt.Sprintf(format, args...))
}
// IsOpen takes a pointer to a boolean.
// Value of this pointer indicated whether TabItem is currently selected.
// NOTE: The item will NOT be opened/closed if this value is changed.
// It has only one-side effect.
func (t *TabItemWidget) IsOpen(open *bool) *TabItemWidget {
t.open = open
return t
}
// Flags allows to set item's flags.
func (t *TabItemWidget) Flags(flags TabItemFlags) *TabItemWidget {
t.flags = flags
return t
}
// Layout is a layout displayed when item is opened.
func (t *TabItemWidget) Layout(widgets ...Widget) *TabItemWidget {
t.layout = Layout(widgets)
return t
}
// BuildTabItem executes tab item build steps.
func (t *TabItemWidget) BuildTabItem() {
if imgui.BeginTabItemV(t.label, t.open, imgui.TabItemFlags(t.flags)) {
t.layout.Build()
imgui.EndTabItem()
}
}
var _ Widget = &TabBarWidget{}
// TabBarWidget is a bar of TabItemWidgets.
type TabBarWidget struct {
id ID
flags TabBarFlags
tabItems []*TabItemWidget
}
// TabBar creates new TabBarWidget.
func TabBar() *TabBarWidget {
return &TabBarWidget{
id: GenAutoID("TabBar"),
flags: 0,
}
}
// Flags allows to set TabBArFlags.
func (t *TabBarWidget) Flags(flags TabBarFlags) *TabBarWidget {
t.flags = flags
return t
}
// ID manually sets widget's ID.
func (t *TabBarWidget) ID(id ID) *TabBarWidget {
t.id = id
return t
}
// TabItems sets list of TabItemWidgets in the bar.
func (t *TabBarWidget) TabItems(items ...*TabItemWidget) *TabBarWidget {
t.tabItems = items
return t
}
// Build implements Widget interface.
func (t *TabBarWidget) Build() {
if imgui.BeginTabBarV(t.id.String(), imgui.TabBarFlags(t.flags)) {
for _, ti := range t.tabItems {
ti.BuildTabItem()
}
imgui.EndTabBar()
}
}
var _ Widget = &TooltipWidget{}
// TooltipWidget sets a tooltip on the previous widget.
// The tooltip can be anything.
type TooltipWidget struct {
tip string
layout Layout
to Layout
}
// Tooltip creates new tooltip with given label
// NOTE: you can set the empty label and use Layout() method.
func Tooltip(tip string) *TooltipWidget {
return &TooltipWidget{
tip: Context.FontAtlas.RegisterString(tip),
layout: nil,
}
}
// Tooltipf sets formated label.
func Tooltipf(format string, args ...any) *TooltipWidget {
return Tooltip(fmt.Sprintf(format, args...))
}
// Layout sets a custom layout of tooltip.
func (t *TooltipWidget) Layout(widgets ...Widget) *TooltipWidget {
t.layout = Layout(widgets)
return t
}
// To sets layout to which the tooltip should be attached.
// NOTE: This is an optional approach. By default tooltip is attached to the previous widget.
func (t *TooltipWidget) To(layout ...Widget) *TooltipWidget {
t.to = Layout(layout)
return t
}
// Build implements Widget interface.
func (t *TooltipWidget) Build() {
if t.to != nil {
t.to.Range(func(w Widget) {
w.Build()
t.buildTooltip()
})
return
}
t.buildTooltip()
}
func (t *TooltipWidget) buildTooltip() {
if imgui.IsItemHovered() {
if t.layout != nil {
imgui.BeginTooltip()
t.layout.Build()
imgui.EndTooltip()
} else {
imgui.SetTooltip(t.tip)
}
}
}
var _ Widget = &SpacingWidget{}
// SpacingWidget increases a spacing between two widgets a bit.
type SpacingWidget struct{}
// Spacing creates new SpacingWidget.
func Spacing() *SpacingWidget {
return &SpacingWidget{}
}
// Build implements Widget interface.
func (s *SpacingWidget) Build() {
imgui.Spacing()
}
var _ Widget = &ColorEditWidget{}
// ColorEditWidget is a widget that provides a color editor.
type ColorEditWidget struct {
label ID
color *color.RGBA
flags ColorEditFlags
width float32
onChange func()
}
// ColorEdit creates new ColorEditWidget.
func ColorEdit(label string, c *color.RGBA) *ColorEditWidget {
return &ColorEditWidget{
label: GenAutoID(label),
color: c,
// flags: ColorEditFlagsNone,
}
}
// OnChange sets callback that will be executed when color is changed.
func (ce *ColorEditWidget) OnChange(cb func()) *ColorEditWidget {
ce.onChange = cb
return ce
}
// Flags allows to set ColorEditFlags.
func (ce *ColorEditWidget) Flags(f ColorEditFlags) *ColorEditWidget {
ce.flags = f
return ce
}
// Size sets width of the color editor.
func (ce *ColorEditWidget) Size(width float32) *ColorEditWidget {
ce.width = width
return ce
}
// Build implements Widget interface.
func (ce *ColorEditWidget) Build() {
c := ToVec4Color(*ce.color)
col := [4]float32{
c.X,
c.Y,
c.Z,
c.W,
}
if ce.width > 0 {
imgui.PushItemWidth(ce.width)
}
if imgui.ColorEdit4V(
Context.FontAtlas.RegisterString(ce.label.String()),
&col,
imgui.ColorEditFlags(ce.flags),
) {
*ce.color = Vec4ToRGBA(imgui.Vec4{
X: col[0],
Y: col[1],
Z: col[2],
W: col[3],
})
if ce.onChange != nil {
ce.onChange()
}
}
if ce.width > 0 {
imgui.PopItemWidth()
}
}