public interface IMemento<T>
{
IMemento<T> Restore(T target);
}
abstract class TileMemento : IMemento<Tile[]>
{
public abstract IMemento<Tile[]> Restore(Tile[] target);
}
class AddTileMemento : TileMemento
{
private int index;
public AddTileMemento(int index)
{
this.index = index;
}
public override IMemento<Tile[]> Restore(Tile[] target)
{
Tile removed = target[index];
IMemento<Tile[]> inverse = new RemoveTileMemento(index, removed);
target[index] = null;
return inverse;
}
}
class RemoveTileMemento : TileMemento
{
private int index;
private Tile removed;
public RemoveTileMemento(int index, Tile removed)
{
this.index = index;
this.removed = removed;
}
public override IMemento<Tile[]> Restore(Tile[] target)
{
IMemento<Tile[]> inverse = new AddTileMemento(index);
target[index] = removed;
return inverse;
}
}
These classes are added to an undo/redo history class:
public class UndoRedoHistory<T>
{
/// <summary>
/// The subject that this undo redo history is about
/// </summary>
protected T subject;
...
which is accessed with the 'Ctrl'+'Z' and 'Ctrl'+'Y' commands:
// [Redo]
if (isControlDown && hid.IsKeyHit(PlayerIndex.One, Microsoft.Xna.Framework.Input.Keys.Y))
{
if (level.UndoRedoHistory.CanRedo)
{
level.UndoRedoHistory.Redo();
}
}
// [Undo]
if (isControlDown && hid.IsKeyHit(PlayerIndex.One, Microsoft.Xna.Framework.Input.Keys.Z))
{
if (level.UndoRedoHistory.CanUndo)
{
level.UndoRedoHistory.Undo();
}
}
The problem is that the UndoRedoHistory class can only store one type for T for each instance.
If I would like to search an UndoRedoHistory that involves other types of objects and their mementos other than just 'Tile' but in order of action, how would I go about such a process?
So for example the UndoRedoHistory could contain:
TileMemento // Add tile action
LightMemento // Changed light intensity action
EntityMemento // Removed an entity