Undo Manager

Overview

The HUndoManager class manages a history of actions in order to enable undo/redo functionality.
It comes with the ability to keep track of camera and matrix changes, and can be further enhanced to accept user defined undo actions.

Using the Undo Manager

HBaseView is initialized with an HUndoManager class already set up. In addition to this, MVO already keeps track of various camera and matrix changes (via the operators).

Traversing the history list

To undo or redo operations, first retrieve a pointer to the HUndoManager class:

HUndoManager *hmanager = m_pHView->GetUndoManager();

Once you have the HUndoManager class, you can call its Undo() and Redo() methods to get the desired result.

Enhancing the Undo Manager

The UndoManager can also accept user defined undo items. In order to do so, the HUndoItem class must subclassed. The HUndoItemCamera subclass will be used as an example.

Subclassing HUndoItem

The constructor should contain all of the necessary information for undoing (and possibly redoing) an action. For HUndoItemCamera, the state of the (the "before" state, as in the state of the camera right before we change it) is stored.


The Undo() and function must perform everything needed to undo or redo. In the case of HUndoItemCamera, Undo will show the camera state before an undo, and transition the camera to the stored camera state. Afterwards, it will update the stored camera state to be the state of the camera BEFORE the undo, so that the Redo operation will work.

void HUndoItemCamera::Undo(bool setuponly)
{
HCamera oldcamera;
m_pView->GetCamera(&oldcamera);
if(!setuponly)
{
{
HC_Open_Segment_By_Key(m_pView->GetSceneKey());
HC_Close_Segment();
}
}
m_Camera = oldcamera;
}

The Redo() function is similar to the Undo() function, except it operates in reverse.

Note: Both the Undo() and Redo() functions accept a boolean "setuponly" argument.
This argument is a flag to indicate whether or not we want to visually display the undo operation.
In "setup only" mode, an Undo/Redo should do everything as usual EXCEPT for updating the screen.
This "setuponly" flag is necessary for some of the other HUndoManager's features to operate nicely, and allows you to traverse the history list "behind the scenes", if need be. It is, however, mainly used for aesthetics, so not accounting for it should not break the undo manager.

It is also a good idea to override the GetName() function, so that you can identify what sort of UndoItem you are looking at.

virtual const char * GetName() { return "HUndoItemCamera"; };

Adding items to the UndoManager

Now that you have an HUndoItem all ready to go, you need to add it into the UndoManager's history list. If you are at the end of the history list, the item will be appended; if you had previously performed some undos, the item will be inserted at the location, and the rest of history will be discarded.

hmanager->AddUndoItem(undoitem);

top_level:2 prog_guide:1