-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_checkbox.go
57 lines (46 loc) · 1.04 KB
/
component_checkbox.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
package zgui
import (
"zgui/events"
)
type CheckboxOptions struct {
Box *BoxOptions
Mark *TextOptions
}
type CheckboxComponent struct {
*LineBoxComponent
Mark *LabelComponent
opt *CheckboxOptions
}
func NewCheckboxComponent(options *CheckboxOptions) *CheckboxComponent {
cb := &CheckboxComponent{
LineBoxComponent: NewLineBoxComponent(options.Box),
Mark: NewLabelComponent("X", options.Mark),
opt: options,
}
// Set mark to fill parent
cb.Add(cb.Mark, &Constraints{
x: NewCenterConstraint(),
y: NewCenterConstraint(),
width: NewFillConstraint(),
height: NewFillConstraint(),
})
// but uncheck it
cb.SetChecked(false)
cb.On(events.Pressed, func() {
cb.Toggle()
})
return cb
}
func (cb *CheckboxComponent) Toggle() {
cb.SetChecked(!cb.IsChecked())
}
func (cb *CheckboxComponent) IsChecked() bool {
return cb.Mark.GetState() != StateHidden
}
func (cb *CheckboxComponent) SetChecked(val bool) {
if val {
cb.Mark.SetState(StateNormal)
} else {
cb.Mark.SetState(StateHidden)
}
}