1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 01:27:29 +08:00

Remove BindableList usage

This commit is contained in:
Dean Herbert 2021-07-06 16:40:23 +09:00
parent fe70b52086
commit 93ef783339
2 changed files with 37 additions and 58 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
@ -72,31 +73,36 @@ namespace osu.Game.Skinning
protected virtual void UpdateSkinSources()
{
SkinSources.Clear();
ResetSources();
var skinSources = new List<ISkin>();
foreach (var skin in parentSource.AllSources)
{
switch (skin)
{
case LegacySkin legacySkin:
SkinSources.Add(GetLegacyRulesetTransformedSkin(legacySkin));
skinSources.Add(GetLegacyRulesetTransformedSkin(legacySkin));
break;
default:
SkinSources.Add(skin);
skinSources.Add(skin);
break;
}
}
int lastDefaultSkinIndex = SkinSources.IndexOf(SkinSources.OfType<DefaultSkin>().LastOrDefault());
int lastDefaultSkinIndex = skinSources.IndexOf(skinSources.OfType<DefaultSkin>().LastOrDefault());
// Ruleset resources should be given the ability to override game-wide defaults
// This is achieved by placing them before the last instance of DefaultSkin.
// Note that DefaultSkin may not be present in some test scenes.
if (lastDefaultSkinIndex >= 0)
SkinSources.Insert(lastDefaultSkinIndex, rulesetResourcesSkin);
skinSources.Insert(lastDefaultSkinIndex, rulesetResourcesSkin);
else
SkinSources.Add(rulesetResourcesSkin);
skinSources.Add(rulesetResourcesSkin);
foreach (var skin in skinSources)
AddSource(skin);
}
protected ISkin GetLegacyRulesetTransformedSkin(ISkin legacySkin)

View File

@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
@ -27,13 +26,13 @@ namespace osu.Game.Skinning
/// <summary>
/// Skins which should be exposed by this container, in order of lookup precedence.
/// </summary>
protected readonly BindableList<ISkin> SkinSources = new BindableList<ISkin>();
protected IEnumerable<ISkin> SkinSources => skinSources.Keys;
/// <summary>
/// A dictionary mapping each <see cref="ISkin"/> from the <see cref="SkinSources"/>
/// to one that performs the "allow lookup" checks before proceeding with a lookup.
/// </summary>
private readonly Dictionary<ISkin, DisableableSkinSource> disableableSkinSources = new Dictionary<ISkin, DisableableSkinSource>();
private readonly Dictionary<ISkin, DisableableSkinSource> skinSources = new Dictionary<ISkin, DisableableSkinSource>();
[CanBeNull]
private ISkinSource fallbackSource;
@ -60,7 +59,7 @@ namespace osu.Game.Skinning
: this()
{
if (skin != null)
SkinSources.Add(skin);
AddSource(skin);
}
/// <summary>
@ -70,61 +69,35 @@ namespace osu.Game.Skinning
protected SkinProvidingContainer()
{
RelativeSizeAxes = Axes.Both;
}
SkinSources.BindCollectionChanged(((_, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var skin in args.NewItems.Cast<ISkin>())
{
disableableSkinSources.Add(skin, new DisableableSkinSource(skin, this));
public void AddSource(ISkin skin)
{
skinSources.Add(skin, new DisableableSkinSource(skin, this));
if (skin is ISkinSource source)
source.SourceChanged += OnSourceChanged;
}
if (skin is ISkinSource source)
source.SourceChanged += OnSourceChanged;
}
break;
public void RemoveSource(ISkin skin)
{
skinSources.Remove(skin);
case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Remove:
foreach (var skin in args.OldItems.Cast<ISkin>())
{
disableableSkinSources.Remove(skin);
if (skin is ISkinSource source)
source.SourceChanged += OnSourceChanged;
}
if (skin is ISkinSource source)
source.SourceChanged -= OnSourceChanged;
}
break;
case NotifyCollectionChangedAction.Replace:
foreach (var skin in args.OldItems.Cast<ISkin>())
{
disableableSkinSources.Remove(skin);
if (skin is ISkinSource source)
source.SourceChanged -= OnSourceChanged;
}
foreach (var skin in args.NewItems.Cast<ISkin>())
{
disableableSkinSources.Add(skin, new DisableableSkinSource(skin, this));
if (skin is ISkinSource source)
source.SourceChanged += OnSourceChanged;
}
break;
}
}), true);
public void ResetSources()
{
foreach (var skin in AllSources.ToArray())
RemoveSource(skin);
}
public ISkin FindProvider(Func<ISkin, bool> lookupFunction)
{
foreach (var skin in SkinSources)
{
if (lookupFunction(disableableSkinSources[skin]))
if (lookupFunction(skinSources[skin]))
return skin;
}
@ -151,7 +124,7 @@ namespace osu.Game.Skinning
foreach (var skin in SkinSources)
{
Drawable sourceDrawable;
if ((sourceDrawable = disableableSkinSources[skin]?.GetDrawableComponent(component)) != null)
if ((sourceDrawable = skinSources[skin]?.GetDrawableComponent(component)) != null)
return sourceDrawable;
}
@ -163,7 +136,7 @@ namespace osu.Game.Skinning
foreach (var skin in SkinSources)
{
Texture sourceTexture;
if ((sourceTexture = disableableSkinSources[skin]?.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
if ((sourceTexture = skinSources[skin]?.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
return sourceTexture;
}
@ -175,7 +148,7 @@ namespace osu.Game.Skinning
foreach (var skin in SkinSources)
{
ISample sourceSample;
if ((sourceSample = disableableSkinSources[skin]?.GetSample(sampleInfo)) != null)
if ((sourceSample = skinSources[skin]?.GetSample(sampleInfo)) != null)
return sourceSample;
}
@ -187,7 +160,7 @@ namespace osu.Game.Skinning
foreach (var skin in SkinSources)
{
IBindable<TValue> bindable;
if ((bindable = disableableSkinSources[skin]?.GetConfig<TLookup, TValue>(lookup)) != null)
if ((bindable = skinSources[skin]?.GetConfig<TLookup, TValue>(lookup)) != null)
return bindable;
}