mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 17:07:38 +08:00
Added GrowToFitContent Parameter.
This commit is contained in:
parent
43f2ba6596
commit
cda7faecf7
@ -24,6 +24,27 @@ namespace osu.Game.Graphics.Containers
|
||||
/// </summary>
|
||||
public ScaleMode Scaling { get; set; } = ScaleMode.Vertical;
|
||||
|
||||
/// <summary>
|
||||
/// If this is true, all wrapper containers will be set to grow with their content
|
||||
/// and not shrink back, this is used to fix the position of children that change
|
||||
/// in size when using AutoSizeAxes.
|
||||
/// </summary>
|
||||
public bool GrowToFitContent
|
||||
{
|
||||
get => growToFitContent;
|
||||
set
|
||||
{
|
||||
if (growToFitContent != value)
|
||||
{
|
||||
foreach (GrowToFitContainer c in Children)
|
||||
c.GrowToFitContentUpdated = true;
|
||||
growToFitContent = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool growToFitContent = true;
|
||||
|
||||
private readonly LayoutValue layout = new LayoutValue(Invalidation.DrawInfo, InvalidationSource.Parent);
|
||||
|
||||
public UprightAspectMaintainingContainer()
|
||||
@ -44,10 +65,11 @@ namespace osu.Game.Graphics.Containers
|
||||
|
||||
public override void Add(Drawable drawable)
|
||||
{
|
||||
base.Add(new GrowToFitContainer
|
||||
{
|
||||
Child = drawable
|
||||
});
|
||||
var wrapper = new GrowToFitContainer();
|
||||
wrapper.Wrap(drawable);
|
||||
wrapper.AutoSizeAxes = Axes.None;
|
||||
drawable.Origin = drawable.Anchor = Anchor.Centre;
|
||||
base.Add(wrapper);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -58,7 +80,7 @@ namespace osu.Game.Graphics.Containers
|
||||
// Decomposes the inverse of the parent DrawInfo.Matrix into rotation, shear and scale.
|
||||
var parentMatrix = Parent.DrawInfo.Matrix;
|
||||
|
||||
// Remove Translation.
|
||||
// Remove Translation.>
|
||||
parentMatrix.M31 = 0.0f;
|
||||
parentMatrix.M32 = 0.0f;
|
||||
|
||||
@ -108,17 +130,55 @@ namespace osu.Game.Graphics.Containers
|
||||
|
||||
public class GrowToFitContainer : Container
|
||||
{
|
||||
private readonly LayoutValue layout = new LayoutValue(Invalidation.RequiredParentSizeToFit, InvalidationSource.Child);
|
||||
|
||||
private Vector2 maxChildSize;
|
||||
|
||||
public bool GrowToFitContentUpdated { get; set; }
|
||||
|
||||
public GrowToFitContainer()
|
||||
{
|
||||
AddLayout(layout);
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
if ((Child.RelativeSizeAxes & Axes.X) != 0)
|
||||
RelativeSizeAxes |= Axes.X;
|
||||
else
|
||||
Width = Math.Max(Child.Width, Width);
|
||||
UprightAspectMaintainingContainer parent = (UprightAspectMaintainingContainer)Parent;
|
||||
|
||||
if ((Child.RelativeSizeAxes & Axes.Y) != 0)
|
||||
RelativeSizeAxes |= Axes.Y;
|
||||
else
|
||||
Height = Math.Max(Child.Height, Height);
|
||||
if (!layout.IsValid || GrowToFitContentUpdated)
|
||||
{
|
||||
if ((Child.RelativeSizeAxes & Axes.X) != 0)
|
||||
RelativeSizeAxes |= Axes.X;
|
||||
else
|
||||
{
|
||||
if (parent.GrowToFitContent)
|
||||
Width = Math.Max(Child.Width * Child.Scale.X, maxChildSize.X);
|
||||
else
|
||||
Width = Child.Width * Child.Scale.X;
|
||||
}
|
||||
|
||||
if ((Child.RelativeSizeAxes & Axes.Y) != 0)
|
||||
RelativeSizeAxes |= Axes.Y;
|
||||
else
|
||||
{
|
||||
if (parent.GrowToFitContent)
|
||||
Height = Math.Max(Child.Height * Child.Scale.Y, maxChildSize.Y);
|
||||
else
|
||||
Height = Child.Height * Child.Scale.Y;
|
||||
}
|
||||
|
||||
// reset max_child_size or update it
|
||||
if (!parent.GrowToFitContent)
|
||||
maxChildSize = Child.Size;
|
||||
else
|
||||
{
|
||||
maxChildSize.X = MathF.Max(maxChildSize.X, Child.Size.X);
|
||||
maxChildSize.Y = MathF.Max(maxChildSize.Y, Child.Size.Y);
|
||||
}
|
||||
|
||||
GrowToFitContentUpdated = false;
|
||||
layout.Validate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user