doodle/ui/label.go
Noah Petherbridge b67c4b67b2 Add Initial "Doodad Palette" UX
* Add a tab bar to the top of the Palette window that has two
  radiobuttons for "Palette" and "Doodads"
* UI: add the concept of a Hidden() widget and the corresponding Hide()
  and Show() methods. Hidden widgets are skipped over when evaluating
  Frame packing, rendering, and event supervision.
* The Palette Window in editor mode now displays one of two tabs:
  * Palette: the old color swatch palette now lives here.
  * Doodads: the new Doodad palette.
* The Doodad Palette shows a grid of buttons (2 per row) showing the
  available Doodad drawings in the user's config folder.
* The Doodad buttons act as radiobuttons for now and have no other
  effect. TODO will be making them react to drag-drop events.
* UI: added a `Children()` method as the inverse of `Parent()` for
  container widgets (like Frame, Window and Button) to expose their
  children. The BaseWidget just returns an empty []Widget.
* Console: added a `repl` command that keeps the dev console open and
  prefixes every command with `$` filled out -- for rapid JavaScript
  console evaluation.
2018-10-08 13:06:42 -07:00

106 lines
2.0 KiB
Go

package ui
import (
"fmt"
"git.kirsle.net/apps/doodle/render"
)
// DefaultFont is the default font settings used for a Label.
var DefaultFont = render.Text{
Size: 12,
Color: render.Black,
}
// Label is a simple text label widget.
type Label struct {
BaseWidget
// Configurable fields for the constructor.
Text string
TextVariable *string
Font render.Text
width int32
height int32
}
// NewLabel creates a new label.
func NewLabel(c Label) *Label {
w := &Label{
Text: c.Text,
TextVariable: c.TextVariable,
Font: DefaultFont,
}
if !c.Font.IsZero() {
w.Font = c.Font
}
w.IDFunc(func() string {
return fmt.Sprintf(`Label<"%s">`, w.text().Text)
})
return w
}
// text returns the label's displayed text, coming from the TextVariable if
// available or else the Text attribute instead.
func (w *Label) text() render.Text {
if w.TextVariable != nil {
w.Font.Text = *w.TextVariable
return w.Font
}
w.Font.Text = w.Text
return w.Font
}
// Value returns the current text value displayed in the widget, whether it was
// the hardcoded value or a TextVariable.
func (w *Label) Value() string {
return w.text().Text
}
// Compute the size of the label widget.
func (w *Label) Compute(e render.Engine) {
rect, err := e.ComputeTextRect(w.text())
if err != nil {
log.Error("%s: failed to compute text rect: %s", w, err)
return
}
var (
padX = w.Font.Padding + w.Font.PadX
padY = w.Font.Padding + w.Font.PadY
)
if !w.FixedSize() {
w.resizeAuto(render.Rect{
W: rect.W + (padX * 2),
H: rect.H + (padY * 2),
})
}
w.MoveTo(render.Point{
X: rect.X + w.BoxThickness(1),
Y: rect.Y + w.BoxThickness(1),
})
}
// Present the label widget.
func (w *Label) Present(e render.Engine, P render.Point) {
if w.Hidden() {
return
}
border := w.BoxThickness(1)
var (
padX = w.Font.Padding + w.Font.PadX
padY = w.Font.Padding + w.Font.PadY
)
w.DrawBox(e, P)
e.DrawText(w.text(), render.Point{
X: P.X + border + padX,
Y: P.Y + border + padY,
})
}