View Full Version : How would you implement an Undo functionality in an app?
Michael Hartlef
14-08-2010, 14:40
Hi folks,
I am working on some kind of game editor in thinbasic. One of my design questions is how to implement an undo/redo mechanism. The app is data driven so of course I have to keep track of which data's are changed and were. When I think about how to handle this with open dialog, I think it will become quite tricky. Is it enought to save the state of data when a dialog is confirmed and so mark this as an undo point? Or should this already be implemented inside a dialog where you can change parameters?
Did anyone already worked on something like this and would like to share their experience? Any ideas?
Best wishes
Michael Hartlef
ErosOlmi
14-08-2010, 14:56
Michael,
interesting challenge. What kind of objects will your game editor handle?
I think (suppose) every object will has its own set of data?
If so, every time you change an object data you can store its previous set of data somewhere in a global timing sequence (so an array of some kind can do the job). In this way you have all the info to go back any step.
Than you have to record object creation and deletion (with object info record) in order to undo/redo deletion/creation
Just an idea of some aspects.
Ciao
Eros
Michael Hartlef
14-08-2010, 15:11
Hi Eros,
thanks for sharing your thoughts.
Michael,
interesting challenge. What kind of objects will your game editor handle?
I think (suppose) every object will has its own set of data?
Basically any kind. For I focus on 2D stuff. Images, sounds, maps, etc. I want it to be modular, so i imagine it being very open. And yes, every type of object can have different datasets.
If so, every time you change an object data you can store its previous set of data somewhere in a global timing sequence (so an array of some kind can do the job). In this way you have all the info to go back any step.
Than you have to record object creation and deletion (with object info record) in order to undo/redo deletion/creation
Just an idea of some aspects.
Ciao
Eros
I think the array idea is a good one. Any maybe storing the data there as strings, just like we use this for the user data approach inside TBGL.
Mike, I was looking for an example in BASIC for you...as c++ uses stl containers.
Found this, it is visual basic, but I think it gives the idea of how you use stacks. Stacks are Last In First Out and useful for this type of thing.
I will eventually have to develop such a system some day too. It is nice to have undo feature that is for sure!
http://www.equin.co.uk/articles/undoarticle.htm
Michael,
here are 2 different ways to do that (there are surely more...):
http://en.wikipedia.org/wiki/Command_pattern
http://en.wikipedia.org/wiki/Memento_pattern
Here are 2 implementations of them (PureBasic):
http://www.purebasic.fr/english/viewtopic.php?f=12&t=42868
http://www.purebasic.fr/english/viewtopic.php?f=12&t=42867
Specially the command pattern implementation should be easy to be translated into any other language as it is small and efficient.
efgee
Michael Hartlef
19-08-2010, 06:23
Thanks guys for the tips and suggestions. I will study them when I reach that part in my editor. :occasion:
Petr Schreiber
19-08-2010, 08:15
Hi Mike,
I used very primitive approach for this in my 3d editors years back - I logged the actions to file, like:
ADDED vertex 11 {0.5, 0.6, 0.3, 255, 0, 0, 1}
DELETED vertex 11 {0.5, 0.6, 0.3, 255, 0, 0, 1}
DELETED ALL "modelbackup.m15"
The good thing was that it did not consumed any memory of the program, being on the HDD.
Petr
Lionheart008
19-08-2010, 17:17
I have found this one, mike :)
a) "undo.h" file for you for studying (as attachment)
b) for openGL ES (iPhone) the text and two links I have searched some minutes before.
(as attachment)
c) two links for this one:
http://stackoverflow.com/questions/2708055/opengl-es-simple-undo-last-drawing
http://www.iphonedevsdk.com/forum/iphone-sdk-development/35281-grab-image-opengl-es-context.html
'-----------------------------------------------------------------------------------
d) for my "playing with editfield" demo (http://community.thinbasic.com/index.php?topic=3600.msg26613;topicseen#new), I have used for undo this one:
... Case %IDC_BUTTON12
If CBCTLMSG = %BN_CLICKED Or CBCTLMSG = 1 Then
Control Send CBHNDL, %IDC_TEXTBOX1,%EM_CANUNDO,0,0 To erg
If erg Then
Control Send CBHNDL, %IDC_TEXTBOX1,%EM_UNDO,0,0 To erg
If erg Then
txt="EDITFIELD 1: the last operation was making undo!"
Control Set Text CBHNDL, %IDC_TEXTBOX2, txt...
"%EM_CANUNDO" und "%EM_UNDO" does already exists for thinbasic I noticed some days ago ;)
definitions for these both "edit control messages" are:
%EM_CANUNDO = &HC6
%EM_UNDO = &HC7
...class...
Method CanUndo() As Long
'True if can undo
Method = me.GetSend(%EM_CANUNDO, 0, 0)
End Method
Method ClearUndo()
'empty undo buffer
me.Send(%EM_EMPTYUNDOBUFFER, 0, 0)
End Method
good luck!
best regards, frank
Michael Hartlef
20-08-2010, 19:03
Again, thanks for the tips!