1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

Avoid paragraphs splitting or truncating their text unnecessarily

This commit is contained in:
Ell 2022-12-22 19:50:50 +01:00
parent f8567cfc99
commit ee62554fee
2 changed files with 9 additions and 3 deletions

View file

@ -68,6 +68,7 @@ Improvements
- Ensure paragraphs display up-to-date versions of their text callbacks
- Set cornflower blue as the default link color
- Added TextField.OnCopyPasteException to allow handling exceptions thrown by TextCopy
- Avoid paragraphs splitting or truncating their text unnecessarily
Fixes
- Fixed parents of elements that prevent spill not being notified properly

View file

@ -107,6 +107,7 @@ namespace MLEM.Ui.Elements {
private StyleProp<TextAlignment> alignment;
private StyleProp<GenericFont> regularFont;
private TokenizedString tokenizedText;
private Vector2? lastAlignSplitSize;
/// <summary>
/// Creates a new paragraph with the given settings.
@ -130,7 +131,8 @@ namespace MLEM.Ui.Elements {
/// <inheritdoc />
protected override Vector2 CalcActualSize(RectangleF parentArea) {
var size = base.CalcActualSize(parentArea);
this.AlignAndSplit(size);
this.TokenizeIfNecessary();
this.AlignAndSplitIfNecessary(size);
var textSize = this.tokenizedText.GetArea(Vector2.Zero, this.TextScale * this.TextScaleMultiplier * this.Scale).Size;
return new Vector2(this.AutoAdjustWidth ? textSize.X + this.ScaledPadding.Width : size.X, textSize.Y + this.ScaledPadding.Height);
}
@ -193,6 +195,7 @@ namespace MLEM.Ui.Elements {
// tokenize the text
this.tokenizedText = this.System.TextFormatter.Tokenize(this.RegularFont, this.Text, this.Alignment);
this.lastAlignSplitSize = null;
// add links to the paragraph
this.RemoveChildren(c => c is Link);
@ -200,8 +203,10 @@ namespace MLEM.Ui.Elements {
this.AddChild(new Link(Anchor.TopLeft, link, this.TextScale * this.TextScaleMultiplier));
}
private void AlignAndSplit(Vector2 size) {
this.TokenizeIfNecessary();
private void AlignAndSplitIfNecessary(Vector2 size) {
if (size == this.lastAlignSplitSize)
return;
this.lastAlignSplitSize = size;
var width = size.X - this.ScaledPadding.Width;
var scale = this.TextScale * this.TextScaleMultiplier * this.Scale;
if (this.TruncateIfLong) {