CommandChangeState.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Collections.Generic;
  2. namespace PaintDotNet.Annotation.Command
  3. {
  4. public class CommandChangeState : Command
  5. {
  6. // Selected object(s) before operation
  7. List<DrawObject> listBefore;
  8. // Selected object(s) after operation
  9. List<DrawObject> listAfter;
  10. // Create this command BEFORE operation.
  11. public CommandChangeState(GraphicsList graphicsList)
  12. {
  13. // Keep objects state before operation.
  14. FillList(graphicsList, ref listBefore);
  15. }
  16. // Call this function AFTER operation.
  17. public void NewState(GraphicsList graphicsList)
  18. {
  19. // Keep objects state after operation.
  20. FillList(graphicsList, ref listAfter);
  21. }
  22. public override void Undo(GraphicsList list)
  23. {
  24. // Replace all objects in the list with objects from listBefore
  25. ReplaceObjects(list, listBefore);
  26. }
  27. public override void Redo(GraphicsList list)
  28. {
  29. // Replace all objects in the list with objects from listAfter
  30. ReplaceObjects(list, listAfter);
  31. }
  32. // Replace objects in graphicsList with objects from list
  33. private void ReplaceObjects(GraphicsList graphicsList, List<DrawObject> list)
  34. {
  35. for (int i = 0; i < graphicsList.Count; i++)
  36. {
  37. DrawObject replacement = null;
  38. foreach (DrawObject o in list)
  39. {
  40. if (o.ID == graphicsList[i].ID)
  41. {
  42. replacement = o;
  43. break;
  44. }
  45. }
  46. if (replacement != null)
  47. {
  48. graphicsList.Replace(i, replacement);
  49. }
  50. }
  51. }
  52. // Fill list from selection
  53. private void FillList(GraphicsList graphicsList, ref List<DrawObject> listToFill)
  54. {
  55. listToFill = new List<DrawObject>();
  56. foreach (DrawObject o in graphicsList.Selection)
  57. {
  58. listToFill.Add(o.Clone());
  59. }
  60. }
  61. }
  62. }