123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Globalization;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Windows.Forms;
- using PaintDotNet.Annotation.Enum;
- using PaintDotNet.Base.SettingModel;
- namespace PaintDotNet.Annotation.Label
- {
- /// <summary>
- /// 标注->直线->线段
- /// </summary>
- public class DrawLineSegment : DrawObject
- {
- private const string entryStart = "Start";
- private const string entryEnd = "End";
- /// <summary>
- /// Graphic objects for hit test
- /// </summary>
- private GraphicsPath areaPath = null;
- private Pen areaPen = null;
- private Region areaRegion = null;
- // Create the outline for our custom end cap.
- GraphicsPath hPath = new GraphicsPath();
- // Construct the hook-shaped end cap.
- CustomLineCap HookCap;
- /// <summary>
- /// 标注样式信息model
- /// </summary>
- private LabelStyleModel.LineChildLineSegment labelLineSegment;
- public LabelStyleModel.LineChildLineSegment LabelLineSegment
- {
- set
- {
- this.labelLineSegment = value;
- }
- }
- public DrawLineSegment(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
- {
- this.objectType = DrawClass.Label;
- this.drawToolType = DrawToolType.DrawLineSegment;
- labelLineSegment = surfaceBox.GetLabelStyleModel().lineChildLineSegment;
- startPoint.X = x1;
- startPoint.Y = y1;
- endPoint.X = x2;
- endPoint.Y = y2;
-
- //自定义线形的线帽
- hPath.AddLine(new PointF(-10, 0), new PointF(10, 0));
- HookCap = new CustomLineCap(null, hPath);
- HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
- /*//求出亮点之间的距离
- int xy = (startPoint.X - endPoint.X) * (startPoint.X - endPoint.X) + (startPoint.Y - endPoint.Y) * (startPoint.Y - endPoint.Y);
- int k = (int)Math.Sqrt(xy);
- //没有则创建这个文件
- StreamWriter sw = File.AppendText("e:\\temp\\testtxt.txt");
- sw.Write(DateTime.Now + " : " + xy + "\r\n");
- sw.Flush();
- sw.Close();
- sw.Dispose();*/
- Initialize();
- }
- public DrawLineSegment(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
- {
- this.objectType = DrawClass.Label;
- this.drawToolType = DrawToolType.DrawLineSegment;
- this.ISurfaceBox = surfaceBox;
- labelLineSegment = (LabelStyleModel.LineChildLineSegment)parentStyleModel;
- startPoint.X = points[0].X;
- startPoint.Y = points[0].Y;
- endPoint.X = points[1].X;
- endPoint.Y = points[1].Y;
- hPath.AddLine(new PointF(-10, 0), new PointF(10, 0));
- HookCap = new CustomLineCap(null, hPath);
- HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
- }
- /// <summary>
- /// Clone this instance
- /// </summary>
- public override DrawObject Clone()
- {
- DrawLineSegment drawLineSegment = new DrawLineSegment(ISurfaceBox, 0, 0, 1, 0);
- drawLineSegment.objectType = DrawClass.Label;
- drawLineSegment.drawToolType = DrawToolType.DrawLineSegment;
- drawLineSegment.ISurfaceBox = this.ISurfaceBox;
- drawLineSegment.startPoint = this.startPoint;
- drawLineSegment.endPoint = this.endPoint;
- drawLineSegment.hPath.AddLine(new PointF(-10, 0), new PointF(10, 0));
- drawLineSegment.HookCap = new CustomLineCap(null, hPath);
- drawLineSegment.HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
- FillDrawObjectFields(drawLineSegment);
- return drawLineSegment;
- }
- public override DrawObject Clone(ISurfaceBox surfaceBox)
- {
- DrawLineSegment drawLineSegment = new DrawLineSegment(surfaceBox, 0, 0, 1, 0);
- drawLineSegment.objectType = DrawClass.Label;
- drawLineSegment.drawToolType = DrawToolType.DrawLineSegment;
- drawLineSegment.ISurfaceBox = surfaceBox;
- drawLineSegment.startPoint = this.startPoint;
- drawLineSegment.endPoint = this.endPoint;
- drawLineSegment.hPath.AddLine(new PointF(-10, 0), new PointF(10, 0));
- drawLineSegment.HookCap = new CustomLineCap(null, hPath);
- drawLineSegment.HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
- drawLineSegment.labelLineSegment = this.labelLineSegment;
- FillDrawObjectFields(drawLineSegment);
- return drawLineSegment;
- }
- public override void Draw(Graphics g)
- {
- g.SmoothingMode = SmoothingMode.AntiAlias;
- Color color = Color.FromArgb(this.labelLineSegment.lineColor);
- Pen pen = new Pen(color, this.labelLineSegment.lineWidth);
- pen.DashStyle = (DashStyle)this.labelLineSegment.lineStyle;
- pen.CustomStartCap = HookCap;
- pen.CustomEndCap = HookCap;
- g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
- pen.Dispose();
-
-
- }
- public override int HandleCount
- {
- get
- {
- return 2;
- }
- }
- /// <summary>
- /// Get handle pointscroll by 1-based number
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- public override PointF GetHandle(int handleNumber)
- {
- if (handleNumber == 1)
- return startPoint;
- else
- return endPoint;
- }
- /// <summary>
- /// Hit test.
- /// Return value: -1 - no hit
- /// 0 - hit anywhere
- /// > 1 - handle number
- /// </summary>
- /// <param name="pointscroll"></param>
- /// <returns></returns>
- 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 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(System.Runtime.Serialization.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);
- }
- /// <summary>
- /// Invalidate object.
- /// When object is invalidated, path used for hit test
- /// is released and should be created again.
- /// </summary>
- protected void Invalidate()
- {
- if (AreaPath != null)
- {
- AreaPath.Dispose();
- AreaPath = null;
- }
- if (AreaPen != null)
- {
- AreaPen.Dispose();
- AreaPen = null;
- }
- if (AreaRegion != null)
- {
- AreaRegion.Dispose();
- AreaRegion = null;
- }
- }
- /// <summary>
- /// Create graphic objects used from hit test.
- /// </summary>
- 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;
- }
- /// <summary>
- /// Draw tracker for selected object
- /// </summary>
- /// <param name="g"></param>
- 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<PointF> GetPoints()
- {
- List<PointF> points = new List<PointF>();
- points.Add(startPoint);
- points.Add(endPoint);
- return points;
- }
- public override ParentStyleModel GetStyle()
- {
- return labelLineSegment;
- }
- }
- }
|