// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osuTK; using System; using System.Collections.Generic; using osu.Framework.Extensions.EnumExtensions; namespace osu.Framework.Graphics.Containers { /// /// Holds extension methods for . /// public static class ContainerExtensions { /// /// Wraps the given with the given /// such that the can be used instead of the /// without affecting the layout. The must not contain any children before wrapping. /// /// The type of the . /// The type of the children of . /// The that should wrap the given . /// The that should be wrapped by the given . /// The given . public static TContainer Wrap(this TContainer container, TChild drawable) where TContainer : Container where TChild : Drawable { if (container.Children.Count != 0) throw new InvalidOperationException($"You may not wrap a {nameof(Container)} that has children."); container.RelativeSizeAxes = drawable.RelativeSizeAxes; container.AutoSizeAxes = Axes.Both & ~drawable.RelativeSizeAxes; container.Anchor = drawable.Anchor; container.Origin = drawable.Origin; container.Position = drawable.Position; container.Rotation = drawable.Rotation; drawable.Position = Vector2.Zero; drawable.Rotation = 0; // For anchor/origin positioning to be preserved correctly, // relatively sized axes must be lifted to the wrapping container. if (container.RelativeSizeAxes.HasFlagFast(Axes.X)) { container.Width = drawable.Width; drawable.Width = 1; } if (container.RelativeSizeAxes.HasFlagFast(Axes.Y)) { container.Height = drawable.Height; drawable.Height = 1; } container.Add(drawable); return container; } /// /// Set a specified on . /// /// The container type. /// The type of children contained by . /// The that will have a child set. /// The that should be set to the . /// The given . public static TContainer WithChild(this TContainer container, TChild child) where TContainer : IContainerCollection where TChild : Drawable { container.Child = child; return container; } /// /// Set specified on . /// /// The container type. /// The type of children contained by . /// The that will have children set. /// The that should be set to the . /// The given . public static TContainer WithChildren(this TContainer container, IEnumerable children) where TContainer : IContainerCollection where TChild : Drawable { container.ChildrenEnumerable = children; return container; } } }