Allen Posted June 27, 2016 Share Posted June 27, 2016 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 More sharing options...
WombatTurkey Posted June 27, 2016 Share Posted June 27, 2016 Array.unshift might work, will always append the value to the front, then you can access the last action by Array[0]? Then you can do a counter: Array[++counter] or whatever to cycle through the actions not sure just off the top of my head Link to comment Share on other sites More sharing options...
lekzd Posted June 27, 2016 Share Posted June 27, 2016 I think, that you need to use a Command pattern from this book http://gameprogrammingpatterns.com/command.html It's not so easy way, but very powerful and scallable Link to comment Share on other sites More sharing options...
mattstyles Posted June 27, 2016 Share Posted June 27, 2016 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 drhayes 1 Link to comment Share on other sites More sharing options...
Recommended Posts