0

I'm working on a very basic shape drawing GUI program. I have everything working (draw/move/select) and am now working on redo/undo functionality. Both redo/undo work for draw, but I'm running into trouble with move that I think should be pretty simple. When my shapes move, they go through the following:

         for(IShape i : selectedList){
            undoStack.push(i);
            drawInstanceTwo.shapeList.remove(i);
            move.updateShape(i, start, end);
            drawInstanceTwo.shapeList.push(i, canvas, state);   
    }

The idea here being that it will push the shapes current form (Point x/y) onto the undoStack so that if I wish to undo the movement, I can pop it from the stack upon pressing my undo button. Move.updateShape changes the points of the shape, and then it is pushed onto my overall shapeList to repaint the GUI. The issue I'm running into is that later in my undo method I call:

priorShape = undoStack.pop();

And it returns the shape AFTER the points have been changed. Why is this? I already pushed it onto the stack as it was prior to the points being edited, so why is it changing within the stack? Thanks for any suggestions!

Polyphase29
  • 475
  • 1
  • 4
  • 17
  • The shape you pushed onto the stack is `i`. The shape you modified with `move` is `i`. They are the same shape. If you want to push an unmoved copy, then you will have to make a copy. – Matt Timmermans Mar 02 '18 at 17:40
  • Hmm ok. I tried that earlier but maybe I didn't do it correctly. So even though I push it onto the stack before I change it, it will still change when on the stack? – Polyphase29 Mar 02 '18 at 17:42
  • Yup. Remember the previous version of the house is at "123 Beacon St.". Bulldoze the house at "123 Beacon St.". To see the previous version of the house, go to the address you remembered before. – Matt Timmermans Mar 02 '18 at 17:46
  • So, and I realize this is a silly question, is there a certain way I should make a copy? Because I just tried again and my copy is also updating. iCopy = i; undoStack.push(iCopy); Thanks for your help btw. – Polyphase29 Mar 02 '18 at 17:48
  • you just made another copy of the address. How to copy your whole shape will depend on a shape. But that's probably too expensive for an undo stack. You should just remember the information you need to move it back. – Matt Timmermans Mar 02 '18 at 17:50
  • just adding example to what @MattTimmermans said.. for example shape is rectangle so you just remember, say, it's left-bottom coordinates, height and width, so you see you can redraw that rectangle. – Polish Mar 02 '18 at 18:05

0 Answers0