2021-06-09 17:41:16 +00: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-07-06 07:40:23 +00:00
|
|
|
using System.Collections.Generic;
|
2021-06-28 09:43:12 +00:00
|
|
|
using System.Linq;
|
2021-06-10 10:41:41 +00:00
|
|
|
using JetBrains.Annotations;
|
2021-06-09 17:41:16 +00:00
|
|
|
using osu.Framework.Allocation;
|
2021-06-10 21:25:19 +00:00
|
|
|
using osu.Framework.Audio;
|
2021-06-09 17:41:16 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-06-24 07:07:38 +00:00
|
|
|
using osu.Framework.IO.Stores;
|
2021-06-10 21:25:19 +00:00
|
|
|
using osu.Framework.Platform;
|
2021-06-09 17:41:16 +00:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Rulesets;
|
2021-06-21 23:52:37 +00:00
|
|
|
using osu.Game.Rulesets.UI;
|
2021-06-09 17:41:16 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
/// <summary>
|
2021-06-21 23:52:37 +00:00
|
|
|
/// A type of <see cref="SkinProvidingContainer"/> specialized for <see cref="DrawableRuleset"/> and other gameplay-related components.
|
2021-06-22 09:05:17 +00:00
|
|
|
/// Providing access to parent skin sources and the beatmap skin each surrounded with the ruleset legacy skin transformer.
|
2021-06-09 17:41:16 +00:00
|
|
|
/// </summary>
|
|
|
|
public class RulesetSkinProvidingContainer : SkinProvidingContainer
|
|
|
|
{
|
2021-06-10 12:05:08 +00:00
|
|
|
protected readonly Ruleset Ruleset;
|
|
|
|
protected readonly IBeatmap Beatmap;
|
2021-06-09 17:41:16 +00:00
|
|
|
|
2021-06-21 23:52:37 +00:00
|
|
|
/// <remarks>
|
2021-06-22 09:05:17 +00:00
|
|
|
/// This container already re-exposes all parent <see cref="ISkinSource"/> sources in a ruleset-usable form.
|
2021-06-21 23:52:37 +00:00
|
|
|
/// Therefore disallow falling back to any parent <see cref="ISkinSource"/> any further.
|
|
|
|
/// </remarks>
|
|
|
|
protected override bool AllowFallingBackToParent => false;
|
2021-06-22 07:20:09 +00:00
|
|
|
|
2021-06-09 17:41:16 +00:00
|
|
|
protected override Container<Drawable> Content { get; }
|
|
|
|
|
2021-06-10 10:41:41 +00:00
|
|
|
public RulesetSkinProvidingContainer(Ruleset ruleset, IBeatmap beatmap, [CanBeNull] ISkin beatmapSkin)
|
2021-06-09 17:41:16 +00:00
|
|
|
{
|
2021-06-10 12:05:08 +00:00
|
|
|
Ruleset = ruleset;
|
|
|
|
Beatmap = beatmap;
|
2021-06-09 17:41:16 +00:00
|
|
|
|
2021-06-21 01:16:58 +00:00
|
|
|
InternalChild = new BeatmapSkinProvidingContainer(beatmapSkin is LegacySkin ? GetLegacyRulesetTransformedSkin(beatmapSkin) : beatmapSkin)
|
2021-06-09 17:41:16 +00:00
|
|
|
{
|
|
|
|
Child = Content = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-25 06:55:29 +00:00
|
|
|
private ISkinSource parentSource;
|
2021-06-09 17:41:16 +00:00
|
|
|
|
2021-06-28 06:18:29 +00:00
|
|
|
private ResourceStoreBackedSkin rulesetResourcesSkin;
|
2021-06-28 06:11:28 +00:00
|
|
|
|
2021-06-25 06:55:29 +00:00
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
|
2021-06-09 17:41:16 +00:00
|
|
|
{
|
2021-06-25 06:55:29 +00:00
|
|
|
parentSource = parent.Get<ISkinSource>();
|
2021-06-28 05:39:55 +00:00
|
|
|
parentSource.SourceChanged += OnSourceChanged;
|
2021-06-25 06:55:29 +00:00
|
|
|
|
2021-06-28 06:11:28 +00:00
|
|
|
if (Ruleset.CreateResourceStore() is IResourceStore<byte[]> resources)
|
2021-06-28 08:16:26 +00:00
|
|
|
rulesetResourcesSkin = new ResourceStoreBackedSkin(resources, parent.Get<GameHost>(), parent.Get<AudioManager>());
|
|
|
|
|
2021-06-28 05:39:55 +00:00
|
|
|
// ensure sources are populated and ready for use before childrens' asynchronous load flow.
|
2021-06-25 06:55:29 +00:00
|
|
|
UpdateSkinSources();
|
2021-06-28 06:11:28 +00:00
|
|
|
|
2021-06-25 06:55:29 +00:00
|
|
|
return base.CreateChildDependencies(parent);
|
2021-06-09 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnSourceChanged()
|
|
|
|
{
|
2021-06-25 06:55:29 +00:00
|
|
|
UpdateSkinSources();
|
2021-06-09 17:41:16 +00:00
|
|
|
base.OnSourceChanged();
|
|
|
|
}
|
|
|
|
|
2021-06-25 06:55:29 +00:00
|
|
|
protected virtual void UpdateSkinSources()
|
2021-06-09 17:41:16 +00:00
|
|
|
{
|
2021-07-06 07:40:23 +00:00
|
|
|
ResetSources();
|
|
|
|
|
|
|
|
var skinSources = new List<ISkin>();
|
2021-06-10 10:04:34 +00:00
|
|
|
|
2021-06-25 06:55:29 +00:00
|
|
|
foreach (var skin in parentSource.AllSources)
|
2021-06-21 01:16:58 +00:00
|
|
|
{
|
2021-06-22 07:19:55 +00:00
|
|
|
switch (skin)
|
|
|
|
{
|
|
|
|
case LegacySkin legacySkin:
|
2021-07-06 07:40:23 +00:00
|
|
|
skinSources.Add(GetLegacyRulesetTransformedSkin(legacySkin));
|
2021-06-22 07:19:55 +00:00
|
|
|
break;
|
2021-06-21 01:16:58 +00:00
|
|
|
|
2021-06-22 07:19:55 +00:00
|
|
|
default:
|
2021-07-06 07:40:23 +00:00
|
|
|
skinSources.Add(skin);
|
2021-06-22 07:19:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-06-21 01:16:58 +00:00
|
|
|
}
|
2021-06-22 17:38:24 +00:00
|
|
|
|
2021-07-06 07:40:23 +00:00
|
|
|
int lastDefaultSkinIndex = skinSources.IndexOf(skinSources.OfType<DefaultSkin>().LastOrDefault());
|
2021-06-24 07:07:38 +00:00
|
|
|
|
2021-06-28 06:38:42 +00:00
|
|
|
// Ruleset resources should be given the ability to override game-wide defaults
|
2021-06-28 09:43:12 +00:00
|
|
|
// This is achieved by placing them before the last instance of DefaultSkin.
|
2021-06-28 06:38:42 +00:00
|
|
|
// Note that DefaultSkin may not be present in some test scenes.
|
2021-06-28 09:43:12 +00:00
|
|
|
if (lastDefaultSkinIndex >= 0)
|
2021-07-06 07:40:23 +00:00
|
|
|
skinSources.Insert(lastDefaultSkinIndex, rulesetResourcesSkin);
|
2021-06-28 06:11:28 +00:00
|
|
|
else
|
2021-07-06 07:40:23 +00:00
|
|
|
skinSources.Add(rulesetResourcesSkin);
|
|
|
|
|
|
|
|
foreach (var skin in skinSources)
|
|
|
|
AddSource(skin);
|
2021-06-11 13:26:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 01:16:58 +00:00
|
|
|
protected ISkin GetLegacyRulesetTransformedSkin(ISkin legacySkin)
|
2021-06-11 13:26:53 +00:00
|
|
|
{
|
2021-06-21 01:16:58 +00:00
|
|
|
if (legacySkin == null)
|
|
|
|
return null;
|
2021-06-11 13:26:53 +00:00
|
|
|
|
2021-06-21 01:16:58 +00:00
|
|
|
var rulesetTransformed = Ruleset.CreateLegacySkinProvider(legacySkin, Beatmap);
|
2021-06-11 13:26:53 +00:00
|
|
|
if (rulesetTransformed != null)
|
|
|
|
return rulesetTransformed;
|
2021-06-10 10:04:34 +00:00
|
|
|
|
2021-06-21 01:16:58 +00:00
|
|
|
return legacySkin;
|
2021-06-09 17:41:16 +00:00
|
|
|
}
|
2021-06-21 23:52:37 +00:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
2021-06-10 10:04:34 +00:00
|
|
|
|
2021-06-25 06:55:29 +00:00
|
|
|
if (parentSource != null)
|
|
|
|
parentSource.SourceChanged -= OnSourceChanged;
|
2021-06-28 06:11:28 +00:00
|
|
|
|
|
|
|
rulesetResourcesSkin?.Dispose();
|
2021-06-09 17:41:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|