Skip to content

Commit

Permalink
Merge pull request #5636 from Joehuu/sprite-text-ellipsis-added
Browse files Browse the repository at this point in the history
Expose truncated state of `SpriteText`
  • Loading branch information
peppy committed Jun 6, 2023
2 parents 4de1bae + dd8c362 commit 5dca93c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 10 additions & 0 deletions osu.Framework/Graphics/Sprites/SpriteText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ public string EllipsisString
}
}

/// <summary>
/// When <see cref="Truncate"/> is enabled, this indicates whether <see cref="Text"/> has been visually truncated.
/// </summary>
protected bool IsTruncated { get; private set; }

private bool requiresAutoSizedWidth => explicitWidth == null && (RelativeSizeAxes & Axes.X) == 0;

private bool requiresAutoSizedHeight => explicitHeight == null && (RelativeSizeAxes & Axes.Y) == 0;
Expand Down Expand Up @@ -459,6 +464,8 @@ private void computeCharacters()
if (charactersCache.IsValid)
return;

IsTruncated = false;

charactersBacking.Clear();

// Todo: Re-enable this assert after autosize is split into two passes.
Expand All @@ -476,6 +483,9 @@ private void computeCharacters()
textBuilder.Reset();
textBuilder.AddText(displayedText);
textBounds = textBuilder.Bounds;

if (textBuilder is TruncatingTextBuilder truncatingTextBuilder)
IsTruncated = truncatingTextBuilder.IsTruncated;
}
finally
{
Expand Down
12 changes: 8 additions & 4 deletions osu.Framework/Text/TruncatingTextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace osu.Framework.Text
{
public sealed class TruncatingTextBuilder : TextBuilder
{
/// <summary>
/// Indicates whether <see cref="SpriteText.Text"/> has been visually truncated.
/// </summary>
public bool IsTruncated { get; private set; }

private readonly char[] neverFixedWidthCharacters;
private readonly char fallbackCharacter;
private readonly ITexturedGlyphLookupStore store;
Expand All @@ -19,7 +24,6 @@ public sealed class TruncatingTextBuilder : TextBuilder
private readonly bool useFontSizeAsHeight;
private readonly Vector2 spacing;

private bool ellipsisAdded;
private bool addingEllipsis; // Only used temporarily during the addition of the ellipsis.

/// <summary>
Expand Down Expand Up @@ -53,10 +57,10 @@ public override void Reset()
{
base.Reset();

ellipsisAdded = false;
IsTruncated = false;
}

protected override bool CanAddCharacters => (base.CanAddCharacters && !ellipsisAdded) || addingEllipsis;
protected override bool CanAddCharacters => (base.CanAddCharacters && !IsTruncated) || addingEllipsis;

protected override bool HasAvailableSpace(float length) => base.HasAvailableSpace(length) || addingEllipsis;

Expand Down Expand Up @@ -104,7 +108,7 @@ protected override void OnWidthExceeded()
finally
{
addingEllipsis = false;
ellipsisAdded = true;
IsTruncated = true;
}
}
}
Expand Down

0 comments on commit 5dca93c

Please sign in to comment.