2023-02-01 23:48:16 -07:00
|
|
|
package basicElements
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
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-19 14:49:34 -07:00
|
|
|
|
|
|
|
onFlexibleHeightChange func ()
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
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-02-07 09:27:59 -07:00
|
|
|
element.Core, element.core = core.NewCore (
|
|
|
|
element.handleResize,
|
|
|
|
element.redo,
|
|
|
|
element.redo,
|
|
|
|
theme.C("basic", "label"))
|
|
|
|
face := element.core.FontFace (
|
|
|
|
theme.FontStyleRegular,
|
|
|
|
theme.FontSizeNormal)
|
2023-01-08 23:03:19 -07:00
|
|
|
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-02-07 09:27:59 -07:00
|
|
|
func (element *Label) redo () {
|
|
|
|
face := element.core.FontFace (
|
|
|
|
theme.FontStyleRegular,
|
|
|
|
theme.FontSizeNormal)
|
|
|
|
element.drawer.SetFace(face)
|
|
|
|
element.updateMinimumSize()
|
|
|
|
bounds := element.Bounds()
|
|
|
|
if element.wrap {
|
|
|
|
element.drawer.SetMaxWidth(bounds.Dx())
|
|
|
|
element.drawer.SetMaxHeight(bounds.Dy())
|
|
|
|
}
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:54:43 -07:00
|
|
|
func (element *Label) handleResize () {
|
|
|
|
bounds := element.Bounds()
|
2023-01-16 09:54:02 -07:00
|
|
|
if element.wrap {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.drawer.SetMaxWidth(bounds.Dx())
|
|
|
|
element.drawer.SetMaxHeight(bounds.Dy())
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
2023-01-16 09:54:02 -07:00
|
|
|
element.draw()
|
2023-01-08 23:03:19 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:49:34 -07:00
|
|
|
// FlexibleHeightFor returns the reccomended height for this element based on
|
|
|
|
// the given width in order to allow the text to wrap properly.
|
|
|
|
func (element *Label) FlexibleHeightFor (width int) (height int) {
|
2023-01-16 16:04:41 -07:00
|
|
|
if element.wrap {
|
|
|
|
return element.drawer.ReccomendedHeightFor(width)
|
|
|
|
} else {
|
|
|
|
_, height = element.MinimumSize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:49:34 -07:00
|
|
|
// OnFlexibleHeightChange sets a function to be called when the parameters
|
|
|
|
// affecting this element's flexible height are changed.
|
|
|
|
func (element *Label) OnFlexibleHeightChange (callback func ()) {
|
|
|
|
element.onFlexibleHeightChange = callback
|
|
|
|
}
|
|
|
|
|
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
|
2023-01-17 22:42:04 -07:00
|
|
|
element.drawer.SetText([]rune(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()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-10 19:01:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Label) updateMinimumSize () {
|
|
|
|
if element.wrap {
|
|
|
|
em := element.drawer.Em().Round()
|
2023-02-07 09:27:59 -07:00
|
|
|
if em < 1 { em = element.core.Config().Padding() }
|
2023-01-10 19:01:30 -07:00
|
|
|
element.core.SetMinimumSize (
|
|
|
|
em, element.drawer.LineHeight().Round())
|
2023-01-19 14:49:34 -07:00
|
|
|
if element.onFlexibleHeightChange != nil {
|
|
|
|
element.onFlexibleHeightChange()
|
|
|
|
}
|
2023-01-10 19:01:30 -07:00
|
|
|
} else {
|
|
|
|
bounds := element.drawer.LayoutBounds()
|
|
|
|
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
func (element *Label) draw () {
|
2023-01-31 14:18:10 -07:00
|
|
|
bounds := element.Bounds()
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-02-07 09:27:59 -07:00
|
|
|
pattern := element.core.Pattern (
|
|
|
|
theme.PatternBackground,
|
|
|
|
theme.PatternState { })
|
2023-01-31 14:18:10 -07:00
|
|
|
artist.FillRectangle(element, pattern, bounds)
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
|
2023-02-07 09:27:59 -07:00
|
|
|
foreground := element.core.Pattern (
|
|
|
|
theme.PatternForeground,
|
|
|
|
theme.PatternState { })
|
2023-01-31 16:04:12 -07:00
|
|
|
element.drawer.Draw (element, foreground, bounds.Min.Sub(textBounds.Min))
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|