From b14cc68c142343d188e5af6bdcc84cf11180e93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Tue, 2 Mar 2021 21:13:10 +0100 Subject: [PATCH 1/3] Add failing assertion --- .../Sprites/TestSceneSpriteTextMaxWidth.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/osu.Framework.Tests/Visual/Sprites/TestSceneSpriteTextMaxWidth.cs b/osu.Framework.Tests/Visual/Sprites/TestSceneSpriteTextMaxWidth.cs index 9f8c2e0119..920d3358bb 100644 --- a/osu.Framework.Tests/Visual/Sprites/TestSceneSpriteTextMaxWidth.cs +++ b/osu.Framework.Tests/Visual/Sprites/TestSceneSpriteTextMaxWidth.cs @@ -7,6 +7,7 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Sprites; +using osu.Framework.Text; using osuTK.Graphics; namespace osu.Framework.Tests.Visual.Sprites @@ -70,6 +71,8 @@ public void TestFixedSizeMoreThanMaxWidth() [Test] public void TestMaxWidthWithRelativeSize() { + float textWidth = 0; + createTest(s => { s.RelativeSizeAxes = Axes.X; @@ -77,9 +80,11 @@ public void TestMaxWidthWithRelativeSize() s.Text = "some very long text that should exceed the max width"; s.Truncate = true; }, Axes.Y); + AddStep("store text width", () => textWidth = display.Text.TextBuilder.Bounds.X); AddStep("set parent size", () => display.Width = 100); AddAssert("size <= max", () => display.Text.DrawWidth <= 50); + AddAssert("width increased", () => display.Text.TextBuilder.Bounds.X > textWidth); } private void createTest(Action initFunc, Axes autoSizeAxes = Axes.Both) @@ -93,7 +98,7 @@ private void createTest(Action initFunc, Axes autoSizeAxes = Axes.Bo private class VisualDisplay : CompositeDrawable { - public readonly SpriteText Text; + public readonly TestSpriteText Text; public VisualDisplay(Action initFunc, Axes autoSizeAxes = Axes.Both) { @@ -109,11 +114,19 @@ public VisualDisplay(Action initFunc, Axes autoSizeAxes = Axes.Both) Alpha = 0.2f, Colour = Color4.Pink }, - Text = new SpriteText { AllowMultiline = false } + Text = new TestSpriteText { AllowMultiline = false } }; initFunc?.Invoke(Text); } } + + private class TestSpriteText : SpriteText + { + public TextBuilder TextBuilder { get; private set; } + + protected override TextBuilder CreateTextBuilder(ITexturedGlyphLookupStore store) + => TextBuilder = base.CreateTextBuilder(store); + } } } From ef6dba9cbb06081994b6750ed0d71d5342158849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Tue, 2 Mar 2021 21:24:40 +0100 Subject: [PATCH 2/3] Fix text builder not invalidating on indirect relative size changes --- osu.Framework/Graphics/Sprites/SpriteText.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/osu.Framework/Graphics/Sprites/SpriteText.cs b/osu.Framework/Graphics/Sprites/SpriteText.cs index b96c4fc843..4339310ef9 100644 --- a/osu.Framework/Graphics/Sprites/SpriteText.cs +++ b/osu.Framework/Graphics/Sprites/SpriteText.cs @@ -6,7 +6,6 @@ using System.Diagnostics; using osu.Framework.Allocation; using osu.Framework.Bindables; -using osu.Framework.Caching; using osu.Framework.Development; using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Graphics.Containers; @@ -55,6 +54,7 @@ public SpriteText() AddLayout(parentScreenSpaceCache); AddLayout(localScreenSpaceCache); AddLayout(shadowOffsetCache); + AddLayout(textBuilderCache); } [BackgroundDependencyLoader] @@ -575,12 +575,12 @@ private void invalidate(bool characters = false, bool textBuilder = false) /// protected virtual char FallbackCharacter => '?'; - private readonly Cached textBuilderBacking = new Cached(); + private readonly LayoutValue textBuilderCache = new LayoutValue(Invalidation.DrawSize); /// /// Invalidates the current , causing a new one to be created next time it's required via . /// - protected void InvalidateTextBuilder() => textBuilderBacking.Invalidate(); + protected void InvalidateTextBuilder() => textBuilderCache.Invalidate(); /// /// Creates a to generate the character layout for this . @@ -613,10 +613,10 @@ protected virtual TextBuilder CreateTextBuilder(ITexturedGlyphLookupStore store) private TextBuilder getTextBuilder() { - if (!textBuilderBacking.IsValid) - textBuilderBacking.Value = CreateTextBuilder(store); + if (!textBuilderCache.IsValid) + textBuilderCache.Value = CreateTextBuilder(store); - return textBuilderBacking.Value; + return textBuilderCache.Value; } public override string ToString() => $@"""{displayedText}"" " + base.ToString(); From 8c1ce0322d1297b310a392b426d51f2e943dfefb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Wed, 3 Mar 2021 20:12:47 +0100 Subject: [PATCH 3/3] Only invalidate text builder layout value from parent --- osu.Framework/Graphics/Sprites/SpriteText.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Framework/Graphics/Sprites/SpriteText.cs b/osu.Framework/Graphics/Sprites/SpriteText.cs index 4339310ef9..84fa78d4a1 100644 --- a/osu.Framework/Graphics/Sprites/SpriteText.cs +++ b/osu.Framework/Graphics/Sprites/SpriteText.cs @@ -575,7 +575,7 @@ private void invalidate(bool characters = false, bool textBuilder = false) /// protected virtual char FallbackCharacter => '?'; - private readonly LayoutValue textBuilderCache = new LayoutValue(Invalidation.DrawSize); + private readonly LayoutValue textBuilderCache = new LayoutValue(Invalidation.DrawSize, InvalidationSource.Parent); /// /// Invalidates the current , causing a new one to be created next time it's required via .