2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2018-04-20 23:17:57 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
|
{
|
2019-02-05 16:50:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A container which overrides existing skin options with beatmap-local values.
|
|
|
|
|
/// </summary>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public class LocalSkinOverrideContainer : Container, ISkinSource
|
|
|
|
|
{
|
|
|
|
|
public event Action SourceChanged;
|
|
|
|
|
|
2018-12-13 13:51:18 +08:00
|
|
|
|
private readonly Bindable<bool> beatmapSkins = new Bindable<bool>();
|
|
|
|
|
private readonly Bindable<bool> beatmapHitsounds = new Bindable<bool>();
|
2018-04-25 15:15:23 +08:00
|
|
|
|
|
2019-04-25 16:36:17 +08:00
|
|
|
|
private readonly ISkin skin;
|
2019-02-05 16:50:32 +08:00
|
|
|
|
private ISkinSource fallbackSource;
|
|
|
|
|
|
2019-04-25 16:36:17 +08:00
|
|
|
|
public LocalSkinOverrideContainer(ISkin skin)
|
2019-02-05 16:50:32 +08:00
|
|
|
|
{
|
2019-04-25 16:36:17 +08:00
|
|
|
|
this.skin = skin;
|
2019-02-05 16:50:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 23:17:57 +08:00
|
|
|
|
public Drawable GetDrawableComponent(string componentName)
|
|
|
|
|
{
|
|
|
|
|
Drawable sourceDrawable;
|
2019-04-25 16:36:17 +08:00
|
|
|
|
if (beatmapSkins.Value && (sourceDrawable = skin.GetDrawableComponent(componentName)) != null)
|
2018-04-20 23:17:57 +08:00
|
|
|
|
return sourceDrawable;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
|
2018-04-20 23:17:57 +08:00
|
|
|
|
return fallbackSource?.GetDrawableComponent(componentName);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-04-20 23:17:57 +08:00
|
|
|
|
public Texture GetTexture(string componentName)
|
|
|
|
|
{
|
|
|
|
|
Texture sourceTexture;
|
2019-04-25 16:36:17 +08:00
|
|
|
|
if (beatmapSkins.Value && (sourceTexture = skin.GetTexture(componentName)) != null)
|
2018-04-20 23:17:57 +08:00
|
|
|
|
return sourceTexture;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
|
2018-04-20 23:17:57 +08:00
|
|
|
|
return fallbackSource.GetTexture(componentName);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-04-20 23:30:41 +08:00
|
|
|
|
public SampleChannel GetSample(string sampleName)
|
|
|
|
|
{
|
|
|
|
|
SampleChannel sourceChannel;
|
2019-04-25 16:36:17 +08:00
|
|
|
|
if (beatmapHitsounds.Value && (sourceChannel = skin.GetSample(sampleName)) != null)
|
2018-04-20 23:30:41 +08:00
|
|
|
|
return sourceChannel;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
|
2018-04-20 23:30:41 +08:00
|
|
|
|
return fallbackSource?.GetSample(sampleName);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-01-07 19:12:39 +08:00
|
|
|
|
public TValue GetValue<TConfiguration, TValue>(Func<TConfiguration, TValue> query) where TConfiguration : SkinConfiguration
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-01-07 19:12:39 +08:00
|
|
|
|
TValue val;
|
2019-04-25 16:36:17 +08:00
|
|
|
|
if ((skin as Skin)?.Configuration is TConfiguration conf)
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (beatmapSkins.Value && (val = query.Invoke(conf)) != null)
|
2018-12-08 05:16:09 +08:00
|
|
|
|
return val;
|
2019-01-07 19:12:39 +08:00
|
|
|
|
|
|
|
|
|
return fallbackSource == null ? default : fallbackSource.GetValue(query);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onSourceChanged() => SourceChanged?.Invoke();
|
|
|
|
|
|
2018-07-11 16:07:14 +08:00
|
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2018-07-11 16:07:14 +08:00
|
|
|
|
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
fallbackSource = dependencies.Get<ISkinSource>();
|
2019-02-27 13:30:04 +08:00
|
|
|
|
if (fallbackSource != null)
|
|
|
|
|
fallbackSource.SourceChanged += onSourceChanged;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
dependencies.CacheAs<ISkinSource>(this);
|
|
|
|
|
|
2019-02-27 13:30:04 +08:00
|
|
|
|
var config = dependencies.Get<OsuConfigManager>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-12-13 13:51:18 +08:00
|
|
|
|
config.BindWith(OsuSetting.BeatmapSkins, beatmapSkins);
|
|
|
|
|
config.BindWith(OsuSetting.BeatmapHitsounds, beatmapHitsounds);
|
|
|
|
|
|
|
|
|
|
beatmapSkins.BindValueChanged(_ => onSourceChanged());
|
2019-02-27 13:30:04 +08:00
|
|
|
|
beatmapHitsounds.BindValueChanged(_ => onSourceChanged());
|
|
|
|
|
|
|
|
|
|
return dependencies;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
2019-02-27 20:04:14 +08:00
|
|
|
|
// Must be done before base.Dispose()
|
|
|
|
|
SourceChanged = null;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
|
|
if (fallbackSource != null)
|
|
|
|
|
fallbackSource.SourceChanged -= onSourceChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|