2023-02-01 23:48:16 -07:00
|
|
|
package basicElements
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
import "image"
|
2023-02-08 23:30:14 -07:00
|
|
|
// import "runtime/debug"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-01-08 23:03:19 -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-03-04 14:18:43 -07:00
|
|
|
// import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
// import "git.tebibyte.media/sashakoshka/tomo/shatter"
|
2023-02-15 16:45:58 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
|
2023-01-09 19:25:11 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-01-13 11:51:22 -07:00
|
|
|
// Button is a clickable button.
|
2023-01-08 23:03:19 -07:00
|
|
|
type Button struct {
|
2023-01-09 19:25:11 -07:00
|
|
|
*core.Core
|
2023-01-30 15:01:47 -07:00
|
|
|
*core.FocusableCore
|
2023-01-09 19:25:11 -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
|
2023-01-27 15:55:49 -07:00
|
|
|
|
|
|
|
pressed bool
|
|
|
|
text string
|
2023-01-19 14:49:34 -07:00
|
|
|
|
2023-02-08 12:36:14 -07:00
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
2023-03-04 22:05:56 -07:00
|
|
|
|
2023-03-04 22:23:45 -07:00
|
|
|
showText bool
|
|
|
|
hasIcon bool
|
|
|
|
iconId theme.Icon
|
2023-02-07 22:22:40 -07:00
|
|
|
|
2023-01-19 14:49:34 -07:00
|
|
|
onClick func ()
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-13 11:51:22 -07:00
|
|
|
// NewButton creates a new button with the specified label text.
|
2023-01-08 23:03:19 -07:00
|
|
|
func NewButton (text string) (element *Button) {
|
2023-03-04 22:23:45 -07:00
|
|
|
element = &Button { showText: true }
|
2023-02-08 12:36:14 -07:00
|
|
|
element.theme.Case = theme.C("basic", "button")
|
2023-03-14 23:41:23 -06:00
|
|
|
element.Core, element.core = core.NewCore(element, element.drawAll)
|
2023-01-30 15:01:47 -07:00
|
|
|
element.FocusableCore,
|
2023-03-14 23:41:23 -06:00
|
|
|
element.focusableControl = core.NewFocusableCore(element.core, element.drawAndPush)
|
2023-01-08 23:03:19 -07:00
|
|
|
element.SetText(text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Button) HandleMouseDown (x, y int, button input.Button) {
|
2023-02-08 23:30:14 -07:00
|
|
|
if !element.Enabled() { return }
|
2023-01-30 15:01:47 -07:00
|
|
|
if !element.Focused() { element.Focus() }
|
2023-02-01 23:48:16 -07:00
|
|
|
if button != input.ButtonLeft { return }
|
2023-01-15 22:31:04 -07:00
|
|
|
element.pressed = true
|
2023-03-04 14:18:43 -07:00
|
|
|
element.drawAndPush()
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Button) HandleMouseUp (x, y int, button input.Button) {
|
|
|
|
if button != input.ButtonLeft { return }
|
2023-01-15 22:31:04 -07:00
|
|
|
element.pressed = false
|
|
|
|
within := image.Point { x, y }.
|
|
|
|
In(element.Bounds())
|
2023-02-08 23:30:14 -07:00
|
|
|
if element.Enabled() && within && element.onClick != nil {
|
2023-01-15 22:31:04 -07:00
|
|
|
element.onClick()
|
|
|
|
}
|
2023-03-04 14:18:43 -07:00
|
|
|
element.drawAndPush()
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Button) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
2023-01-27 15:55:49 -07:00
|
|
|
if !element.Enabled() { return }
|
2023-02-01 23:48:16 -07:00
|
|
|
if key == input.KeyEnter {
|
2023-01-08 23:03:19 -07:00
|
|
|
element.pressed = true
|
2023-03-04 14:18:43 -07:00
|
|
|
element.drawAndPush()
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Button) HandleKeyUp(key input.Key, modifiers input.Modifiers) {
|
|
|
|
if key == input.KeyEnter && element.pressed {
|
2023-01-08 23:03:19 -07:00
|
|
|
element.pressed = false
|
2023-03-04 14:18:43 -07:00
|
|
|
element.drawAndPush()
|
2023-01-27 15:55:49 -07:00
|
|
|
if !element.Enabled() { return }
|
2023-01-15 22:31:04 -07:00
|
|
|
if element.onClick != nil {
|
2023-01-08 23:03:19 -07:00
|
|
|
element.onClick()
|
|
|
|
}
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-09 13:14:36 -07:00
|
|
|
|
2023-01-13 11:51:22 -07:00
|
|
|
// OnClick sets the function to be called when the button is clicked.
|
2023-01-09 13:14:36 -07:00
|
|
|
func (element *Button) OnClick (callback func ()) {
|
|
|
|
element.onClick = callback
|
|
|
|
}
|
|
|
|
|
2023-01-13 11:51:22 -07:00
|
|
|
// SetEnabled sets whether this button can be clicked or not.
|
2023-01-08 23:03:19 -07:00
|
|
|
func (element *Button) SetEnabled (enabled bool) {
|
2023-01-30 15:01:47 -07:00
|
|
|
element.focusableControl.SetEnabled(enabled)
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-13 11:51:22 -07:00
|
|
|
// SetText sets the button's label text.
|
2023-01-08 23:03:19 -07:00
|
|
|
func (element *Button) 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-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-03-04 14:18:43 -07:00
|
|
|
element.drawAndPush()
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
2023-03-19 23:12:19 -06:00
|
|
|
// SetIcon sets the icon of the button. Passing theme.IconNone removes the
|
|
|
|
// current icon if it exists.
|
2023-03-04 22:05:56 -07:00
|
|
|
func (element *Button) SetIcon (id theme.Icon) {
|
2023-03-19 23:12:19 -06:00
|
|
|
if id == theme.IconNone {
|
|
|
|
element.hasIcon = false
|
|
|
|
element.updateMinimumSize()
|
|
|
|
element.drawAndPush()
|
|
|
|
} else {
|
|
|
|
if element.hasIcon && element.iconId == id { return }
|
|
|
|
element.hasIcon = true
|
|
|
|
element.iconId = id
|
|
|
|
}
|
2023-03-04 22:05:56 -07:00
|
|
|
element.updateMinimumSize()
|
|
|
|
element.drawAndPush()
|
|
|
|
}
|
|
|
|
|
2023-03-04 22:23:45 -07:00
|
|
|
// ShowText sets whether or not the button's text will be displayed.
|
|
|
|
func (element *Button) ShowText (showText bool) {
|
|
|
|
if element.showText == showText { return }
|
|
|
|
element.showText = showText
|
|
|
|
element.updateMinimumSize()
|
|
|
|
element.drawAndPush()
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:22:40 -07:00
|
|
|
// SetTheme sets the element's theme.
|
|
|
|
func (element *Button) 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()
|
2023-03-04 14:18:43 -07:00
|
|
|
element.drawAndPush()
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
|
|
|
func (element *Button) 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()
|
2023-03-04 14:18:43 -07:00
|
|
|
element.drawAndPush()
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) updateMinimumSize () {
|
2023-03-19 23:12:19 -06:00
|
|
|
padding := element.theme.Padding(theme.PatternButton)
|
|
|
|
margin := element.theme.Margin(theme.PatternButton)
|
2023-03-04 22:23:45 -07:00
|
|
|
|
2023-03-19 23:12:19 -06:00
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
minimumSize := textBounds.Sub(textBounds.Min)
|
2023-03-04 22:23:45 -07:00
|
|
|
|
2023-03-04 22:05:56 -07:00
|
|
|
if element.hasIcon {
|
|
|
|
icon := element.theme.Icon(element.iconId, theme.IconSizeSmall)
|
|
|
|
if icon != nil {
|
|
|
|
bounds := icon.Bounds()
|
2023-03-04 22:23:45 -07:00
|
|
|
if element.showText {
|
2023-03-19 23:12:19 -06:00
|
|
|
minimumSize.Max.X += bounds.Dx()
|
2023-03-04 22:23:45 -07:00
|
|
|
minimumSize.Max.X += margin.X
|
2023-03-19 23:12:19 -06:00
|
|
|
} else {
|
|
|
|
minimumSize.Max.X = bounds.Dx()
|
2023-03-04 22:05:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-04 22:31:41 -07:00
|
|
|
|
|
|
|
minimumSize = padding.Inverse().Apply(minimumSize)
|
2023-01-28 23:49:01 -07:00
|
|
|
element.core.SetMinimumSize(minimumSize.Dx(), minimumSize.Dy())
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
2023-02-26 20:20:17 -07:00
|
|
|
func (element *Button) state () theme.State {
|
|
|
|
return theme.State {
|
2023-01-28 23:49:01 -07:00
|
|
|
Disabled: !element.Enabled(),
|
2023-02-07 09:27:59 -07:00
|
|
|
Focused: element.Focused(),
|
2023-01-28 23:49:01 -07:00
|
|
|
Pressed: element.pressed,
|
2023-02-07 09:27:59 -07:00
|
|
|
}
|
2023-02-16 20:41:07 -07:00
|
|
|
}
|
2023-02-07 09:27:59 -07:00
|
|
|
|
2023-03-04 14:18:43 -07:00
|
|
|
func (element *Button) drawAndPush () {
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.drawAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) drawAll () {
|
|
|
|
element.drawBackground()
|
|
|
|
element.drawText()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) drawBackground () []image.Rectangle {
|
2023-02-16 20:41:07 -07:00
|
|
|
state := element.state()
|
|
|
|
bounds := element.Bounds()
|
2023-02-08 12:36:14 -07:00
|
|
|
pattern := element.theme.Pattern(theme.PatternButton, state)
|
2023-03-04 14:18:43 -07:00
|
|
|
|
|
|
|
pattern.Draw(element.core, bounds)
|
|
|
|
return []image.Rectangle { bounds }
|
2023-02-16 20:41:07 -07:00
|
|
|
}
|
2023-01-28 23:49:01 -07:00
|
|
|
|
2023-03-04 22:05:56 -07:00
|
|
|
func (element *Button) drawText () {
|
2023-02-16 20:41:07 -07:00
|
|
|
state := element.state()
|
|
|
|
bounds := element.Bounds()
|
2023-02-26 20:20:17 -07:00
|
|
|
foreground := element.theme.Color(theme.ColorForeground, state)
|
2023-02-16 20:41:07 -07:00
|
|
|
sink := element.theme.Sink(theme.PatternButton)
|
2023-03-04 22:05:56 -07:00
|
|
|
margin := element.theme.Margin(theme.PatternButton)
|
2023-03-04 22:31:41 -07:00
|
|
|
|
|
|
|
offset := image.Pt (
|
|
|
|
bounds.Dx() / 2,
|
|
|
|
bounds.Dy() / 2).Add(bounds.Min)
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-03-04 22:23:45 -07:00
|
|
|
if element.showText {
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
2023-03-04 22:31:41 -07:00
|
|
|
offset.X -= textBounds.Dx() / 2
|
|
|
|
offset.Y -= textBounds.Dy() / 2
|
2023-03-04 22:23:45 -07:00
|
|
|
offset.Y -= textBounds.Min.Y
|
|
|
|
offset.X -= textBounds.Min.X
|
2023-02-08 12:36:14 -07:00
|
|
|
}
|
2023-03-04 22:05:56 -07:00
|
|
|
|
|
|
|
if element.hasIcon {
|
|
|
|
icon := element.theme.Icon(element.iconId, theme.IconSizeSmall)
|
|
|
|
if icon != nil {
|
|
|
|
iconBounds := icon.Bounds()
|
2023-03-04 22:23:45 -07:00
|
|
|
addedWidth := iconBounds.Dx()
|
2023-03-04 22:31:41 -07:00
|
|
|
iconOffset := offset
|
2023-03-04 22:23:45 -07:00
|
|
|
|
|
|
|
if element.showText {
|
|
|
|
addedWidth += margin.X
|
|
|
|
}
|
|
|
|
|
2023-03-04 22:05:56 -07:00
|
|
|
iconOffset.X -= addedWidth / 2
|
|
|
|
iconOffset.Y =
|
|
|
|
bounds.Min.Y +
|
|
|
|
(bounds.Dy() -
|
|
|
|
iconBounds.Dy()) / 2
|
|
|
|
if element.pressed {
|
2023-03-04 22:31:41 -07:00
|
|
|
iconOffset = iconOffset.Add(sink)
|
2023-03-04 22:05:56 -07:00
|
|
|
}
|
|
|
|
offset.X += addedWidth / 2
|
|
|
|
|
|
|
|
icon.Draw(element.core, foreground, iconOffset)
|
|
|
|
}
|
|
|
|
}
|
2023-03-04 22:31:41 -07:00
|
|
|
|
|
|
|
if element.showText {
|
2023-03-05 09:20:33 -07:00
|
|
|
if element.pressed {
|
|
|
|
offset = offset.Add(sink)
|
|
|
|
}
|
2023-03-04 22:31:41 -07:00
|
|
|
element.drawer.Draw(element.core, foreground, offset)
|
|
|
|
}
|
2023-02-16 20:41:07 -07:00
|
|
|
}
|