From 9b4cfb33bdc841b698c8fd7ba58892cabac36303 Mon Sep 17 00:00:00 2001 From: maxluli Date: Mon, 20 Jun 2022 14:02:17 +0200 Subject: [PATCH] Added support for SVG painting for all the current pencils --- Paint_2/BezierPencil.cs | 74 ++++++++++++++++++++++++++++---- Paint_2/EllipseBorderPencil.cs | 53 ++++++++++++++++++++--- Paint_2/EllipsePencil.cs | 51 +++++++++++++++++++--- Paint_2/RectangleBorderPencil.cs | 50 ++++++++++++++++++--- Paint_2/RectanglePencil.cs | 51 +++++++++++++++++++--- Paint_2/Sketch.cs | 16 +++---- 6 files changed, 256 insertions(+), 39 deletions(-) diff --git a/Paint_2/BezierPencil.cs b/Paint_2/BezierPencil.cs index 73c36a6..50b9a9a 100644 --- a/Paint_2/BezierPencil.cs +++ b/Paint_2/BezierPencil.cs @@ -40,7 +40,7 @@ namespace Paint_2 public BezierPencil(string name) { - NeedsFullRefresh = false; + NeedsFullRefresh = true; Drawings = new List>(); DrawingsRedo = new List>(); Colors = new List(); @@ -86,11 +86,11 @@ namespace Paint_2 { pointSize = new Size(Widths[0]/2, Widths[0]/2); gr.FillEllipse(new SolidBrush(Colors[i]), new Rectangle(new Point(points[i].X - pointSize.Width / 2, points[i].Y - pointSize.Height / 2), pointSize)); - ContinuousBezierGenerator(gr, points, i, Colors[Colors.Count-1], Widths[Widths.Count -1]); + ContinuousBezierGenerator(gr, points, Colors[Colors.Count-1], Widths[Widths.Count -1]); } } } - private void ContinuousBezierGenerator(Graphics gr, List points, int i,Color color,int width) + private void ContinuousBezierGenerator(Graphics gr, List points,Color color,int width) { if (points.Count >= 4 && points.Count % 2 == 0) { @@ -120,6 +120,69 @@ namespace Paint_2 } } } + public string PaintSVG() + { + string result = ""; + string newLine = Environment.NewLine; + + Size pointSize; + List points = new List(); + foreach (List drawing in Drawings) + { + if (drawing.Count > 0) + { + points.Add(drawing[0]); + } + } + if (points.Count > 0) + { + for (int i = 0; i < points.Count; i++) + { + pointSize = new Size(Widths[0] / 2, Widths[0] / 2); + //Not really usefull in the SVG world because only the last layer will be displayed + //result += ""; + //result += newLine; + result += ContinuousBezierGeneratorSVG(points, Colors[Colors.Count - 1], Widths[Widths.Count - 1]); + } + } + return result; + } + private string ContinuousBezierGeneratorSVG(List points, Color color, int width) + { + string result = ""; + string newLine = Environment.NewLine; + + if (points.Count >= 4 && points.Count % 2 == 0) + { + float precision = 0.01f; + for (float t = 0; t <= 1; t += precision) + { + List DumpList = new List(); + List WorkingList = new List(points); + + while (WorkingList.Count != 1) + { + if (WorkingList.Count > 1) + { + for (int pos = 0; pos < WorkingList.Count - 1; pos++) + { + DumpList.Add(Utils.Lerp(WorkingList[pos], WorkingList[pos + 1], t)); + } + } + else + { + DumpList.Add(Utils.Lerp(WorkingList[0], WorkingList[1], precision)); + } + WorkingList = new List(DumpList); + DumpList = new List(); + } + result += ""; + result += newLine; + //gr.FillEllipse(new SolidBrush(color), new Rectangle(WorkingList[0].X - Widths[0] / 2, WorkingList[0].Y - Widths[0] / 2, width, width)); + } + } + return result; + } private void BezierGenerator(Graphics gr, Point p1, Point p2, Point p3, Point p4, int i) { gr.DrawLine(new Pen(Colors[i - 4], Widths[i - 4]), p1, p2); @@ -182,10 +245,5 @@ namespace Paint_2 result.Color = Color; return (Object)(result); } - - public string PaintSVG() - { - throw new NotImplementedException(); - } } } diff --git a/Paint_2/EllipseBorderPencil.cs b/Paint_2/EllipseBorderPencil.cs index ebfa8be..1026d0e 100644 --- a/Paint_2/EllipseBorderPencil.cs +++ b/Paint_2/EllipseBorderPencil.cs @@ -13,7 +13,7 @@ using System.Drawing; namespace Paint_2 { - internal class EllipseBorderPencil:PaintTool + internal class EllipseBorderPencil : PaintTool { private List> _drawings; private List> _drawingsRedo; @@ -63,6 +63,52 @@ namespace Paint_2 } Drawings[Drawings.Count - 1] = myDrawing; } + public string PaintSVG() + { + string result = ""; + 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) + { + start.X = p1.X; + end.X = p2.X; + } + 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; + } + Size size = new Size((end.X - start.X) / 2, (end.Y - start.Y) / 2); + result += ""; + result += newLine; + } + drawingCounter++; + } + return result; + } public void Paint(Bitmap canvas) { Graphics gr = Graphics.FromImage(canvas); @@ -148,10 +194,5 @@ namespace Paint_2 result.Color = Color; return (Object)(result); } - - public string PaintSVG() - { - throw new NotImplementedException(); - } } } diff --git a/Paint_2/EllipsePencil.cs b/Paint_2/EllipsePencil.cs index 4778b98..fc0baa5 100644 --- a/Paint_2/EllipsePencil.cs +++ b/Paint_2/EllipsePencil.cs @@ -104,6 +104,52 @@ namespace Paint_2 drawingCounter++; } } + public string PaintSVG() + { + string result = ""; + 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) + { + start.X = p1.X; + end.X = p2.X; + } + 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; + } + Size size = new Size((end.X - start.X) / 2, (end.Y - start.Y) / 2); + result += ""; + result += newLine; + } + drawingCounter++; + } + return result; + } public void Start(Color color, int width) { Utils.StandartStart(color, width); @@ -149,10 +195,5 @@ namespace Paint_2 result.Color = Color; return (Object)(result); } - - public string PaintSVG() - { - throw new NotImplementedException(); - } } } diff --git a/Paint_2/RectangleBorderPencil.cs b/Paint_2/RectangleBorderPencil.cs index 130e98a..2b2f5ef 100644 --- a/Paint_2/RectangleBorderPencil.cs +++ b/Paint_2/RectangleBorderPencil.cs @@ -104,6 +104,51 @@ namespace Paint_2 drawingCounter++; } } + public string PaintSVG() + { + string result = ""; + 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) + { + start.X = p1.X; + end.X = p2.X; + } + 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; + } + result += ""; + result += newLine; + } + drawingCounter++; + } + return result; + } public void Start(Color color, int width) { Utils.StandartStart(color, width); @@ -149,11 +194,6 @@ namespace Paint_2 result.Color = Color; return (Object)(result); } - - public string PaintSVG() - { - throw new NotImplementedException(); - } } } diff --git a/Paint_2/RectanglePencil.cs b/Paint_2/RectanglePencil.cs index 22ccfe2..3edbb22 100644 --- a/Paint_2/RectanglePencil.cs +++ b/Paint_2/RectanglePencil.cs @@ -104,6 +104,52 @@ namespace Paint_2 drawingCounter++; } } + public string PaintSVG() + { + string result = ""; + 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) + { + start.X = p1.X; + end.X = p2.X; + } + 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; + } + 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++; + } + return result; + } public void Start(Color color, int width) { Utils.StandartStart(color, width); @@ -149,10 +195,5 @@ namespace Paint_2 result.Color = Color; return (Object)(result); } - - public string PaintSVG() - { - throw new NotImplementedException(); - } } } diff --git a/Paint_2/Sketch.cs b/Paint_2/Sketch.cs index ca94d16..4f101b5 100644 --- a/Paint_2/Sketch.cs +++ b/Paint_2/Sketch.cs @@ -30,7 +30,7 @@ namespace Paint_2 public string Name { get => _name; set => _name = value; } public string Id { get => id; set => id = value; } - public Sketch(string name,Size sketchSize, List toolList,string guid) + public Sketch(string name, Size sketchSize, List toolList, string guid) { IsDrawing = false; Id = guid; @@ -52,7 +52,7 @@ namespace Paint_2 CurrentTool = null; } } - public Sketch(List toolList,string guid) : this("Layer 1",new Size(500, 500), toolList,guid) + public Sketch(List toolList, string guid) : this("Layer 1", new Size(500, 500), toolList, guid) { //empty } @@ -123,13 +123,9 @@ namespace Paint_2 foreach (PaintTool tool in ToolList) { - //[DEBUG] REMOVE THIS IF AFTER TESTING PLEASE - if (tool is Pencil || tool is DotPencil) - { - result += ""; - result += newLine; - result += tool.PaintSVG(); - } + result += ""; + result += newLine; + result += tool.PaintSVG(); } return result; } @@ -148,7 +144,7 @@ namespace Paint_2 return Color.FromArgb(0x34, 0xF4, 0xFF); } public override string ToString() - { + { return Name; } }