2023-03-30 21:19:04 -06:00
|
|
|
package elements
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
import "image"
|
2023-03-30 23:06:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-05-02 20:19:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/ability"
|
2023-02-15 16:45:58 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
|
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-05-02 20:19:29 -06:00
|
|
|
entity tomo.Entity
|
2023-02-15 16:45:58 -07:00
|
|
|
drawer textdraw.Drawer
|
2023-01-27 15:55:49 -07:00
|
|
|
|
2023-04-14 20:03:22 -06:00
|
|
|
enabled bool
|
2023-01-27 15:55:49 -07:00
|
|
|
pressed bool
|
|
|
|
text string
|
2023-03-04 22:05:56 -07:00
|
|
|
|
2023-03-04 22:23:45 -07:00
|
|
|
showText bool
|
|
|
|
hasIcon bool
|
2023-03-30 23:06:29 -06:00
|
|
|
iconId tomo.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-04-14 21:58:14 -06:00
|
|
|
element = &Button { showText: true, enabled: true }
|
2023-05-02 20:19:29 -06:00
|
|
|
element.entity = tomo.NewEntity(element).(buttonEntity)
|
2023-03-30 23:06:29 -06:00
|
|
|
element.theme.Case = tomo.C("tomo", "button")
|
|
|
|
element.drawer.SetFace (element.theme.FontFace (
|
|
|
|
tomo.FontStyleRegular,
|
|
|
|
tomo.FontSizeNormal))
|
2023-01-08 23:03:19 -07:00
|
|
|
element.SetText(text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-14 23:14:36 -06:00
|
|
|
// Entity returns this element's entity.
|
|
|
|
func (element *Button) Entity () tomo.Entity {
|
|
|
|
return element.entity
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
|
|
|
|
2023-04-15 10:35:00 -06:00
|
|
|
// Draw causes the element to draw to the specified destination canvas.
|
2023-05-02 20:19:29 -06:00
|
|
|
func (element *Button) Draw (destination artist.Canvas) {
|
2023-04-15 10:35:00 -06:00
|
|
|
state := element.state()
|
|
|
|
bounds := element.entity.Bounds()
|
|
|
|
pattern := element.theme.Pattern(tomo.PatternButton, state)
|
|
|
|
|
|
|
|
pattern.Draw(destination, bounds)
|
|
|
|
|
|
|
|
foreground := element.theme.Color(tomo.ColorForeground, state)
|
|
|
|
sink := element.theme.Sink(tomo.PatternButton)
|
|
|
|
margin := element.theme.Margin(tomo.PatternButton)
|
|
|
|
|
|
|
|
offset := image.Pt (
|
|
|
|
bounds.Dx() / 2,
|
|
|
|
bounds.Dy() / 2).Add(bounds.Min)
|
|
|
|
|
|
|
|
if element.showText {
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
offset.X -= textBounds.Dx() / 2
|
|
|
|
offset.Y -= textBounds.Dy() / 2
|
|
|
|
offset.Y -= textBounds.Min.Y
|
|
|
|
offset.X -= textBounds.Min.X
|
|
|
|
}
|
|
|
|
|
|
|
|
if element.hasIcon {
|
|
|
|
icon := element.theme.Icon(element.iconId, tomo.IconSizeSmall)
|
|
|
|
if icon != nil {
|
|
|
|
iconBounds := icon.Bounds()
|
|
|
|
addedWidth := iconBounds.Dx()
|
|
|
|
iconOffset := offset
|
|
|
|
|
|
|
|
if element.showText {
|
|
|
|
addedWidth += margin.X
|
|
|
|
}
|
|
|
|
|
|
|
|
iconOffset.X -= addedWidth / 2
|
|
|
|
iconOffset.Y =
|
|
|
|
bounds.Min.Y +
|
|
|
|
(bounds.Dy() -
|
|
|
|
iconBounds.Dy()) / 2
|
|
|
|
if element.pressed {
|
|
|
|
iconOffset = iconOffset.Add(sink)
|
|
|
|
}
|
|
|
|
offset.X += addedWidth / 2
|
|
|
|
|
|
|
|
icon.Draw(destination, foreground, iconOffset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if element.showText {
|
|
|
|
if element.pressed {
|
|
|
|
offset = offset.Add(sink)
|
|
|
|
}
|
|
|
|
element.drawer.Draw(destination, foreground, offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 20:03:22 -06:00
|
|
|
// OnClick sets the function to be called when the button is clicked.
|
|
|
|
func (element *Button) OnClick (callback func ()) {
|
|
|
|
element.onClick = callback
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-04-14 20:03:22 -06:00
|
|
|
// Focus gives this element input focus.
|
|
|
|
func (element *Button) Focus () {
|
|
|
|
if !element.entity.Focused() { element.entity.Focus() }
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
2023-01-09 13:14:36 -07:00
|
|
|
|
2023-04-14 20:03:22 -06:00
|
|
|
// Enabled returns whether this button is enabled or not.
|
|
|
|
func (element *Button) Enabled () bool {
|
|
|
|
return element.enabled
|
2023-01-09 13:14:36 -07:00
|
|
|
}
|
|
|
|
|
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-04-14 20:03:22 -06:00
|
|
|
if element.enabled == enabled { return }
|
|
|
|
element.enabled = enabled
|
|
|
|
element.entity.Invalidate()
|
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-04-14 20:03:22 -06:00
|
|
|
element.entity.Invalidate()
|
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-30 23:06:29 -06:00
|
|
|
func (element *Button) SetIcon (id tomo.Icon) {
|
|
|
|
if id == tomo.IconNone {
|
2023-03-19 23:12:19 -06:00
|
|
|
element.hasIcon = false
|
|
|
|
} else {
|
|
|
|
if element.hasIcon && element.iconId == id { return }
|
|
|
|
element.hasIcon = true
|
|
|
|
element.iconId = id
|
|
|
|
}
|
2023-03-04 22:05:56 -07:00
|
|
|
element.updateMinimumSize()
|
2023-04-14 20:03:22 -06:00
|
|
|
element.entity.Invalidate()
|
2023-03-04 22:05:56 -07:00
|
|
|
}
|
|
|
|
|
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()
|
2023-04-14 20:03:22 -06:00
|
|
|
element.entity.Invalidate()
|
2023-03-04 22:23:45 -07:00
|
|
|
}
|
|
|
|
|
2023-02-07 22:22:40 -07:00
|
|
|
// SetTheme sets the element's theme.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *Button) SetTheme (new tomo.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 (
|
2023-03-30 23:06:29 -06:00
|
|
|
tomo.FontStyleRegular,
|
|
|
|
tomo.FontSizeNormal))
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-04-14 20:03:22 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *Button) SetConfig (new tomo.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-04-14 20:03:22 -06:00
|
|
|
element.entity.Invalidate()
|
2023-02-07 22:22:40 -07:00
|
|
|
}
|
|
|
|
|
2023-04-14 20:03:22 -06:00
|
|
|
func (element *Button) HandleFocusChange () {
|
2023-04-14 23:14:36 -06:00
|
|
|
element.entity.Invalidate()
|
2023-04-14 20:03:22 -06:00
|
|
|
}
|
|
|
|
|
2023-04-20 12:44:54 -06:00
|
|
|
func (element *Button) HandleMouseDown (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers,
|
|
|
|
) {
|
2023-04-14 20:03:22 -06:00
|
|
|
if !element.Enabled() { return }
|
|
|
|
element.Focus()
|
|
|
|
if button != input.ButtonLeft { return }
|
|
|
|
element.pressed = true
|
2023-04-14 23:14:36 -06:00
|
|
|
element.entity.Invalidate()
|
2023-04-14 20:03:22 -06:00
|
|
|
}
|
|
|
|
|
2023-04-20 12:44:54 -06:00
|
|
|
func (element *Button) HandleMouseUp (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers,
|
|
|
|
) {
|
2023-04-14 20:03:22 -06:00
|
|
|
if button != input.ButtonLeft { return }
|
|
|
|
element.pressed = false
|
2023-04-20 12:44:54 -06:00
|
|
|
within := position.In(element.entity.Bounds())
|
2023-04-14 20:03:22 -06:00
|
|
|
if element.Enabled() && within && element.onClick != nil {
|
|
|
|
element.onClick()
|
|
|
|
}
|
2023-04-14 23:14:36 -06:00
|
|
|
element.entity.Invalidate()
|
2023-04-14 20:03:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
|
|
|
if !element.Enabled() { return }
|
|
|
|
if key == input.KeyEnter {
|
|
|
|
element.pressed = true
|
2023-04-14 23:14:36 -06:00
|
|
|
element.entity.Invalidate()
|
2023-04-14 20:03:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) HandleKeyUp(key input.Key, modifiers input.Modifiers) {
|
|
|
|
if key == input.KeyEnter && element.pressed {
|
|
|
|
element.pressed = false
|
2023-04-14 23:14:36 -06:00
|
|
|
element.entity.Invalidate()
|
2023-04-14 20:03:22 -06:00
|
|
|
if !element.Enabled() { return }
|
|
|
|
if element.onClick != nil {
|
|
|
|
element.onClick()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) updateMinimumSize () {
|
|
|
|
padding := element.theme.Padding(tomo.PatternButton)
|
|
|
|
margin := element.theme.Margin(tomo.PatternButton)
|
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
minimumSize := textBounds.Sub(textBounds.Min)
|
|
|
|
|
|
|
|
if element.hasIcon {
|
|
|
|
icon := element.theme.Icon(element.iconId, tomo.IconSizeSmall)
|
|
|
|
if icon != nil {
|
|
|
|
bounds := icon.Bounds()
|
|
|
|
if element.showText {
|
|
|
|
minimumSize.Max.X += bounds.Dx()
|
|
|
|
minimumSize.Max.X += margin.X
|
|
|
|
} else {
|
|
|
|
minimumSize.Max.X = bounds.Dx()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
minimumSize = padding.Inverse().Apply(minimumSize)
|
|
|
|
element.entity.SetMinimumSize(minimumSize.Dx(), minimumSize.Dy())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) state () tomo.State {
|
|
|
|
return tomo.State {
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
Focused: element.entity.Focused(),
|
|
|
|
Pressed: element.pressed,
|
2023-03-04 22:31:41 -07:00
|
|
|
}
|
2023-02-16 20:41:07 -07:00
|
|
|
}
|