2018-07-25 16:03:49 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2018-08-02 01:52:52 +00:00
|
|
|
"errors"
|
2018-08-01 00:18:13 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-12-30 22:22:50 +00:00
|
|
|
"git.kirsle.net/apps/doodle/lib/render"
|
|
|
|
"git.kirsle.net/apps/doodle/lib/ui/theme"
|
2018-07-25 16:03:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Button is a clickable button.
|
|
|
|
type Button struct {
|
|
|
|
BaseWidget
|
2018-08-02 01:52:52 +00:00
|
|
|
child Widget
|
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.
|
2018-08-02 01:52:52 +00:00
|
|
|
func NewButton(name string, child Widget) *Button {
|
2018-07-26 03:25:02 +00:00
|
|
|
w := &Button{
|
2018-08-02 01:52:52 +00:00
|
|
|
child: child,
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
2018-08-02 01:52:52 +00:00
|
|
|
w.IDFunc(func() string {
|
|
|
|
return fmt.Sprintf("Button<%s>", name)
|
|
|
|
})
|
2018-07-26 03:25:02 +00:00
|
|
|
|
2018-08-01 00:18:13 +00:00
|
|
|
w.Configure(Config{
|
|
|
|
BorderSize: 2,
|
|
|
|
BorderStyle: BorderRaised,
|
|
|
|
OutlineSize: 1,
|
|
|
|
OutlineColor: theme.ButtonOutlineColor,
|
|
|
|
Background: theme.ButtonBackgroundColor,
|
|
|
|
})
|
2018-07-26 04:24:37 +00:00
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Handle(MouseOver, func(p render.Point) {
|
2018-07-26 03:25:02 +00:00
|
|
|
w.hovering = true
|
2018-07-26 04:24:37 +00:00
|
|
|
w.SetBackground(theme.ButtonHoverColor)
|
2018-07-26 03:25:02 +00:00
|
|
|
})
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Handle(MouseOut, func(p render.Point) {
|
2018-07-26 03:25:02 +00:00
|
|
|
w.hovering = false
|
2018-07-26 04:24:37 +00:00
|
|
|
w.SetBackground(theme.ButtonBackgroundColor)
|
2018-07-26 03:25:02 +00:00
|
|
|
})
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Handle(MouseDown, func(p render.Point) {
|
2018-07-26 03:25:02 +00:00
|
|
|
w.clicked = true
|
2018-07-26 04:24:37 +00:00
|
|
|
w.SetBorderStyle(BorderSunken)
|
2018-07-26 03:25:02 +00:00
|
|
|
})
|
2018-08-17 03:37:19 +00:00
|
|
|
w.Handle(MouseUp, func(p render.Point) {
|
2018-07-26 03:25:02 +00:00
|
|
|
w.clicked = false
|
2018-07-26 04:24:37 +00:00
|
|
|
w.SetBorderStyle(BorderRaised)
|
2018-07-26 03:25:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return w
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 20:06:42 +00:00
|
|
|
// Children returns the button's child widget.
|
|
|
|
func (w *Button) Children() []Widget {
|
|
|
|
return []Widget{w.child}
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
// Compute the size of the button.
|
|
|
|
func (w *Button) Compute(e render.Engine) {
|
|
|
|
// Compute the size of the inner widget first.
|
2018-08-02 01:52:52 +00:00
|
|
|
w.child.Compute(e)
|
|
|
|
|
|
|
|
// Auto-resize only if we haven't been given a fixed size.
|
|
|
|
if !w.FixedSize() {
|
|
|
|
size := w.child.Size()
|
|
|
|
w.Resize(render.Rect{
|
|
|
|
W: size.W + w.BoxThickness(2),
|
|
|
|
H: size.H + w.BoxThickness(2),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetText conveniently sets the button text, for Label children only.
|
|
|
|
func (w *Button) SetText(text string) error {
|
|
|
|
if label, ok := w.child.(*Label); ok {
|
2018-08-05 19:54:57 +00:00
|
|
|
label.Text = text
|
2018-08-02 01:52:52 +00:00
|
|
|
}
|
|
|
|
return errors.New("child is not a Label widget")
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Present the button.
|
2018-08-05 19:54:57 +00:00
|
|
|
func (w *Button) Present(e render.Engine, P render.Point) {
|
2018-10-08 20:06:42 +00:00
|
|
|
if w.Hidden() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
w.Compute(e)
|
2018-10-08 17:38:49 +00:00
|
|
|
w.MoveTo(P)
|
2018-08-02 01:52:52 +00:00
|
|
|
var (
|
|
|
|
S = w.Size()
|
|
|
|
ChildSize = w.child.Size()
|
|
|
|
)
|
2018-07-25 16:03:49 +00:00
|
|
|
|
2018-07-26 04:24:37 +00:00
|
|
|
// Draw the widget's border and everything.
|
2018-08-05 19:54:57 +00:00
|
|
|
w.DrawBox(e, P)
|
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
|
|
|
|
2018-08-02 01:52:52 +00:00
|
|
|
// Where to place the child widget.
|
|
|
|
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-08-02 01:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're bigger than we need to be, center the child widget.
|
|
|
|
if S.Bigger(ChildSize) {
|
|
|
|
moveTo.X = P.X + (S.W / 2) - (ChildSize.W / 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the text label inside.
|
2018-08-05 19:54:57 +00:00
|
|
|
w.child.Present(e, moveTo)
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|