12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using PaintDotNet.Measurement.Enum;
- using System;
- namespace PaintDotNet.Measurement.HistoryMementos
- {
- public class FlipLayerHistoryMemento
- : HistoryMemento
- {
- private IDocumentWorkspace historyWorkspace;
- private int layerIndex;
- private FlipType flipType;
- private void Flip(Surface surface)
- {
- switch (this.flipType)
- {
- case FlipType.Horizontal:
- for (int y = 0; y < surface.Height; ++y)
- {
- for (int x = 0; x < surface.Width / 2; ++x)
- {
- ColorBgra temp = surface[x, y];
- surface[x, y] = surface[surface.Width - x - 1, y];
- surface[surface.Width - x - 1, y] = temp;
- }
- }
- break;
- case FlipType.Vertical:
- for (int x = 0; x < surface.Width; ++x)
- {
- for (int y = 0; y < surface.Height / 2; ++y)
- {
- ColorBgra temp = surface[x, y];
- surface[x, y] = surface[x, surface.Height - y - 1];
- surface[x, surface.Height - y - 1] = temp;
- }
- }
- break;
- case FlipType.Center:
- for (int y = 0; y < surface.Height; ++y)
- {
- for (int x = 0; x < surface.Width / 2; ++x)
- {
- ColorBgra temp = surface[x, y];
- surface[x, y] = surface[surface.Width - x - 1, surface.Height - y - 1];
- surface[surface.Width - x - 1, surface.Height - y - 1] = temp;
- }
- }
- break;
- default:
- throw new InvalidOperationException("FlipType was invalid");
- }
- return;
- }
- protected override HistoryMemento OnUndo()
- {
- FlipLayerHistoryMemento fha = new FlipLayerHistoryMemento(this.Name, this.Image,
- this.historyWorkspace, layerIndex, flipType);
- BitmapLayer layer = (BitmapLayer)this.historyWorkspace.GetDocument().Layers[layerIndex];
- Flip(layer.Surface);
- layer.Invalidate();
- return fha;
- }
- public FlipLayerHistoryMemento(string name, ImageResource image, IDocumentWorkspace historyWorkspace, int layerIndex, FlipType flipType)
- : base(name, image)
- {
- this.historyWorkspace = historyWorkspace;
- this.layerIndex = layerIndex;
- this.flipType = flipType;
- }
- }
- }
|