CommandChangeState.cs 2.3 KB

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