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/button.go

149 lines
3.8 KiB
Go
Raw Normal View History

2023-02-02 06:48:16 +00:00
package basicElements
2023-01-09 06:03:19 +00:00
import "image"
2023-02-02 06:48:16 +00:00
import "git.tebibyte.media/sashakoshka/tomo/input"
2023-01-09 06:03:19 +00:00
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-01-10 02:25:11 +00:00
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
2023-01-09 06:03:19 +00:00
2023-01-13 18:51:22 +00:00
// Button is a clickable button.
2023-01-09 06:03:19 +00:00
type Button struct {
2023-01-10 02:25:11 +00:00
*core.Core
2023-01-30 22:01:47 +00:00
*core.FocusableCore
2023-01-10 02:25:11 +00:00
core core.CoreControl
2023-01-30 22:01:47 +00:00
focusableControl core.FocusableCoreControl
2023-01-09 06:03:19 +00:00
drawer artist.TextDrawer
pressed bool
text string
onClick func ()
2023-01-09 06:03:19 +00:00
}
2023-01-13 18:51:22 +00:00
// NewButton creates a new button with the specified label text.
2023-01-09 06:03:19 +00:00
func NewButton (text string) (element *Button) {
element = &Button { }
2023-02-07 16:27:59 +00:00
element.Core, element.core = core.NewCore (
element.draw,
element.redo,
element.redo,
theme.C("basic", "button"))
2023-01-30 22:01:47 +00:00
element.FocusableCore,
2023-02-07 16:27:59 +00:00
element.focusableControl = core.NewFocusableCore(element.redo)
2023-01-09 06:03:19 +00:00
element.SetText(text)
return
}
2023-02-07 16:27:59 +00:00
func (element *Button) redo () {
element.drawer.SetFace (
element.core.FontFace(theme.FontStyleRegular,
theme.FontSizeNormal))
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
}
2023-02-02 06:48:16 +00:00
func (element *Button) HandleMouseDown (x, y int, button input.Button) {
if !element.Enabled() { return }
2023-01-30 22:01:47 +00:00
if !element.Focused() { element.Focus() }
2023-02-02 06:48:16 +00:00
if button != input.ButtonLeft { return }
2023-01-16 05:31:04 +00:00
element.pressed = true
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
2023-01-16 05:31:04 +00:00
}
}
2023-02-02 06:48:16 +00:00
func (element *Button) HandleMouseUp (x, y int, button input.Button) {
if button != input.ButtonLeft { return }
2023-01-16 05:31:04 +00:00
element.pressed = false
if element.core.HasImage() {
2023-01-09 06:03:19 +00:00
element.draw()
element.core.DamageAll()
2023-01-16 05:31:04 +00:00
}
2023-01-09 06:03:19 +00:00
2023-01-16 05:31:04 +00:00
within := image.Point { x, y }.
In(element.Bounds())
2023-01-09 06:03:19 +00:00
if !element.Enabled() { return }
2023-01-16 05:31:04 +00:00
if within && element.onClick != nil {
element.onClick()
}
}
func (element *Button) HandleMouseMove (x, y int) { }
func (element *Button) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
2023-01-16 05:31:04 +00:00
2023-02-02 06:48:16 +00:00
func (element *Button) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
if !element.Enabled() { return }
2023-02-02 06:48:16 +00:00
if key == input.KeyEnter {
2023-01-09 06:03:19 +00:00
element.pressed = true
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
2023-01-09 06:03:19 +00:00
}
2023-01-16 05:31:04 +00:00
}
}
2023-01-09 06:03:19 +00:00
2023-02-02 06:48:16 +00:00
func (element *Button) HandleKeyUp(key input.Key, modifiers input.Modifiers) {
if key == input.KeyEnter && element.pressed {
2023-01-09 06:03:19 +00:00
element.pressed = false
2023-02-07 16:27:59 +00:00
element.redo()
if !element.Enabled() { return }
2023-01-16 05:31:04 +00:00
if element.onClick != nil {
2023-01-09 06:03:19 +00:00
element.onClick()
}
2023-01-16 05:31:04 +00:00
}
}
2023-01-09 20:14:36 +00:00
2023-01-13 18:51:22 +00:00
// OnClick sets the function to be called when the button is clicked.
2023-01-09 20:14:36 +00:00
func (element *Button) OnClick (callback func ()) {
element.onClick = callback
}
2023-01-13 18:51:22 +00:00
// SetEnabled sets whether this button can be clicked or not.
2023-01-09 06:03:19 +00:00
func (element *Button) SetEnabled (enabled bool) {
2023-01-30 22:01:47 +00:00
element.focusableControl.SetEnabled(enabled)
2023-01-09 06:03:19 +00:00
}
2023-01-13 18:51:22 +00:00
// SetText sets the button's label text.
2023-01-09 06:03:19 +00:00
func (element *Button) SetText (text string) {
if element.text == text { return }
element.text = text
element.drawer.SetText([]rune(text))
2023-01-09 06:03:19 +00:00
textBounds := element.drawer.LayoutBounds()
2023-02-07 16:27:59 +00:00
minimumSize := textBounds.Inset(-element.core.Config().Padding())
element.core.SetMinimumSize(minimumSize.Dx(), minimumSize.Dy())
2023-02-07 16:27:59 +00:00
element.redo()
2023-01-09 06:03:19 +00:00
}
func (element *Button) draw () {
bounds := element.Bounds()
2023-01-09 06:03:19 +00:00
2023-02-07 16:27:59 +00:00
state := theme.PatternState {
Disabled: !element.Enabled(),
2023-02-07 16:27:59 +00:00
Focused: element.Focused(),
Pressed: element.pressed,
2023-02-07 16:27:59 +00:00
}
pattern := element.core.Pattern(theme.PatternButton, state)
artist.FillRectangle(element, pattern, bounds)
2023-01-09 06:03:19 +00:00
textBounds := element.drawer.LayoutBounds()
offset := image.Point {
2023-02-07 16:27:59 +00:00
X: bounds.Min.X + (bounds.Dx() - textBounds.Dx()) / 2,
Y: bounds.Min.Y + (bounds.Dy() - textBounds.Dy()) / 2,
2023-01-09 06:03:19 +00:00
}
// account for the fact that the bounding rectangle will be shifted over
// due to the bounds origin being at the baseline of the first line
offset.Y -= textBounds.Min.Y
offset.X -= textBounds.Min.X
2023-02-07 16:27:59 +00:00
foreground := element.core.Pattern(theme.PatternForeground, state)
element.drawer.Draw(element, foreground, offset)
2023-01-09 06:03:19 +00:00
}