1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 02:02:53 +08:00

Improved UprightUnscaledContainer

This commit is contained in:
HiddenNode 2022-08-22 14:24:52 +01:00
parent 2367dc9610
commit 1098e24c40
4 changed files with 73 additions and 83 deletions

View File

@ -8,7 +8,6 @@ using osu.Game.Configuration;
using osu.Game.Screens.Play.HUD;
using osu.Game.Skinning;
using osuTK;
using System;
namespace osu.Game.Extensions
{
@ -80,40 +79,5 @@ namespace osu.Game.Extensions
container.Add(child.CreateInstance());
}
}
/// <summary>
/// Keeps the drawable upright and unstretched preventing it from being rotated, sheared, scaled or flipped with its Parent.
/// </summary>
/// <param name="drawable">The drawable.</param>
public static void KeepUprightAndUnscaled(this Drawable drawable)
{
// Decomposes the inverse of the parent FrawInfo.Matrix into rotation, shear and scale.
var parentMatrix = drawable.Parent.DrawInfo.Matrix;
parentMatrix.Transpose();
// Remove Translation.
parentMatrix.M13 = 0.0f;
parentMatrix.M23 = 0.0f;
Matrix3 C = parentMatrix.Inverted();
// Extract the rotation.
float angle = MathF.Atan2(C.M21, C.M11);
drawable.Rotation = MathHelper.RadiansToDegrees(angle);
// Remove rotation from the C matrix so that it only contains shear and scale.
Matrix3 m = Matrix3.CreateRotationZ(-angle);
m.Transpose();
C = m * C;
// Extract shear and scale.
float alpha, sx, sy;
sx = C.M11;
sy = C.M22;
alpha = C.M12 / C.M22;
drawable.Scale = new Vector2(sx, sy);
drawable.Shear = new Vector2(-alpha, 0);
}
}
}

View File

@ -1,21 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Graphics.Containers;
namespace osu.Game.Graphics.Containers
{
/// <summary>
/// A container that grows in size to fit its child and retains its size when its child shrinks
/// </summary>
public class GrowToFitContainer : Container
{
protected override void Update()
{
base.Update();
Height = Math.Max(Child.Height, Height);
Width = Math.Max(Child.Width, Width);
}
}
}

View File

@ -4,6 +4,8 @@
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Layout;
using osuTK;
using System;
namespace osu.Game.Graphics.Containers
{
@ -12,21 +14,81 @@ namespace osu.Game.Graphics.Containers
/// </summary>
public class UprightUnscaledContainer : Container
{
protected override Container<Drawable> Content => content;
private readonly Container content;
public UprightUnscaledContainer()
{
InternalChild = content = new GrowToFitContainer();
AddLayout(layout);
}
private LayoutValue layout = new LayoutValue(Invalidation.DrawInfo, InvalidationSource.Parent);
private readonly LayoutValue layout = new LayoutValue(Invalidation.DrawInfo, InvalidationSource.Parent);
protected override void Update()
{
base.Update();
if (!layout.IsValid)
{
Extensions.DrawableExtensions.KeepUprightAndUnscaled(this);
keepUprightAndUnscaled();
layout.Validate();
}
}
/// <summary>
/// Keeps the drawable upright and unstretched preventing it from being rotated, sheared, scaled or flipped with its Parent.
/// </summary>
private void keepUprightAndUnscaled()
{
// Decomposes the inverse of the parent FrawInfo.Matrix into rotation, shear and scale.
var parentMatrix = Parent.DrawInfo.Matrix;
// Remove Translation.
parentMatrix.M31 = 0.0f;
parentMatrix.M32 = 0.0f;
Matrix3 reversedParrent = parentMatrix.Inverted();
// Extract the rotation.
float angle = MathF.Atan2(reversedParrent.M12, reversedParrent.M11);
Rotation = MathHelper.RadiansToDegrees(angle);
// Remove rotation from the C matrix so that it only contains shear and scale.
Matrix3 m = Matrix3.CreateRotationZ(-angle);
reversedParrent *= m;
// Extract shear and scale.
float sx = reversedParrent.M11;
float sy = reversedParrent.M22;
float alpha = reversedParrent.M21 / reversedParrent.M22;
Scale = new Vector2(sx, sy);
Shear = new Vector2(-alpha, 0);
}
/// <summary>
/// A container that grows in size to fit its children and retains its size when its children shrink
/// </summary>
private class GrowToFitContainer : Container
{
protected override Container<Drawable> Content => content;
private readonly Container content;
public GrowToFitContainer()
{
InternalChild = content = new Container
{
AutoSizeAxes = Axes.Both,
};
}
protected override void Update()
{
base.Update();
Height = Math.Max(content.Height, Height);
Width = Math.Max(content.Width, Width);
}
}
}
}

View File

@ -60,17 +60,12 @@ namespace osu.Game.Screens.Play.HUD
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Child = new GrowToFitContainer
Child = timeCurrent = new OsuSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Child = timeCurrent = new OsuSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Colour = colours.BlueLighter,
Font = OsuFont.Numeric,
}
Colour = colours.BlueLighter,
Font = OsuFont.Numeric,
}
}
},
@ -84,17 +79,12 @@ namespace osu.Game.Screens.Play.HUD
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Child = new GrowToFitContainer
Child = progress = new OsuSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Child = progress = new OsuSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Colour = colours.BlueLighter,
Font = OsuFont.Numeric,
}
Colour = colours.BlueLighter,
Font = OsuFont.Numeric,
}
}
},
@ -108,17 +98,12 @@ namespace osu.Game.Screens.Play.HUD
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Child = new GrowToFitContainer
Child = timeLeft = new OsuSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Child = timeLeft = new OsuSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Colour = colours.BlueLighter,
Font = OsuFont.Numeric,
}
Colour = colours.BlueLighter,
Font = OsuFont.Numeric,
}
}
}