Tools Dev - Undo Systems

posted in achild's Journal
Published January 17, 2013
Advertisement
(warning: this post is purely about code)

Do not, I repeat, Do not wait to add undo/redo to your tool/editor "later"...
... and even when you plan ahead, expect it not to be perfect the first time. In fact, it can even affect the user experience.

I have created 3 totally different types of undo systems from scratch.

#1

was for this calendar application and was completely action based: it saved your action and it's parameters and had a small virtual machine of sorts that could do and undo these actions. There were only a handful of them after all.

It was super-lightweight, but insanely hard to debug. I doubt I will ever go that route again. Partly my fault, but it caught me off guard the complexities this created. Also, this was "the time" I tried to implement undo/redo after-the-fact.

#2's system tried (implemented in Stickimator) to be clever. #2 was combined with an action callback system using predefined enums so that it could be automated. "boost::any" was used to store the undo and redo data, and some template-magic was used to automatically undo or redo the change. As long as the different data types were specialized, it would handle everything for me without any extra code. Neat!

All actions were in one place in this giant switch statement, again much like a VM. This kept everything in one nice place, and all actions were simply done like soeventAction.Fire(Action(ACTION_FOO, fooData), true);
As long as fooData's type was specialized, the rest was automatic. And ACTION_FOO had to be in the giant switch statement.

This got unwieldy fast. Adding features became hacks (like accumulating multiple actions into a single undo), and the code for actions tended to get messy. It became clear that it wasn't a horrible attempt, but it wasn't going to suffice. Changing it would take a good bit of refactoring, and there would be no immediate obvious benefit in the software once all the crippled parts were fixed. So, I was going to wait for the first real release of Stickimator, but once I started adding more to the old version to get the release out, it seemed a better idea to just rewrite what there was rather than rewrite more later.

#3 is based on, yes, the command pattern. Sort of. Very similar to QT's command/undo structure, it supports:

  • Command logic is encapsulated per command instead of one-place-for-all-commands
  • Undo/Redo logic per command instead of forcing a single method for the entire system. Undo/redo based on action logic OR data OR a mix! Again, encapsulation benefits (versus specialization here, data structure there, command logic over there...). I wonder that I didn't go this way from the get-go?
  • Accumulate (like QT's merge), and Freeze to stop accumulating. Accumulation is automatic when a command supports it. Freeze is explicit, of course.
  • Cancel! If you are doing some command which accumulates with the left mouse button, and you right-click, you might expect it to cancel. Not just undo, but undo without a redo. No trace left. That's what cancel is for.
  • Get/SetText for undo/redo menu descriptions, history list, logging, etc.
  • ApplyTo *
  • Command Stacks to keep undo/redo list
  • Command Macros

* ApplyTo is neat but is Stickimator-specific. Stickimator edits works on a per frame basis. So, what if you want your changes to affect multiple frames? Multiple animations? Some sort of grouping functionality (that can get messy!!)? Well, ApplyTo can be overridden in supported commands to take the most recent command(s) on the CommandStack and apply them to selected frames and/or animations. This will be a super-neat feature in Stickimator for quick editing and fixes.

The best part? Tons of Stickimator's code was magically cleaned up in all this process. I had not realized how much command-specific code had intermingled with other parts of the code, such as input-logic, making it very complex. And it still is triggered very simply:// These events are handled by the main application and can be called from any controls/views that// have them. The main application links eventCommand directly to CommandStack::AddCommand, and again,// accumulation is automatic if supported and the most recent command isn't frozen.eventCommand.Fire(make_shared(...));// or in some casesshared_ptr cmd(make_shared(...));if (cmd->IsValid()) // do this; don't throw from CommandFoo's constructor if there is a problem eventCommand.Fire(cmd);// also there's this - the ID ensures that the action only occurs on the expected commandeventCommandAccum.Fire(COMMAND_ID_FOO, ACCUM_FREEZE); // or ACCUM_CANCEL
Also, my undo/redo didn't have command descriptions before. happy.png

So that is why there are no new neat screenshots this time. Hopefully next time I will have some fun stuff to show! Until then!

[size=2][edit] clarified a couple small things; fixed a code typo
4 likes 1 comments

Comments

3Ddreamer

This is way cool, dude. Great advice to not wait on the undo/ redo. I made this a favorite webpage.

January 23, 2013 03:59 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement