1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-22 01:12:56 +08:00
osu-lazer/osu.Game/Skinning/SkinReloadableDrawable.cs

98 lines
3.1 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 17:19:50 +08:00
using System;
using osu.Framework.Allocation;
using osu.Framework.Extensions.ObjectExtensions;
2020-11-06 21:11:49 +08:00
using osu.Framework.Graphics.Pooling;
using osu.Framework.Threading;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Skinning
{
/// <summary>
/// A poolable drawable implementation which has a pre-wired callback (see <see cref="SkinChanged"/>) that fires
/// once on load and again on any subsequent skin change.
2018-04-13 17:19:50 +08:00
/// </summary>
2022-11-24 13:32:20 +08:00
public abstract partial class SkinReloadableDrawable : PoolableDrawable
2018-04-13 17:19:50 +08:00
{
private ScheduledDelegate? pendingSkinChange;
/// <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]
private void load(ISkinSource source)
{
CurrentSkin = source;
CurrentSkin.SourceChanged += onChange;
}
protected override void LoadAsyncComplete()
{
base.LoadAsyncComplete();
skinChanged();
}
/// <summary>
/// Force any pending <see cref="SkinChanged"/> calls to be performed immediately.
/// </summary>
/// <remarks>
/// When a skin change occurs, the handling provided by this class is scheduled.
/// In some cases, such a sample playback, this can result in the sample being played
/// just before it is updated to a potentially different sample.
///
/// Calling this method will ensure any pending update operations are run immediately.
/// It is recommended to call this before consuming the result of skin changes for anything non-drawable.
/// </remarks>
protected void FlushPendingSkinChanges()
{
if (pendingSkinChange == null)
return;
pendingSkinChange.RunTask();
pendingSkinChange = null;
}
/// <summary>
/// Called when a change is made to the skin.
/// </summary>
/// <param name="skin">The new skin.</param>
protected virtual void SkinChanged(ISkinSource skin)
2018-04-13 17:19:50 +08:00
{
}
private void onChange()
{
// schedule required to avoid calls after disposed.
2019-07-29 17:35:22 +08:00
// note that this has the side-effect of components only performing a skin change when they are alive.
pendingSkinChange?.Cancel();
pendingSkinChange = Scheduler.Add(skinChanged);
}
private void skinChanged()
{
SkinChanged(CurrentSkin);
OnSkinChanged?.Invoke();
pendingSkinChange = null;
2018-04-13 17:19:50 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (CurrentSkin.IsNotNull())
CurrentSkin.SourceChanged -= onChange;
OnSkinChanged = null;
}
2018-04-13 17:19:50 +08:00
}
}