2021-06-10 01:41:16 +08:00
|
|
|
// 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.
|
|
|
|
|
2021-06-10 18:41:41 +08:00
|
|
|
using JetBrains.Annotations;
|
2021-06-10 01:41:16 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-06-11 05:25:19 +08:00
|
|
|
using osu.Framework.Audio;
|
2021-06-10 01:41:16 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-06-24 15:07:38 +08:00
|
|
|
using osu.Framework.IO.Stores;
|
2021-06-11 05:25:19 +08:00
|
|
|
using osu.Framework.Platform;
|
2021-06-10 01:41:16 +08:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Rulesets;
|
2021-06-22 07:52:37 +08:00
|
|
|
using osu.Game.Rulesets.UI;
|
2021-06-10 01:41:16 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
/// <summary>
|
2021-06-22 07:52:37 +08:00
|
|
|
/// A type of <see cref="SkinProvidingContainer"/> specialized for <see cref="DrawableRuleset"/> and other gameplay-related components.
|
2021-06-22 17:05:17 +08:00
|
|
|
/// Providing access to parent skin sources and the beatmap skin each surrounded with the ruleset legacy skin transformer.
|
2021-06-10 01:41:16 +08:00
|
|
|
/// </summary>
|
|
|
|
public class RulesetSkinProvidingContainer : SkinProvidingContainer
|
|
|
|
{
|
2021-06-10 20:05:08 +08:00
|
|
|
protected readonly Ruleset Ruleset;
|
|
|
|
protected readonly IBeatmap Beatmap;
|
2021-06-10 01:41:16 +08:00
|
|
|
|
2021-06-22 07:52:37 +08:00
|
|
|
/// <remarks>
|
2021-06-22 17:05:17 +08:00
|
|
|
/// This container already re-exposes all parent <see cref="ISkinSource"/> sources in a ruleset-usable form.
|
2021-06-22 07:52:37 +08:00
|
|
|
/// 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
|
|
|
|
2021-06-10 01:41:16 +08:00
|
|
|
protected override Container<Drawable> Content { get; }
|
|
|
|
|
2021-06-10 18:41:41 +08:00
|
|
|
public RulesetSkinProvidingContainer(Ruleset ruleset, IBeatmap beatmap, [CanBeNull] ISkin beatmapSkin)
|
2021-06-10 01:41:16 +08:00
|
|
|
{
|
2021-06-10 20:05:08 +08:00
|
|
|
Ruleset = ruleset;
|
|
|
|
Beatmap = beatmap;
|
2021-06-10 01:41:16 +08:00
|
|
|
|
2021-06-21 09:16:58 +08:00
|
|
|
InternalChild = new BeatmapSkinProvidingContainer(beatmapSkin is LegacySkin ? GetLegacyRulesetTransformedSkin(beatmapSkin) : beatmapSkin)
|
2021-06-10 01:41:16 +08:00
|
|
|
{
|
|
|
|
Child = Content = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-23 15:09:25 +08:00
|
|
|
[Resolved]
|
|
|
|
private SkinManager skinManager { get; set; }
|
|
|
|
|
2021-06-10 01:41:16 +08:00
|
|
|
[Resolved]
|
2021-06-22 15:19:55 +08:00
|
|
|
private ISkinSource skinSource { get; set; }
|
2021-06-10 01:41:16 +08:00
|
|
|
|
2021-06-28 14:18:29 +08:00
|
|
|
private ResourceStoreBackedSkin rulesetResourcesSkin;
|
2021-06-28 14:11:28 +08:00
|
|
|
|
2021-06-10 01:41:16 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2021-06-28 14:11:28 +08:00
|
|
|
private void load(GameHost host, AudioManager audio)
|
2021-06-10 01:41:16 +08:00
|
|
|
{
|
2021-06-28 14:11:28 +08:00
|
|
|
if (Ruleset.CreateResourceStore() is IResourceStore<byte[]> resources)
|
2021-06-28 14:18:29 +08:00
|
|
|
rulesetResourcesSkin = new ResourceStoreBackedSkin(resources, host, audio);
|
2021-06-28 14:11:28 +08:00
|
|
|
|
2021-06-10 20:05:08 +08:00
|
|
|
UpdateSkins();
|
2021-06-22 15:19:55 +08:00
|
|
|
skinSource.SourceChanged += OnSourceChanged;
|
2021-06-10 01:41:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnSourceChanged()
|
|
|
|
{
|
2021-06-10 20:05:08 +08:00
|
|
|
UpdateSkins();
|
2021-06-10 01:41:16 +08:00
|
|
|
base.OnSourceChanged();
|
|
|
|
}
|
|
|
|
|
2021-06-10 20:05:08 +08:00
|
|
|
protected virtual void UpdateSkins()
|
2021-06-10 01:41:16 +08:00
|
|
|
{
|
2021-06-10 16:55:22 +08:00
|
|
|
SkinSources.Clear();
|
2021-06-10 18:04:34 +08:00
|
|
|
|
2021-06-22 15:19:55 +08:00
|
|
|
foreach (var skin in skinSource.AllSources)
|
2021-06-21 09:16:58 +08:00
|
|
|
{
|
2021-06-22 15:19:55 +08:00
|
|
|
switch (skin)
|
|
|
|
{
|
|
|
|
case LegacySkin legacySkin:
|
|
|
|
SkinSources.Add(GetLegacyRulesetTransformedSkin(legacySkin));
|
|
|
|
break;
|
2021-06-21 09:16:58 +08:00
|
|
|
|
2021-06-22 15:19:55 +08:00
|
|
|
default:
|
|
|
|
SkinSources.Add(skin);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-21 09:16:58 +08:00
|
|
|
}
|
2021-06-23 01:38:24 +08:00
|
|
|
|
2021-06-28 14:11:28 +08:00
|
|
|
int defaultSkinIndex = SkinSources.IndexOf(skinManager.DefaultSkin);
|
2021-06-24 15:07:38 +08:00
|
|
|
|
2021-06-28 14:38:42 +08:00
|
|
|
// Ruleset resources should be given the ability to override game-wide defaults
|
|
|
|
// This is achieved by placing them before an instance of DefaultSkin.
|
|
|
|
// Note that DefaultSkin may not be present in some test scenes.
|
2021-06-28 14:11:28 +08:00
|
|
|
if (defaultSkinIndex >= 0)
|
|
|
|
SkinSources.Insert(defaultSkinIndex, rulesetResourcesSkin);
|
|
|
|
else
|
|
|
|
SkinSources.Add(rulesetResourcesSkin);
|
2021-06-11 21:26:53 +08:00
|
|
|
}
|
|
|
|
|
2021-06-21 09:16:58 +08:00
|
|
|
protected ISkin GetLegacyRulesetTransformedSkin(ISkin legacySkin)
|
2021-06-11 21:26:53 +08:00
|
|
|
{
|
2021-06-21 09:16:58 +08:00
|
|
|
if (legacySkin == null)
|
|
|
|
return null;
|
2021-06-11 21:26:53 +08:00
|
|
|
|
2021-06-21 09:16:58 +08:00
|
|
|
var rulesetTransformed = Ruleset.CreateLegacySkinProvider(legacySkin, Beatmap);
|
2021-06-11 21:26:53 +08:00
|
|
|
if (rulesetTransformed != null)
|
|
|
|
return rulesetTransformed;
|
2021-06-10 18:04:34 +08:00
|
|
|
|
2021-06-21 09:16:58 +08:00
|
|
|
return legacySkin;
|
2021-06-10 01:41:16 +08:00
|
|
|
}
|
2021-06-22 07:52:37 +08:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
2021-06-10 18:04:34 +08:00
|
|
|
|
2021-06-22 15:19:55 +08:00
|
|
|
if (skinSource != null)
|
|
|
|
skinSource.SourceChanged -= OnSourceChanged;
|
2021-06-28 14:11:28 +08:00
|
|
|
|
|
|
|
rulesetResourcesSkin?.Dispose();
|
2021-06-10 01:41:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|