// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osuTK; namespace osu.Framework.Graphics.Containers { /// /// A filling its parent while preserving a given target /// according to a . /// This is useful, for example, to automatically scale the user interface according to /// the window resolution, or to provide automatic HiDPI display support. /// public class DrawSizePreservingFillContainer : Container { private readonly Container content; protected override Container Content => content; /// /// The target to be enforced according to . /// public Vector2 TargetDrawSize = new Vector2(1024, 768); /// /// The strategy to be used for enforcing . The default strategy /// is Minimum, which preserves the aspect ratio of all children while ensuring one of the /// two axes matches while the other is always larger. /// public DrawSizePreservationStrategy Strategy; public DrawSizePreservingFillContainer() { AddInternal(content = new Container { RelativeSizeAxes = Axes.Both, }); RelativeSizeAxes = Axes.Both; } protected override void Update() { base.Update(); Vector2 drawSizeRatio = Vector2.Divide(Parent.ChildSize, TargetDrawSize); switch (Strategy) { case DrawSizePreservationStrategy.Minimum: content.Scale = new Vector2(Math.Min(drawSizeRatio.X, drawSizeRatio.Y)); break; case DrawSizePreservationStrategy.Maximum: content.Scale = new Vector2(Math.Max(drawSizeRatio.X, drawSizeRatio.Y)); break; case DrawSizePreservationStrategy.Average: content.Scale = new Vector2(0.5f * (drawSizeRatio.X + drawSizeRatio.Y)); break; case DrawSizePreservationStrategy.Separate: content.Scale = drawSizeRatio; break; } content.Size = Vector2.Divide(Vector2.One, content.Scale); } } /// /// Strategies used by to enforce its /// . /// public enum DrawSizePreservationStrategy { /// /// Preserves the aspect ratio of all children while ensuring one of the /// two axes matches /// while the other is always larger. /// Minimum, /// /// Preserves the aspect ratio of all children while ensuring one of the /// two axes matches /// while the other is always smaller. /// Maximum, /// /// Preserves the aspect ratio of all children while one axis is always larger and /// the other always smaller than , /// achieving a good compromise. /// Average, /// /// Ensures is perfectly /// matched while aspect ratio of children is disregarded. /// Separate, } }