views:

367

answers:

1

So I managed to learn some Flex and created a small app that:

  • Loads an image to an Image component
  • Transform the image (rotate, flip) with the Matrix
  • Apply filter(s)

Now I am thinking about to create some undo redo functionality. Each time I do a transformation / add an filter I want to be able to go back to the previous image (before the action).

My thought is to have an Array and add the previous bitmap to the stack. But I see that there are some differences between transformations and filters I have also seen the ImageSnapshot and whot i can do and it looks like what I am after.

I am a bit new to Flex in general and I hope someone here can give me any advice on this and hopefully som directions.

Thanks for any advice! Best regards,

Ran

A: 

I suggest looking into the Command design pattern.

Essentially, your array would contain not the image, but tokens representing the operation taken on the image. Each token would have enough information to undo (and redo?) its operation.

var c:Command = new RotateCommand(90, CLOCKWISE);
c.doWork();
history.push(c);

// Undo
var c:Command = history.pop();
c.undoWork();

then in the commands, roughly:

public function doWork():void {
    var newTransform:Matrix = calculateTransform(angle, direction); // 90, CLOCKWISE
    image.transform.matrix.concat(newTransform);
}

public function undoWork():void {
    var newTransform:Matrix = calculateTransform(-angle, direction); // reverse operation
    image.transform.matrix.concat(newTransform);
Michael Brewer-Davis
Interesting take, but sometimes doing the opposite is not always the same as treating it like it's never been done. It works in this case, but what about situations like deleting the child of an object, is there any native way to deal with that? I'm interested since this is a requirement that I will be tackling soon in my current project and I don't want to have to maintain all the undo functionality manually.
invertedSpear
doWork() and undoWork() would often be more different than above. (For DeleteChildCommand, doWork() would have to save enough data that undoWork() could recreate the deleted child.) Interested to see any other alternatives.
Michael Brewer-Davis
An interesting take in another SO question: http://stackoverflow.com/questions/2006404/making-undo-in-python
Michael Brewer-Davis