2023-01-08 23:03:19 -07:00
|
|
|
package basic
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
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-27 15:55:49 -07:00
|
|
|
*core.SelectableCore
|
2023-01-09 19:25:11 -07:00
|
|
|
core core.CoreControl
|
2023-01-27 15:55:49 -07:00
|
|
|
selectableControl core.SelectableCoreControl
|
2023-01-08 23:03:19 -07:00
|
|
|
drawer artist.TextDrawer
|
2023-01-27 15:55:49 -07:00
|
|
|
|
|
|
|
pressed bool
|
|
|
|
text string
|
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-01-27 15:55:49 -07:00
|
|
|
element = &Button { }
|
2023-01-09 19:25:11 -07:00
|
|
|
element.Core, element.core = core.NewCore(element)
|
2023-01-27 15:55:49 -07:00
|
|
|
element.SelectableCore,
|
|
|
|
element.selectableControl = core.NewSelectableCore (func () {
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
})
|
2023-01-08 23:03:19 -07:00
|
|
|
element.drawer.SetFace(theme.FontFaceRegular())
|
|
|
|
element.SetText(text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-15 22:31:04 -07:00
|
|
|
func (element *Button) Resize (width, height int) {
|
|
|
|
element.core.AllocateCanvas(width, height)
|
|
|
|
element.draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) HandleMouseDown (x, y int, button tomo.Button) {
|
2023-01-27 15:55:49 -07:00
|
|
|
if !element.Enabled() { return }
|
|
|
|
if !element.Selected() { element.Select() }
|
2023-01-15 22:31:04 -07:00
|
|
|
if button != tomo.ButtonLeft { return }
|
|
|
|
element.pressed = true
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) HandleMouseUp (x, y int, button tomo.Button) {
|
|
|
|
if button != tomo.ButtonLeft { return }
|
|
|
|
element.pressed = false
|
|
|
|
if element.core.HasImage() {
|
2023-01-08 23:03:19 -07:00
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-01-15 22:31:04 -07:00
|
|
|
within := image.Point { x, y }.
|
|
|
|
In(element.Bounds())
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-01-27 15:55:49 -07:00
|
|
|
if !element.Enabled() { return }
|
2023-01-15 22:31:04 -07:00
|
|
|
if within && element.onClick != nil {
|
|
|
|
element.onClick()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) HandleMouseMove (x, y int) { }
|
2023-01-19 14:49:34 -07:00
|
|
|
func (element *Button) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
|
2023-01-15 22:31:04 -07:00
|
|
|
|
2023-01-20 15:40:28 -07:00
|
|
|
func (element *Button) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) {
|
2023-01-27 15:55:49 -07:00
|
|
|
if !element.Enabled() { return }
|
2023-01-15 22:31:04 -07:00
|
|
|
if key == tomo.KeyEnter {
|
2023-01-08 23:03:19 -07:00
|
|
|
element.pressed = true
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
2023-01-15 22:31:04 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-01-15 22:31:04 -07:00
|
|
|
func (element *Button) HandleKeyUp(key tomo.Key, modifiers tomo.Modifiers) {
|
|
|
|
if key == tomo.KeyEnter && element.pressed {
|
2023-01-08 23:03:19 -07:00
|
|
|
element.pressed = false
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
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-27 15:55:49 -07:00
|
|
|
element.selectableControl.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-01-08 23:03:19 -07:00
|
|
|
textBounds := element.drawer.LayoutBounds()
|
2023-01-28 23:49:01 -07:00
|
|
|
_, inset := theme.ButtonPattern(theme.PatternState { })
|
|
|
|
minimumSize := inset.Inverse().Apply(textBounds).Inset(-theme.Padding())
|
|
|
|
element.core.SetMinimumSize(minimumSize.Dx(), minimumSize.Dy())
|
2023-01-08 23:03:19 -07:00
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) draw () {
|
|
|
|
bounds := element.core.Bounds()
|
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
pattern, inset := theme.ButtonPattern(theme.PatternState {
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
Selected: element.Selected(),
|
|
|
|
Pressed: element.pressed,
|
|
|
|
})
|
|
|
|
|
|
|
|
artist.FillRectangle(element.core, pattern, bounds)
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
innerBounds := inset.Apply(bounds)
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
offset := image.Point {
|
2023-01-28 23:49:01 -07:00
|
|
|
X: innerBounds.Min.X + (innerBounds.Dx() - textBounds.Dx()) / 2,
|
|
|
|
Y: innerBounds.Min.X + (innerBounds.Dy() - textBounds.Dy()) / 2,
|
2023-01-08 23:03:19 -07: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-01-28 23:49:01 -07:00
|
|
|
foreground, _ := theme.ForegroundPattern (theme.PatternState {
|
|
|
|
Disabled: !element.Enabled(),
|
|
|
|
})
|
2023-01-08 23:03:19 -07:00
|
|
|
element.drawer.Draw(element.core, foreground, offset)
|
|
|
|
}
|