Added Redo feature

This commit is contained in:
2022-05-31 14:07:29 +02:00
parent 88b568500c
commit 9826092ce4
6 changed files with 94 additions and 3 deletions

View File

@@ -15,23 +15,33 @@ namespace Paint_2
public class DotPencil:PaintTool
{
private List<List<Point>> _drawings;
private List<List<Point>> _drawingsRedo;
private List<Color> _colors;
private List<Color> _colorsRedo;
private List<int> _widths;
private List<int> _widthsRedo;
private Color _color;
private string _name;
private int _width;
public List<List<Point>> Drawings { get => _drawings; set => _drawings = value; }
public List<List<Point>> DrawingsRedo { get => _drawingsRedo; set => _drawingsRedo = value; }
public List<Color> Colors { get => _colors; set => _colors = value; }
public List<Color> ColorsRedo { get => _colorsRedo; set => _colorsRedo = value; }
public List<int> Widths { get => _widths; set => _widths = value; }
public List<int> WidthsRedo { get => _widthsRedo; set => _widthsRedo = value; }
public Color Color { get => _color; set => _color = value; }
public int Width { get => _width; set => _width = value; }
public string Name { get => _name; set => _name = value; }
public int Width { get => _width; set => _width = value; }
public DotPencil(string name)
{
Drawings = new List<List<Point>>();
DrawingsRedo = new List<List<Point>>();
Colors = new List<Color>();
ColorsRedo = new List<Color>();
Widths = new List<int>();
WidthsRedo = new List<int>();
Name = name;
}
public void Add(Point point)
@@ -108,18 +118,38 @@ namespace Paint_2
public void Undo()
{
int last = Drawings.Count - 1;
if (Drawings.Count > 1)
{
DrawingsRedo.Add(Drawings[last]);
Drawings.RemoveAt(Drawings.Count - 1);
ColorsRedo.Add(Colors[last]);
Colors.RemoveAt(Colors.Count - 1);
WidthsRedo.Add(Widths[last]);
Widths.RemoveAt(Widths.Count - 1);
}
else
{
DrawingsRedo.Add(Drawings[0]);
Drawings.Clear();
ColorsRedo.Add(Colors[0]);
Colors.Clear();
WidthsRedo.Add(Widths[0]);
Widths.Clear();
}
}
public void Redo()
{
if (DrawingsRedo.Count > 0 && WidthsRedo.Count > 0 && ColorsRedo.Count > 0)
{
Drawings.Add(DrawingsRedo[DrawingsRedo.Count - 1]);
DrawingsRedo.RemoveAt(DrawingsRedo.Count - 1);
Colors.Add(ColorsRedo[ColorsRedo.Count - 1]);
ColorsRedo.RemoveAt(ColorsRedo.Count - 1);
Widths.Add(WidthsRedo[WidthsRedo.Count - 1]);
WidthsRedo.RemoveAt(WidthsRedo.Count - 1);
}
}
}
}

View File

@@ -74,6 +74,7 @@
this.label2 = new System.Windows.Forms.Label();
this.lsbTools = new System.Windows.Forms.ListBox();
this.btnUndo = new System.Windows.Forms.Button();
this.btnRedo = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.canvas)).BeginInit();
this.panelFile.SuspendLayout();
this.panelDrawing.SuspendLayout();
@@ -574,6 +575,7 @@
//
this.panelTools.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.panelTools.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(31)))), ((int)(((byte)(31)))), ((int)(((byte)(31)))));
this.panelTools.Controls.Add(this.btnRedo);
this.panelTools.Controls.Add(this.btnUndo);
this.panelTools.Controls.Add(this.btnRandomColor);
this.panelTools.Controls.Add(this.label2);
@@ -646,12 +648,26 @@
this.btnUndo.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(214)))), ((int)(((byte)(214)))), ((int)(((byte)(214)))));
this.btnUndo.Location = new System.Drawing.Point(3, 547);
this.btnUndo.Name = "btnUndo";
this.btnUndo.Size = new System.Drawing.Size(157, 43);
this.btnUndo.Size = new System.Drawing.Size(75, 43);
this.btnUndo.TabIndex = 35;
this.btnUndo.Text = "Undo";
this.btnUndo.UseVisualStyleBackColor = false;
this.btnUndo.Click += new System.EventHandler(this.btnUndo_Click);
//
// btnRedo
//
this.btnRedo.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(41)))), ((int)(((byte)(41)))), ((int)(((byte)(41)))));
this.btnRedo.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnRedo.Font = new System.Drawing.Font("Cascadia Code", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnRedo.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(214)))), ((int)(((byte)(214)))), ((int)(((byte)(214)))));
this.btnRedo.Location = new System.Drawing.Point(85, 547);
this.btnRedo.Name = "btnRedo";
this.btnRedo.Size = new System.Drawing.Size(75, 43);
this.btnRedo.TabIndex = 36;
this.btnRedo.Text = "Redo";
this.btnRedo.UseVisualStyleBackColor = false;
this.btnRedo.Click += new System.EventHandler(this.btnRedo_Click);
//
// PaintForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 18F);
@@ -739,6 +755,7 @@
private System.Windows.Forms.Label lblWidth;
private System.Windows.Forms.Button btnRandomColor;
private System.Windows.Forms.Button btnUndo;
private System.Windows.Forms.Button btnRedo;
}
}

View File

@@ -253,5 +253,11 @@ namespace Paint_2
sketch.Undo();
ForceRefresh();
}
private void btnRedo_Click(object sender, EventArgs e)
{
sketch.Redo();
ForceRefresh();
}
}
}

View File

@@ -15,9 +15,12 @@ namespace Paint_2
internal interface PaintTool
{
List<List<Point>> Drawings { get; set; }
List<List<Point>> DrawingsRedo { get; set; }
List<Color> Colors { get; set; }
List<Color> ColorsRedo { get; set; }
Color Color { get; set; }
List<int> Widths { get; set; }
List<int> WidthsRedo { get; set; }
int Width { get; set; }
string Name { get; set; }
@@ -25,6 +28,7 @@ namespace Paint_2
void Stop(Point point);
void Add(Point point);
void Undo();
void Redo();
void Clear();
List<Color> GetLastColors(int colorNumber);
void Paint(Bitmap canvas);

View File

@@ -15,23 +15,33 @@ namespace Paint_2
internal class Pencil : PaintTool
{
private List<List<Point>> _drawings;
private List<List<Point>> _drawingsRedo;
private List<Color> _colors;
private List<Color> _colorsRedo;
private List<int> _widths;
private List<int> _widthsRedo;
private Color _color;
private string _name;
private int _width;
public List<List<Point>> Drawings { get => _drawings; set => _drawings = value; }
public List<List<Point>> DrawingsRedo { get => _drawingsRedo; set => _drawingsRedo = value; }
public List<Color> Colors { get => _colors; set => _colors = value; }
public List<Color> ColorsRedo { get => _colorsRedo; set => _colorsRedo = value; }
public List<int> Widths { get => _widths; set => _widths = value; }
public List<int> WidthsRedo { get => _widthsRedo; set => _widthsRedo = value; }
public Color Color { get => _color; set => _color = value; }
public int Width { get => _width; set => _width = value; }
public string Name { get => _name; set => _name = value; }
public int Width { get => _width; set => _width = value; }
public Pencil(string name)
{
Drawings = new List<List<Point>>();
DrawingsRedo = new List<List<Point>>();
Colors = new List<Color>();
ColorsRedo = new List<Color>();
Widths = new List<int>();
WidthsRedo = new List<int>();
Name = name;
}
public void Add(Point point)
@@ -109,18 +119,38 @@ namespace Paint_2
public void Undo()
{
int last = Drawings.Count - 1;
if (Drawings.Count > 1)
{
DrawingsRedo.Add(Drawings[last]);
Drawings.RemoveAt(Drawings.Count -1);
ColorsRedo.Add(Colors[last]);
Colors.RemoveAt(Colors.Count -1);
WidthsRedo.Add(Widths[last]);
Widths.RemoveAt(Widths.Count -1);
}
else
{
DrawingsRedo.Add(Drawings[0]);
Drawings.Clear();
ColorsRedo.Add(Colors[0]);
Colors.Clear();
WidthsRedo.Add(Widths[0]);
Widths.Clear();
}
}
public void Redo()
{
if (DrawingsRedo.Count > 0 && WidthsRedo.Count > 0 && ColorsRedo.Count > 0)
{
Drawings.Add(DrawingsRedo[DrawingsRedo.Count -1]);
DrawingsRedo.RemoveAt(DrawingsRedo.Count - 1);
Colors.Add(ColorsRedo[ColorsRedo.Count -1]);
ColorsRedo.RemoveAt(ColorsRedo.Count - 1);
Widths.Add(WidthsRedo[WidthsRedo.Count -1]);
WidthsRedo.RemoveAt(WidthsRedo.Count - 1);
}
}
}
}

View File

@@ -68,6 +68,10 @@ namespace Paint_2
{
CurrentTool.Undo();
}
public void Redo()
{
CurrentTool.Redo();
}
public void StartDrawing(Point location,Color color,int width)
{
CurrentTool.Start(color,width);