CommandDelete.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections.Generic;
  2. namespace PaintDotNet.Annotation.Command
  3. {
  4. class CommandDelete : Command
  5. {
  6. List<DrawObject> cloneList; // contains selected items which are deleted
  7. // Create this command BEFORE applying Delete All function.
  8. public CommandDelete(GraphicsList graphicsList)
  9. {
  10. cloneList = new List<DrawObject>();
  11. // Make clone of the list selection.
  12. foreach (DrawObject o in graphicsList.Selection)
  13. {
  14. cloneList.Add(o.Clone());
  15. }
  16. }
  17. public override void Undo(GraphicsList list)
  18. {
  19. list.UnselectAll();
  20. // Add all objects from cloneList to list.
  21. foreach (DrawObject o in cloneList)
  22. {
  23. list.Add(o);
  24. }
  25. }
  26. public override void Redo(GraphicsList list)
  27. {
  28. // Delete from list all objects kept in cloneList
  29. int n = list.Count;
  30. for (int i = n - 1; i >= 0; i--)
  31. {
  32. bool toDelete = false;
  33. DrawObject objectToDelete = list[i];
  34. foreach (DrawObject o in cloneList)
  35. {
  36. if (objectToDelete.ID == o.ID)
  37. {
  38. toDelete = true;
  39. break;
  40. }
  41. }
  42. if (toDelete)
  43. {
  44. list.RemoveAt(i);
  45. }
  46. }
  47. }
  48. }
  49. }