Advertisement
_takumi

UndoRedo.cs

Dec 10th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.87 KB | None | 0 0
  1. using MyLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Dynamic;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8.  
  9. namespace PolygonDraft
  10. {
  11.     public interface ICommand
  12.     {
  13.         void Execute();
  14.         void Undo();
  15.     }
  16.  
  17.     public static class UndoRedoManager
  18.     {
  19.         public static Stack<List<ICommand>> OnUndo { get; private set; } = new Stack<List<ICommand>>();
  20.         public static Stack<List<ICommand>> OnRedo { get; private set; } = new Stack<List<ICommand>>();
  21.  
  22.         public static void Undo()
  23.         {
  24.             if (OnUndo.Count == 0)
  25.                 return;
  26.             OnUndo.Peek()?.ForEach(cmd => cmd.Undo());
  27.             List<ICommand> cmds = OnUndo.Pop();
  28.             cmds.Reverse();
  29.             OnRedo.Push(cmds);
  30.         }
  31.  
  32.         public static void Redo()
  33.         {
  34.             if (OnRedo.Count == 0)
  35.                 return;
  36.             OnRedo.Peek().ForEach(cmd => cmd.Execute());
  37.             List<ICommand> cmds = OnRedo.Pop();
  38.             cmds.Reverse();
  39.             OnUndo.Push(cmds);
  40.         }
  41.  
  42.         public static void AddCmd(params ICommand[] cmds)
  43.         {
  44.             OnUndo.Push(cmds.ToList());
  45.             OnRedo.Clear();
  46.         }
  47.  
  48.         public static void ChangeLog(Stack<List<ICommand>> onUndo, Stack<List<ICommand>> onRedo)
  49.         {
  50.             OnUndo = onUndo;
  51.             OnRedo = onRedo;
  52.         }
  53.  
  54.         public static void ClearLog()
  55.         {
  56.             OnUndo.Clear();
  57.             OnRedo.Clear();
  58.         }
  59.     }
  60.  
  61.     [Serializable]
  62.     public sealed class DragDropCmd : ICommand
  63.     {
  64.         private Polygon _polygon;
  65.         private List<Vertex> _dragged = new List<Vertex>();
  66.         private List<Point> _startingPoints = new List<Point>();
  67.         private List<Point> _endPoints;
  68.  
  69.         public DragDropCmd(Polygon polygon)
  70.         {
  71.             _polygon = polygon;
  72.         }
  73.  
  74.         public void Execute()
  75.         {
  76.             for (int i = 0; i < _dragged.Count; i++)
  77.             {
  78.                 _dragged[i].X = _endPoints[i].X;
  79.                 _dragged[i].Y = _endPoints[i].Y;
  80.             }
  81.             if (_polygon.Count >= 3)
  82.                 _polygon.MakeConvex();
  83.         }
  84.  
  85.         public void Undo()
  86.         {
  87.             for (int i = 0; i < _dragged.Count; i++)
  88.             {
  89.                 _dragged[i].X = _startingPoints[i].X;
  90.                 _dragged[i].Y = _startingPoints[i].Y;
  91.             }
  92.             if (_polygon.Count >= 3)
  93.                 _polygon.MakeConvex();
  94.         }
  95.  
  96.         public static bool CanDrag(Polygon polygon, int x, int y)
  97.         {
  98.             bool canDrag = false;
  99.             bool polygonDragged = polygon.Contains(new Point(x, y));
  100.             bool vertexDragged = polygon.Exists(vertex => vertex.Check(x, y));
  101.             polygon.ForEach(vertex =>
  102.             {
  103.                 if (vertex.Check(x, y) || (polygonDragged && !vertexDragged))
  104.                     canDrag = true;
  105.             });
  106.             return canDrag;
  107.         }
  108.  
  109.         public void DragStart(int x, int y)
  110.         {
  111.             bool polygonDragged = _polygon.Contains(new Point(x, y));
  112.             bool vertexDragged = _polygon.Exists(vertex => vertex.Check(x, y));
  113.             _polygon.ForEach(vertex =>
  114.             {
  115.                 if (vertex.Check(x, y) || (polygonDragged && !vertexDragged))
  116.                 {
  117.                     vertex.IsDragged = true;
  118.                     vertex.Dx = x - vertex.X;
  119.                     vertex.Dy = y - vertex.Y;
  120.                     _dragged.Add(vertex);
  121.                     _startingPoints.Add(new Point(vertex.X, vertex.Y));
  122.                 }
  123.             });
  124.         }
  125.  
  126.         public void DragDo(int mouseX, int mouseY)
  127.         {
  128.             _polygon.ForEach(vertex =>
  129.             {
  130.                 if (vertex.IsDragged)
  131.                 {
  132.                     vertex.X = mouseX - vertex.Dx;
  133.                     vertex.Y = mouseY - vertex.Dy;
  134.                 }
  135.             });
  136.         }
  137.  
  138.         public void DragEnd()
  139.         {
  140.             _endPoints = (from vertex in _polygon
  141.                           where vertex.IsDragged
  142.                           select new Point(vertex.X, vertex.Y)).ToList();
  143.             _polygon.ForEach(vertex => vertex.IsDragged = false);
  144.         }
  145.     }
  146.  
  147.     [Serializable]
  148.     public sealed class AddVertexCmd : ICommand
  149.     {
  150.         private Vertex _vertex;
  151.         private Polygon _polygon;
  152.  
  153.         public AddVertexCmd(Vertex vertex, Polygon polygon)
  154.         {
  155.             _vertex = vertex;
  156.             _polygon = polygon;
  157.         }
  158.  
  159.         public void Execute()
  160.         {
  161.             _polygon.Add(_vertex);
  162.             if (_polygon.Count >= 3)
  163.             {
  164.                 _polygon.MakeConvex();
  165.             }
  166.         }
  167.  
  168.         public void Undo()
  169.         {
  170.             _polygon.Remove(_vertex);
  171.             if (_polygon.Count >= 3)
  172.                 _polygon.MakeConvex();
  173.         }
  174.     }
  175.  
  176.     [Serializable]
  177.     public sealed class DeleteVertexCmd : ICommand
  178.     {
  179.         private Polygon _polygon;
  180.         private List<Vertex> _vertices;
  181.  
  182.         public DeleteVertexCmd(Polygon polygon, Point point)
  183.         {
  184.             _polygon = polygon;
  185.             _vertices = new List<Vertex>
  186.             {
  187.                 polygon.FindLast(point)
  188.             };
  189.         }
  190.  
  191.         public DeleteVertexCmd(Polygon polygon, List<Vertex> vertices)
  192.         {
  193.             _polygon = polygon;
  194.             _vertices = vertices;
  195.         }
  196.  
  197.         public static bool CanDelete(Polygon polygon, Point point)
  198.         {
  199.             return polygon.Exists(vertex => vertex.Check(point.X, point.Y));
  200.         }
  201.  
  202.         public void Execute()
  203.         {
  204.             _polygon.RemoveRange(_vertices);
  205.         }
  206.  
  207.         public void Undo()
  208.         {
  209.             _polygon.AddRange(_vertices);
  210.             if (_polygon.Count >= 3)
  211.                 _polygon.MakeConvex();
  212.         }
  213.     }
  214.  
  215.     [Serializable]
  216.     public sealed class ChangeTypeCmd : ICommand
  217.     {
  218.         private Type _prevType;
  219.         private Type _newType;
  220.  
  221.         public ChangeTypeCmd(Type prevType, Type newType)
  222.         {
  223.             _prevType = prevType;
  224.             _newType = newType;
  225.         }
  226.  
  227.         public static MainControl Form { get; set; }
  228.  
  229.         public void Execute()
  230.         {
  231.             Form.ChosenType = _newType;
  232.         }
  233.  
  234.         public void Undo()
  235.         {
  236.             Form.ChosenType = _prevType;
  237.         }
  238.     }
  239.  
  240.     [Serializable]
  241.     public sealed class ChangeSizeCmd : ICommand
  242.     {
  243.         private Polygon _polygon;
  244.         private int _prevSize;
  245.         private int _newSize;
  246.  
  247.         public ChangeSizeCmd(Polygon polygon, int prevSize, int newRadius)
  248.         {
  249.             _polygon = polygon;
  250.             _prevSize = prevSize;
  251.             _newSize = newRadius;
  252.         }
  253.  
  254.         public void Execute()
  255.         {
  256.             _polygon.VertexSize = _newSize;
  257.         }
  258.  
  259.         public void Undo()
  260.         {
  261.             _polygon.VertexSize = _prevSize;
  262.         }
  263.     }
  264.  
  265.     [Serializable]
  266.     public sealed class ChangeBackColorCmd : ICommand
  267.     {
  268.         private Color _prevColor;
  269.         private Color _newColor;
  270.  
  271.         public ChangeBackColorCmd(Color prevColor, Color newColor)
  272.         {
  273.             _prevColor = prevColor;
  274.             _newColor = newColor;
  275.         }
  276.  
  277.         public static Form Form { get; set; }
  278.  
  279.         public void Execute()
  280.         {
  281.             Form.BackColor = _newColor;
  282.         }
  283.  
  284.         public void Undo()
  285.         {
  286.             Form.BackColor = _prevColor;
  287.         }
  288.     }
  289.  
  290.     [Serializable]
  291.     public sealed class ChangeHullColorCmd : ICommand
  292.     {
  293.         private Polygon _polygon;
  294.         private Color _prevColor;
  295.         private Color _newColor;
  296.  
  297.         public ChangeHullColorCmd(Polygon polygon, Color newColor)
  298.         {
  299.             _polygon = polygon;
  300.             _prevColor = polygon.HullColor;
  301.             _newColor = newColor;
  302.         }
  303.  
  304.         public void Execute()
  305.         {
  306.             _polygon.HullColor = _newColor;
  307.         }
  308.  
  309.         public void Undo()
  310.         {
  311.             _polygon.HullColor = _prevColor;
  312.         }
  313.     }
  314.  
  315.     [Serializable]
  316.     public sealed class ChangeVertexColorCmd : ICommand
  317.     {
  318.         private Polygon _polygon;
  319.         private Color _prevColor;
  320.         private Color _newColor;
  321.  
  322.         public ChangeVertexColorCmd(Polygon polygon, Color newColor)
  323.         {
  324.             _polygon = polygon;
  325.             _prevColor = polygon.VertexColor;
  326.             _newColor = newColor;
  327.         }
  328.  
  329.         public void Execute()
  330.         {
  331.             _polygon.VertexColor = _newColor;
  332.         }
  333.  
  334.         public void Undo()
  335.         {
  336.             _polygon.VertexColor = _prevColor;
  337.         }
  338.     }
  339.  
  340.     [Serializable]
  341.     public sealed class ChangeShapeCmd : ICommand
  342.     {
  343.         private int _prevIndex;
  344.         private int _newIndex;
  345.  
  346.         public ChangeShapeCmd(int prevIndex, int newIndex)
  347.         {
  348.             _prevIndex = prevIndex;
  349.             _newIndex = newIndex;
  350.         }
  351.  
  352.         //private string _prevName;
  353.         //private string _newName;
  354.  
  355.         //public ChangeShapeCmd(string prevName, string newName)
  356.         //{
  357.         //    _prevName = prevName;
  358.         //    _newName = newName;
  359.         //}
  360.  
  361.  
  362.         public static ToolStripItemCollection Collection { get; set; }
  363.  
  364.         public void Execute()
  365.         {
  366.             foreach (ToolStripMenuItem item in Collection)
  367.             {
  368.                 item.Checked = false;
  369.             }
  370.             ((ToolStripMenuItem)Collection[_newIndex]).Checked = true;
  371.         }
  372.  
  373.         public void Undo()
  374.         {
  375.             foreach (ToolStripMenuItem item in Collection)
  376.             {
  377.                 item.Checked = false;
  378.             }
  379.             ((ToolStripMenuItem)Collection[_prevIndex]).Checked = true;
  380.         }
  381.     }
  382.  
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement