123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- using PaintDotNet.Annotation.Enum;
- using PaintDotNet.Base.SettingModel;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Globalization;
- using System.Runtime.Serialization;
- using System.Windows.Forms;
- namespace PaintDotNet.Annotation.Label
- {
- using PointEnumerator = IEnumerator<PointF>;
- using PointList = List<PointF>;
- /// <summary>
- /// 标注->曲线->铅笔
- /// </summary>
- public class DrawPencil : DrawObject
- {
- private PointList pointArray;
- private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
- private GraphicsPath areaPath = null;
- private Pen areaPen = null;
- private Region areaRegion = null;
- /// <summary>
- /// 标注样式信息model
- /// </summary>
- private LabelStyleModel.Pencil labelStyleModel;
- public LabelStyleModel.Pencil LabelStyleModel
- {
- set
- {
- this.labelStyleModel = value;
- }
- }
- public DrawPencil() : base()
- {
- this.objectType = DrawClass.Label;
- this.drawToolType = DrawToolType.DrawPencil;
- pointArray = new PointList();
- Initialize();
- }
- public DrawPencil(ISurfaceBox surfaceBox, int x1, int y1) : base()
- {
- this.objectType = DrawClass.Label;
- this.drawToolType = DrawToolType.DrawPencil;
- labelStyleModel = surfaceBox.GetLabelStyleModel().pencilModel;
- pointArray = new PointList();
- pointArray.Add(new PointF(x1, y1));
- Initialize();
- }
- public DrawPencil(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
- {
- this.objectType = DrawClass.Label;
- this.drawToolType = DrawToolType.DrawPencil;
- this.ISurfaceBox = surfaceBox;
- LabelStyleModel = (LabelStyleModel.Pencil)parentStyleModel;
- pointArray = points;
- }
- /// <summary>
- /// Clone this instance
- /// </summary>
- public override DrawObject Clone()
- {
- DrawPencil drawPencil = new DrawPencil();
- drawPencil.objectType = DrawClass.Label;
- drawPencil.drawToolType = DrawToolType.DrawPencil;
- drawPencil.ISurfaceBox = this.ISurfaceBox;
- drawPencil.labelStyleModel = this.labelStyleModel;
- foreach (PointF p in this.pointArray)
- {
- drawPencil.pointArray.Add(p);
- }
- FillDrawObjectFields(drawPencil);
- return drawPencil;
- }
- public override DrawObject Clone(ISurfaceBox surfaceBox)
- {
- DrawPencil drawPencil = new DrawPencil();
- drawPencil.objectType = DrawClass.Label;
- drawPencil.drawToolType = DrawToolType.DrawPencil;
- drawPencil.ISurfaceBox = surfaceBox;
- drawPencil.labelStyleModel = this.labelStyleModel;
- foreach (PointF p in this.pointArray)
- {
- drawPencil.pointArray.Add(p);
- }
- FillDrawObjectFields(drawPencil);
- return drawPencil;
- }
- public override void Draw(Graphics g)
- {
- if (HandleCount >= 3)
- {
- g.SmoothingMode = SmoothingMode.AntiAlias;
- // 从配置内取值 颜色、线宽、线样式
- Color color = Color.FromArgb(labelStyleModel.lineColor);
- int lineWidth = labelStyleModel.lineWidth;
- Pen pen = new Pen(color, lineWidth);
- pen.DashStyle = (DashStyle)this.labelStyleModel.lineStyle;
- g.DrawCurve(pen, pointArray.ToArray());
- pen.Dispose();
- }
- }
- public void AddPoint(PointF point)
- {
- pointArray.Add(point);
- }
- public override int HandleCount
- {
- get
- {
- return pointArray.Count;
- }
- }
- /// <summary>
- /// Get handle pointscroll by 1-based number
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- public override PointF GetHandle(int handleNumber)
- {
- if (handleNumber < 1)
- handleNumber = 1;
- if (handleNumber > pointArray.Count)
- handleNumber = pointArray.Count;
- return pointArray[handleNumber - 1];
- }
- public override Cursor GetHandleCursor(int handleNumber)
- {
- return handleCursor;
- }
- public override void MoveHandleTo(Point point, int handleNumber)
- {
- if (handleNumber < 1)
- handleNumber = 1;
- if (handleNumber > pointArray.Count)
- handleNumber = pointArray.Count;
- pointArray[handleNumber - 1] = point;
- Invalidate();
- }
- public override void Move(int deltaX, int deltaY)
- {
- int n = pointArray.Count;
- for (int i = 0; i < n; i++)
- {
- PointF point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
- pointArray[i] = point;
- }
- Invalidate();
- }
- /// <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 object used for hit test
- /// </summary>
- protected virtual void CreateObjects()
- {
- if (AreaPath != null)
- return;
- AreaPath = new GraphicsPath();
- AreaPath.AddRectangle(GetBoundingBox());
- AreaPath.CloseFigure();
- 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;
- }
- }
- /// <summary>
- /// Draw tracker for selected object
- /// </summary>
- /// <param name="g"></param>
- public override void DrawTracker(Graphics g)
- {
- if (!Selected)
- return;
- RectangleF rectangleF = GetBoundingBox();
- g.DrawRectangle(new Pen(Color.White), rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
- }
- /// <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 RectangleF GetBoundingBox()
- {
- RectangleF rectangle;
- float minx = 0, maxx = 0, miny = 0, maxy = 0;
- for (int i = 0; i < pointArray.Count; i++)
- {
- if (i == 0)
- {
- minx = maxx = pointArray[i].X;
- miny = maxy = pointArray[i].Y;
- }
- else
- {
- if (pointArray[i].X > maxx) maxx = pointArray[i].X;
- if (pointArray[i].X < minx) minx = pointArray[i].X;
- if (pointArray[i].Y > maxy) maxy = pointArray[i].Y;
- if (pointArray[i].Y < miny) miny = pointArray[i].Y;
- }
- }
- rectangle = new RectangleF(minx, miny, maxx - minx, maxy - miny);
- return rectangle;
- }
- public override List<PointF> GetPoints()
- {
- return pointArray;
- }
- public override ParentStyleModel GetStyle()
- {
- return labelStyleModel;
- }
- }
- }
|