// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; 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 { /// /// A function that converts coordinates from gamefield to screen space. /// public Func GamefieldToScreenSpace => scaledContent.GamefieldToScreenSpace; /// /// The scaled content. /// private readonly ScaledContainer scaledContent; protected override Container Content => scaledContent; /// /// 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, }); } private class ScaledContainer : Container { /// /// A function that converts coordinates from gamefield to screen space. /// public Func GamefieldToScreenSpace => content.ToScreenSpace; /// /// 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; private readonly Container content; protected override Container Content => content; public ScaledContainer() { AddInternal(content = new Container { RelativeSizeAxes = Axes.Both }); } protected override void Update() { base.Update(); content.Scale = sizeScale; content.Size = Vector2.Divide(Vector2.One, sizeScale); } /// /// 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; } } } } }