// 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 System.Linq; using osu.Framework.Allocation; using osu.Framework.Graphics.Containers; namespace osu.Game.Tests.Visual { /// /// A which providing ad-hoc dependencies to the child drawables. /// /// To provide a dependency, specify the dependency type to , then specify the dependency value to either or . /// For each type specified in , the first value compatible with the type is selected and provided to the children. /// /// /// /// The and values of the dependencies must be set while this is not loaded. /// public class DependencyProvidingContainer : Container { /// /// The types of the dependencies provided to the children. /// // TODO: should be an init-only property when C# 9 public Type[] Types { get; set; } = Array.Empty(); /// /// The dependency values provided to the children. /// public object[] Values { get; set; } = Array.Empty(); protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) { var dependencyContainer = new DependencyContainer(base.CreateChildDependencies(parent)); foreach (var type in Types) { object value = Values.FirstOrDefault(v => type.IsInstanceOfType(v)) ?? Children.FirstOrDefault(d => type.IsInstanceOfType(d)) ?? throw new InvalidOperationException($"The type {type} is specified in this {nameof(DependencyProvidingContainer)}, but no corresponding value is provided."); dependencyContainer.CacheAs(type, value); } return dependencyContainer; } } }