mirror of
https://github.com/osukey/osukey.git
synced 2025-07-01 16:29:58 +09:00
Add option to fix label width of a LabelledDrawable
This commit is contained in:
@ -2,11 +2,14 @@
|
|||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Graphics.Shapes;
|
using osu.Framework.Graphics.Shapes;
|
||||||
|
using osu.Framework.Testing;
|
||||||
using osu.Game.Graphics.Sprites;
|
using osu.Game.Graphics.Sprites;
|
||||||
using osu.Game.Graphics.UserInterfaceV2;
|
using osu.Game.Graphics.UserInterfaceV2;
|
||||||
|
using osuTK;
|
||||||
using osuTK.Graphics;
|
using osuTK.Graphics;
|
||||||
|
|
||||||
namespace osu.Game.Tests.Visual.UserInterface
|
namespace osu.Game.Tests.Visual.UserInterface
|
||||||
@ -21,6 +24,45 @@ namespace osu.Game.Tests.Visual.UserInterface
|
|||||||
[TestCase(true)]
|
[TestCase(true)]
|
||||||
public void TestNonPadded(bool hasDescription) => createPaddedComponent(hasDescription, false);
|
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)
|
private void createPaddedComponent(bool hasDescription = false, bool padded = true)
|
||||||
{
|
{
|
||||||
AddStep("create component", () =>
|
AddStep("create component", () =>
|
||||||
|
@ -14,6 +14,27 @@ namespace osu.Game.Graphics.UserInterfaceV2
|
|||||||
public abstract class LabelledDrawable<T> : CompositeDrawable
|
public abstract class LabelledDrawable<T> : CompositeDrawable
|
||||||
where T : Drawable
|
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_VERTICAL = 10;
|
||||||
protected const float CONTENT_PADDING_HORIZONTAL = 15;
|
protected const float CONTENT_PADDING_HORIZONTAL = 15;
|
||||||
protected const float CORNER_RADIUS = 15;
|
protected const float CORNER_RADIUS = 15;
|
||||||
@ -23,6 +44,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected readonly T Component;
|
protected readonly T Component;
|
||||||
|
|
||||||
|
private readonly GridContainer grid;
|
||||||
private readonly OsuTextFlowContainer labelText;
|
private readonly OsuTextFlowContainer labelText;
|
||||||
private readonly OsuTextFlowContainer descriptionText;
|
private readonly OsuTextFlowContainer descriptionText;
|
||||||
|
|
||||||
@ -56,7 +78,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
|
|||||||
Spacing = new Vector2(0, 12),
|
Spacing = new Vector2(0, 12),
|
||||||
Children = new Drawable[]
|
Children = new Drawable[]
|
||||||
{
|
{
|
||||||
new GridContainer
|
grid = new GridContainer
|
||||||
{
|
{
|
||||||
RelativeSizeAxes = Axes.X,
|
RelativeSizeAxes = Axes.X,
|
||||||
AutoSizeAxes = Axes.Y,
|
AutoSizeAxes = Axes.Y,
|
||||||
@ -69,7 +91,13 @@ namespace osu.Game.Graphics.UserInterfaceV2
|
|||||||
Anchor = Anchor.CentreLeft,
|
Anchor = Anchor.CentreLeft,
|
||||||
Origin = Anchor.CentreLeft,
|
Origin = Anchor.CentreLeft,
|
||||||
AutoSizeAxes = Axes.Both,
|
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
|
new Container
|
||||||
{
|
{
|
||||||
@ -87,7 +115,6 @@ namespace osu.Game.Graphics.UserInterfaceV2
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) },
|
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))
|
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]
|
[BackgroundDependencyLoader]
|
||||||
|
Reference in New Issue
Block a user