osu/osu.Game/Skinning/SkinReloadableDrawable.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
2.0 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.
2018-04-13 09:19:50 +00:00
using System;
using osu.Framework.Allocation;
using osu.Framework.Extensions.ObjectExtensions;
2020-11-06 13:11:49 +00:00
using osu.Framework.Graphics.Pooling;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Skinning
{
/// <summary>
/// A drawable which has a callback when the skin changes.
/// </summary>
2020-11-06 13:11:49 +00:00
public abstract partial class SkinReloadableDrawable : PoolableDrawable
{
/// <summary>
/// Invoked when <see cref="CurrentSkin"/> has changed.
/// </summary>
public event Action? OnSkinChanged;
/// <summary>
/// The current skin source.
/// </summary>
protected ISkinSource CurrentSkin { get; private set; } = null!;
[BackgroundDependencyLoader]
2018-03-20 07:26:36 +00:00
private void load(ISkinSource source)
{
CurrentSkin = source;
CurrentSkin.SourceChanged += onChange;
}
2018-04-13 09:19:50 +00:00
private void onChange() =>
// schedule required to avoid calls after disposed.
2019-07-29 09:35:22 +00:00
// note that this has the side-effect of components only performing a skin change when they are alive.
Scheduler.AddOnce(skinChanged);
2018-04-13 09:19:50 +00:00
protected override void LoadAsyncComplete()
{
base.LoadAsyncComplete();
skinChanged();
}
private void skinChanged()
{
SkinChanged(CurrentSkin);
OnSkinChanged?.Invoke();
}
2018-04-13 09:19:50 +00:00
/// <summary>
/// Called when a change is made to the skin.
/// </summary>
/// <param name="skin">The new skin.</param>
protected virtual void SkinChanged(ISkinSource skin)
{
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (CurrentSkin.IsNotNull())
CurrentSkin.SourceChanged -= onChange;
OnSkinChanged = null;
}
}
}