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

Add option to fix label width of a LabelledDrawable

This commit is contained in:
Bartłomiej Dach 2021-06-08 16:33:56 +02:00
parent 25af4dfa8a
commit 88266eac63
2 changed files with 90 additions and 3 deletions

View File

@ -2,11 +2,14 @@
// See the LICENCE file in the repository root for full licence text.
using NUnit.Framework;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Testing;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterfaceV2;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Tests.Visual.UserInterface
@ -21,6 +24,45 @@ namespace osu.Game.Tests.Visual.UserInterface
[TestCase(true)]
public void TestNonPadded(bool hasDescription) => createPaddedComponent(hasDescription, false);
[Test]
public void TestFixedWidth()
{
const float label_width = 200;
AddStep("create components", () => Child = new FillFlowContainer
{
RelativeSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10),
Children = new Drawable[]
{
new NonPaddedLabelledDrawable
{
Label = "short",
FixedLabelWidth = label_width
},
new NonPaddedLabelledDrawable
{
Label = "very very very very very very very very very very very long",
FixedLabelWidth = label_width
},
new PaddedLabelledDrawable
{
Label = "short",
FixedLabelWidth = label_width
},
new PaddedLabelledDrawable
{
Label = "very very very very very very very very very very very long",
FixedLabelWidth = label_width
}
}
});
AddStep("unset label width", () => this.ChildrenOfType<LabelledDrawable<Drawable>>().ForEach(d => d.FixedLabelWidth = null));
AddStep("reset label width", () => this.ChildrenOfType<LabelledDrawable<Drawable>>().ForEach(d => d.FixedLabelWidth = label_width));
}
private void createPaddedComponent(bool hasDescription = false, bool padded = true)
{
AddStep("create component", () =>

View File

@ -14,6 +14,27 @@ namespace osu.Game.Graphics.UserInterfaceV2
public abstract class LabelledDrawable<T> : CompositeDrawable
where T : Drawable
{
private float? fixedLabelWidth;
/// <summary>
/// The fixed width of the label of this <see cref="LabelledDrawable{T}"/>.
/// If <c>null</c>, the label portion will auto-size to its content.
/// Can be used in layout scenarios where several labels must match in length for the components to be aligned properly.
/// </summary>
public float? FixedLabelWidth
{
get => fixedLabelWidth;
set
{
if (fixedLabelWidth == value)
return;
fixedLabelWidth = value;
updateLabelWidth();
}
}
protected const float CONTENT_PADDING_VERTICAL = 10;
protected const float CONTENT_PADDING_HORIZONTAL = 15;
protected const float CORNER_RADIUS = 15;
@ -23,6 +44,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
/// </summary>
protected readonly T Component;
private readonly GridContainer grid;
private readonly OsuTextFlowContainer labelText;
private readonly OsuTextFlowContainer descriptionText;
@ -56,7 +78,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
Spacing = new Vector2(0, 12),
Children = new Drawable[]
{
new GridContainer
grid = new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
@ -69,7 +91,13 @@ namespace osu.Game.Graphics.UserInterfaceV2
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = 20 }
Padding = new MarginPadding
{
Right = 20,
// ensure that the label is always vertically padded even if the component itself isn't.
// this may become an issue if the label is taller than the component.
Vertical = padded ? 0 : CONTENT_PADDING_VERTICAL
}
},
new Container
{
@ -87,7 +115,6 @@ namespace osu.Game.Graphics.UserInterfaceV2
},
},
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) },
ColumnDimensions = new[] { new Dimension(GridSizeMode.AutoSize) }
},
descriptionText = new OsuTextFlowContainer(s => s.Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold, italics: true))
{
@ -99,6 +126,24 @@ namespace osu.Game.Graphics.UserInterfaceV2
}
}
};
updateLabelWidth();
}
private void updateLabelWidth()
{
if (fixedLabelWidth == null)
{
grid.ColumnDimensions = new[] { new Dimension(GridSizeMode.AutoSize) };
labelText.RelativeSizeAxes = Axes.None;
labelText.AutoSizeAxes = Axes.Both;
}
else
{
grid.ColumnDimensions = new[] { new Dimension(GridSizeMode.Absolute, fixedLabelWidth.Value) };
labelText.AutoSizeAxes = Axes.Y;
labelText.RelativeSizeAxes = Axes.X;
}
}
[BackgroundDependencyLoader]