2019-07-22 03:34:40 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 08:43:03 +00:00
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2018-03-16 01:36:26 +00:00
|
|
|
using System;
|
2018-02-23 11:34:08 +00:00
|
|
|
using osu.Framework.Allocation;
|
2022-11-09 04:44:59 +00:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2020-11-06 13:11:49 +00:00
|
|
|
using osu.Framework.Graphics.Pooling;
|
2023-03-15 08:00:34 +00:00
|
|
|
using osu.Framework.Threading;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2018-02-23 11:34:08 +00:00
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
/// <summary>
|
2023-02-15 06:14:38 +00:00
|
|
|
/// 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-02-23 11:34:08 +00:00
|
|
|
/// </summary>
|
2020-11-06 13:11:49 +00:00
|
|
|
public abstract partial class SkinReloadableDrawable : PoolableDrawable
|
2018-02-23 11:34:08 +00:00
|
|
|
{
|
2023-03-15 08:00:34 +00:00
|
|
|
private ScheduledDelegate? pendingSkinChange;
|
|
|
|
|
2019-12-17 10:49:13 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Invoked when <see cref="CurrentSkin"/> has changed.
|
|
|
|
/// </summary>
|
2022-11-09 04:44:59 +00:00
|
|
|
public event Action? OnSkinChanged;
|
2019-12-17 10:49:13 +00:00
|
|
|
|
2019-09-26 08:04:38 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The current skin source.
|
|
|
|
/// </summary>
|
2022-11-09 04:44:59 +00:00
|
|
|
protected ISkinSource CurrentSkin { get; private set; } = null!;
|
2019-09-26 08:04:38 +00:00
|
|
|
|
2023-03-16 06:58:42 +00:00
|
|
|
[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>
|
2023-03-15 09:49:59 +00:00
|
|
|
protected void FlushPendingSkinChanges()
|
|
|
|
{
|
|
|
|
if (pendingSkinChange == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pendingSkinChange.RunTask();
|
|
|
|
pendingSkinChange = null;
|
|
|
|
}
|
|
|
|
|
2023-03-16 06:58:42 +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)
|
2018-02-23 11:34:08 +00:00
|
|
|
{
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2023-03-15 08:00:34 +00:00
|
|
|
private void onChange()
|
|
|
|
{
|
2019-07-22 03:34:40 +00:00
|
|
|
// 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.
|
2023-03-15 08:00:34 +00:00
|
|
|
pendingSkinChange?.Cancel();
|
|
|
|
pendingSkinChange = Scheduler.Add(skinChanged);
|
|
|
|
}
|
|
|
|
|
2019-12-17 10:49:13 +00:00
|
|
|
private void skinChanged()
|
|
|
|
{
|
2021-05-27 05:50:42 +00:00
|
|
|
SkinChanged(CurrentSkin);
|
2019-12-17 10:49:13 +00:00
|
|
|
OnSkinChanged?.Invoke();
|
2023-03-15 09:49:59 +00:00
|
|
|
|
|
|
|
pendingSkinChange = null;
|
2018-02-23 11:34:08 +00:00
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
2018-08-23 05:53:16 +00:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
if (CurrentSkin.IsNotNull())
|
2019-09-26 08:04:38 +00:00
|
|
|
CurrentSkin.SourceChanged -= onChange;
|
2019-12-17 10:49:13 +00:00
|
|
|
|
|
|
|
OnSkinChanged = null;
|
2018-08-23 05:53:16 +00:00
|
|
|
}
|
2018-02-23 11:34:08 +00:00
|
|
|
}
|
|
|
|
}
|