2023-01-17 14:38:57 -07:00
|
|
|
package basic
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|
|
|
|
2023-01-29 23:30:13 -07:00
|
|
|
var checkboxCase = theme.C("basic", "checkbox")
|
|
|
|
|
2023-01-17 14:38:57 -07:00
|
|
|
// Checkbox is a toggle-able checkbox with a label.
|
|
|
|
type Checkbox struct {
|
|
|
|
*core.Core
|
2023-01-30 15:01:47 -07:00
|
|
|
*core.FocusableCore
|
2023-01-17 14:38:57 -07:00
|
|
|
core core.CoreControl
|
2023-01-30 15:01:47 -07:00
|
|
|
focusableControl core.FocusableCoreControl
|
2023-01-17 14:38:57 -07:00
|
|
|
drawer artist.TextDrawer
|
2023-01-27 15:55:49 -07:00
|
|
|
|
|
|
|
pressed bool
|
|
|
|
checked bool
|
|
|
|
text string
|
2023-01-19 14:49:34 -07:00
|
|
|
|
2023-01-27 15:55:49 -07:00
|
|
|
onToggle func ()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckbox creates a new cbeckbox with the specified label text.
|
|
|
|
func NewCheckbox (text string, checked bool) (element *Checkbox) {
|
2023-01-27 15:55:49 -07:00
|
|
|
element = &Checkbox { checked: checked }
|
2023-01-17 14:38:57 -07:00
|
|
|
element.Core, element.core = core.NewCore(element)
|
2023-01-30 15:01:47 -07:00
|
|
|
element.FocusableCore,
|
|
|
|
element.focusableControl = core.NewFocusableCore (func () {
|
2023-01-27 15:55:49 -07:00
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
})
|
2023-01-17 14:38:57 -07:00
|
|
|
element.drawer.SetFace(theme.FontFaceRegular())
|
|
|
|
element.SetText(text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize changes this element's size.
|
|
|
|
func (element *Checkbox) Resize (width, height int) {
|
|
|
|
element.core.AllocateCanvas(width, height)
|
|
|
|
element.draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Checkbox) HandleMouseDown (x, y int, button tomo.Button) {
|
2023-01-27 15:55:49 -07:00
|
|
|
if !element.Enabled() { return }
|
2023-01-30 15:01:47 -07:00
|
|
|
element.Focus()
|
2023-01-17 14:38:57 -07:00
|
|
|
element.pressed = true
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Checkbox) HandleMouseUp (x, y int, button tomo.Button) {
|
2023-01-27 15:55:49 -07:00
|
|
|
if button != tomo.ButtonLeft || !element.pressed { return }
|
2023-01-17 14:38:57 -07:00
|
|
|
|
|
|
|
element.pressed = false
|
|
|
|
within := image.Point { x, y }.
|
|
|
|
In(element.Bounds())
|
|
|
|
if within {
|
|
|
|
element.checked = !element.checked
|
|
|
|
}
|
|
|
|
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
2023-01-27 15:55:49 -07:00
|
|
|
if within && element.onToggle != nil {
|
|
|
|
element.onToggle()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Checkbox) HandleMouseMove (x, y int) { }
|
2023-01-19 22:42:57 -07:00
|
|
|
func (element *Checkbox) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
|
2023-01-17 14:38:57 -07:00
|
|
|
|
2023-01-20 15:40:28 -07:00
|
|
|
func (element *Checkbox) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) {
|
2023-01-17 14:38:57 -07:00
|
|
|
if key == tomo.KeyEnter {
|
|
|
|
element.pressed = true
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Checkbox) HandleKeyUp (key tomo.Key, modifiers tomo.Modifiers) {
|
|
|
|
if key == tomo.KeyEnter && element.pressed {
|
|
|
|
element.pressed = false
|
|
|
|
element.checked = !element.checked
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
2023-01-27 15:55:49 -07:00
|
|
|
if element.onToggle != nil {
|
|
|
|
element.onToggle()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:55:49 -07:00
|
|
|
// OnToggle sets the function to be called when the checkbox is toggled.
|
|
|
|
func (element *Checkbox) OnToggle (callback func ()) {
|
|
|
|
element.onToggle = callback
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value reports whether or not the checkbox is currently checked.
|
|
|
|
func (element *Checkbox) Value () (checked bool) {
|
|
|
|
return element.checked
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEnabled sets whether this checkbox can be toggled or not.
|
|
|
|
func (element *Checkbox) SetEnabled (enabled bool) {
|
2023-01-30 15:01:47 -07:00
|
|
|
element.focusableControl.SetEnabled(enabled)
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetText sets the checkbox's label text.
|
|
|
|
func (element *Checkbox) SetText (text string) {
|
|
|
|
if element.text == text { return }
|
|
|
|
|
|
|
|
element.text = text
|
2023-01-17 22:42:04 -07:00
|
|
|
element.drawer.SetText([]rune(text))
|
2023-01-17 14:38:57 -07:00
|
|
|
textBounds := element.drawer.LayoutBounds()
|
2023-01-27 15:55:49 -07:00
|
|
|
|
|
|
|
if text == "" {
|
|
|
|
element.core.SetMinimumSize(textBounds.Dy(), textBounds.Dy())
|
|
|
|
} else {
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
textBounds.Dy() + theme.Padding() + textBounds.Dx(),
|
|
|
|
textBounds.Dy())
|
|
|
|
}
|
|
|
|
|
2023-01-17 14:38:57 -07:00
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Checkbox) draw () {
|
|
|
|
bounds := element.core.Bounds()
|
|
|
|
boxBounds := image.Rect(0, 0, bounds.Dy(), bounds.Dy())
|
|
|
|
|
2023-01-29 23:30:13 -07:00
|
|
|
backgroundPattern, _ := theme.BackgroundPattern(theme.PatternState {
|
|
|
|
Case: checkboxCase,
|
|
|
|
})
|
2023-01-28 23:49:01 -07:00
|
|
|
artist.FillRectangle ( element.core, backgroundPattern, bounds)
|
|
|
|
|
|
|
|
pattern, inset := theme.ButtonPattern(theme.PatternState {
|
2023-01-29 23:30:13 -07:00
|
|
|
Case: checkboxCase,
|
2023-01-28 23:49:01 -07:00
|
|
|
Disabled: !element.Enabled(),
|
2023-01-30 15:01:47 -07:00
|
|
|
Focused: element.Focused(),
|
2023-01-28 23:49:01 -07:00
|
|
|
Pressed: element.pressed,
|
|
|
|
})
|
|
|
|
artist.FillRectangle(element.core, pattern, boxBounds)
|
2023-01-17 14:38:57 -07:00
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
offset := image.Point {
|
|
|
|
X: bounds.Dy() + theme.Padding(),
|
|
|
|
}
|
|
|
|
|
|
|
|
offset.Y -= textBounds.Min.Y
|
|
|
|
offset.X -= textBounds.Min.X
|
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
foreground, _ := theme.ForegroundPattern (theme.PatternState {
|
2023-01-29 23:30:13 -07:00
|
|
|
Case: checkboxCase,
|
2023-01-28 23:49:01 -07:00
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
})
|
2023-01-17 14:38:57 -07:00
|
|
|
element.drawer.Draw(element.core, foreground, offset)
|
|
|
|
|
|
|
|
if element.checked {
|
2023-01-28 23:49:01 -07:00
|
|
|
checkBounds := inset.Apply(boxBounds).Inset(2)
|
|
|
|
artist.FillRectangle(element.core, foreground, checkBounds)
|
2023-01-17 14:38:57 -07:00
|
|
|
}
|
|
|
|
}
|