1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 02:17:46 +08:00
osu-lazer/osu.Game/Skinning/RulesetSkinProvidingContainer.cs

118 lines
4.1 KiB
C#
Raw Normal View History

// 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.
2022-06-17 15:37:17 +08:00
#nullable disable
2021-07-06 15:40:23 +08:00
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.IO.Stores;
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
using osu.Game.Rulesets.UI;
namespace osu.Game.Skinning
{
/// <summary>
/// A type of <see cref="SkinProvidingContainer"/> specialized for <see cref="DrawableRuleset"/> and other gameplay-related components.
/// Providing access to parent skin sources and the beatmap skin each surrounded with the ruleset legacy skin transformer.
/// </summary>
2022-11-24 13:32:20 +08:00
public partial class RulesetSkinProvidingContainer : SkinProvidingContainer
{
protected readonly Ruleset Ruleset;
protected readonly IBeatmap Beatmap;
/// <remarks>
/// This container already re-exposes all parent <see cref="ISkinSource"/> sources in a ruleset-usable form.
/// Therefore disallow falling back to any parent <see cref="ISkinSource"/> any further.
/// </remarks>
protected override bool AllowFallingBackToParent => false;
2021-06-22 15:20:09 +08:00
protected override Container<Drawable> Content { get; }
public RulesetSkinProvidingContainer(Ruleset ruleset, IBeatmap beatmap, [CanBeNull] ISkin beatmapSkin)
{
Ruleset = ruleset;
Beatmap = beatmap;
InternalChild = new BeatmapSkinProvidingContainer(GetRulesetTransformedSkin(beatmapSkin))
{
Child = Content = new Container
{
RelativeSizeAxes = Axes.Both,
}
};
}
private ResourceStoreBackedSkin rulesetResourcesSkin;
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
if (Ruleset.CreateResourceStore() is IResourceStore<byte[]> resources)
rulesetResourcesSkin = new ResourceStoreBackedSkin(resources, parent.Get<GameHost>(), parent.Get<AudioManager>());
return base.CreateChildDependencies(parent);
}
protected override void RefreshSources()
{
// Populate a local list first so we can adjust the returned order as we go.
var sources = new List<ISkin>();
Debug.Assert(ParentSource != null);
foreach (var source in ParentSource.AllSources)
{
switch (source)
{
case Skin skin:
sources.Add(GetRulesetTransformedSkin(skin));
break;
default:
sources.Add(source);
break;
}
}
2021-06-23 01:38:24 +08:00
2022-09-15 15:02:57 +08:00
// TODO: check
2022-09-17 23:14:49 +08:00
int lastDefaultSkinIndex = sources.IndexOf(sources.OfType<TrianglesSkin>().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)
sources.Insert(lastDefaultSkinIndex, rulesetResourcesSkin);
else
sources.Add(rulesetResourcesSkin);
2021-07-06 15:40:23 +08:00
SetSources(sources);
}
protected ISkin GetRulesetTransformedSkin(ISkin skin)
{
if (skin == null)
return null;
var rulesetTransformed = Ruleset.CreateSkinTransformer(skin, Beatmap);
if (rulesetTransformed != null)
return rulesetTransformed;
return skin;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
rulesetResourcesSkin?.Dispose();
}
}
}