/* * When Redo is clicked: pop redo_stack and add it back to the Shapes List */ private void redoButton_Click(object sender, EventArgs e) { if (redo_stack.Count > 0) { ShapeAction ra = redo_stack.Pop(); if (ra.TypeOfAction == "Erase") { ShapesDict.Remove(ra.Key); Actions.Add(ra); } else if (ra.TypeOfAction == "Resize" || ra.TypeOfAction == "Move") { Actions.Add(new ShapeAction(ra.TypeOfAction, ra.Key, ShapesDict[ra.Key])); ShapesDict[ra.Key] = ra.Shape; } else if (ra.TypeOfAction == "Draw") { ShapesDict.Add(ra.Key, ra.Shape); //add ra (popped object from redo_stack) back to Actions List Actions.Add(ra); } } drawing_panel.Invalidate(); }
/* * When Undo is clicked, remove the last shape added to the Shapes List * and push it onto the redo_stack. * * TODO: Handle if 'Clear All' was clicked previously. */ private void undo_button_Click(object sender, EventArgs e) { // check if there are any actions to undo if (Actions.Count > 0) { //lookup last action ShapeAction lastAction = Actions.Last(); // if last action was an Draw then remove shape from dict to undo the "Draw" Action if (lastAction.TypeOfAction == "Draw") { //save last action to a redo stack redo_stack.Push(lastAction); ShapesDict.Remove(lastAction.Key); } else if (lastAction.TypeOfAction == "Resize" || lastAction.TypeOfAction == "Move") { //store initial size of shape in redo_stack redo_stack.Push(new ShapeAction("Resize", lastAction.Key, ShapesDict[lastAction.Key])); //set shape back to size stored in lastAction ShapesDict[lastAction.Key] = lastAction.Shape; } else if (lastAction.TypeOfAction == "Erase") {// else revert ShapesDict back to the state that lastAction saved ShapesDict[lastAction.Key] = lastAction.Shape; //save last action to a redo stack redo_stack.Push(lastAction); } //pop last action from actions list Actions.RemoveAt(Actions.Count - 1); } // Add all shapes from the clear_all_stack back into the dict //else if(just_cleared) //{ // foreach(IShape shape in clear_all_stack) // { // //Shapes.Add(shape); // ShapesDict.Add(++shapeCount, shape); // } // just_cleared = false; //} drawing_panel.Invalidate(); }