2023-02-02 06:48:16 +00:00
|
|
|
package basicElements
|
2023-01-09 06:03:19 +00:00
|
|
|
|
2023-03-13 21:10:27 +00:00
|
|
|
import "golang.org/x/image/math/fixed"
|
2023-01-09 06:03:19 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
2023-02-08 05:22:40 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
2023-02-15 23:45:58 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
|
2023-01-10 02:25:11 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
2023-01-09 06:03:19 +00:00
|
|
|
|
2023-01-11 02:01:30 +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
|
2023-01-11 02:01:30 +00:00
|
|
|
|
|
|
|
wrap bool
|
2023-01-09 06:03:19 +00:00
|
|
|
text string
|
2023-02-15 23:45:58 +00:00
|
|
|
drawer textdraw.Drawer
|
2023-03-13 21:10:27 +00:00
|
|
|
|
|
|
|
forcedColumns int
|
|
|
|
forcedRows int
|
2023-01-19 21:49:34 +00:00
|
|
|
|
2023-02-08 19:36:14 +00:00
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
2023-02-08 05:22:40 +00:00
|
|
|
|
2023-01-19 21:49:34 +00:00
|
|
|
onFlexibleHeightChange func ()
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 02:01:30 +00: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-02-08 19:36:14 +00:00
|
|
|
element = &Label { }
|
|
|
|
element.theme.Case = theme.C("basic", "label")
|
2023-02-08 05:22:40 +00:00
|
|
|
element.Core, element.core = core.NewCore(element.handleResize)
|
2023-01-11 02:01:30 +00:00
|
|
|
element.SetWrap(wrap)
|
2023-01-09 06:03:19 +00:00
|
|
|
element.SetText(text)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-07 16:27:59 +00:00
|
|
|
func (element *Label) redo () {
|
2023-02-08 05:22:40 +00:00
|
|
|
face := element.theme.FontFace (
|
2023-02-07 16:27:59 +00:00
|
|
|
theme.FontStyleRegular,
|
2023-02-08 19:36:14 +00:00
|
|
|
theme.FontSizeNormal)
|
2023-02-07 16:27:59 +00:00
|
|
|
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 19:54:43 +00:00
|
|
|
func (element *Label) handleResize () {
|
|
|
|
bounds := element.Bounds()
|
2023-01-16 16:54:02 +00:00
|
|
|
if element.wrap {
|
2023-01-31 19:54:43 +00:00
|
|
|
element.drawer.SetMaxWidth(bounds.Dx())
|
|
|
|
element.drawer.SetMaxHeight(bounds.Dy())
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
2023-01-16 16:54:02 +00:00
|
|
|
element.draw()
|
2023-01-09 06:03:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:10:27 +00:00
|
|
|
// EmCollapse forces a minimum width and height upon the label. The width is
|
|
|
|
// measured in emspaces, and the height is measured in lines. If a zero value is
|
|
|
|
// given for a dimension, its minimum will be determined by the label's content.
|
|
|
|
// If the label's content is greater than these dimensions, it will be truncated
|
|
|
|
// to fit.
|
|
|
|
func (element *Label) EmCollapse (columns int, rows int) {
|
|
|
|
element.forcedColumns = columns
|
|
|
|
element.forcedRows = rows
|
|
|
|
element.updateMinimumSize()
|
|
|
|
}
|
|
|
|
|
2023-01-19 21:49:34 +00: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 23:04:41 +00:00
|
|
|
if element.wrap {
|
|
|
|
return element.drawer.ReccomendedHeightFor(width)
|
|
|
|
} else {
|
|
|
|
_, height = element.MinimumSize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 21:49:34 +00: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-11 02:01:30 +00:00
|
|
|
// 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
|
2023-01-18 05:42:04 +00:00
|
|
|
element.drawer.SetText([]rune(text))
|
2023-01-11 02:01:30 +00:00
|
|
|
element.updateMinimumSize()
|
|
|
|
|
2023-01-09 06:03:19 +00:00
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
2023-01-19 21:49:34 +00:00
|
|
|
element.core.DamageAll()
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 02:01:30 +00: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 23:00:36 +00:00
|
|
|
if !wrap {
|
|
|
|
element.drawer.SetMaxWidth(0)
|
|
|
|
element.drawer.SetMaxHeight(0)
|
|
|
|
}
|
2023-01-11 02:01:30 +00:00
|
|
|
element.wrap = wrap
|
|
|
|
element.updateMinimumSize()
|
|
|
|
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
2023-01-19 21:49:34 +00:00
|
|
|
element.core.DamageAll()
|
2023-01-11 02:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 05:22:40 +00:00
|
|
|
// SetTheme sets the element's theme.
|
|
|
|
func (element *Label) SetTheme (new theme.Theme) {
|
2023-02-08 19:36:14 +00:00
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
2023-02-08 05:22:40 +00:00
|
|
|
element.drawer.SetFace (element.theme.FontFace (
|
|
|
|
theme.FontStyleRegular,
|
2023-02-08 19:36:14 +00:00
|
|
|
theme.FontSizeNormal))
|
2023-02-08 05:22:40 +00:00
|
|
|
element.updateMinimumSize()
|
|
|
|
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
|
|
|
func (element *Label) SetConfig (new config.Config) {
|
2023-02-08 19:36:14 +00:00
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.config.Config = new
|
2023-02-08 05:22:40 +00:00
|
|
|
element.updateMinimumSize()
|
|
|
|
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 02:01:30 +00:00
|
|
|
func (element *Label) updateMinimumSize () {
|
2023-03-13 21:10:27 +00:00
|
|
|
var width, height int
|
|
|
|
|
2023-01-11 02:01:30 +00:00
|
|
|
if element.wrap {
|
|
|
|
em := element.drawer.Em().Round()
|
2023-02-27 03:20:17 +00:00
|
|
|
if em < 1 {
|
|
|
|
em = element.theme.Padding(theme.PatternBackground)[0]
|
|
|
|
}
|
2023-03-13 21:10:27 +00:00
|
|
|
width, height = em, element.drawer.LineHeight().Round()
|
2023-01-19 21:49:34 +00:00
|
|
|
if element.onFlexibleHeightChange != nil {
|
|
|
|
element.onFlexibleHeightChange()
|
|
|
|
}
|
2023-01-11 02:01:30 +00:00
|
|
|
} else {
|
|
|
|
bounds := element.drawer.LayoutBounds()
|
2023-03-13 21:10:27 +00:00
|
|
|
width, height = bounds.Dx(), bounds.Dy()
|
2023-01-11 02:01:30 +00:00
|
|
|
}
|
2023-03-13 21:10:27 +00:00
|
|
|
|
|
|
|
if element.forcedColumns > 0 {
|
|
|
|
width = int (
|
|
|
|
element.drawer.Em().
|
|
|
|
Mul(fixed.I(element.forcedColumns)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if element.forcedRows > 0 {
|
|
|
|
height = int (
|
|
|
|
element.drawer.LineHeight().
|
|
|
|
Mul(fixed.I(element.forcedRows)))
|
|
|
|
}
|
|
|
|
|
|
|
|
element.core.SetMinimumSize(width, height)
|
2023-01-11 02:01:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 06:03:19 +00:00
|
|
|
func (element *Label) draw () {
|
2023-01-31 21:18:10 +00:00
|
|
|
bounds := element.Bounds()
|
2023-01-09 06:03:19 +00:00
|
|
|
|
2023-02-08 05:22:40 +00:00
|
|
|
pattern := element.theme.Pattern (
|
2023-02-07 16:27:59 +00:00
|
|
|
theme.PatternBackground,
|
2023-02-27 03:20:17 +00:00
|
|
|
theme.State { })
|
|
|
|
pattern.Draw(element.core, bounds)
|
2023-01-09 06:03:19 +00:00
|
|
|
|
|
|
|
textBounds := element.drawer.LayoutBounds()
|
|
|
|
|
2023-03-13 21:10:27 +00:00
|
|
|
foreground := element.theme.Color (
|
2023-02-27 03:20:17 +00:00
|
|
|
theme.ColorForeground,
|
|
|
|
theme.State { })
|
2023-02-13 06:49:33 +00:00
|
|
|
element.drawer.Draw(element.core, foreground, bounds.Min.Sub(textBounds.Min))
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|