From 84c3a977d670874a2583df837cb355d38fa0965d Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 15 Aug 2016 02:11:21 +0300 Subject: [PATCH] Adding freehand drawing support --- ShareX.ScreenCaptureLib/Enums.cs | 1 + .../Shapes/Drawing/FreehandDrawingShape.cs | 126 ++++++++++++++++++ .../Shapes/ShapeManager.cs | 4 + .../ShareX.ScreenCaptureLib.csproj | 1 + 4 files changed, 132 insertions(+) create mode 100644 ShareX.ScreenCaptureLib/Shapes/Drawing/FreehandDrawingShape.cs diff --git a/ShareX.ScreenCaptureLib/Enums.cs b/ShareX.ScreenCaptureLib/Enums.cs index 0cbf34302..d10c49a98 100644 --- a/ShareX.ScreenCaptureLib/Enums.cs +++ b/ShareX.ScreenCaptureLib/Enums.cs @@ -174,6 +174,7 @@ public enum ShapeType // Localized DrawingRectangle, DrawingRoundedRectangle, DrawingEllipse, + DrawingFreehand, DrawingLine, DrawingArrow, DrawingText, diff --git a/ShareX.ScreenCaptureLib/Shapes/Drawing/FreehandDrawingShape.cs b/ShareX.ScreenCaptureLib/Shapes/Drawing/FreehandDrawingShape.cs new file mode 100644 index 000000000..02e240559 --- /dev/null +++ b/ShareX.ScreenCaptureLib/Shapes/Drawing/FreehandDrawingShape.cs @@ -0,0 +1,126 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2016 ShareX Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using ShareX.HelpersLib; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Drawing2D; + +namespace ShareX.ScreenCaptureLib +{ + public class FreehandDrawingShape : BaseDrawingShape + { + public override ShapeType ShapeType { get; } = ShapeType.DrawingFreehand; + + public override bool ShowResizeNodes { get; } = false; + + public Point LastPosition + { + get + { + if (points.Count > 0) + { + return points[points.Count - 1]; + } + + return Point.Empty; + } + set + { + if (points.Count > 0) + { + points[points.Count - 1] = value; + } + } + } + + private List points = new List(); + private bool isPolygonMode; + + public override bool Intersects(Point position) + { + return false; + } + + public override void OnUpdate() + { + if (Manager.IsCreating) + { + if (Manager.IsCornerMoving) + { + Move(InputManager.MouseVelocity); + } + else + { + Point pos = InputManager.MousePosition0Based; + + if (points.Count == 0 || (!Manager.IsProportionalResizing && LastPosition != pos)) + { + points.Add(pos); + } + + if (Manager.IsProportionalResizing) + { + if (!isPolygonMode) + { + points.Add(pos); + } + + LastPosition = pos; + } + + isPolygonMode = Manager.IsProportionalResizing; + + Rectangle = points.CreateRectangle(); + } + } + else if (Manager.IsMoving) + { + Move(InputManager.MouseVelocity); + } + } + + public override void OnDraw(Graphics g) + { + if (points.Count > 1 && BorderSize > 0 && BorderColor.A > 0) + { + g.SmoothingMode = SmoothingMode.HighQuality; + + using (Pen pen = new Pen(BorderColor, BorderSize)) + using (GraphicsPath gp = new GraphicsPath()) + { + for (int i = 0; i < points.Count - 1; i++) + { + gp.AddLine(points[i], points[i + 1]); + } + + g.DrawPath(pen, gp); + } + + g.SmoothingMode = SmoothingMode.None; + } + } + } +} \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs index 35f3fabd8..2ad9a9d1b 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs @@ -1274,6 +1274,9 @@ private BaseShape CreateShape(ShapeType shapeType) case ShapeType.DrawingEllipse: shape = new EllipseDrawingShape(); break; + case ShapeType.DrawingFreehand: + shape = new FreehandDrawingShape(); + break; case ShapeType.DrawingLine: shape = new LineDrawingShape(); break; @@ -1381,6 +1384,7 @@ private void CheckHover() switch (CurrentShapeType) { case ShapeType.RegionFreehand: + case ShapeType.DrawingFreehand: case ShapeType.DrawingLine: case ShapeType.DrawingArrow: case ShapeType.DrawingStep: diff --git a/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj b/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj index 5b5a8815a..113513326 100644 --- a/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj +++ b/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj @@ -92,6 +92,7 @@ +