This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/basic/checkbox.go

193 lines
5.0 KiB
Go
Raw Normal View History

2023-02-01 23:48:16 -07:00
package basicElements
2023-01-17 14:38:57 -07:00
import "image"
2023-02-01 23:48:16 -07:00
import "git.tebibyte.media/sashakoshka/tomo/input"
2023-01-17 14:38:57 -07:00
import "git.tebibyte.media/sashakoshka/tomo/theme"
2023-02-07 22:22:40 -07:00
import "git.tebibyte.media/sashakoshka/tomo/config"
2023-02-27 15:00:28 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-02-15 16:45:58 -07:00
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
2023-01-17 14:38:57 -07:00
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
// 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-02-15 16:45:58 -07:00
drawer textdraw.Drawer
pressed bool
checked bool
text string
2023-02-08 12:36:14 -07:00
config config.Wrapped
theme theme.Wrapped
2023-02-07 22:22:40 -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-02-08 12:36:14 -07:00
element = &Checkbox { checked: checked }
element.theme.Case = theme.C("basic", "checkbox")
2023-02-07 22:22:40 -07:00
element.Core, element.core = core.NewCore(element.draw)
2023-01-30 15:01:47 -07:00
element.FocusableCore,
2023-02-07 09:27:59 -07:00
element.focusableControl = core.NewFocusableCore(element.redo)
2023-01-17 14:38:57 -07:00
element.SetText(text)
return
}
2023-02-01 23:48:16 -07:00
func (element *Checkbox) HandleMouseDown (x, y int, button input.Button) {
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()
element.core.DamageAll()
2023-01-17 14:38:57 -07:00
}
}
2023-02-01 23:48:16 -07:00
func (element *Checkbox) HandleMouseUp (x, y int, button input.Button) {
if button != input.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()
element.core.DamageAll()
2023-01-17 14:38:57 -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-02-01 23:48:16 -07:00
func (element *Checkbox) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
if key == input.KeyEnter {
2023-01-17 14:38:57 -07:00
element.pressed = true
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
2023-01-17 14:38:57 -07:00
}
}
}
2023-02-01 23:48:16 -07:00
func (element *Checkbox) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
if key == input.KeyEnter && element.pressed {
2023-01-17 14:38:57 -07:00
element.pressed = false
element.checked = !element.checked
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
2023-01-17 14:38:57 -07:00
}
if element.onToggle != nil {
element.onToggle()
2023-01-17 14:38:57 -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
element.drawer.SetText([]rune(text))
2023-02-07 22:22:40 -07:00
element.updateMinimumSize()
2023-02-07 22:22:40 -07:00
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
}
// SetTheme sets the element's theme.
func (element *Checkbox) SetTheme (new theme.Theme) {
2023-02-08 12:36:14 -07:00
if new == element.theme.Theme { return }
element.theme.Theme = new
2023-02-07 22:22:40 -07:00
element.drawer.SetFace (element.theme.FontFace (
theme.FontStyleRegular,
2023-02-08 12:36:14 -07:00
theme.FontSizeNormal))
2023-02-07 22:22:40 -07:00
element.updateMinimumSize()
element.redo()
}
// SetConfig sets the element's configuration.
func (element *Checkbox) SetConfig (new config.Config) {
2023-02-08 12:36:14 -07:00
if new == element.config.Config { return }
element.config.Config = new
2023-02-07 22:22:40 -07:00
element.updateMinimumSize()
element.redo()
}
func (element *Checkbox) updateMinimumSize () {
textBounds := element.drawer.LayoutBounds()
if element.text == "" {
element.core.SetMinimumSize(textBounds.Dy(), textBounds.Dy())
} else {
2023-02-26 20:20:17 -07:00
margin := element.theme.Margin(theme.PatternBackground)
element.core.SetMinimumSize (
2023-02-26 20:20:17 -07:00
textBounds.Dy() + margin.X + textBounds.Dx(),
textBounds.Dy())
}
2023-02-07 22:22:40 -07:00
}
func (element *Checkbox) redo () {
2023-01-17 14:38:57 -07:00
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
2023-01-17 14:38:57 -07:00
}
}
func (element *Checkbox) draw () {
bounds := element.Bounds()
2023-01-31 16:39:17 -07:00
boxBounds := image.Rect(0, 0, bounds.Dy(), bounds.Dy()).Add(bounds.Min)
2023-01-17 14:38:57 -07:00
2023-02-26 20:20:17 -07:00
state := theme.State {
Disabled: !element.Enabled(),
2023-01-30 15:01:47 -07:00
Focused: element.Focused(),
Pressed: element.pressed,
2023-02-07 09:27:59 -07:00
On: element.checked,
}
2023-02-07 22:22:40 -07:00
backgroundPattern := element.theme.Pattern (
2023-02-08 12:36:14 -07:00
theme.PatternBackground, state)
2023-02-26 20:20:17 -07:00
backgroundPattern.Draw(element.core, bounds)
2023-02-07 09:27:59 -07:00
2023-02-08 12:36:14 -07:00
pattern := element.theme.Pattern(theme.PatternButton, state)
pattern.Draw(element.core, boxBounds)
2023-01-17 14:38:57 -07:00
textBounds := element.drawer.LayoutBounds()
2023-02-26 20:20:17 -07:00
margin := element.theme.Margin(theme.PatternBackground)
2023-01-31 16:39:17 -07:00
offset := bounds.Min.Add(image.Point {
2023-02-26 20:20:17 -07:00
X: bounds.Dy() + margin.X,
2023-01-31 16:39:17 -07:00
})
2023-01-17 14:38:57 -07:00
offset.Y -= textBounds.Min.Y
offset.X -= textBounds.Min.X
2023-02-26 20:20:17 -07:00
foreground := element.theme.Color(theme.ColorForeground, state)
2023-02-12 23:49:33 -07:00
element.drawer.Draw(element.core, foreground, offset)
2023-01-17 14:38:57 -07:00
}