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 PaintDotNet.Base.CommTool;
using PaintDotNet.Annotation.Enum;
using System.Collections.Generic;
using PaintDotNet.Annotation.Measure;
namespace PaintDotNet.Annotation.Other
{
///
/// 光密度测量直线
///
public class OpticalDensityLine : MeasureDrawObject
{
///
/// 可能最后用不到
///
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 MeasureStyleModel.MeasureLine measureLineStyleModel;
public OpticalDensityLine(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
{
this.objectType = DrawClass.Other;
this.drawToolType = DrawToolType.OpticalDensityLine;
measureLineStyleModel = surfaceBox.GetMeasureStyleModel().measureLine;
startPoint.X = x1;
startPoint.Y = y1;
endPoint.X = x2;
endPoint.Y = y2;
Initialize();
}
public OpticalDensityLine(ISurfaceBox surfaceBox, List points, ParentStyleModel parentStyleModel, Object content) : base()
{
this.objectType = DrawClass.Other;
this.drawToolType = DrawToolType.OpticalDensityLine;
this.ISurfaceBox = surfaceBox;
measureLineStyleModel = (MeasureStyleModel.MeasureLine)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()
{
OpticalDensityLine opticalDensityLine = new OpticalDensityLine(ISurfaceBox, 0, 0, 1, 0);
opticalDensityLine.objectType = DrawClass.Label;
opticalDensityLine.drawToolType = DrawToolType.OpticalDensityLine;
opticalDensityLine.ISurfaceBox = this.ISurfaceBox;
opticalDensityLine.startPoint = this.startPoint;
opticalDensityLine.endPoint = this.endPoint;
FillDrawObjectFields(opticalDensityLine);
return opticalDensityLine;
}
public override void Draw(Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
Color color = Color.FromArgb(this.measureLineStyleModel.lineColor);
Pen pen = new Pen(color, this.measureLineStyleModel.lineWidth);
pen.DashStyle = (DashStyle)this.measureLineStyleModel.lineStyle;
double length = BasicCalculationHelper.GetDistance(this.startPoint, this.endPoint, 2);
double angle = Math.Round(BasicCalculationHelper.AngleText(this.startPoint, this.endPoint, new PointF(this.startPoint.X + 10, this.startPoint.Y)), 10);
//判断第二个点相对于第一个点的象限
int x2 = (int)(this.endPoint.X - this.startPoint.X);
if (x2 == 0)
x2 = 1;
int y2 = (int)(this.endPoint.Y - this.startPoint.Y);
if (y2 == 0)
y2 = 1;
int i2 = 0;
if (x2 > 0 && y2 > 0) //第4象限
{
i2 = 4;
}
else if (x2 > 0 && y2 < 0) //第1象限
{
i2 = 1;
}
else if (x2 < 0 && y2 < 0) //第2象限
{
i2 = 2;
}
else if (x2 < 0 && y2 > 0) //第3象限
{
i2 = 3;
}
double sAngle1;
double eAngle1;
double sAngle2;
double eAngle2;
if (i2 == 1 || i2 == 2)
{
sAngle1 = 360 - angle;
eAngle1 = 180 - angle;
sAngle2 = 270 - angle;
eAngle2 = 90 - angle;
}
else
{
sAngle1 = angle;
eAngle1 = 180 + angle;
sAngle2 = 270 + angle;
eAngle2 = 90 + angle;
}
Point point1 = new Point();
Point point2 = new Point();
Point point3 = new Point();
Point point4 = new Point();
Point point5 = new Point();
Point point6 = new Point();
if (sAngle1 is double.NaN && eAngle1 is double.NaN && sAngle2 is double.NaN && eAngle2 is double.NaN)
{
}
else
{
point1 = BasicCalculationHelper.GetAnglePoint(new Point((int)this.startPoint.X + (int)(length * kS), (int)this.startPoint.Y), new Point((int)this.startPoint.X, (int)this.startPoint.Y), sAngle1);
point2 = BasicCalculationHelper.GetAnglePoint(new Point((int)this.endPoint.X + (int)(length * kE), (int)this.endPoint.Y), new Point((int)this.endPoint.X, (int)this.endPoint.Y), eAngle1);
point3 = BasicCalculationHelper.GetAnglePoint(new Point((point1.X + 20), point1.Y), point1, sAngle2);
point4 = BasicCalculationHelper.GetAnglePoint(new Point((point1.X + 20), point1.Y), point1, eAngle2);
point5 = BasicCalculationHelper.GetAnglePoint(new Point((point2.X + 20), point2.Y), point2, sAngle2);
point6 = BasicCalculationHelper.GetAnglePoint(new Point((point2.X + 20), point2.Y), point2, eAngle2);
}
g.DrawLine(pen, point3, point4);
g.DrawLine(pen, point5, point6);
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();
}
public override void SaveToStream(SerializationInfo info, int orderNumber)
{
info.AddValue(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryStart, orderNumber),
startPoint);
info.AddValue(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryEnd, orderNumber),
endPoint);
base.SaveToStream(info, orderNumber);
}
public override void LoadFromStream(SerializationInfo info, int orderNumber)
{
startPoint = (Point)info.GetValue(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryStart, orderNumber),
typeof(Point));
endPoint = (Point)info.GetValue(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryEnd, orderNumber),
typeof(Point));
base.LoadFromStream(info, orderNumber);
}
///
/// 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.Black);
for (int i = 1; i <= HandleCount; i++)
{
g.FillRectangle(brush, GetHandleRectangle(i));
}
brush.Dispose();
}
public override List GetPoints()
{
List points = new List();
points.Add(startPoint);
points.Add(endPoint);
return points;
}
public override ParentStyleModel GetStyle()
{
return measureLineStyleModel;
}
}
}