FlattenFunction.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using PaintDotNet.Measurement.HistoryMementos;
  2. using System.Collections.Generic;
  3. namespace PaintDotNet.Measurement.HistoryFunctions
  4. {
  5. public sealed class FlattenFunction
  6. : HistoryFunction
  7. {
  8. public static string StaticName
  9. {
  10. get
  11. {
  12. return PdnResources.GetString("FlattenFunction.Name");
  13. }
  14. }
  15. public override HistoryMemento OnExecute(IDocumentWorkspace historyWorkspace)
  16. {
  17. object savedSelection = null;
  18. List<HistoryMemento> actions = new List<HistoryMemento>();
  19. if (!historyWorkspace.GetSelection().IsEmpty)
  20. {
  21. savedSelection = historyWorkspace.GetSelection().Save();
  22. DeselectFunction da = new DeselectFunction();
  23. HistoryMemento hm = da.Execute(historyWorkspace);
  24. actions.Add(hm);
  25. }
  26. ReplaceDocumentHistoryMemento rdha = new ReplaceDocumentHistoryMemento(null, null, historyWorkspace);
  27. actions.Add(rdha);
  28. CompoundHistoryMemento chm = new CompoundHistoryMemento(
  29. StaticName,
  30. PdnResources.GetImageResource("Icons.MenuImageFlattenIcon.png"),
  31. actions);
  32. // TODO: we can save memory here by serializing, then flattening on to an existing layer
  33. Document flat = historyWorkspace.GetDocument().Flatten();
  34. EnterCriticalRegion();
  35. historyWorkspace.SetDocument(flat);
  36. if (savedSelection != null)
  37. {
  38. SelectionHistoryMemento shm = new SelectionHistoryMemento(null, null, historyWorkspace);
  39. historyWorkspace.GetSelection().Restore(savedSelection);
  40. chm.PushNewAction(shm);
  41. }
  42. return chm;
  43. }
  44. public FlattenFunction()
  45. : base(PaintDotNet.Measurement.Enum.ActionFlags.None)
  46. {
  47. }
  48. }
  49. }