This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/basic/label.go

114 lines
2.8 KiB
Go
Raw Normal View History

2023-01-09 06:03:19 +00:00
package basic
import "image"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-01-10 02:25:11 +00:00
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
2023-01-09 06:03:19 +00:00
// Label is a simple text box.
2023-01-09 06:03:19 +00:00
type Label struct {
2023-01-10 02:25:11 +00:00
*core.Core
core core.CoreControl
wrap bool
2023-01-09 06:03:19 +00:00
text string
drawer artist.TextDrawer
}
// 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-09 06:03:19 +00:00
element = &Label { }
2023-01-10 02:25:11 +00:00
element.Core, element.core = core.NewCore(element)
2023-01-09 06:03:19 +00:00
face := theme.FontFaceRegular()
element.drawer.SetFace(face)
element.SetWrap(wrap)
2023-01-09 06:03:19 +00:00
element.SetText(text)
return
}
// Resize resizes the label and re-wraps the text if wrapping is enabled.
2023-01-16 05:36:23 +00:00
func (element *Label) Resize (width, height int) {
element.core.AllocateCanvas(width, height)
if element.wrap {
element.drawer.SetMaxWidth(width)
element.drawer.SetMaxHeight(height)
2023-01-09 06:03:19 +00:00
}
element.draw()
2023-01-09 06:03:19 +00:00
return
}
// MinimumHeightFor returns the reccomended height for this element based on the
// given width in order to allow the text to wrap properly.
func (element *Label) MinimumHeightFor (width int) (height int) {
if element.wrap {
return element.drawer.ReccomendedHeightFor(width)
} else {
_, height = element.MinimumSize()
return
}
}
// SetText sets the label's text.
2023-01-09 06:03:19 +00:00
func (element *Label) SetText (text string) {
if element.text == text { return }
element.text = text
element.drawer.SetText([]rune(text))
element.updateMinimumSize()
2023-01-09 06:03:19 +00:00
if element.core.HasImage () {
element.draw()
element.core.PushAll()
}
}
// 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 }
if !wrap {
element.drawer.SetMaxWidth(0)
element.drawer.SetMaxHeight(0)
}
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())
element.core.NotifyFlexibleHeightChange()
} else {
bounds := element.drawer.LayoutBounds()
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
}
}
2023-01-09 06:03:19 +00:00
func (element *Label) draw () {
bounds := element.core.Bounds()
artist.FillRectangle (
2023-01-09 06:03:19 +00:00
element.core,
theme.BackgroundPattern(),
2023-01-09 06:03:19 +00:00
bounds)
textBounds := element.drawer.LayoutBounds()
foreground := theme.ForegroundPattern(true)
2023-01-09 06:03:19 +00:00
element.drawer.Draw (element.core, foreground, image.Point {
X: 0 - textBounds.Min.X,
Y: 0 - textBounds.Min.Y,
})
}