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-10 19:01:30 -07:00
|
|
|
// Label is a simple text box.
|
2023-01-08 23:03:19 -07:00
|
|
|
type Label struct {
|
2023-01-09 19:25:11 -07:00
|
|
|
*core.Core
|
|
|
|
core core.CoreControl
|
2023-01-10 19:01:30 -07:00
|
|
|
|
|
|
|
wrap bool
|
2023-01-08 23:03:19 -07:00
|
|
|
text string
|
|
|
|
drawer artist.TextDrawer
|
|
|
|
}
|
|
|
|
|
2023-01-10 19:01:30 -07:00
|
|
|
// NewLabel creates a new label. If wrap is set to true, the text inside will be
|
|
|
|
// wrapped.
|
|
|
|
func NewLabel (text string, wrap bool) (element *Label) {
|
2023-01-08 23:03:19 -07:00
|
|
|
element = &Label { }
|
2023-01-09 19:25:11 -07:00
|
|
|
element.Core, element.core = core.NewCore(element)
|
2023-01-08 23:03:19 -07:00
|
|
|
face := theme.FontFaceRegular()
|
|
|
|
element.drawer.SetFace(face)
|
2023-01-10 19:01:30 -07:00
|
|
|
element.SetWrap(wrap)
|
2023-01-08 23:03:19 -07:00
|
|
|
element.SetText(text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-10 19:01:30 -07:00
|
|
|
// Handle handles and event.
|
2023-01-08 23:03:19 -07:00
|
|
|
func (element *Label) Handle (event tomo.Event) {
|
|
|
|
switch event.(type) {
|
|
|
|
case tomo.EventResize:
|
|
|
|
resizeEvent := event.(tomo.EventResize)
|
|
|
|
element.core.AllocateCanvas (
|
|
|
|
resizeEvent.Width,
|
|
|
|
resizeEvent.Height)
|
2023-01-12 16:00:36 -07:00
|
|
|
if element.wrap {
|
|
|
|
element.drawer.SetMaxWidth (resizeEvent.Width)
|
|
|
|
element.drawer.SetMaxHeight(resizeEvent.Height)
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
element.draw()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-10 19:01:30 -07:00
|
|
|
// SetText sets the label's text.
|
2023-01-08 23:03:19 -07:00
|
|
|
func (element *Label) SetText (text string) {
|
|
|
|
if element.text == text { return }
|
|
|
|
|
|
|
|
element.text = text
|
|
|
|
element.drawer.SetText(text)
|
2023-01-10 19:01:30 -07:00
|
|
|
element.updateMinimumSize()
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 19:01:30 -07:00
|
|
|
// SetWrap sets wether or not the label's text wraps. If the text is set to
|
|
|
|
// wrap, the element will have a minimum size of a single character and
|
|
|
|
// automatically wrap its text. If the text is set to not wrap, the element will
|
|
|
|
// have a minimum size that fits its text.
|
|
|
|
func (element *Label) SetWrap (wrap bool) {
|
|
|
|
if wrap == element.wrap { return }
|
2023-01-12 16:00:36 -07:00
|
|
|
if !wrap {
|
|
|
|
element.drawer.SetMaxWidth(0)
|
|
|
|
element.drawer.SetMaxHeight(0)
|
|
|
|
}
|
2023-01-10 19:01:30 -07:00
|
|
|
element.wrap = wrap
|
|
|
|
element.updateMinimumSize()
|
|
|
|
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.PushAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Label) updateMinimumSize () {
|
|
|
|
if element.wrap {
|
|
|
|
em := element.drawer.Em().Round()
|
|
|
|
if em < 1 { em = theme.Padding() }
|
|
|
|
element.core.SetMinimumSize (
|
|
|
|
em, element.drawer.LineHeight().Round())
|
|
|
|
} else {
|
|
|
|
bounds := element.drawer.LayoutBounds()
|
|
|
|
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
func (element *Label) draw () {
|
|
|
|
bounds := element.core.Bounds()
|
|
|
|
|
|
|
|
artist.Rectangle (
|
|
|
|
element.core,
|
|
|
|
theme.BackgroundImage(),
|
|
|
|
nil, 0,
|
|
|
|
bounds)
|
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
|
|
|
|
foreground := theme.ForegroundImage()
|
|
|
|
element.drawer.Draw (element.core, foreground, image.Point {
|
|
|
|
X: 0 - textBounds.Min.X,
|
|
|
|
Y: 0 - textBounds.Min.Y,
|
|
|
|
})
|
|
|
|
}
|