2019-01-13 04:53:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pushes slot update notifications out to all subscribers.
|
|
|
|
|
/// </summary>
|
2019-09-03 02:30:58 +00:00
|
|
|
|
public sealed class SlotPublisher<T>
|
2019-01-13 04:53:50 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-09-03 02:30:58 +00:00
|
|
|
|
/// All <see cref="ISlotViewer{T}"/> instances that provide a view on individual <see cref="ISlotInfo"/> content.
|
2019-01-13 04:53:50 +00:00
|
|
|
|
/// </summary>
|
2019-09-03 02:30:58 +00:00
|
|
|
|
public List<ISlotViewer<T>> Subscribers { get; } = new List<ISlotViewer<T>>();
|
2019-01-13 04:53:50 +00:00
|
|
|
|
|
2019-09-03 02:30:58 +00:00
|
|
|
|
public ISlotInfo Previous { get; private set; }
|
|
|
|
|
public SlotTouchType PreviousType { get; private set; } = SlotTouchType.None;
|
|
|
|
|
public PKM PreviousPKM { get; private set; }
|
2019-01-13 04:53:50 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Notifies all <see cref="Subscribers"/> with the latest slot change details.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="slot">Last interacted slot</param>
|
|
|
|
|
/// <param name="type">Last interacted slot interaction type</param>
|
2019-09-03 02:30:58 +00:00
|
|
|
|
/// <param name="pkm">Last interacted slot interaction data</param>
|
|
|
|
|
public void NotifySlotChanged(ISlotInfo slot, SlotTouchType type, PKM pkm)
|
2019-01-13 04:53:50 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var sub in Subscribers)
|
2019-09-03 02:30:58 +00:00
|
|
|
|
ResetView(sub, slot, type, pkm);
|
2019-01-13 04:53:50 +00:00
|
|
|
|
Previous = slot;
|
|
|
|
|
PreviousType = type;
|
2019-09-03 02:30:58 +00:00
|
|
|
|
PreviousPKM = pkm;
|
2019-01-13 04:53:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-03 02:30:58 +00:00
|
|
|
|
private void ResetView(ISlotViewer<T> sub, ISlotInfo slot, SlotTouchType type, PKM pkm)
|
2019-01-13 04:53:50 +00:00
|
|
|
|
{
|
2019-09-03 02:30:58 +00:00
|
|
|
|
if (PreviousPKM != null)
|
2019-01-13 04:53:50 +00:00
|
|
|
|
sub.NotifySlotOld(Previous);
|
|
|
|
|
|
2019-09-03 02:30:58 +00:00
|
|
|
|
if (!(slot is SlotInfoBox b) || sub.ViewIndex == b.Box)
|
|
|
|
|
sub.NotifySlotChanged(slot, type, pkm);
|
2019-01-13 04:53:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-03 02:30:58 +00:00
|
|
|
|
public void ResetView(ISlotViewer<T> sub) => ResetView(sub, Previous, PreviousType, PreviousPKM);
|
2019-01-13 04:53:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|