From 88b568500cff3f51f9cfc590db7ea6451abaa7c6 Mon Sep 17 00:00:00 2001 From: maxluli Date: Tue, 31 May 2022 13:50:18 +0200 Subject: [PATCH] Added the undo feature --- Paint_2/DotPencil.cs | 16 ++++++++++++++++ Paint_2/Form1.Designer.cs | 19 ++++++++++++++++++- Paint_2/Form1.cs | 14 ++++++++++++-- Paint_2/PaintTool.cs | 1 + Paint_2/Pencil.cs | 16 ++++++++++++++++ Paint_2/Sketch.cs | 11 ++++++++++- 6 files changed, 73 insertions(+), 4 deletions(-) diff --git a/Paint_2/DotPencil.cs b/Paint_2/DotPencil.cs index e820810..c32d646 100644 --- a/Paint_2/DotPencil.cs +++ b/Paint_2/DotPencil.cs @@ -105,5 +105,21 @@ namespace Paint_2 { 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(); + } + } } } diff --git a/Paint_2/Form1.Designer.cs b/Paint_2/Form1.Designer.cs index 1de84a4..4576ec9 100644 --- a/Paint_2/Form1.Designer.cs +++ b/Paint_2/Form1.Designer.cs @@ -73,6 +73,7 @@ this.btnRandomColor = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.lsbTools = new System.Windows.Forms.ListBox(); + this.btnUndo = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.canvas)).BeginInit(); this.panelFile.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.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.label2); this.panelTools.Controls.Add(this.lsbTools); @@ -632,10 +634,24 @@ this.lsbTools.ItemHeight = 18; this.lsbTools.Location = new System.Drawing.Point(10, 358); 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.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 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 18F); @@ -722,6 +738,7 @@ private System.Windows.Forms.Label lblHeight; private System.Windows.Forms.Label lblWidth; private System.Windows.Forms.Button btnRandomColor; + private System.Windows.Forms.Button btnUndo; } } diff --git a/Paint_2/Form1.cs b/Paint_2/Form1.cs index dad0e82..87196b0 100644 --- a/Paint_2/Form1.cs +++ b/Paint_2/Form1.cs @@ -86,6 +86,11 @@ namespace Paint_2 result += "R:" + color.R + " G:" + color.G + " B:" + color.B; return result; } + private void ForceRefresh() + { + canvas.Image = sketch.ForcePaint(); + RefreshUi(); + } private void RefreshUi() { canvas.Image = sketch.Paint(); @@ -114,7 +119,6 @@ namespace Paint_2 { btnRandomColor.ForeColor = Color.Red; } - } private Color GetHoverColor() { @@ -135,7 +139,7 @@ namespace Paint_2 private void btnClear_Click(object sender, EventArgs e) { sketch.Clear(); - RefreshUi(); + ForceRefresh(); } private void nupPencilWidth_ValueChanged(object sender, EventArgs e) @@ -243,5 +247,11 @@ namespace Paint_2 randomColor = !randomColor; RefreshUi(); } + + private void btnUndo_Click(object sender, EventArgs e) + { + sketch.Undo(); + ForceRefresh(); + } } } diff --git a/Paint_2/PaintTool.cs b/Paint_2/PaintTool.cs index d3281aa..1bd1578 100644 --- a/Paint_2/PaintTool.cs +++ b/Paint_2/PaintTool.cs @@ -24,6 +24,7 @@ namespace Paint_2 void Start(Color color,int width); void Stop(Point point); void Add(Point point); + void Undo(); void Clear(); List GetLastColors(int colorNumber); void Paint(Bitmap canvas); diff --git a/Paint_2/Pencil.cs b/Paint_2/Pencil.cs index 0cdf6ad..7fbd63c 100644 --- a/Paint_2/Pencil.cs +++ b/Paint_2/Pencil.cs @@ -106,5 +106,21 @@ namespace Paint_2 { 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(); + } + } } } diff --git a/Paint_2/Sketch.cs b/Paint_2/Sketch.cs index f8a49c6..ec9d5a9 100644 --- a/Paint_2/Sketch.cs +++ b/Paint_2/Sketch.cs @@ -64,6 +64,10 @@ namespace Paint_2 Console.WriteLine("Oooops..."); } } + public void Undo() + { + CurrentTool.Undo(); + } public void StartDrawing(Point location,Color color,int width) { CurrentTool.Start(color,width); @@ -87,7 +91,6 @@ namespace Paint_2 } public void Clear() { - Drawing = new Bitmap(SketchSize.Width, SketchSize.Height); foreach (PaintTool tool in ToolList) { tool.Clear(); @@ -95,6 +98,12 @@ namespace Paint_2 } public Bitmap Paint() { + CurrentTool.Paint(Drawing); + return Drawing; + } + public Bitmap ForcePaint() + { + Drawing = new Bitmap(SketchSize.Width,SketchSize.Height); foreach (PaintTool tool in ToolList) { tool.Paint(Drawing);