// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using OpenTK; namespace osu.Game.Rulesets.UI { /// /// A which can have its internal coordinate system scaled to a specific size. /// public class ScalableContainer : Container { /// /// The scaled content. /// public readonly Container ScaledContent; protected override Container Content => content; private readonly Container content; /// /// A which can have its internal coordinate system scaled to a specific size. /// /// The width to scale the internal coordinate space to. /// May be null if scaling based on is desired. If is also null, no scaling will occur. /// /// The height to scale the internal coordinate space to. /// May be null if scaling based on is desired. If is also null, no scaling will occur. /// public ScalableContainer(float? customWidth = null, float? customHeight = null) { AddInternal(ScaledContent = new ScaledContainer { CustomWidth = customWidth, CustomHeight = customHeight, RelativeSizeAxes = Axes.Both, Child = content = new Container { RelativeSizeAxes = Axes.Both } }); } public class ScaledContainer : Container { /// /// The value to scale the width of the content to match. /// If null, is used. /// public float? CustomWidth; /// /// The value to scale the height of the content to match. /// if null, is used. /// public float? CustomHeight; /// /// The scale that is required for the size of the content to match and . /// private Vector2 sizeScale { get { if (CustomWidth.HasValue && CustomHeight.HasValue) return Vector2.Divide(DrawSize, new Vector2(CustomWidth.Value, CustomHeight.Value)); if (CustomWidth.HasValue) return new Vector2(DrawSize.X / CustomWidth.Value); if (CustomHeight.HasValue) return new Vector2(DrawSize.Y / CustomHeight.Value); return Vector2.One; } } /// /// Scale the content to the required container size by multiplying by . /// protected override Vector2 DrawScale => sizeScale * base.DrawScale; protected override void Update() { base.Update(); RelativeChildSize = sizeScale; } } } }