Skip to content

Commit

Permalink
Merge pull request #60 from andy840119/should-not-masking-shader
Browse files Browse the repository at this point in the history
Should not masking shader parts.
  • Loading branch information
andy840119 authored Oct 3, 2021
2 parents ee9c7a0 + 184fdfa commit 379aa52
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
29 changes: 29 additions & 0 deletions osu.Framework.Font.Tests/Extensions/RectangleFExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Extensions;
using osu.Framework.Graphics.Primitives;

namespace osu.Framework.Font.Tests.Extensions
{
public class RectangleFExtensionsTest
{
[TestCase(2, Anchor.Centre, 0, 5, 40, 20)]
[TestCase(2, Anchor.TopLeft, 10, 10, 40, 20)]
[TestCase(2, Anchor.TopRight, -10, 10, 40, 20)]
[TestCase(2, Anchor.BottomLeft, 10, 0, 40, 20)]
[TestCase(2, Anchor.BottomRight, -10, 0, 40, 20)]
public void TestScale(float scale, Anchor origin, float x, float y, float width, float height)
{
var rectangle = new RectangleF(10, 10, 20, 10);
var target = rectangle.Scale(scale, origin);

Assert.AreEqual(target.X, x);
Assert.AreEqual(target.Y, y);
Assert.AreEqual(target.Width, width);
Assert.AreEqual(target.Height, height);
}
}
}
54 changes: 54 additions & 0 deletions osu.Framework.Font/Graphics/Extensions/RectangleFExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Graphics.Primitives;
using osuTK;

namespace osu.Framework.Graphics.Extensions
{
public static class RectangleFExtensions
{
public static RectangleF Scale(this RectangleF source, float scale, Anchor origin = Anchor.Centre)
=> source.Scale(new Vector2(scale), origin);

public static RectangleF Scale(this RectangleF source, Vector2 scale, Anchor origin = Anchor.Centre)
{
var newWidth = source.Width * scale.X;
var newHeight = source.Height * scale.Y;

var x = source.X - getXScale(origin) * (newWidth - source.Width);
var y = source.Y - getYScale(origin) * (newHeight - source.Height);

return new RectangleF(x, y, newWidth, newHeight);

static float getXScale(Anchor origin)
{
if (origin.HasFlagFast(Anchor.x0))
return 0;

if (origin.HasFlagFast(Anchor.x1))
return 0.5f;

if (origin.HasFlagFast(Anchor.x2))
return 1f;

return 100;
}

static float getYScale(Anchor origin)
{
if (origin.HasFlagFast(Anchor.y0))
return 0;

if (origin.HasFlagFast(Anchor.y1))
return 0.5f;

if (origin.HasFlagFast(Anchor.y2))
return 1f;

return 100;
}
}
}
}
2 changes: 1 addition & 1 deletion osu.Framework.Font/Graphics/MultiShaderBufferedDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected override void DrawContents()
else
{
// should draw origin content if no shader effects.
DrawFrameBuffer(SharedData.CurrentEffectBuffer, DrawRectangle, Color4.White);
base.DrawContents();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@

using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics.Extensions;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shaders;

namespace osu.Framework.Graphics.Sprites
{
public partial class KaraokeSpriteText<T>
{
protected override Quad ComputeScreenSpaceDrawQuad()
{
// make draw size become bigger (for not masking the shader).
var newRectangle = DrawRectangle.Scale(2);
return ToScreenSpace(newRectangle);
}

// todo: should have a better way to let user able to customize formats?
protected override DrawNode CreateDrawNode()
=> new KaraokeSpriteTextShaderEffectDrawNode(this, new MultiShaderBufferedDrawNodeSharedData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using osu.Framework.Graphics.Extensions;
using osu.Framework.Graphics.OpenGL.Vertices;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shaders;
Expand All @@ -13,6 +14,13 @@ namespace osu.Framework.Graphics.Sprites
{
public partial class LyricSpriteText
{
protected override Quad ComputeScreenSpaceDrawQuad()
{
// make draw size become bigger (for not masking the shader).
var newRectangle = DrawRectangle.Scale(2);
return ToScreenSpace(newRectangle);
}

// todo: should have a better way to let user able to customize formats?
protected override DrawNode CreateDrawNode()
=> new LyricSpriteTextShaderEffectDrawNode(this, new MultiShaderBufferedDrawNodeSharedData());
Expand Down

0 comments on commit 379aa52

Please sign in to comment.