I have a rather large application I’m trying to make in Visual-C++, and I am now trying to add undo/redo functionality.
Having a rather large amount of events (button clicks, label text changed, etc.), I would like to find a way to undo/redo without adding code to every function.
For Instance, I would like a class that can read every event done and store it automatically. Then in my undo/redo events, I can just get the latest action stored.
If that is not possible, I would not mind some other way.
Any help?
Declare a class that represent two operations – undo and redo.
Also create two vectors of that class.
For each operation you want to apply undo/redo, push an instance of that class into the undo vector. There should be as many derived classes as there are opreations you want to undo.
For example, if a button click paints the background to green, you create a class instance whose undo metdho paints the background to the previous color, and its redo method paints the background to green, and stuff it into the undo vector.
When you undo – you pop the last class instance and call its undo method, which will paint the background to the previous color. Then you push it to the redo vector.
When you redo, you pop the redo vector for the class instance at the top and invoke its redo method, them stuff it back to the undo vector.
There are some corner cases (boundaries), you’ll tackle them when encountered.. 🙂