2018-07-25 16:03:49 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2018-08-01 00:18:13 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
"git.kirsle.net/apps/doodle/render"
|
|
|
|
"git.kirsle.net/apps/doodle/ui/theme"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Button is a clickable button.
|
|
|
|
type Button struct {
|
|
|
|
BaseWidget
|
2018-07-26 04:24:37 +00:00
|
|
|
Label Label
|
2018-07-26 03:25:02 +00:00
|
|
|
|
|
|
|
// Private options.
|
|
|
|
hovering bool
|
|
|
|
clicked bool
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewButton creates a new Button.
|
|
|
|
func NewButton(label Label) *Button {
|
2018-07-26 03:25:02 +00:00
|
|
|
w := &Button{
|
2018-07-26 04:24:37 +00:00
|
|
|
Label: label,
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
2018-07-26 03:25:02 +00:00
|
|
|
|
2018-08-01 00:18:13 +00:00
|
|
|
w.Configure(Config{
|
|
|
|
Padding: 4,
|
|
|
|
BorderSize: 2,
|
|
|
|
BorderStyle: BorderRaised,
|
|
|
|
OutlineSize: 1,
|
|
|
|
OutlineColor: theme.ButtonOutlineColor,
|
|
|
|
Background: theme.ButtonBackgroundColor,
|
|
|
|
})
|
2018-07-26 04:24:37 +00:00
|
|
|
|
2018-07-26 03:25:02 +00:00
|
|
|
w.Handle("MouseOver", func(p render.Point) {
|
|
|
|
w.hovering = true
|
2018-07-26 04:24:37 +00:00
|
|
|
w.SetBackground(theme.ButtonHoverColor)
|
2018-07-26 03:25:02 +00:00
|
|
|
})
|
|
|
|
w.Handle("MouseOut", func(p render.Point) {
|
|
|
|
w.hovering = false
|
2018-07-26 04:24:37 +00:00
|
|
|
w.SetBackground(theme.ButtonBackgroundColor)
|
2018-07-26 03:25:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
w.Handle("MouseDown", func(p render.Point) {
|
|
|
|
w.clicked = true
|
2018-07-26 04:24:37 +00:00
|
|
|
w.SetBorderStyle(BorderSunken)
|
2018-07-26 03:25:02 +00:00
|
|
|
})
|
|
|
|
w.Handle("MouseUp", func(p render.Point) {
|
|
|
|
w.clicked = false
|
2018-07-26 04:24:37 +00:00
|
|
|
w.SetBorderStyle(BorderRaised)
|
2018-07-26 03:25:02 +00:00
|
|
|
})
|
|
|
|
|
2018-08-01 00:18:13 +00:00
|
|
|
w.IDFunc(func() string {
|
|
|
|
return fmt.Sprintf("Button<%s>", w.Label.Text.Text)
|
|
|
|
})
|
|
|
|
|
2018-07-26 03:25:02 +00:00
|
|
|
return w
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetText quickly changes the text of the label.
|
|
|
|
func (w *Button) SetText(text string) {
|
|
|
|
w.Label.Text.Text = text
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the size of the button.
|
|
|
|
func (w *Button) Compute(e render.Engine) {
|
|
|
|
// Compute the size of the inner widget first.
|
|
|
|
w.Label.Compute(e)
|
|
|
|
size := w.Label.Size()
|
|
|
|
w.Resize(render.Rect{
|
2018-07-26 04:24:37 +00:00
|
|
|
W: size.W + w.BoxThickness(2),
|
|
|
|
H: size.H + w.BoxThickness(2),
|
2018-07-25 16:03:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present the button.
|
|
|
|
func (w *Button) Present(e render.Engine) {
|
|
|
|
w.Compute(e)
|
|
|
|
P := w.Point()
|
|
|
|
|
2018-07-26 04:24:37 +00:00
|
|
|
// Draw the widget's border and everything.
|
|
|
|
w.DrawBox(e)
|
2018-07-25 16:03:49 +00:00
|
|
|
|
2018-07-26 04:24:37 +00:00
|
|
|
// Offset further if we are currently sunken.
|
|
|
|
var clickOffset int32
|
2018-07-26 03:25:02 +00:00
|
|
|
if w.clicked {
|
2018-07-26 04:24:37 +00:00
|
|
|
clickOffset++
|
2018-07-26 03:25:02 +00:00
|
|
|
}
|
2018-07-25 16:03:49 +00:00
|
|
|
|
|
|
|
// Draw the text label inside.
|
2018-07-26 02:38:54 +00:00
|
|
|
w.Label.MoveTo(render.Point{
|
2018-07-26 04:24:37 +00:00
|
|
|
X: P.X + w.BoxThickness(1) + clickOffset,
|
|
|
|
Y: P.Y + w.BoxThickness(1) + clickOffset,
|
2018-07-25 16:03:49 +00:00
|
|
|
})
|
|
|
|
w.Label.Present(e)
|
|
|
|
}
|