mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 21:52:55 +08:00
Simplify DependencyProvidingContainer
Using an array of tuple for dependencies instead of using children.
This commit is contained in:
parent
5a0a223b1b
commit
87c39909c6
@ -47,15 +47,16 @@ namespace osu.Game.Rulesets.Catch.Tests
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
};
|
||||
droppedObjectContainer = new DroppedObjectContainer();
|
||||
Child = new DependencyProvidingContainer
|
||||
{
|
||||
Types = new[]
|
||||
CachedDependencies = new (Type, object)[]
|
||||
{
|
||||
typeof(DroppedObjectContainer),
|
||||
(typeof(DroppedObjectContainer), droppedObjectContainer),
|
||||
},
|
||||
Children = new Drawable[]
|
||||
{
|
||||
droppedObjectContainer = new DroppedObjectContainer(),
|
||||
droppedObjectContainer,
|
||||
catcher = new TestCatcher(trailContainer, difficulty),
|
||||
trailContainer
|
||||
},
|
||||
|
@ -2,7 +2,6 @@
|
||||
// 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;
|
||||
|
||||
@ -10,39 +9,24 @@ namespace osu.Game.Tests.Visual
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="Container"/> which providing ad-hoc dependencies to the child drawables.
|
||||
/// <para>
|
||||
/// To provide a dependency, specify the dependency type to <see cref="Types"/>, then specify the dependency value to either <see cref="Values"/> or <see cref="Container{Drawable}.Children"/>.
|
||||
/// For each type specified in <see cref="Types"/>, the first value compatible with the type is selected and provided to the children.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The <see cref="Types"/> and values of the dependencies must be set while this <see cref="DependencyProvidingContainer"/> is not loaded.
|
||||
/// The <see cref="CachedDependencies"/> must be set while this <see cref="DependencyProvidingContainer"/> is not loaded.
|
||||
/// </remarks>
|
||||
public class DependencyProvidingContainer : Container
|
||||
{
|
||||
/// <summary>
|
||||
/// The types of the dependencies provided to the children.
|
||||
/// The dependencies provided to the children.
|
||||
/// </summary>
|
||||
// TODO: should be an init-only property when C# 9
|
||||
public Type[] Types { get; set; } = Array.Empty<Type>();
|
||||
|
||||
/// <summary>
|
||||
/// The dependency values provided to the children.
|
||||
/// </summary>
|
||||
public object[] Values { get; set; } = Array.Empty<object>();
|
||||
public (Type, object)[] CachedDependencies { get; set; } = Array.Empty<(Type, object)>();
|
||||
|
||||
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.");
|
||||
|
||||
foreach (var (type, value) in CachedDependencies)
|
||||
dependencyContainer.CacheAs(type, value);
|
||||
}
|
||||
|
||||
return dependencyContainer;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user