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
|
|
|
|
core core.CoreControl
|
2023-01-09 11:06:55 -07:00
|
|
|
|
2023-01-09 13:14:36 -07:00
|
|
|
pressed bool
|
|
|
|
enabled bool
|
2023-01-08 23:03:19 -07:00
|
|
|
onClick func ()
|
|
|
|
|
|
|
|
text string
|
|
|
|
drawer artist.TextDrawer
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
element = &Button { enabled: true }
|
2023-01-09 19:25:11 -07:00
|
|
|
element.Core, element.core = core.NewCore(element)
|
2023-01-08 23:03:19 -07:00
|
|
|
element.drawer.SetFace(theme.FontFaceRegular())
|
2023-01-10 09:51:46 -07:00
|
|
|
element.core.SetSelectable(true)
|
2023-01-08 23:03:19 -07:00
|
|
|
element.SetText(text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-13 11:51:22 -07:00
|
|
|
// Handle handles an event.
|
2023-01-08 23:03:19 -07:00
|
|
|
func (element *Button) Handle (event tomo.Event) {
|
|
|
|
switch event.(type) {
|
|
|
|
case tomo.EventResize:
|
|
|
|
resizeEvent := event.(tomo.EventResize)
|
|
|
|
element.core.AllocateCanvas (
|
|
|
|
resizeEvent.Width,
|
|
|
|
resizeEvent.Height)
|
|
|
|
element.draw()
|
|
|
|
|
|
|
|
case tomo.EventMouseDown:
|
|
|
|
if !element.enabled { break }
|
|
|
|
|
|
|
|
mouseDownEvent := event.(tomo.EventMouseDown)
|
2023-01-09 13:14:36 -07:00
|
|
|
element.Select()
|
2023-01-08 23:03:19 -07:00
|
|
|
if mouseDownEvent.Button != tomo.ButtonLeft { break }
|
|
|
|
element.pressed = true
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
|
|
|
|
2023-01-09 16:19:19 -07:00
|
|
|
case tomo.EventKeyDown:
|
|
|
|
keyDownEvent := event.(tomo.EventKeyDown)
|
|
|
|
if keyDownEvent.Key == tomo.KeyEnter {
|
|
|
|
element.pressed = true
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
case tomo.EventMouseUp:
|
|
|
|
if !element.enabled { break }
|
|
|
|
|
|
|
|
mouseUpEvent := event.(tomo.EventMouseUp)
|
|
|
|
if mouseUpEvent.Button != tomo.ButtonLeft { break }
|
|
|
|
element.pressed = false
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
within := image.Point { mouseUpEvent.X, mouseUpEvent.Y }.
|
|
|
|
In(element.Bounds())
|
|
|
|
|
|
|
|
if within && element.onClick != nil {
|
|
|
|
element.onClick()
|
|
|
|
}
|
2023-01-09 13:14:36 -07:00
|
|
|
|
2023-01-09 16:19:19 -07:00
|
|
|
case tomo.EventKeyUp:
|
|
|
|
keyDownEvent := event.(tomo.EventKeyUp)
|
2023-01-11 20:30:14 -07:00
|
|
|
if keyDownEvent.Key == tomo.KeyEnter && element.pressed {
|
2023-01-09 16:19:19 -07:00
|
|
|
element.pressed = false
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
|
|
|
if element.onClick != nil {
|
|
|
|
element.onClick()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 13:14:36 -07:00
|
|
|
case tomo.EventSelect:
|
2023-01-11 13:46:48 -07:00
|
|
|
element.core.SetSelected(true)
|
2023-01-11 20:30:14 -07:00
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
2023-01-09 13:14:36 -07:00
|
|
|
|
|
|
|
case tomo.EventDeselect:
|
2023-01-11 13:46:48 -07:00
|
|
|
element.core.SetSelected(false)
|
2023-01-11 20:30:14 -07:00
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Select requests that this button's parent container send it a selection
|
|
|
|
// event.
|
2023-01-09 13:14:36 -07:00
|
|
|
func (element *Button) Select () {
|
|
|
|
element.core.Select()
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if element.enabled == enabled { return }
|
|
|
|
element.enabled = enabled
|
2023-01-10 09:51:46 -07:00
|
|
|
element.core.SetSelectable(enabled)
|
2023-01-08 23:03:19 -07:00
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
element.drawer.SetText(text)
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
theme.Padding() * 2 + textBounds.Dx(),
|
|
|
|
theme.Padding() * 2 + textBounds.Dy())
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Button) draw () {
|
|
|
|
bounds := element.core.Bounds()
|
|
|
|
|
2023-01-14 19:01:00 -07:00
|
|
|
artist.FillRectangle (
|
2023-01-08 23:03:19 -07:00
|
|
|
element.core,
|
2023-01-14 19:01:00 -07:00
|
|
|
theme.ButtonPattern (
|
2023-01-09 13:14:36 -07:00
|
|
|
element.enabled,
|
2023-01-14 19:01:00 -07:00
|
|
|
element.Selected(),
|
|
|
|
element.pressed),
|
2023-01-08 23:03:19 -07:00
|
|
|
bounds)
|
|
|
|
|
|
|
|
innerBounds := bounds
|
|
|
|
innerBounds.Min.X += theme.Padding()
|
|
|
|
innerBounds.Min.Y += theme.Padding()
|
|
|
|
innerBounds.Max.X -= theme.Padding()
|
|
|
|
innerBounds.Max.Y -= theme.Padding()
|
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
offset := image.Point {
|
|
|
|
X: theme.Padding() + (innerBounds.Dx() - textBounds.Dx()) / 2,
|
|
|
|
Y: theme.Padding() + (innerBounds.Dy() - textBounds.Dy()) / 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
if element.pressed {
|
|
|
|
offset = offset.Add(theme.SinkOffsetVector())
|
|
|
|
}
|
|
|
|
|
2023-01-14 19:01:00 -07:00
|
|
|
foreground := theme.ForegroundPattern(element.enabled)
|
2023-01-08 23:03:19 -07:00
|
|
|
element.drawer.Draw(element.core, foreground, offset)
|
|
|
|
}
|