Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - [Matrix1Utils] - Basic Multi-level Undo/Redo

Author
Message
MonoCoder
18
Years of Service
User Offline
Joined: 4th Dec 2005
Location: england
Posted: 19th Aug 2012 12:11 Edited at: 19th Aug 2012 12:16
I'm trying to implement undo/redo into something I'm working on, and after a bit of reading I've thrown this trial run together. I think I'm following a loose approximation of command pattern? Not totally sure.

Essentially, every destructive thing the user can do is enacted by calling an ACTION__class__actionType function with the parameters to be used. The ACTION function handles calling the function proper and adding data to the history.

Every entry in the history corresponds to one action, and has a descriptive name, pointers to ACTION_UNDO/REDO functions (specific to the type of action performed), and a pointer to some memory where the parameters required for undoing/redoing have been placed.

ACTION_UNDO/REDO functions, when called, dig up those parameters from memory and call an appropriate function (going through the functions like this, rather than just setting values directly, should hopefully keep consistent any other elements indirectly affected by a given action).

The history is a simple stack (edit: well, sort of; you can move through it without popping things off - until you perform a new action, which loses all actions in your current position's "future"). When the total number of actions has been used (128 here), the array is rotated and the earliest actions kicked out to make room for new ones.

Possible issues
- Inflexibility: I imagine trying to overload or do much fancy with passing pointers or arrayptr's will break this.
- Bloat: You'll need a lot of probably redundant-seeming code to handle all possible actions.
- Logic: Complicated actions might resist easy implementation in this manner. You need to make sure you store the correct data when the action is performed so it can be succesfully reversed and recreated later.
- Fiddliness: Peeking and poking bytes.

This example: Use
Click textboxes to focus them, and type (alphanumeric, symbols, and backspace) to enter text.
Click the undo/redo buttons, or press ctrl+z/ctrl+y to undo/redo.
Actions are recorded in the console. Console is set-up to sit next to the program on a second monitor; comment out lines 7-8 to cancel that.



Any feedback much appreciated
MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 19th Aug 2012 14:29 Edited at: 20th Aug 2012 00:56
Will hopefully try this shortly...

However I always police this thread to ensure external plugins are linked to for future readers or newcomers who do not know how to use the search functions yet and for shear courtesy

Thanks for sharing this...

Login to post a reply

Server time is: 2024-04-18 02:02:31
Your offset time is: 2024-04-18 02:02:31