ShareX/ShareX.ScreenCaptureLib/Shapes/Drawing/LineDrawingShape.cs

236 lines
7.1 KiB
C#
Raw Normal View History

2016-05-06 05:03:57 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2017-01-11 21:39:40 +13:00
Copyright (c) 2007-2017 ShareX Team
2016-05-06 05:03:57 +12:00
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 <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2016-05-24 04:18:09 +12:00
using ShareX.HelpersLib;
2016-05-06 05:03:57 +12:00
using System.Drawing;
using System.Drawing.Drawing2D;
namespace ShareX.ScreenCaptureLib
{
public class LineDrawingShape : BaseDrawingShape
{
2017-08-03 03:32:51 +12:00
public const int MaximumCenterPointCount = 5;
2016-05-06 05:03:57 +12:00
public override ShapeType ShapeType { get; } = ShapeType.DrawingLine;
2017-11-16 22:40:56 +13:00
public Point[] Points { get; private set; }
public bool CenterNodeActive { get; private set; }
public int CenterPointCount { get; private set; }
2017-11-16 21:47:26 +13:00
public override bool IsValidShape => Rectangle.Width > 1 || Rectangle.Height > 1;
private void AdjustPoints(int centerPointCount)
{
Point[] newPoints = new Point[2 + centerPointCount];
if (Points != null)
{
newPoints[0] = Points[0];
newPoints[newPoints.Length - 1] = Points[Points.Length - 1];
}
for (int i = 0; i < newPoints.Length; i++)
{
if (newPoints[i] == null) newPoints[i] = new Point();
}
Points = newPoints;
}
2016-05-24 04:18:09 +12:00
2017-11-16 22:21:05 +13:00
private void AutoPositionCenterPoints()
{
if (!CenterNodeActive)
{
for (int i = 1; i < Points.Length - 1; i++)
{
Points[i] = new Point((int)MathHelpers.Lerp(Points[0].X, Points[Points.Length - 1].X, i / (CenterPointCount + 1f)),
(int)MathHelpers.Lerp(Points[0].Y, Points[Points.Length - 1].Y, i / (CenterPointCount + 1f)));
}
}
}
2017-08-03 03:32:51 +12:00
public override void OnConfigLoad()
{
base.OnConfigLoad();
2017-11-16 21:47:26 +13:00
int previousCenterPointCount = CenterPointCount;
CenterPointCount = AnnotationOptions.LineCenterPointCount.Between(0, MaximumCenterPointCount);
AdjustPoints(CenterPointCount);
if (CenterPointCount != previousCenterPointCount)
{
CenterNodeActive = false;
AutoPositionCenterPoints();
}
2017-08-03 03:32:51 +12:00
if (Manager.NodesVisible)
{
OnNodeVisible();
}
}
public override void OnConfigSave()
{
base.OnConfigSave();
AnnotationOptions.LineCenterPointCount = CenterPointCount;
}
public override void OnUpdate()
{
base.OnUpdate();
2017-11-16 21:47:26 +13:00
if (Manager.IsCreating)
{
2017-11-16 21:47:26 +13:00
Points[0] = StartPosition;
Points[Points.Length - 1] = EndPosition;
}
else
{
AutoPositionCenterPoints();
Rectangle = Points.CreateRectangle();
}
}
public override void OnDraw(Graphics g)
{
DrawLine(g);
}
protected void DrawLine(Graphics g)
2016-05-06 05:03:57 +12:00
{
2016-11-30 07:32:52 +13:00
if (Shadow)
{
2017-11-16 21:47:26 +13:00
Point[] shadowPoints = new Point[Points.Length];
2017-11-16 21:47:26 +13:00
for (int i = 0; i < shadowPoints.Length; i++)
{
2017-11-16 21:47:26 +13:00
shadowPoints[i] = Points[i].Add(ShadowOffset);
}
DrawLine(g, ShadowColor, BorderSize, shadowPoints);
2016-11-30 07:32:52 +13:00
}
2017-11-16 21:47:26 +13:00
DrawLine(g, BorderColor, BorderSize, Points);
2016-11-30 07:32:52 +13:00
}
2017-11-16 21:47:26 +13:00
protected void DrawLine(Graphics g, Color borderColor, int borderSize, Point[] points)
2016-11-30 07:32:52 +13:00
{
if (borderSize > 0 && borderColor.A > 0)
2016-05-06 05:03:57 +12:00
{
g.SmoothingMode = SmoothingMode.HighQuality;
2016-11-30 07:32:52 +13:00
if (borderSize.IsEvenNumber())
{
g.PixelOffsetMode = PixelOffsetMode.Half;
}
2016-11-30 07:32:52 +13:00
using (Pen pen = CreatePen(borderColor, borderSize))
2016-05-06 05:03:57 +12:00
{
2017-11-16 21:47:26 +13:00
if (CenterNodeActive && points.Length > 2)
2016-11-30 07:32:52 +13:00
{
2017-11-16 21:47:26 +13:00
g.DrawCurve(pen, points);
2016-11-30 07:32:52 +13:00
}
else
{
2017-11-16 21:47:26 +13:00
g.DrawLine(pen, points[0], points[points.Length - 1]);
2016-11-30 07:32:52 +13:00
}
2016-05-06 05:03:57 +12:00
}
g.SmoothingMode = SmoothingMode.None;
g.PixelOffsetMode = PixelOffsetMode.Default;
2016-05-06 05:03:57 +12:00
}
}
2016-11-30 07:32:52 +13:00
protected virtual Pen CreatePen(Color borderColor, int borderSize)
{
2016-11-30 07:32:52 +13:00
return new Pen(borderColor, borderSize)
{
2016-11-30 07:32:52 +13:00
StartCap = LineCap.Round,
EndCap = LineCap.Round,
LineJoin = LineJoin.Round
2016-11-30 07:32:52 +13:00
};
}
public override void Move(int x, int y)
{
2017-11-16 21:47:26 +13:00
for (int i = 0; i < Points.Length; i++)
{
2017-11-16 21:47:26 +13:00
Points[i] = Points[i].Add(x, y);
}
}
public override void Resize(int x, int y, bool fromBottomRight)
{
if (fromBottomRight)
{
2017-11-16 21:47:26 +13:00
Points[Points.Length - 1] = Points[Points.Length - 1].Add(x, y);
}
else
{
2017-11-16 21:47:26 +13:00
Points[0] = Points[0].Add(x, y);
}
}
2016-08-06 01:35:44 +12:00
public override void OnNodeVisible()
{
2017-11-16 22:21:05 +13:00
for (int i = 0; i < Manager.ResizeNodes.Length; i++)
{
2017-11-16 22:21:05 +13:00
Manager.ResizeNodes[i].Visible = i < Points.Length;
}
}
2016-08-06 01:35:44 +12:00
public override void OnNodeUpdate()
{
2017-11-16 21:47:26 +13:00
for (int i = 0; i < Points.Length; i++)
{
2017-11-16 21:47:26 +13:00
if (Manager.ResizeNodes[i].IsDragging)
{
2017-11-16 21:47:26 +13:00
Manager.IsResizing = true;
if (i > 0 && i < Points.Length - 1)
{
CenterNodeActive = true;
}
2017-11-16 21:47:26 +13:00
Points[i] = InputManager.ClientMousePosition;
}
}
2016-08-06 01:35:44 +12:00
}
2017-11-16 21:47:26 +13:00
public override void OnNodePositionUpdate()
{
for (int i = 0; i < Points.Length; i++)
{
2017-11-16 21:47:26 +13:00
Manager.ResizeNodes[i].Position = Points[i];
2017-05-12 19:36:20 +12:00
2017-11-16 22:21:05 +13:00
if (i < Points.Length - 1)
{
Manager.ResizeNodes[i].Visible = !Manager.ResizeNodes[i].Rectangle.IntersectsWith(Manager.ResizeNodes[Points.Length - 1].Rectangle);
}
}
2016-08-06 01:35:44 +12:00
}
2016-05-06 05:03:57 +12:00
}
}