From 9826092ce462bd919aec547d5df2d66da4a150e5 Mon Sep 17 00:00:00 2001 From: maxluli Date: Tue, 31 May 2022 14:07:29 +0200 Subject: [PATCH] Added Redo feature --- Paint_2/DotPencil.cs | 32 +++++++++++++++++++++++++++++++- Paint_2/Form1.Designer.cs | 19 ++++++++++++++++++- Paint_2/Form1.cs | 6 ++++++ Paint_2/PaintTool.cs | 4 ++++ Paint_2/Pencil.cs | 32 +++++++++++++++++++++++++++++++- Paint_2/Sketch.cs | 4 ++++ 6 files changed, 94 insertions(+), 3 deletions(-) diff --git a/Paint_2/DotPencil.cs b/Paint_2/DotPencil.cs index c32d646..000c19b 100644 --- a/Paint_2/DotPencil.cs +++ b/Paint_2/DotPencil.cs @@ -15,23 +15,33 @@ namespace Paint_2 public class DotPencil:PaintTool { private List> _drawings; + private List> _drawingsRedo; private List _colors; + private List _colorsRedo; private List _widths; + private List _widthsRedo; private Color _color; private string _name; private int _width; + public List> Drawings { get => _drawings; set => _drawings = value; } + public List> DrawingsRedo { get => _drawingsRedo; set => _drawingsRedo = value; } public List Colors { get => _colors; set => _colors = value; } + public List ColorsRedo { get => _colorsRedo; set => _colorsRedo = value; } public List Widths { get => _widths; set => _widths = value; } + public List 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>(); + DrawingsRedo = new List>(); Colors = new List(); + ColorsRedo = new List(); Widths = new List(); + WidthsRedo = new List(); 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); + } + } } } diff --git a/Paint_2/Form1.Designer.cs b/Paint_2/Form1.Designer.cs index 4576ec9..2cabb6d 100644 --- a/Paint_2/Form1.Designer.cs +++ b/Paint_2/Form1.Designer.cs @@ -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; } } diff --git a/Paint_2/Form1.cs b/Paint_2/Form1.cs index 87196b0..d49081c 100644 --- a/Paint_2/Form1.cs +++ b/Paint_2/Form1.cs @@ -253,5 +253,11 @@ namespace Paint_2 sketch.Undo(); ForceRefresh(); } + + private void btnRedo_Click(object sender, EventArgs e) + { + sketch.Redo(); + ForceRefresh(); + } } } diff --git a/Paint_2/PaintTool.cs b/Paint_2/PaintTool.cs index 1bd1578..c0f5481 100644 --- a/Paint_2/PaintTool.cs +++ b/Paint_2/PaintTool.cs @@ -15,9 +15,12 @@ namespace Paint_2 internal interface PaintTool { List> Drawings { get; set; } + List> DrawingsRedo { get; set; } List Colors { get; set; } + List ColorsRedo { get; set; } Color Color { get; set; } List Widths { get; set; } + List 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 GetLastColors(int colorNumber); void Paint(Bitmap canvas); diff --git a/Paint_2/Pencil.cs b/Paint_2/Pencil.cs index 7fbd63c..1c24e24 100644 --- a/Paint_2/Pencil.cs +++ b/Paint_2/Pencil.cs @@ -15,23 +15,33 @@ namespace Paint_2 internal class Pencil : PaintTool { private List> _drawings; + private List> _drawingsRedo; private List _colors; + private List _colorsRedo; private List _widths; + private List _widthsRedo; private Color _color; private string _name; private int _width; + public List> Drawings { get => _drawings; set => _drawings = value; } + public List> DrawingsRedo { get => _drawingsRedo; set => _drawingsRedo = value; } public List Colors { get => _colors; set => _colors = value; } + public List ColorsRedo { get => _colorsRedo; set => _colorsRedo = value; } public List Widths { get => _widths; set => _widths = value; } + public List 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>(); + DrawingsRedo = new List>(); Colors = new List(); + ColorsRedo = new List(); Widths = new List(); + WidthsRedo = new List(); 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); + } + } } } diff --git a/Paint_2/Sketch.cs b/Paint_2/Sketch.cs index ec9d5a9..b477ce9 100644 --- a/Paint_2/Sketch.cs +++ b/Paint_2/Sketch.cs @@ -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);