Jump to content

Create UnDo function


Recommended Posts

Hello guys,

At the moment I am trying to create a undo function in my painting app. But I have no idea about how to keep track the " last drawing". Is it possible that every time input is down then save the x and y in the an array to keep track the "last drawing"? 

Link to comment
Share on other sites

If you think about state a little differently then history, replayability or time-travel become easy...

Change in state is triggered by actions within the problem space.

Keep track of changes and you have a time-traveller.

For example...

Store the entire state of the system somewhere, this is what you would load/save when you perform IO.

Store each change (or mutation, or intent, or action, whatever you want to call it) in a list.

To undo, pop the last action off of the stack and apply all other changes to your initial state, replace current state with this newly inferred state.

If your changes are atomic and deterministic (in a drawing app they almost certainly are) then you can reproduce any state you've visited and you've done it not by storing loads of data for each state, just by inferring state from a much smaller list of actions.

The only downside here is that you are storing 2 states, the initial system state and the current one, the initial one can be made irrelevant if all of your actions have an opposite action, that way you can replay the history list backwards (using the inverse of each action) from the current state.

Time travel is real

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...