| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 | 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.Windows.Forms;namespace PaintDotNet.Annotation.Label{    using static PaintDotNet.Base.SettingModel.LabelStyleModel;    using PointList = List<PointF>;    /// <summary>    /// 污迹处理->多边形    /// </summary>    public class DrawSmudgePolygon : DrawObject    {        /// <summary>        /// 鼠标形状        /// </summary>        private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));        /// <summary>        /// 点集合        /// </summary>        private PointList pointArray;        /// <summary>        /// 标注样式信息model        /// </summary>        public PolygonPolygon polygonStyle;        private RectangleF rectangleF;        public DrawSmudgePolygon() : base()        {            this.objectType = DrawClass.Label;            this.drawToolType = DrawToolType.DrawSmudgePolygon;            pointArray = new PointList();            Initialize();        }        public DrawSmudgePolygon(ISurfaceBox surfaceBox, int x1, int y1) : base()//DrawSmudgePolygon        {            this.objectType = DrawClass.Label;            this.drawToolType = DrawToolType.DrawSmudgePolygon;            polygonStyle = surfaceBox.GetLabelStyleModel().polygonPolygon;            pointArray = new PointList();            startPoint.X = x1;            startPoint.Y = y1;            pointArray.Add(new PointF(x1, y1));            Initialize();        }        public DrawSmudgePolygon(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()        {            this.objectType = DrawClass.Label;            this.drawToolType = DrawToolType.DrawSmudgePolygon;            this.ISurfaceBox = surfaceBox;            polygonStyle = (PolygonPolygon)parentStyleModel;            pointArray = points;            startPoint = points[points.Count - 2];            endPoint = points[points.Count - 1];        }        /// <summary>        /// Clone this instance        /// </summary>        public override DrawObject Clone()        {            DrawSmudgePolygon drawSmudgePolygon = new DrawSmudgePolygon();            drawSmudgePolygon.objectType = DrawClass.Label;            drawSmudgePolygon.drawToolType = DrawToolType.DrawSmudgePolygon;            drawSmudgePolygon.ISurfaceBox = this.ISurfaceBox;            foreach (PointF p in this.pointArray)            {                drawSmudgePolygon.pointArray.Add(p);            }            drawSmudgePolygon.startPoint = this.startPoint;            drawSmudgePolygon.endPoint = this.endPoint;            FillDrawObjectFields(drawSmudgePolygon);            return drawSmudgePolygon;        }        public override DrawObject Clone(ISurfaceBox surfaceBox)        {            DrawSmudgePolygon drawSmudgePolygon = new DrawSmudgePolygon();            drawSmudgePolygon.objectType = DrawClass.Label;            drawSmudgePolygon.drawToolType = DrawToolType.DrawSmudgePolygon;            drawSmudgePolygon.ISurfaceBox = surfaceBox;            foreach (PointF p in this.pointArray)            {                drawSmudgePolygon.pointArray.Add(p);            }            drawSmudgePolygon.startPoint = this.startPoint;            drawSmudgePolygon.endPoint = this.endPoint;            drawSmudgePolygon.polygonStyle = this.polygonStyle;            FillDrawObjectFields(drawSmudgePolygon);            return drawSmudgePolygon;        }        public override void Draw(Graphics g)        {            if (HandleCount >= 3)            {                g.SmoothingMode = SmoothingMode.AntiAlias;                Color color = Color.FromArgb(this.polygonStyle.lineColor);                Pen pen = new Pen(color, PenWidth);                int penWidth = this.polygonStyle.lineWidth;                pen.DashStyle = (DashStyle)this.polygonStyle.lineStyle;                pen.Width = penWidth;                //Color fillColor = Color.FromArgb(this.polygonStyle.fillColor);                //SolidBrush fillBrush = new SolidBrush(fillColor);                //g.FillPolygon(fillBrush, pointArray.ToArray());                g.DrawPolygon(pen, pointArray.ToArray());                pen.Dispose();                //fillBrush.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;        }        public override void Move(int deltaX, int deltaY)        {            int n = pointArray.Count;            PointF point;            for (int i = 0; i < n; i++)            {                point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));                pointArray[i] = point;            }        }        /// <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.Black);            for (int i = 1; i <= HandleCount; i++)            {                g.FillRectangle(brush, GetHandleRectangle(i));            }            brush.Dispose();            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)        {            int counter = 0;            int i;            double xinters;            PointF p1, p2;            int pointCount = pointArray.Count;            p1 = pointArray[0];            for (i = 1; i <= pointCount; i++)            {                p2 = pointArray[i % pointCount];                if (point.Y > Math.Min(p1.Y, p2.Y)//校验点的Y大于线段端点的最小Y                    && point.Y <= Math.Max(p1.Y, p2.Y))//校验点的Y小于线段端点的最大Y                {                    if (point.X <= Math.Max(p1.X, p2.X))//校验点的X小于等线段端点的最大X(使用校验点的左射线判断).                    {                        if (p1.Y != p2.Y)//线段不平行于X轴                        {                            xinters = (point.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;                            if (p1.X == p2.X || point.X <= xinters)                            {                                counter++;                            }                        }                    }                }                p1 = p2;            }            if (counter % 2 == 0)            {                return false;            }            else            {                return true;            }        }        public override bool IntersectsWith(Rectangle rectangle)        {            return rectangleF.IntersectsWith(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;        }        internal void setNextPoint(PointF p)        {            AddPoint(p);            startPoint = endPoint;            endPoint = p;        }        internal void setEndPoint(PointF p)        {            endPoint = p;        }        public override List<PointF> GetPoints()        {            return pointArray;        }        public override ParentStyleModel GetStyle()        {            return polygonStyle;        }    }}
 |