using PaintDotNet.Base.SettingModel;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Runtime.Serialization;
using System.Windows.Forms;
using static PaintDotNet.Base.SettingModel.LabelStyleModel;
using PaintDotNet.Annotation.Enum;
using System.Collections.Generic;
namespace PaintDotNet.Annotation.Label
{
///
/// 标注->直线->直线
///
public class DrawLine : DrawObject
{
private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
///
/// 可能最后用不到
///
private const string entryStart = "Start";
///
/// 可能最后用不到
///
private const string entryEnd = "End";
///
/// Graphic objects for hit test
///
private GraphicsPath areaPath = null;
private Pen areaPen = null;
private Region areaRegion = null;
///
/// 标注样式信息model
///
public LineChildLine labelLineStyleModel;
public DrawLine(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
{
this.objectType = DrawClass.Label;
this.drawToolType = DrawToolType.DrawLine;
labelLineStyleModel = surfaceBox.GetLabelStyleModel().lineChildLine;
startPoint.X = x1;
startPoint.Y = y1;
endPoint.X = x2;
endPoint.Y = y2;
Initialize();
}
public DrawLine(ISurfaceBox surfaceBox, List points, ParentStyleModel parentStyleModel, Object content) : base()
{
this.objectType = DrawClass.Label;
this.drawToolType = DrawToolType.DrawLine;
this.ISurfaceBox = surfaceBox;
labelLineStyleModel = (LineChildLine)parentStyleModel;
startPoint.X = points[0].X;
startPoint.Y = points[0].Y;
endPoint.X = points[1].X;
endPoint.Y = points[1].Y;
}
///
/// Clone this instance
///
public override DrawObject Clone()
{
DrawLine drawLine = new DrawLine(ISurfaceBox, 0, 0, 1, 0);
drawLine.objectType = DrawClass.Label;
drawLine.drawToolType = DrawToolType.DrawLine;
drawLine.ISurfaceBox = this.ISurfaceBox;
drawLine.startPoint = this.startPoint;
drawLine.endPoint = this.endPoint;
drawLine.labelLineStyleModel = this.labelLineStyleModel;
FillDrawObjectFields(drawLine);
return drawLine;
}
public override DrawObject Clone(ISurfaceBox surfaceBox)
{
DrawLine drawLine = new DrawLine(surfaceBox, 0, 0, 1, 0);
drawLine.objectType = DrawClass.Label;
drawLine.drawToolType = DrawToolType.DrawLine;
drawLine.ISurfaceBox = surfaceBox;
drawLine.startPoint = this.startPoint;
drawLine.endPoint = this.endPoint;
drawLine.labelLineStyleModel = this.labelLineStyleModel;
FillDrawObjectFields(drawLine);
return drawLine;
}
public override void Draw(Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
Color color = Color.FromArgb(this.labelLineStyleModel.lineColor);
Pen pen = new Pen(color, this.labelLineStyleModel.lineWidth);
pen.DashStyle = (DashStyle)this.labelLineStyleModel.lineStyle;
g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
pen.Dispose();
}
public override int HandleCount
{
get
{
return 2;
}
}
///
/// Get handle pointscroll by 1-based number
///
///
///
public override PointF GetHandle(int handleNumber)
{
if (handleNumber == 1)
return startPoint;
else
return endPoint;
}
///
/// Hit test.
/// Return value: -1 - no hit
/// 0 - hit anywhere
/// > 1 - handle number
///
///
///
public override int HitTest(Point point)
{
if (Selected)
{
for (int i = 1; i <= HandleCount; i++)
{
if (GetHandleRectangle(i).Contains(point))
return i;
}
}
if (PointInObject(point))
return 0;
return -1;
}
protected override bool PointInObject(Point point)
{
CreateObjects();
return AreaRegion.IsVisible(point);
}
public override bool IntersectsWith(Rectangle rectangle)
{
CreateObjects();
return AreaRegion.IsVisible(rectangle);
}
public override Cursor GetHandleCursor(int handleNumber)
{
switch (handleNumber)
{
case 1:
case 2:
return handleCursor;// Cursors.SizeAll;
default:
return Cursors.Default;
}
}
public override void MoveHandleTo(Point point, int handleNumber)
{
if (handleNumber == 1)
startPoint = point;
else
endPoint = point;
Invalidate();
}
public override void Move(int deltaX, int deltaY)
{
int x = ISurfaceBox.UnscaleScalar(deltaX);
int y = ISurfaceBox.UnscaleScalar(deltaY);
startPoint.X += x;
startPoint.Y += y;
endPoint.X += x;
endPoint.Y += y;
Invalidate();
}
///
/// Invalidate object.
/// When object is invalidated, path used for hit test
/// is released and should be created again.
///
protected void Invalidate()
{
if (AreaPath != null)
{
AreaPath.Dispose();
AreaPath = null;
}
if (AreaPen != null)
{
AreaPen.Dispose();
AreaPen = null;
}
if (AreaRegion != null)
{
AreaRegion.Dispose();
AreaRegion = null;
}
}
///
/// Create graphic objects used from hit test.
///
protected virtual void CreateObjects()
{
if (AreaPath != null)
return;
// Create path which contains wide line
// for easy mouse selection
AreaPath = new GraphicsPath();
AreaPen = new Pen(Color.Black, 7);
AreaPath.AddLine(startPoint.X-1, startPoint.Y-1, endPoint.X+1, endPoint.Y+1);
AreaPath.Widen(AreaPen);
// Create region from the path
AreaRegion = new Region(AreaPath);
}
protected GraphicsPath AreaPath
{
get
{
return areaPath;
}
set
{
areaPath = value;
}
}
protected Pen AreaPen
{
get
{
return areaPen;
}
set
{
areaPen = value;
}
}
protected Region AreaRegion
{
get
{
return areaRegion;
}
set
{
areaRegion = value;
}
}
public override RectangleF GetBoundingBox()
{
RectangleF rectangle = GetNotmalizedRectangle(startPoint, endPoint);
return rectangle;
}
public static RectangleF GetNotmalizedRectangle(PointF a, PointF b)
{
RectangleF rect = new RectangleF();
if (a.X < b.X)
{
rect.X = a.X;
rect.Width = b.X - a.X;
}
else
{
rect.X = b.X;
rect.Width = a.X - b.X;
}
if (a.Y < b.Y)
{
rect.Y = a.Y;
rect.Height = b.Y - a.Y;
}
else
{
rect.Y = b.Y;
rect.Height = a.Y - b.Y;
}
return rect;
}
///
/// Draw tracker for selected object
///
///
public override void DrawTracker(Graphics g)
{
if (!Selected)
return;
SolidBrush brush = new SolidBrush(Color.FromArgb(MarkpointAreaColor));
Pen pen = new Pen(Color.FromArgb(MarkpointLineColor), MarkpointLineWidth);
for (int i = 1; i <= HandleCount; i++)
{
switch (MarkpointStyle)
{
case 0:
g.FillRectangle(brush, GetHandleRectangle(i));
g.DrawRectangle(pen, GetHandleRectangle(i));
break;
case 1:
g.FillEllipse(brush, GetHandleRectangle(i));
g.DrawEllipse(pen, GetHandleRectangle(i));
break;
case 2:
g.FillPolygon(brush, GetHandlePoint(i));
g.DrawPolygon(pen, GetHandlePoint(i));
break;
}
}
brush.Dispose();
pen.Dispose();
}
public override List GetPoints()
{
List points = new List();
points.Add(startPoint);
points.Add(endPoint);
return points;
}
public override ParentStyleModel GetStyle()
{
return labelLineStyleModel;
}
}
}