using PaintDotNet.Base.SettingModel;
using PaintDotNet.SettingModel;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Runtime.Serialization;
using System.Windows.Forms;
//using PaintDotNet.Startup;
using PaintDotNet.CommTool;
using System.IO;
using static PaintDotNet.SettingModel.LabelStyleModel;
using PaintDotNet.Annotation.Enum;
using System.Collections.Generic;
using System.Drawing.Imaging;
using PaintDotNet.Base.CommTool;
using System.ComponentModel;
namespace PaintDotNet.Annotation.Other
{
///
/// 标注->手动标尺
///
public class HandModeRulerDraw : DrawObject
{
private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
///
/// 标注样式信息model
///
public RulerModel rulerModel;
public int pixelLength = 1;//标尺像素长度
private int[] lengthEnum = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000};
private PointF centerPoint;
private string text;
public HandModeRulerDraw(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
{
this.objectType = DrawClass.Other;
this.drawToolType = DrawToolType.HandModeRulerDraw;
rulerModel = surfaceBox.GetRulerStyleModel();
startPoint.X = x1;
startPoint.Y = y1;
endPoint.X = x2;
endPoint.Y = y2;
}
public HandModeRulerDraw(ISurfaceBox surfaceBox, List points, ParentStyleModel parentStyleModel, Object content) : base()
{
this.objectType = DrawClass.Other;
this.drawToolType = DrawToolType.HandModeRulerDraw;
this.ISurfaceBox = surfaceBox;
rulerModel = (RulerModel)parentStyleModel;
startPoint = points[0];
endPoint = points[1];
centerPoint = points[2];
pixelLength = Convert.ToInt32(content.ToString().Split(',')[0]);
text = content.ToString().Split(',')[1];
}
///
/// Clone this instance
///
public override DrawObject Clone()
{
HandModeRulerDraw handModeRulerDraw = new HandModeRulerDraw(ISurfaceBox, 0, 0, 1, 0);
handModeRulerDraw.objectType = DrawClass.Other;
handModeRulerDraw.drawToolType = DrawToolType.HandModeRulerDraw;
handModeRulerDraw.ISurfaceBox = this.ISurfaceBox;
handModeRulerDraw.startPoint = this.startPoint;
handModeRulerDraw.endPoint = this.endPoint;
handModeRulerDraw.centerPoint = this.centerPoint;
handModeRulerDraw.pixelLength = this.pixelLength;
handModeRulerDraw.text = this.text;
FillDrawObjectFields(handModeRulerDraw);
return handModeRulerDraw;
}
public override void Draw(Graphics g)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
if (text != null && text != "")
{
DrawRulerHelper.drawRuler(this.rulerModel, g, centerPoint, pixelLength, text, out rectangle);
}
}
public void getAllData()
{
Dictionary measurementUnitDictionary = this.ISurfaceBox.getMeasureInfo();
double micronRatio = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少微米长度
pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度
//判断像素长度所属区间并重新赋值
if (pixelLength <= lengthEnum[0])
pixelLength = lengthEnum[0];
else if (pixelLength > lengthEnum[0] && pixelLength <= lengthEnum[1])
pixelLength = lengthEnum[1];
else if (pixelLength > lengthEnum[1] && pixelLength <= lengthEnum[2])
pixelLength = lengthEnum[2];
else if (pixelLength > lengthEnum[2] && pixelLength <= lengthEnum[3])
pixelLength = lengthEnum[3];
else if (pixelLength > lengthEnum[3] && pixelLength <= lengthEnum[4])
pixelLength = lengthEnum[4];
else if (pixelLength > lengthEnum[4] && pixelLength <= lengthEnum[5])
pixelLength = lengthEnum[5];
else if (pixelLength > lengthEnum[5] && pixelLength <= lengthEnum[6])
pixelLength = lengthEnum[6];
else if (pixelLength > lengthEnum[6] && pixelLength <= lengthEnum[7])
pixelLength = lengthEnum[7];
else if (pixelLength > lengthEnum[7] && pixelLength <= lengthEnum[8])
pixelLength = lengthEnum[8];
else if (pixelLength > lengthEnum[8] && pixelLength <= lengthEnum[9])
pixelLength = lengthEnum[9];
else if (pixelLength > lengthEnum[9] && pixelLength <= lengthEnum[10])
pixelLength = lengthEnum[10];
else
pixelLength = lengthEnum[11];
double lineLength = pixelLength * micronRatio;//物理长度
SizeF windowSize = this.ISurfaceBox.GetDocumentSize();//窗体尺寸
//线样式
Pen linePen = new Pen(Color.FromArgb(this.rulerModel.lineColor));
linePen.Width = (int)this.rulerModel.lineWidth;
//边框样式
Pen borderPen = new Pen(Color.FromArgb(this.rulerModel.borderColor));
borderPen.Width = (int)this.rulerModel.borderWidth;
int lineWidthOffset = (int)this.rulerModel.lineWidth / 2 - 1;
int offset = borderPen.Width > 0 ? (int)(borderPen.Width / 2) - 2 : 0;
//背景大小
decimal backLength = pixelLength * this.rulerModel.backgroundSize / 100;
//垂线
decimal verticalLength = pixelLength * this.rulerModel.verticalLineLength / 100;
//文字
text = lineLength + "μm";
decimal textHeight = pixelLength * this.rulerModel.textHeight / 100;
Font textFont = new Font(this.rulerModel.textFont, (float)this.rulerModel.textFontSize);
int fontNewHeight = lineWidthOffset + (int)(textHeight + new Decimal(textFont.Height));
int abc = fontNewHeight > ((int)verticalLength / 2 - 2) ? fontNewHeight : ((int)verticalLength / 2 - 2);
//标尺最终尺寸
int rulerWidth = pixelLength + (int)(backLength * 2) + (int)(linePen.Width * 2) + offset * 2;
int rulerHeight = (int)verticalLength / 2 + abc + (int)(backLength * 2) + lineWidthOffset + 2 + offset * 2;
int rulerPosition = this.rulerModel.rulerPosition;//标尺位置
decimal rulerMargin = this.rulerModel.rulerMargin / 100;//标尺边距
float marginLR = (float)rulerMargin * windowSize.Width;//左右边距
float marginTB = (float)rulerMargin * windowSize.Height;//上下边距
float offsetBorder = borderPen.Width > 0 ? borderPen.Width / 2 : 0;
switch (rulerPosition)
{
case 1://左上
centerPoint.X = rulerWidth / 2f - lineWidthOffset + offsetBorder + marginLR;
centerPoint.Y = fontNewHeight + (float)backLength - lineWidthOffset + offsetBorder + marginTB;
break;
case 2://右上
centerPoint.X = windowSize.Width - (rulerWidth / 2f - lineWidthOffset + offsetBorder) - marginLR;
centerPoint.Y = fontNewHeight + (float)backLength - lineWidthOffset + offsetBorder + marginTB;
break;
case 3://左下
centerPoint.X = rulerWidth / 2f - lineWidthOffset + offsetBorder + marginLR;
centerPoint.Y = windowSize.Height - (rulerHeight - (fontNewHeight + (float)backLength)) - offsetBorder - marginTB;
break;
case 4://右下
centerPoint.X = windowSize.Width - (rulerWidth / 2f - lineWidthOffset + offsetBorder) - marginLR;
centerPoint.Y = windowSize.Height - (rulerHeight - (fontNewHeight + (float)backLength)) - offsetBorder - marginTB;
break;
}
}
public override int HandleCount
{
get
{
return 1;
}
}
///
/// Get handle pointscroll by 1-based number
///
///
///
public override PointF GetHandle(int handleNumber)
{
return new PointF(rectangle.X, rectangle.Y);
}
///
/// 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)
{
return rectangle.Contains(point);
}
public override bool IntersectsWith(Rectangle rectangle)
{
return Rectangle.IntersectsWith(rectangle);
}
public override Cursor GetHandleCursor(int handleNumber)
{
return Cursors.Default;
}
public override void MoveHandleTo(Point point, int handleNumber)
{
if (handleNumber == 2)
{
endPoint.X = point.X;
getAllData();
}
}
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;
centerPoint.X += x;
centerPoint.Y += y;
rectangle.X += x;
rectangle.Y += y;
}
public override RectangleF GetBoundingBox()
{
return rectangle;
}
public override List GetPoints()
{
List points = new List();
points.Add(startPoint);
points.Add(endPoint);
points.Add(centerPoint);
return points;
}
public override ParentStyleModel GetStyle()
{
return rulerModel;
}
public override string GetContent()
{
return pixelLength.ToString() + "," + text;
}
}
}