2019-07-22 12:34:40 +09:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 17:43:03 +09:00
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2022-06-17 16:37:17 +09:00
|
|
|
#nullable disable
|
|
|
|
|
2018-03-16 10:36:26 +09:00
|
|
|
using System;
|
2018-02-23 20:34:08 +09:00
|
|
|
using osu.Framework.Allocation;
|
2020-11-06 22:11:49 +09:00
|
|
|
using osu.Framework.Graphics.Pooling;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2018-02-23 20:34:08 +09:00
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A drawable which has a callback when the skin changes.
|
|
|
|
/// </summary>
|
2020-11-06 22:11:49 +09:00
|
|
|
public abstract class SkinReloadableDrawable : PoolableDrawable
|
2018-02-23 20:34:08 +09:00
|
|
|
{
|
2019-12-17 19:49:13 +09:00
|
|
|
/// <summary>
|
|
|
|
/// Invoked when <see cref="CurrentSkin"/> has changed.
|
|
|
|
/// </summary>
|
|
|
|
public event Action OnSkinChanged;
|
|
|
|
|
2019-09-26 17:04:38 +09:00
|
|
|
/// <summary>
|
|
|
|
/// The current skin source.
|
|
|
|
/// </summary>
|
|
|
|
protected ISkinSource CurrentSkin { get; private set; }
|
|
|
|
|
2018-02-23 20:34:08 +09:00
|
|
|
[BackgroundDependencyLoader]
|
2018-03-20 16:26:36 +09:00
|
|
|
private void load(ISkinSource source)
|
2018-02-23 20:34:08 +09:00
|
|
|
{
|
2019-09-26 17:04:38 +09:00
|
|
|
CurrentSkin = source;
|
|
|
|
CurrentSkin.SourceChanged += onChange;
|
2018-02-23 20:34:08 +09:00
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2019-07-22 12:34:40 +09:00
|
|
|
private void onChange() =>
|
|
|
|
// schedule required to avoid calls after disposed.
|
2019-07-29 18:35:22 +09:00
|
|
|
// note that this has the side-effect of components only performing a skin change when they are alive.
|
2019-12-17 19:49:13 +09:00
|
|
|
Scheduler.AddOnce(skinChanged);
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2018-02-23 20:34:08 +09:00
|
|
|
protected override void LoadAsyncComplete()
|
|
|
|
{
|
|
|
|
base.LoadAsyncComplete();
|
2019-12-17 19:49:13 +09:00
|
|
|
skinChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void skinChanged()
|
|
|
|
{
|
2021-05-27 14:50:42 +09:00
|
|
|
SkinChanged(CurrentSkin);
|
2019-12-17 19:49:13 +09:00
|
|
|
OnSkinChanged?.Invoke();
|
2018-02-23 20:34:08 +09:00
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
2018-02-23 20:34:08 +09:00
|
|
|
/// <summary>
|
|
|
|
/// Called when a change is made to the skin.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="skin">The new skin.</param>
|
2021-05-27 14:50:42 +09:00
|
|
|
protected virtual void SkinChanged(ISkinSource skin)
|
2018-02-23 20:34:08 +09:00
|
|
|
{
|
|
|
|
}
|
2018-08-23 14:53:16 +09:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
2019-09-26 17:04:38 +09:00
|
|
|
if (CurrentSkin != null)
|
|
|
|
CurrentSkin.SourceChanged -= onChange;
|
2019-12-17 19:49:13 +09:00
|
|
|
|
|
|
|
OnSkinChanged = null;
|
2018-08-23 14:53:16 +09:00
|
|
|
}
|
2018-02-23 20:34:08 +09:00
|
|
|
}
|
|
|
|
}
|