Apos.History - A C# library to help with undo and redo.

I wrote a C# library that makes it much easier to add undo and redo to your in game map editors (Or really any of your C# programs.).

Available on GitHub and Nuget:

I’ll be writing the API documentation at some point.

It’s pretty easy to use. Let’s say you want to create two lists of ints and track their history, you can do this:

var historyHandler = new HistoryHandler(Option.None<HistoryHandler>());

var listA = new HistoryList<int>(new List<int>(), Option.Some(historyHandler));
var listB = new HistoryList<int>(new List<int>(), Option.Some(historyHandler));

listA.Add(1);
listA.Add(2);
listA.Add(3);

historyHandler.Undo();
historyHandler.Undo();

historyHandler.Redo();

listA.Add(4);

listB.Add(11);
listB.Add(12);

historyHandler.Undo();
historyHandler.Undo();
historyHandler.Undo();
historyHandler.Undo();
historyHandler.Undo();

Hopefully you get the idea. You can look in the HistoryList.cs for example code on how you can apply this to your own data structures.

If you want to group actions together so that they are all undone at the same time, you can do:

historyHandler.AutoCommit = false;
listA.Add(1);
listA.Add(2);
listA.Add(3);
historyHandler.Commit();
historyHandler.AutoCommit = true;

//This next undo will work on the 3 last adds at the same time.
historyHandler.Undo();

I use this in my game to handle everything in my map editor. Works with my quadtrees, lists, dictionaries, UI, etc.

2 Likes