Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should re-calculate the transforms if change the shader in the karaoke sprite text. #179

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
// Copyright (c) karaoke.dev <[email protected]>. Licensed under the MIT Licence.
// Copyright (c) karaoke.dev <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Font.Tests.Helper;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing;
using osuTK;
using osuTK.Graphics;

namespace osu.Framework.Font.Tests.Visual.Sprites
{
public class TestSceneKaraokeSpriteText : TestScene
{
private readonly KaraokeSpriteText karaokeSpriteText;
private readonly TestKaraokeSpriteText karaokeSpriteText;
private readonly SpriteText transformAmountSpriteText;

private int transformAmount;

[Resolved]
private ShaderManager shaderManager { get; set; }

public TestSceneKaraokeSpriteText()
{
Child = karaokeSpriteText = new KaraokeSpriteText
Children = new Drawable[]
{
Text = "カラオケ!",
Rubies = TestCaseTagHelper.ParseParsePositionTexts(new[] { "[0,1]:か", "[2,3]:お" }),
Romajies = TestCaseTagHelper.ParseParsePositionTexts(new[] { "[1,2]:ra", "[3,4]:ke" }),
LeftTextColour = Color4.Green,
RightTextColour = Color4.Red,
karaokeSpriteText = new TestKaraokeSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = "カラオケ!",
Rubies = TestCaseTagHelper.ParseParsePositionTexts(new[] { "[0,1]:か", "[2,3]:お" }),
Romajies = TestCaseTagHelper.ParseParsePositionTexts(new[] { "[1,2]:ra", "[3,4]:ke" }),
LeftTextColour = Color4.Green,
RightTextColour = Color4.Red,
Scale = new Vector2(2),
TransformAction = () =>
{
transformAmount++;
updateTransformerCountText();
}
},
transformAmountSpriteText = new SpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Y = 100
}
};

updateTransformerCountText();

void updateTransformerCountText()
{
transformAmountSpriteText.Text = $"Transform has been triggered {transformAmount} times";
transformAmountSpriteText.FadeColour(Color4.Red, 100).Then().FadeColour(Color4.White);
}
}

[TestCase("Normal", new[] { "[0,start]:500", "[1,start]:600", "[2,start]:1000", "[3,start]:1500", "[4,start]:2000" })] // Normal time-tag.
Expand Down Expand Up @@ -148,5 +186,49 @@ public void TestMarginPadding(int rubyMargin, int romajiMargin)
karaokeSpriteText.RomajiMargin = romajiMargin;
});
}

[Test]
public void TestLyricShaders()
{
AddStep("Apply the shader", () =>
{
karaokeSpriteText.LeftLyricTextShaders = new[]
{
shaderManager.LocalInternalShader<OutlineShader>().With(s =>
{
s.Radius = 3;
s.Colour = Color4Extensions.FromHex("#FFDD77");
s.OutlineColour = Color4Extensions.FromHex("#CCA532");
})
};
karaokeSpriteText.RightLyricTextShaders = new[]
{
shaderManager.LocalInternalShader<OutlineShader>().With(s =>
{
s.Radius = 3;
s.Colour = Color4Extensions.FromHex("#AA88FF");
s.OutlineColour = Color4Extensions.FromHex("#5932CC");
})
};
});

AddStep("Clear shader", () =>
{
karaokeSpriteText.LeftLyricTextShaders = null;
karaokeSpriteText.RightLyricTextShaders = null;
});
}

private class TestKaraokeSpriteText : KaraokeSpriteText
{
public Action TransformAction;

public override void RefreshStateTransforms()
{
base.RefreshStateTransforms();

TransformAction?.Invoke();
}
}
}
}
41 changes: 25 additions & 16 deletions osu.Framework.Font/Graphics/Sprites/KaraokeSpriteText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public IReadOnlyList<IShader> LeftLyricTextShaders
{
leftLyricText.Shaders = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -160,7 +160,7 @@ public IReadOnlyList<IShader> RightLyricTextShaders
{
rightLyricText.Shaders = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -176,7 +176,7 @@ public string Text
leftLyricText.Text = value;
rightLyricText.Text = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -188,7 +188,7 @@ public IReadOnlyList<PositionText> Rubies
leftLyricText.Rubies = value;
rightLyricText.Rubies = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -200,7 +200,7 @@ public IReadOnlyList<PositionText> Romajies
leftLyricText.Romajies = value;
rightLyricText.Romajies = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -216,7 +216,7 @@ public FontUsage Font
leftLyricText.Font = value;
rightLyricText.Font = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -228,7 +228,7 @@ public FontUsage RubyFont
leftLyricText.RubyFont = value;
rightLyricText.RubyFont = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -240,7 +240,7 @@ public FontUsage RomajiFont
leftLyricText.RomajiFont = value;
rightLyricText.RomajiFont = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ public LyricTextAlignment RubyAlignment
leftLyricText.RubyAlignment = value;
rightLyricText.RubyAlignment = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -290,7 +290,7 @@ public LyricTextAlignment RomajiAlignment
leftLyricText.RomajiAlignment = value;
rightLyricText.RomajiAlignment = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -306,7 +306,7 @@ public Vector2 Spacing
leftLyricText.Spacing = value;
rightLyricText.Spacing = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -318,7 +318,7 @@ public Vector2 RubySpacing
leftLyricText.RubySpacing = value;
rightLyricText.RubySpacing = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -330,7 +330,7 @@ public Vector2 RomajiSpacing
leftLyricText.RomajiSpacing = value;
rightLyricText.RomajiSpacing = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -346,7 +346,7 @@ public int RubyMargin
leftLyricText.RubyMargin = value;
rightLyricText.RubyMargin = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand All @@ -358,7 +358,7 @@ public int RomajiMargin
leftLyricText.RomajiMargin = value;
rightLyricText.RomajiMargin = value;

Invalidate(Invalidation.DrawNode);
Invalidate(Invalidation.Layout);
}
}

Expand Down Expand Up @@ -423,14 +423,23 @@ protected override bool OnInvalidate(Invalidation invalidation, InvalidationSour
{
var result = base.OnInvalidate(invalidation, source);

if (!invalidation.HasFlag(Invalidation.Presence))
if (!invalidation.HasFlag(Invalidation.Layout))
return result;

Schedule(RefreshStateTransforms);

return true;
}

protected override void LoadComplete()
{
base.LoadComplete();

// Because refresh state only triggered if some property changed.
// So we should make sure that it will be triggered at least once.
RefreshStateTransforms();
}

public virtual void RefreshStateTransforms()
{
// reset masking transform.
Expand Down