// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Framework.Allocation; using osu.Framework.Graphics.Containers; namespace osu.Game.Skinning { /// /// A drawable which has a callback when the skin changes. /// public abstract class SkinReloadableDrawable : CompositeDrawable { private readonly Func allowFallback; private ISkinSource skin; /// /// Whether fallback to default skin should be allowed if the custom skin is missing this resource. /// private bool allowDefaultFallback => allowFallback == null || allowFallback.Invoke(skin); /// /// Create a new /// /// A conditional to decide whether to allow fallback to the default implementation if a skinned element is not present. protected SkinReloadableDrawable(Func allowFallback = null) { this.allowFallback = allowFallback; } [BackgroundDependencyLoader] private void load(ISkinSource source) { skin = source; skin.SourceChanged += onChange; } private void onChange() => // schedule required to avoid calls after disposed. Schedule(() => SkinChanged(skin, allowDefaultFallback)); protected override void LoadAsyncComplete() { base.LoadAsyncComplete(); SkinChanged(skin, allowDefaultFallback); } /// /// Called when a change is made to the skin. /// /// The new skin. /// Whether fallback to default skin should be allowed if the custom skin is missing this resource. protected virtual void SkinChanged(ISkinSource skin, bool allowFallback) { } protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); if (skin != null) skin.SourceChanged -= onChange; } } }