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

243 lines
6.0 KiB
Go
Raw Normal View History

2023-03-31 03:19:04 +00:00
package elements
2023-01-09 06:03:19 +00:00
import "image"
2023-05-03 23:40:30 +00:00
import "tomo"
import "tomo/input"
import "art"
2023-05-03 23:40:30 +00:00
import "tomo/textdraw"
2023-01-09 06:03:19 +00:00
2023-05-03 05:07:44 +00:00
var buttonCase = tomo.C("tomo", "button")
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-05-03 02:19:29 +00:00
entity tomo.Entity
2023-02-15 23:45:58 +00:00
drawer textdraw.Drawer
2023-04-15 02:03:22 +00:00
enabled bool
pressed bool
text string
2023-03-05 05:05:56 +00:00
2023-03-05 05:23:45 +00:00
showText bool
hasIcon bool
2023-03-31 05:06:29 +00:00
iconId tomo.Icon
2023-02-08 05:22:40 +00:00
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) {
2023-04-15 03:58:14 +00:00
element = &Button { showText: true, enabled: true }
2023-05-03 05:07:44 +00:00
element.entity = tomo.GetBackend().NewEntity(element)
element.drawer.SetFace (element.entity.Theme().FontFace (
2023-03-31 05:06:29 +00:00
tomo.FontStyleRegular,
2023-05-03 05:07:44 +00:00
tomo.FontSizeNormal,
buttonCase))
2023-01-09 06:03:19 +00:00
element.SetText(text)
return
}
// Entity returns this element's entity.
func (element *Button) Entity () tomo.Entity {
return element.entity
2023-01-16 05:31:04 +00:00
}
2023-04-15 16:35:00 +00:00
// Draw causes the element to draw to the specified destination canvas.
func (element *Button) Draw (destination art.Canvas) {
2023-04-15 16:35:00 +00:00
state := element.state()
bounds := element.entity.Bounds()
2023-05-03 05:07:44 +00:00
pattern := element.entity.Theme().Pattern(tomo.PatternButton, state, buttonCase)
2023-04-15 16:35:00 +00:00
pattern.Draw(destination, bounds)
2023-05-03 05:07:44 +00:00
foreground := element.entity.Theme().Color(tomo.ColorForeground, state, buttonCase)
sink := element.entity.Theme().Sink(tomo.PatternButton, buttonCase)
margin := element.entity.Theme().Margin(tomo.PatternButton, buttonCase)
2023-04-15 16:35:00 +00:00
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 {
2023-05-03 05:07:44 +00:00
icon := element.entity.Theme().Icon(element.iconId, tomo.IconSizeSmall, buttonCase)
2023-04-15 16:35:00 +00:00
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-15 02:03:22 +00:00
// OnClick sets the function to be called when the button is clicked.
func (element *Button) OnClick (callback func ()) {
element.onClick = callback
2023-01-16 05:31:04 +00:00
}
2023-01-09 06:03:19 +00:00
2023-04-15 02:03:22 +00:00
// Focus gives this element input focus.
func (element *Button) Focus () {
if !element.entity.Focused() { element.entity.Focus() }
2023-01-16 05:31:04 +00:00
}
2023-01-09 20:14:36 +00:00
2023-04-15 02:03:22 +00:00
// Enabled returns whether this button is enabled or not.
func (element *Button) Enabled () bool {
return element.enabled
2023-01-09 20:14:36 +00:00
}
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-04-15 02:03:22 +00:00
if element.enabled == enabled { return }
element.enabled = enabled
element.entity.Invalidate()
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-02-08 05:22:40 +00:00
element.updateMinimumSize()
2023-04-15 02:03:22 +00:00
element.entity.Invalidate()
2023-02-08 05:22:40 +00:00
}
// SetIcon sets the icon of the button. Passing theme.IconNone removes the
// current icon if it exists.
2023-03-31 05:06:29 +00:00
func (element *Button) SetIcon (id tomo.Icon) {
if id == tomo.IconNone {
element.hasIcon = false
} else {
if element.hasIcon && element.iconId == id { return }
element.hasIcon = true
element.iconId = id
}
2023-03-05 05:05:56 +00:00
element.updateMinimumSize()
2023-04-15 02:03:22 +00:00
element.entity.Invalidate()
2023-03-05 05:05:56 +00:00
}
2023-03-05 05:23:45 +00: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-15 02:03:22 +00:00
element.entity.Invalidate()
2023-03-05 05:23:45 +00:00
}
2023-05-03 05:07:44 +00:00
func (element *Button) HandleThemeChange () {
element.drawer.SetFace (element.entity.Theme().FontFace (
2023-03-31 05:06:29 +00:00
tomo.FontStyleRegular,
2023-05-03 05:07:44 +00:00
tomo.FontSizeNormal,
buttonCase))
2023-02-08 05:22:40 +00:00
element.updateMinimumSize()
2023-04-15 02:03:22 +00:00
element.entity.Invalidate()
2023-02-08 05:22:40 +00:00
}
2023-04-15 02:03:22 +00:00
func (element *Button) HandleFocusChange () {
element.entity.Invalidate()
2023-04-15 02:03:22 +00:00
}
func (element *Button) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
2023-04-15 02:03:22 +00:00
if !element.Enabled() { return }
element.Focus()
if button != input.ButtonLeft { return }
element.pressed = true
element.entity.Invalidate()
2023-04-15 02:03:22 +00:00
}
func (element *Button) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
2023-04-15 02:03:22 +00:00
if button != input.ButtonLeft { return }
element.pressed = false
within := position.In(element.entity.Bounds())
2023-04-15 02:03:22 +00:00
if element.Enabled() && within && element.onClick != nil {
element.onClick()
}
element.entity.Invalidate()
2023-04-15 02:03:22 +00:00
}
func (element *Button) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
if !element.Enabled() { return }
if key == input.KeyEnter {
element.pressed = true
element.entity.Invalidate()
2023-04-15 02:03:22 +00:00
}
}
func (element *Button) HandleKeyUp(key input.Key, modifiers input.Modifiers) {
if key == input.KeyEnter && element.pressed {
element.pressed = false
element.entity.Invalidate()
2023-04-15 02:03:22 +00:00
if !element.Enabled() { return }
if element.onClick != nil {
element.onClick()
}
}
}
func (element *Button) updateMinimumSize () {
2023-05-03 05:07:44 +00:00
padding := element.entity.Theme().Padding(tomo.PatternButton, buttonCase)
margin := element.entity.Theme().Margin(tomo.PatternButton, buttonCase)
2023-04-15 02:03:22 +00:00
textBounds := element.drawer.LayoutBounds()
minimumSize := textBounds.Sub(textBounds.Min)
if element.hasIcon {
2023-05-03 05:07:44 +00:00
icon := element.entity.Theme().Icon(element.iconId, tomo.IconSizeSmall, buttonCase)
2023-04-15 02:03:22 +00:00
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-05 05:31:41 +00:00
}
}