Click here to Skip to main content
Click here to Skip to main content

Action History - Undo and Redo

, 6 Mar 2004
Rate this:
Please Sign up or sign in to vote.
A useful undo/redo system that can be implemented into most programs easily.

Sample Image

Introduction

Not so long ago, I decided that I needed to add undo and redo functionality to my program. I thought about it for a while and decided that it would become easier if I created some kind of interface which could be easily derived from. Every time the user does anything, a new action is added to a std::vector list and these instructions can be interpreted by your program. I would recommend keeping the amount of data stored about an instruction as small as possible.

Using the code

Basically, for everything your program can do (that you would like to be undone or redone), you create a class derived from the interface IActionBase. This interface contains a run function which should contain the code to complete the action, and then an undo function which should contain the code to undo the action again.

The demo project's main window contains a box which you can drag around the screen. Every time you complete a drag cycle (released the mouse), you will then be able to click the undo button. I have also inserted an option under the "Edit" menu which enables you to clear the undo/redo history.

Technically, with this system, you could implement some kind of history feature like that in Adobe Photoshop so that you can revert to a specific point in history.

To use this system, declare an instance of the class CActionHistory in the CMainFrame class (if you are using MFC). I would recommend that you declare it publicly so that it is easy to access, especially from the CView derived class. Then for every action your program does, derive a class from the interface IActionBase. You then only need to create and add the action to the interface. Please note that the AddAction member function of CActionHistory does not run the action. You have to run the action separately. Here is a small example of its usage:

// create a new action for dragging the box
CActionDragBox* pNewAction = new CActionDragBox(&m_rtBox, this);
// you can run the action whenever you want using the pointer
pNewAction->Run();
// add this actions to the history list
pMainFrm->m_ActionHistory.AddAction(pNewAction);

The CActionHistory class will take care of cleaning up instances of your action classes created with the new operator.

If you are interested in using this, I would suggest that you firstly take a close look at the demo.

I cannot see any problems with this method, but I am no expert and there may be a better method. I hope that this article will be of some benefit to you.

Points of Interest

When I was thinking about implementing an undo/redo system, I discovered the use of interfaces; ever since my programming has been a lot more structured.

History

No changes have been made.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Lea Hayes
Software Developer Rotorz Limited
United Kingdom United Kingdom
I have been fascinated by software and video games since a young age when I was given my first computer, a Dragon 32. Since then I have experimented with numerous methods of development ranging from point-and-click type packages to C++. I soon realized that software development was what I wanted to do.
 
Having invested a lot of time into programming with various languages and technologies I now find it quite easy to pickup new ideas and methodologies. I relish learning new ideas and concepts.
 
Throughout my life I have dabbled in game and engine development. I was awarded a first for the degree "BEng Games and Entertainment Systems Software Engineering" at the University of Greenwich. It was good to finally experience video games from a more professional perspective.
 
Due to various family difficulties I was unable to immediately pursue any sort of software development career. This didn't stop me from dabbling though!
 
Since then I formed a company to focus upon client projects. Up until now the company has primarily dealt with website design and development. I have since decided that it would be fun to go back to my roots and develop games and tools that other developers can use for their games.
 
We have recently released our first game on iPhone/iPad called "Munchy Bunny!" (see: http://itunes.apple.com/us/app/munchy-bunny!/id516575993?mt=8). We hope to expand the game and release to additional platforms.
 
Also, check out our tile system extension for Unity! (see: http://rotorz.com/tilesystem/)
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralRe: Something new? PinmemberSkizmo23-Nov-04 4:02 
QuestionSomething new? PinmemberWREY9-Mar-04 4:46 
AnswerRe: Something new? PinsussLea Hayes9-Mar-04 8:33 
Question"run undo" or "undo run"? PinmemberEnderJSC8-Mar-04 12:40 
AnswerRe: "run undo" or "undo run"? Pinmemberlhayes009-Mar-04 8:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 7 Mar 2004
Article Copyright 2004 by Lea Hayes
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid