From 640796a1b1af0737f3914bd5d232adcad6ed5301 Mon Sep 17 00:00:00 2001 From: maxluli Date: Wed, 22 Jun 2022 09:29:55 +0200 Subject: [PATCH] Reworked 4 pencils and added the ability to CTRL+Drag to change the shapes while drawing them --- Paint_2/BezierPencil.cs | 4 ++ Paint_2/DotPencil.cs | 4 ++ Paint_2/EllipseBorderPencil.cs | 69 ++++++++++----------------- Paint_2/EllipsePencil.cs | 69 ++++++++++----------------- Paint_2/Form1.Designer.cs | 34 +++++++++---- Paint_2/Form1.cs | 82 +++++++++++++++++++++++++++----- Paint_2/PaintTool.cs | 4 +- Paint_2/PaintToolUtils.cs | 27 +++++++++++ Paint_2/Pencil.cs | 4 ++ Paint_2/Project.cs | 46 +++++++++++++++--- Paint_2/RectangleBorderPencil.cs | 66 +++++++++---------------- Paint_2/RectanglePencil.cs | 72 ++++++++-------------------- Paint_2/Sketch.cs | 22 +++++++++ 13 files changed, 289 insertions(+), 214 deletions(-) diff --git a/Paint_2/BezierPencil.cs b/Paint_2/BezierPencil.cs index 59905b7..a804069 100644 --- a/Paint_2/BezierPencil.cs +++ b/Paint_2/BezierPencil.cs @@ -18,6 +18,8 @@ namespace Paint_2 private List> _drawings; private List> _drawingsRedo; private bool _needsFullRefresh; + private bool _isCtrlPressed; + private bool _isShiftPressed; private PaintToolUtils Utils; private List _colors; private List _colorsRedo; @@ -34,6 +36,8 @@ namespace Paint_2 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 bool IsShiftPressed { get => _isShiftPressed; set => _isShiftPressed = value; } + public bool IsCtrlPressed { get => _isCtrlPressed; set => _isCtrlPressed = value; } public Color Color { get => _color; set => _color = value; } public string Name { get => _name; set => _name = value; } public int Width { get => _width; set => _width = value; } diff --git a/Paint_2/DotPencil.cs b/Paint_2/DotPencil.cs index 6f47819..fefcfd1 100644 --- a/Paint_2/DotPencil.cs +++ b/Paint_2/DotPencil.cs @@ -20,6 +20,8 @@ namespace Paint_2 private List> _drawings; private List> _drawingsRedo; private bool _needsFullRefresh; + private bool _isShiftPressed; + private bool _isCtrlPressed; private PaintToolUtils Utils; private List _colors; private List _colorsRedo; @@ -32,6 +34,8 @@ namespace Paint_2 public List> Drawings { get => _drawings; set => _drawings = value; } public List> DrawingsRedo { get => _drawingsRedo; set => _drawingsRedo = value; } public bool NeedsFullRefresh { get => _needsFullRefresh; set => _needsFullRefresh = value; } + public bool IsShiftPressed { get => _isShiftPressed; set => _isShiftPressed = value; } + public bool IsCtrlPressed { get => _isCtrlPressed; set => _isCtrlPressed = 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; } diff --git a/Paint_2/EllipseBorderPencil.cs b/Paint_2/EllipseBorderPencil.cs index 1026d0e..514e8e8 100644 --- a/Paint_2/EllipseBorderPencil.cs +++ b/Paint_2/EllipseBorderPencil.cs @@ -18,6 +18,8 @@ namespace Paint_2 private List> _drawings; private List> _drawingsRedo; private bool _needsFullRefresh; + private bool _isShiftPressed; + private bool _isCtrlPressed; private PaintToolUtils Utils; private List _colors; private List _colorsRedo; @@ -34,6 +36,8 @@ namespace Paint_2 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 bool IsShiftPressed { get => _isShiftPressed; set => _isShiftPressed = value; } + public bool IsCtrlPressed { get => _isCtrlPressed; set => _isCtrlPressed = value; } public Color Color { get => _color; set => _color = value; } public string Name { get => _name; set => _name = value; } public int Width { get => _width; set => _width = value; } @@ -75,32 +79,22 @@ namespace Paint_2 { if (drawing.Count == 2) { - Point p1 = drawing[0]; - Point p2 = drawing[1]; + Point start = drawing[0]; + Point end = drawing[1]; - Point start = new Point(0, 0); - Point end = new Point(0, 0); - - if (p2.X > p1.X) + List points; + if (drawingCounter == Drawings.Count - 1) { - start.X = p1.X; - end.X = p2.X; + points = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, drawing[0], drawing[1]); } else { - start.X = p2.X; - end.X = p1.X; - } - if (p2.Y > p1.Y) - { - start.Y = p1.Y; - end.Y = p2.Y; - } - else - { - start.Y = p2.Y; - end.Y = p1.Y; + points = Utils.ConvertRectangleIntoPositive(false, drawing[0], drawing[1]); } + + start = points[0]; + end = points[1]; + Size size = new Size((end.X - start.X) / 2, (end.Y - start.Y) / 2); result += ""; result += newLine; @@ -117,33 +111,22 @@ namespace Paint_2 { if (drawing.Count == 2) { - Point p1 = drawing[0]; - Point p2 = drawing[1]; + Point start = drawing[0]; + Point end = drawing[1]; - Point start = new Point(0, 0); - Point end = new Point(0, 0); - - if (p2.X > p1.X) + List points; + if (drawingCounter == Drawings.Count - 1) { - start.X = p1.X; - end.X = p2.X; + points = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, drawing[0], drawing[1]); } else { - start.X = p2.X; - end.X = p1.X; - } - if (p2.Y > p1.Y) - { - start.Y = p1.Y; - end.Y = p2.Y; - } - else - { - start.Y = p2.Y; - end.Y = p1.Y; + points = Utils.ConvertRectangleIntoPositive(false, drawing[0], drawing[1]); } + start = points[0]; + end = points[1]; + gr.DrawEllipse(new Pen(Colors[drawingCounter], Widths[drawingCounter]), new Rectangle(start, new Size(end.X - start.X, end.Y - start.Y))); } drawingCounter++; @@ -159,10 +142,8 @@ namespace Paint_2 } public void Stop() { - for (int i = 0; i < Drawings.Count; i++) - { - List Drawing = Drawings[i]; - } + List myDrawing = Drawings.Last(); + Drawings[Drawings.Count - 1] = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, myDrawing[0], myDrawing[1]); } private Bitmap PostProcessing(List Drawing, Bitmap bmp, int width, Color color) { diff --git a/Paint_2/EllipsePencil.cs b/Paint_2/EllipsePencil.cs index fc0baa5..efb2296 100644 --- a/Paint_2/EllipsePencil.cs +++ b/Paint_2/EllipsePencil.cs @@ -17,6 +17,8 @@ namespace Paint_2 private List> _drawings; private List> _drawingsRedo; private bool _needsFullRefresh; + private bool _isShiftPressed; + private bool _isCtrlPressed; private PaintToolUtils Utils; private List _colors; private List _colorsRedo; @@ -33,6 +35,8 @@ namespace Paint_2 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 bool IsShiftPressed { get => _isShiftPressed; set => _isShiftPressed = value; } + public bool IsCtrlPressed { get => _isCtrlPressed; set => _isCtrlPressed = value; } public Color Color { get => _color; set => _color = value; } public string Name { get => _name; set => _name = value; } public int Width { get => _width; set => _width = value; } @@ -72,33 +76,22 @@ namespace Paint_2 { if (drawing.Count == 2) { - Point p1 = drawing[0]; - Point p2 = drawing[1]; + Point start = drawing[0]; + Point end = drawing[1]; - Point start = new Point(0, 0); - Point end = new Point(0, 0); - - if (p2.X > p1.X) + List points; + if (drawingCounter == Drawings.Count - 1) { - start.X = p1.X; - end.X = p2.X; + points = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, drawing[0], drawing[1]); } else { - start.X = p2.X; - end.X = p1.X; - } - if (p2.Y > p1.Y) - { - start.Y = p1.Y; - end.Y = p2.Y; - } - else - { - start.Y = p2.Y; - end.Y = p1.Y; + points = Utils.ConvertRectangleIntoPositive(false, drawing[0], drawing[1]); } + start = points[0]; + end = points[1]; + gr.FillEllipse(new SolidBrush(Colors[drawingCounter]), new Rectangle(start, new Size(end.X - start.X, end.Y - start.Y))); } drawingCounter++; @@ -116,32 +109,22 @@ namespace Paint_2 { if (drawing.Count == 2) { - Point p1 = drawing[0]; - Point p2 = drawing[1]; + Point start = drawing[0]; + Point end = drawing[1]; - Point start = new Point(0, 0); - Point end = new Point(0, 0); - - if (p2.X > p1.X) + List points; + if (drawingCounter == Drawings.Count - 1) { - start.X = p1.X; - end.X = p2.X; + points = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, drawing[0], drawing[1]); } else { - start.X = p2.X; - end.X = p1.X; - } - if (p2.Y > p1.Y) - { - start.Y = p1.Y; - end.Y = p2.Y; - } - else - { - start.Y = p2.Y; - end.Y = p1.Y; + points = Utils.ConvertRectangleIntoPositive(false, drawing[0], drawing[1]); } + + start = points[0]; + end = points[1]; + Size size = new Size((end.X - start.X) / 2, (end.Y - start.Y) / 2); result += ""; result += newLine; @@ -160,10 +143,8 @@ namespace Paint_2 } public void Stop() { - for (int i = 0; i < Drawings.Count; i++) - { - List Drawing = Drawings[i]; - } + List myDrawing = Drawings.Last(); + Drawings[Drawings.Count - 1] = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, myDrawing[0], myDrawing[1]); } private Bitmap PostProcessing(List Drawing, Bitmap bmp, int width, Color color) { diff --git a/Paint_2/Form1.Designer.cs b/Paint_2/Form1.Designer.cs index 9fdad28..8b46f59 100644 --- a/Paint_2/Form1.Designer.cs +++ b/Paint_2/Form1.Designer.cs @@ -69,9 +69,10 @@ this.btnLayerRemove = new System.Windows.Forms.Button(); this.BtnAddLayer = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); - this.DebugLabel = new System.Windows.Forms.Label(); + this.DebugLabel1 = new System.Windows.Forms.Label(); this.panel4 = new System.Windows.Forms.Panel(); this.btnSvgExport = new System.Windows.Forms.Button(); + this.DebugLabel2 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.canvas)).BeginInit(); this.panelFile.SuspendLayout(); this.panelDrawing.SuspendLayout(); @@ -600,14 +601,14 @@ this.label2.Size = new System.Drawing.Size(0, 16); this.label2.TabIndex = 36; // - // DebugLabel + // DebugLabel1 // - this.DebugLabel.AutoSize = true; - this.DebugLabel.Location = new System.Drawing.Point(18, 86); - this.DebugLabel.Name = "DebugLabel"; - this.DebugLabel.Size = new System.Drawing.Size(48, 14); - this.DebugLabel.TabIndex = 38; - this.DebugLabel.Text = "Debug"; + this.DebugLabel1.AutoSize = true; + this.DebugLabel1.Location = new System.Drawing.Point(17, 64); + this.DebugLabel1.Name = "DebugLabel1"; + this.DebugLabel1.Size = new System.Drawing.Size(48, 14); + this.DebugLabel1.TabIndex = 38; + this.DebugLabel1.Text = "Debug"; // // panel4 // @@ -640,15 +641,25 @@ this.btnSvgExport.UseVisualStyleBackColor = false; this.btnSvgExport.Click += new System.EventHandler(this.btnSvgExport_Click); // + // DebugLabel2 + // + this.DebugLabel2.AutoSize = true; + this.DebugLabel2.Location = new System.Drawing.Point(17, 78); + this.DebugLabel2.Name = "DebugLabel2"; + this.DebugLabel2.Size = new System.Drawing.Size(48, 14); + this.DebugLabel2.TabIndex = 39; + this.DebugLabel2.Text = "Debug"; + // // PaintForm // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 14F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(34)))), ((int)(((byte)(34)))), ((int)(((byte)(34))))); this.ClientSize = new System.Drawing.Size(1222, 618); + this.Controls.Add(this.DebugLabel2); this.Controls.Add(this.btnSvgExport); this.Controls.Add(this.panel4); - this.Controls.Add(this.DebugLabel); + this.Controls.Add(this.DebugLabel1); this.Controls.Add(this.panel3); this.Controls.Add(this.panel1); this.Controls.Add(this.panelHoverColor); @@ -665,6 +676,8 @@ this.Name = "PaintForm"; this.Text = "Paint 2"; this.Load += new System.EventHandler(this.PaintForm_Load); + this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.PaintForm_KeyDown); + this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.PaintForm_KeyUp); this.Resize += new System.EventHandler(this.PaintForm_Resize); ((System.ComponentModel.ISupportInitialize)(this.canvas)).EndInit(); this.panelFile.ResumeLayout(false); @@ -728,11 +741,12 @@ private System.Windows.Forms.Button btnEyeDrop; private System.Windows.Forms.Button btnLayerRemove; private System.Windows.Forms.Button BtnAddLayer; - private System.Windows.Forms.Label DebugLabel; + private System.Windows.Forms.Label DebugLabel1; private System.Windows.Forms.Panel pnlLayers; private System.Windows.Forms.Label label3; private System.Windows.Forms.Panel panel4; private System.Windows.Forms.Button btnSvgExport; + private System.Windows.Forms.Label DebugLabel2; } } diff --git a/Paint_2/Form1.cs b/Paint_2/Form1.cs index 2dc7dbf..8232626 100644 --- a/Paint_2/Form1.cs +++ b/Paint_2/Form1.cs @@ -42,6 +42,7 @@ namespace Paint_2 Project = new Project("Untitled project", canvas.Size); SelectedLayers = new List(); SelectedLayers.Add(Project.GetAllLayers()[0].Id); + this.KeyPreview = true; RefreshUi(); } private Point MousePositionToCanvasPosition() @@ -50,6 +51,7 @@ namespace Paint_2 } private void canvas_MouseDown(object sender, MouseEventArgs e) { + canvas.Focus(); Color hoveringColor = GetHoverColor(); Point mousePosition = MousePositionToCanvasPosition(); int toolWidth = (int)nupPencilWidth.Value; @@ -93,7 +95,7 @@ namespace Paint_2 private void RefreshUi() { lsbTools.DataSource = Project.AvaibleTools; - DebugLabel.Text = ""; + DebugLabel1.Text = ""; PaintTool currentTool = Project.GetCurrentTool(SelectedLayers); if (currentTool != null && currentTool.NeedsFullRefresh) @@ -104,7 +106,7 @@ namespace Paint_2 { canvas.Image = Project.PaintLayers(); } - + lblSelectedColor.Text = ColorToString(Project.GetCurrentToolColor(SelectedLayers)); btnSelectedColor.BackColor = Project.GetCurrentToolColor(SelectedLayers); @@ -131,6 +133,26 @@ namespace Paint_2 pbxSample.Image = DrawSample(pbxSample.Size); + DebugLabel1.Text = "Control"; + DebugLabel2.Text = "Shift"; + + if (Project.GetCurrentTool(SelectedLayers).IsCtrlPressed) + { + DebugLabel1.ForeColor = Color.Green; + } + else + { + DebugLabel1.ForeColor = Color.Red; + } + if (Project.GetCurrentTool(SelectedLayers).IsShiftPressed) + { + DebugLabel2.ForeColor = Color.Green; + } + else + { + DebugLabel2.ForeColor = Color.Red; + } + if (Project.RandomColor) { btnRandomColor.BackColor = FLAT_GREEN; @@ -174,7 +196,7 @@ namespace Paint_2 if (currentTool is DotPencil) { DotPencil pencil = currentTool as DotPencil; - pencil.Start(paint_tool_color,paint_tool_width); + pencil.Start(paint_tool_color, paint_tool_width); int precision = 5; for (int i = 0; i <= precision; i++) { @@ -192,8 +214,8 @@ namespace Paint_2 Point p1 = new Point(0 - paint_tool_width / 2, 0 - paint_tool_width / 2); Point p2 = new Point(0 - paint_tool_width / 2, size.Height + paint_tool_width / 2); - Point p3 = new Point(size.Width / 2 - paint_tool_width / 2,size.Height + paint_tool_width / 2); - Point p4 = new Point(size.Width / 2 - paint_tool_width / 2, 0 - paint_tool_width/2); + Point p3 = new Point(size.Width / 2 - paint_tool_width / 2, size.Height + paint_tool_width / 2); + Point p4 = new Point(size.Width / 2 - paint_tool_width / 2, 0 - paint_tool_width / 2); Point p5 = new Point(size.Width + paint_tool_width / 2, 0 - paint_tool_width / 2); Point p6 = new Point(size.Width + paint_tool_width / 2, size.Height + paint_tool_width / 2); // I KNOW THIS IS HARD CODED BUT ITS ONLY PURPOSE IS TO BE PRETTY so it does'nt matter @@ -221,11 +243,10 @@ namespace Paint_2 { if (currentTool is RectanglePencil || currentTool is RectangleBorderPencil || currentTool is EllipsePencil || currentTool is EllipseBorderPencil) { - currentTool.Start(paint_tool_color,paint_tool_width); - currentTool.Add(new Point(0 + size.Width / 10,0 + size.Height / 10)); + currentTool.Start(paint_tool_color, paint_tool_width); + currentTool.Add(new Point(0 + size.Width / 10, 0 + size.Height / 10)); currentTool.Add(new Point(size.Width - size.Width / 10, size.Height - size.Height / 10)); currentTool.Stop(); - currentTool.Paint(map); } } @@ -284,7 +305,7 @@ namespace Paint_2 int toolId = lsbTools.SelectedIndex; Project.ChangeTool(SelectedLayers, toolId); //Yeah that is a little janky but I just want to refresh the tool - nupPencilWidth_ValueChanged(sender,e); + nupPencilWidth_ValueChanged(sender, e); RefreshUi(); } @@ -477,10 +498,10 @@ namespace Paint_2 if (fbd.ShowDialog() == DialogResult.OK) { string SelectedPath = fbd.SelectedPath; - - if (Project.SaveSvg(SelectedPath,tbxProjectName.Text)) + + if (Project.SaveSvg(SelectedPath, tbxProjectName.Text)) { - MessageBox.Show("Svg saved at "+ SelectedPath); + MessageBox.Show("Svg saved at " + SelectedPath); } else { @@ -488,5 +509,42 @@ namespace Paint_2 } } } + + private void PaintForm_KeyPress(object sender, KeyPressEventArgs e) + { + //empty for now + } + + private void PaintForm_KeyDown(object sender, KeyEventArgs e) + { + switch (e.KeyCode) + { + case Keys.ControlKey: + Project.CtrlDown(); + break; + case Keys.ShiftKey: + Project.ShiftDown(); + break; + default: + //send the key to the tools + break; + } + } + + private void PaintForm_KeyUp(object sender, KeyEventArgs e) + { + switch (e.KeyCode) + { + case Keys.ControlKey: + Project.CtrlUp(); + break; + case Keys.ShiftKey: + Project.ShiftUp(); + break; + default: + //send the key to the tools + break; + } + } } } diff --git a/Paint_2/PaintTool.cs b/Paint_2/PaintTool.cs index 68d7d11..386d693 100644 --- a/Paint_2/PaintTool.cs +++ b/Paint_2/PaintTool.cs @@ -13,7 +13,7 @@ using System.Drawing; namespace Paint_2 { - public interface PaintTool:ICloneable + public interface PaintTool : ICloneable { List> Drawings { get; set; } List> DrawingsRedo { get; set; } @@ -25,6 +25,8 @@ namespace Paint_2 bool NeedsFullRefresh { get; set; } int Width { get; set; } string Name { get; set; } + bool IsCtrlPressed { get; set; } + bool IsShiftPressed { get; set; } void Start(Color color, int width); void Stop(); diff --git a/Paint_2/PaintToolUtils.cs b/Paint_2/PaintToolUtils.cs index 151bb56..4089bd5 100644 --- a/Paint_2/PaintToolUtils.cs +++ b/Paint_2/PaintToolUtils.cs @@ -92,6 +92,33 @@ namespace Paint_2 result.Reverse(); return result; } + public List ConvertRectangleIntoPositive(bool IsCtrlPressed,Point start, Point end) + { + Point newStart = new Point(start.X, start.Y); + Point newEnd = new Point(end.X, end.Y); + + int xDiff = end.X - start.X; + int yDiff = end.Y - start.Y; + + Size size = new Size(xDiff, yDiff); + + if (xDiff < 0) + { + newStart = new Point(end.X, newStart.Y); + } + if (yDiff < 0) + { + newStart = new Point(newStart.X, end.Y); + } + + if (IsCtrlPressed) + { + size = new Size(Math.Max(Math.Abs(xDiff), Math.Abs(yDiff)), Math.Max(Math.Abs(xDiff), Math.Abs(yDiff))); + } + + newEnd = new Point(newStart.X + Math.Abs(size.Width), newStart.Y + Math.Abs(size.Height)); + return new List { newStart, newEnd }; + } public Point Lerp(Point start, Point end, float t) { int xDiff = end.X - start.X; diff --git a/Paint_2/Pencil.cs b/Paint_2/Pencil.cs index a15fc7a..4e1ae31 100644 --- a/Paint_2/Pencil.cs +++ b/Paint_2/Pencil.cs @@ -21,6 +21,8 @@ namespace Paint_2 private List> _drawingsRedo; private PaintToolUtils Utils; private bool _needsFullRefresh; + private bool _isShiftPressed; + private bool _isCtrlPressed; private List _colors; private List _colorsRedo; private List _widths; @@ -35,6 +37,8 @@ namespace Paint_2 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 bool IsShiftPressed { get => _isShiftPressed; set => _isShiftPressed = value; } + public bool IsCtrlPressed { get => _isCtrlPressed; set => _isCtrlPressed = value; } public Color Color { get => _color; set => _color = value; } public string Name { get => _name; set => _name = value; } public int Width { get => _width; set => _width = value; } diff --git a/Paint_2/Project.cs b/Paint_2/Project.cs index cb9da13..af7e17b 100644 --- a/Paint_2/Project.cs +++ b/Paint_2/Project.cs @@ -29,6 +29,7 @@ namespace Paint_2 private Random _random; private bool _drawing; private string _name; + public List AvaibleTools { get => _avaibleTools; set => _avaibleTools = value; } //public Sketch CurrentLayer { get => _currentSketch; set => _currentSketch = value; } @@ -272,14 +273,18 @@ namespace Paint_2 } public Bitmap PaintLayers() { - Bitmap result = new Bitmap(CanvasSize.Width, CanvasSize.Height); - Graphics gr = Graphics.FromImage(result); - foreach (string layerId in LayersToPrint) + Bitmap result = null; + if (CanvasSize.Width != 0 && CanvasSize.Height != 0) { - Sketch layer = FindSketch(Layers, layerId); - if (layer != null) + result = new Bitmap(CanvasSize.Width, CanvasSize.Height); + Graphics gr = Graphics.FromImage(result); + foreach (string layerId in LayersToPrint) { - gr.DrawImage(layer.Paint(), Point.Empty); + Sketch layer = FindSketch(Layers, layerId); + if (layer != null) + { + gr.DrawImage(layer.Paint(), Point.Empty); + } } } return result; @@ -416,7 +421,34 @@ namespace Paint_2 } return result; } - + public void CtrlDown() + { + foreach (Sketch layer in Layers) + { + layer.CtrlDown(); + } + } + public void CtrlUp() + { + foreach (Sketch layer in Layers) + { + layer.CtrlUp(); + } + } + public void ShiftDown() + { + foreach (Sketch layer in Layers) + { + layer.ShiftDown(); + } + } + public void ShiftUp() + { + foreach (Sketch layer in Layers) + { + layer.ShiftUp(); + } + } public string ConvertToSVG() { string fileContent = ""; diff --git a/Paint_2/RectangleBorderPencil.cs b/Paint_2/RectangleBorderPencil.cs index 2b2f5ef..80dff0f 100644 --- a/Paint_2/RectangleBorderPencil.cs +++ b/Paint_2/RectangleBorderPencil.cs @@ -17,6 +17,8 @@ namespace Paint_2 private List> _drawings; private List> _drawingsRedo; private bool _needsFullRefresh; + private bool _isShiftPressed; + private bool _isCtrlPressed; private PaintToolUtils Utils; private List _colors; private List _colorsRedo; @@ -33,6 +35,8 @@ namespace Paint_2 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 bool IsShiftPressed { get => _isShiftPressed; set => _isShiftPressed = value; } + public bool IsCtrlPressed { get => _isCtrlPressed; set => _isCtrlPressed = value; } public Color Color { get => _color; set => _color = value; } public string Name { get => _name; set => _name = value; } public int Width { get => _width; set => _width = value; } @@ -72,33 +76,22 @@ namespace Paint_2 { if (drawing.Count == 2) { - Point p1 = drawing[0]; - Point p2 = drawing[1]; + Point start = drawing[0]; + Point end = drawing[1]; - Point start = new Point(0, 0); - Point end = new Point(0, 0); - - if (p2.X > p1.X) + List points; + if (drawingCounter == Drawings.Count - 1) { - start.X = p1.X; - end.X = p2.X; + points = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, drawing[0], drawing[1]); } else { - start.X = p2.X; - end.X = p1.X; - } - if (p2.Y > p1.Y) - { - start.Y = p1.Y; - end.Y = p2.Y; - } - else - { - start.Y = p2.Y; - end.Y = p1.Y; + points = Utils.ConvertRectangleIntoPositive(false, drawing[0], drawing[1]); } + start = points[0]; + end = points[1]; + gr.DrawRectangle(new Pen(Colors[drawingCounter], Widths[drawingCounter]), new Rectangle(start, new Size(end.X - start.X, end.Y - start.Y))); } drawingCounter++; @@ -116,32 +109,19 @@ namespace Paint_2 { if (drawing.Count == 2) { - Point p1 = drawing[0]; - Point p2 = drawing[1]; + Point start = drawing[0]; + Point end = drawing[1]; - Point start = new Point(0, 0); - Point end = new Point(0, 0); - - if (p2.X > p1.X) + List points; + if (drawingCounter == Drawings.Count - 1) { - start.X = p1.X; - end.X = p2.X; + points = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, drawing[0], drawing[1]); } else { - start.X = p2.X; - end.X = p1.X; - } - if (p2.Y > p1.Y) - { - start.Y = p1.Y; - end.Y = p2.Y; - } - else - { - start.Y = p2.Y; - end.Y = p1.Y; + points = Utils.ConvertRectangleIntoPositive(false, drawing[0], drawing[1]); } + result += ""; result += newLine; } @@ -159,10 +139,8 @@ namespace Paint_2 } public void Stop() { - for (int i = 0; i < Drawings.Count; i++) - { - List Drawing = Drawings[i]; - } + List myDrawing = Drawings.Last(); + Drawings[Drawings.Count - 1] = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, myDrawing[0], myDrawing[1]); } private Bitmap PostProcessing(List Drawing, Bitmap bmp, int width, Color color) { diff --git a/Paint_2/RectanglePencil.cs b/Paint_2/RectanglePencil.cs index 3edbb22..96ec7ad 100644 --- a/Paint_2/RectanglePencil.cs +++ b/Paint_2/RectanglePencil.cs @@ -17,6 +17,8 @@ namespace Paint_2 private List> _drawings; private List> _drawingsRedo; private bool _needsFullRefresh; + private bool _isShiftPressed; + private bool _isCtrlPressed; private PaintToolUtils Utils; private List _colors; private List _colorsRedo; @@ -36,6 +38,8 @@ namespace Paint_2 public Color Color { get => _color; set => _color = value; } public string Name { get => _name; set => _name = value; } public int Width { get => _width; set => _width = value; } + public bool IsShiftPressed { get => _isShiftPressed; set => _isShiftPressed = value; } + public bool IsCtrlPressed { get => _isCtrlPressed; set => _isCtrlPressed = value; } public RectanglePencil(string name) { @@ -51,13 +55,14 @@ namespace Paint_2 } public void Add(Point point) { + List myDrawing = Drawings[Drawings.Count - 1]; if (myDrawing.Count == 0 || myDrawing.Count == 1) { myDrawing.Add(point); } else - { + { myDrawing[1] = point; } Drawings[Drawings.Count - 1] = myDrawing; @@ -66,40 +71,21 @@ namespace Paint_2 { Graphics gr = Graphics.FromImage(canvas); int drawingCounter = 0; - Size pointSize; foreach (List drawing in Drawings) { if (drawing.Count == 2) { - Point p1 = drawing[0]; - Point p2 = drawing[1]; - - Point start = new Point(0,0); - Point end = new Point(0,0); - - if (p2.X > p1.X) + List points; + if (drawingCounter == Drawings.Count - 1) { - start.X = p1.X; - end.X = p2.X; + points = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, drawing[0], drawing[1]); } else { - start.X = p2.X; - end.X = p1.X; + points = Utils.ConvertRectangleIntoPositive(false, drawing[0], drawing[1]); } - if (p2.Y > p1.Y) - { - start.Y = p1.Y; - end.Y = p2.Y; - } - else - { - start.Y = p2.Y; - end.Y = p1.Y; - } - - gr.FillRectangle(new SolidBrush(Colors[drawingCounter]), new Rectangle(start, new Size(end.X - start.X, end.Y - start.Y))); + gr.FillRectangle(new SolidBrush(Colors[drawingCounter]), new Rectangle(points[0], new Size(points[1].X - points[0].X, points[1].Y - points[0].Y))); } drawingCounter++; } @@ -110,41 +96,25 @@ namespace Paint_2 string newLine = Environment.NewLine; int drawingCounter = 0; - Size pointSize; + foreach (List drawing in Drawings) { if (drawing.Count == 2) { - Point p1 = drawing[0]; - Point p2 = drawing[1]; - - Point start = new Point(0, 0); - Point end = new Point(0, 0); - - if (p2.X > p1.X) + List points; + if (drawingCounter == Drawings.Count -1) { - start.X = p1.X; - end.X = p2.X; + points = Utils.ConvertRectangleIntoPositive(IsCtrlPressed, drawing[0], drawing[1]); } else { - start.X = p2.X; - end.X = p1.X; - } - if (p2.Y > p1.Y) - { - start.Y = p1.Y; - end.Y = p2.Y; - } - else - { - start.Y = p2.Y; - end.Y = p1.Y; + points = Utils.ConvertRectangleIntoPositive(false, drawing[0], drawing[1]); } + Point start = points[0]; + Point end = points[1]; result += ""; result += newLine; - //gr.DrawRectangle(new Pen(Colors[drawingCounter], Widths[drawingCounter]), new Rectangle(start, new Size(end.X - start.X, end.Y - start.Y))); } drawingCounter++; } @@ -160,10 +130,8 @@ namespace Paint_2 } public void Stop() { - for (int i = 0; i < Drawings.Count; i++) - { - List Drawing = Drawings[i]; - } + List myDrawing = Drawings.Last(); + Drawings[Drawings.Count - 1] = Utils.ConvertRectangleIntoPositive(IsCtrlPressed,myDrawing[0], myDrawing[1]); } private Bitmap PostProcessing(List Drawing, Bitmap bmp, int width, Color color) { diff --git a/Paint_2/Sketch.cs b/Paint_2/Sketch.cs index 4f101b5..de11e66 100644 --- a/Paint_2/Sketch.cs +++ b/Paint_2/Sketch.cs @@ -143,6 +143,28 @@ namespace Paint_2 //Todo return Color.FromArgb(0x34, 0xF4, 0xFF); } + public void CtrlDown() + { + if (!CurrentTool.IsCtrlPressed) + { + CurrentTool.IsCtrlPressed = true; + } + } + public void CtrlUp() + { + CurrentTool.IsCtrlPressed = false; + } + public void ShiftDown() + { + if (!CurrentTool.IsShiftPressed) + { + CurrentTool.IsShiftPressed = true; + } + } + public void ShiftUp() + { + CurrentTool.IsShiftPressed = false; + } public override string ToString() { return Name;