Added the undo feature

This commit is contained in:
2022-05-31 13:50:18 +02:00
parent b1e0b5d618
commit 88b568500c
6 changed files with 73 additions and 4 deletions

View File

@@ -105,5 +105,21 @@ namespace Paint_2
{ {
return Name; return Name;
} }
public void Undo()
{
if (Drawings.Count > 1)
{
Drawings.RemoveAt(Drawings.Count - 1);
Colors.RemoveAt(Colors.Count - 1);
Widths.RemoveAt(Widths.Count - 1);
}
else
{
Drawings.Clear();
Colors.Clear();
Widths.Clear();
}
}
} }
} }

View File

@@ -73,6 +73,7 @@
this.btnRandomColor = new System.Windows.Forms.Button(); this.btnRandomColor = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.lsbTools = new System.Windows.Forms.ListBox(); this.lsbTools = new System.Windows.Forms.ListBox();
this.btnUndo = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.canvas)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.canvas)).BeginInit();
this.panelFile.SuspendLayout(); this.panelFile.SuspendLayout();
this.panelDrawing.SuspendLayout(); this.panelDrawing.SuspendLayout();
@@ -573,6 +574,7 @@
// //
this.panelTools.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 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.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(31)))), ((int)(((byte)(31)))), ((int)(((byte)(31)))));
this.panelTools.Controls.Add(this.btnUndo);
this.panelTools.Controls.Add(this.btnRandomColor); this.panelTools.Controls.Add(this.btnRandomColor);
this.panelTools.Controls.Add(this.label2); this.panelTools.Controls.Add(this.label2);
this.panelTools.Controls.Add(this.lsbTools); this.panelTools.Controls.Add(this.lsbTools);
@@ -632,10 +634,24 @@
this.lsbTools.ItemHeight = 18; this.lsbTools.ItemHeight = 18;
this.lsbTools.Location = new System.Drawing.Point(10, 358); this.lsbTools.Location = new System.Drawing.Point(10, 358);
this.lsbTools.Name = "lsbTools"; this.lsbTools.Name = "lsbTools";
this.lsbTools.Size = new System.Drawing.Size(141, 200); this.lsbTools.Size = new System.Drawing.Size(141, 182);
this.lsbTools.TabIndex = 32; this.lsbTools.TabIndex = 32;
this.lsbTools.SelectedIndexChanged += new System.EventHandler(this.lsbTools_SelectedIndexChanged); this.lsbTools.SelectedIndexChanged += new System.EventHandler(this.lsbTools_SelectedIndexChanged);
// //
// btnUndo
//
this.btnUndo.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(41)))), ((int)(((byte)(41)))), ((int)(((byte)(41)))));
this.btnUndo.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnUndo.Font = new System.Drawing.Font("Cascadia Code", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
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.TabIndex = 35;
this.btnUndo.Text = "Undo";
this.btnUndo.UseVisualStyleBackColor = false;
this.btnUndo.Click += new System.EventHandler(this.btnUndo_Click);
//
// PaintForm // PaintForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 18F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 18F);
@@ -722,6 +738,7 @@
private System.Windows.Forms.Label lblHeight; private System.Windows.Forms.Label lblHeight;
private System.Windows.Forms.Label lblWidth; private System.Windows.Forms.Label lblWidth;
private System.Windows.Forms.Button btnRandomColor; private System.Windows.Forms.Button btnRandomColor;
private System.Windows.Forms.Button btnUndo;
} }
} }

View File

@@ -86,6 +86,11 @@ namespace Paint_2
result += "R:" + color.R + " G:" + color.G + " B:" + color.B; result += "R:" + color.R + " G:" + color.G + " B:" + color.B;
return result; return result;
} }
private void ForceRefresh()
{
canvas.Image = sketch.ForcePaint();
RefreshUi();
}
private void RefreshUi() private void RefreshUi()
{ {
canvas.Image = sketch.Paint(); canvas.Image = sketch.Paint();
@@ -114,7 +119,6 @@ namespace Paint_2
{ {
btnRandomColor.ForeColor = Color.Red; btnRandomColor.ForeColor = Color.Red;
} }
} }
private Color GetHoverColor() private Color GetHoverColor()
{ {
@@ -135,7 +139,7 @@ namespace Paint_2
private void btnClear_Click(object sender, EventArgs e) private void btnClear_Click(object sender, EventArgs e)
{ {
sketch.Clear(); sketch.Clear();
RefreshUi(); ForceRefresh();
} }
private void nupPencilWidth_ValueChanged(object sender, EventArgs e) private void nupPencilWidth_ValueChanged(object sender, EventArgs e)
@@ -243,5 +247,11 @@ namespace Paint_2
randomColor = !randomColor; randomColor = !randomColor;
RefreshUi(); RefreshUi();
} }
private void btnUndo_Click(object sender, EventArgs e)
{
sketch.Undo();
ForceRefresh();
}
} }
} }

View File

@@ -24,6 +24,7 @@ namespace Paint_2
void Start(Color color,int width); void Start(Color color,int width);
void Stop(Point point); void Stop(Point point);
void Add(Point point); void Add(Point point);
void Undo();
void Clear(); void Clear();
List<Color> GetLastColors(int colorNumber); List<Color> GetLastColors(int colorNumber);
void Paint(Bitmap canvas); void Paint(Bitmap canvas);

View File

@@ -106,5 +106,21 @@ namespace Paint_2
{ {
return Name; return Name;
} }
public void Undo()
{
if (Drawings.Count > 1)
{
Drawings.RemoveAt(Drawings.Count -1);
Colors.RemoveAt(Colors.Count -1);
Widths.RemoveAt(Widths.Count -1);
}
else
{
Drawings.Clear();
Colors.Clear();
Widths.Clear();
}
}
} }
} }

View File

@@ -64,6 +64,10 @@ namespace Paint_2
Console.WriteLine("Oooops..."); Console.WriteLine("Oooops...");
} }
} }
public void Undo()
{
CurrentTool.Undo();
}
public void StartDrawing(Point location,Color color,int width) public void StartDrawing(Point location,Color color,int width)
{ {
CurrentTool.Start(color,width); CurrentTool.Start(color,width);
@@ -87,7 +91,6 @@ namespace Paint_2
} }
public void Clear() public void Clear()
{ {
Drawing = new Bitmap(SketchSize.Width, SketchSize.Height);
foreach (PaintTool tool in ToolList) foreach (PaintTool tool in ToolList)
{ {
tool.Clear(); tool.Clear();
@@ -95,6 +98,12 @@ namespace Paint_2
} }
public Bitmap Paint() public Bitmap Paint()
{ {
CurrentTool.Paint(Drawing);
return Drawing;
}
public Bitmap ForcePaint()
{
Drawing = new Bitmap(SketchSize.Width,SketchSize.Height);
foreach (PaintTool tool in ToolList) foreach (PaintTool tool in ToolList)
{ {
tool.Paint(Drawing); tool.Paint(Drawing);