1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153 |
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.Tool.SettingParam;
- using ComPDFKit.Viewer.Layer;
- using ComPDFKitViewer;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Media3D;
- using System.Xml.Linq;
- using static ComPDFKit.Tool.Help.ImportWin32;
- using static System.Net.Mime.MediaTypeNames;
- namespace ComPDFKit.Tool.DrawTool
- {
- public enum PointControlType
- {
- None = -1,
- LeftTop,
- LeftMiddle,
- LeftBottom,
- MiddlBottom,
- RightBottom,
- RightMiddle,
- RightTop,
- MiddleTop,
- Rotate,
- Body,
- Line
- }
- public enum SelectedType
- {
- None = -1,
- Annot,
- PDFEdit
- }
- public enum DrawPointType
- {
- Circle,
- Square,
- Crop
- }
- public enum DrawMoveType
- {
- kDefault,
- /// <summary>
- /// 移动画线
- /// </summary>
- kReferenceLine,
- }
- public class SelectedAnnotData
- {
- /// <summary>
- /// Current size of the rectangle
- /// </summary>
- public Rect Square { get; set; }
- /// <summary>
- /// Current points of the rectangle
- /// </summary>
- public PointCollection Points { get; set; }
- public AnnotData annotData { get; set; }
- public double Angle { get; set; }
- }
- public partial class SelectedRect : DrawingVisual
- {
- /// <summary>
- /// Re-layout child elements
- /// </summary>
- public void Arrange()
- {
- foreach (Visual child in Children)
- {
- if (!(child is UIElement))
- {
- continue;
- }
- UIElement checkChild = child as UIElement;
- try
- {
- double left = Canvas.GetLeft(checkChild);
- double top = Canvas.GetTop(checkChild);
- double width = (double)checkChild.GetValue(FrameworkElement.WidthProperty);
- double height = (double)checkChild.GetValue(FrameworkElement.HeightProperty);
- checkChild.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
- checkChild.Arrange(new Rect(
- double.IsNaN(left) ? 0 : left,
- double.IsNaN(top) ? 0 : top,
- double.IsNaN(width) ? checkChild.DesiredSize.Width : width,
- double.IsNaN(height) ? checkChild.DesiredSize.Height : height));
- }
- catch (Exception ex)
- {
- }
- }
- }
- protected DefaultDrawParam DrawParam = new DefaultDrawParam();
- protected DrawingContext drawDC { get; set; }
- /// <summary>
- /// Data changing event
- /// </summary>
- public event EventHandler<SelectedAnnotData> DataChanging;
- /// <summary>
- /// Data changed event
- /// </summary>
- public event EventHandler<SelectedAnnotData> DataChanged;
- protected bool isHover = false;
- protected bool isSelected = false;
- protected SelectedType selectedType = SelectedType.None;
- public bool CanRotate { get; set; }
- private double angle = 0;
- private Point rotateCenter = new Point(0, 0);
- private int rotateline = 25;
- public SelectedType GetSelectedType()
- {
- return selectedType;
- }
- public void SetIsHover(bool hover)
- {
- isHover = hover;
- }
- public bool GetIsHover()
- {
- return isHover;
- }
- public void SetIsSelected(bool selected)
- {
- isSelected = selected;
- }
- public bool GetIsSelected()
- {
- return isSelected;
- }
- public void SetCurrentDrawPointType(DrawPointType type)
- {
- currentDrawPointType = type;
- }
- public DrawPointType GetCurrentDrawPointType()
- {
- return currentDrawPointType;
- }
- public virtual void OnMouseLeftButtonDown(Point downPoint)
- {
- isMouseDown = true;
- hitControlType = PointControlType.None;
- mouseDownPoint = downPoint;
- moveOffset = new Point(0, 0);
- HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
- if (hitResult != null && hitResult.VisualHit is DrawingVisual)
- {
- //Crop judgment point
- if (currentDrawPointType == DrawPointType.Crop)
- {
- hitControlType = GetHitCropControlIndex(downPoint);
- }
- else
- {
- hitControlType = GetHitControlIndex(downPoint);
- }
- if (hitControlType != PointControlType.None)
- {
- cacheRect = drawRect;
- }
- rotateCenter = new Point(drawRect.Left + drawRect.Width / 2, drawRect.Top + drawRect.Height / 2);
- }
- }
- public virtual void OnMouseLeftButtonUp(Point upPoint)
- {
- if (isMouseDown && hitControlType != PointControlType.None)
- {
- isMouseDown = false;
- cacheRect = SetDrawRect = drawRect;
- Draw();
- if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
- {
- InvokeDataChangEvent(true);
- }
- }
- moveOffset = new Point(0, 0);
- }
- public virtual void OnMouseMove(Point mousePoint, out bool Tag, double width, double height)
- {
- PDFViewerActualWidth = width;
- PDFViewerActualHeight = height;
- Tag = false;
- if (isMouseDown && hitControlType != PointControlType.None)
- {
- Tag = isMouseDown;
- if (CalcHitPointMove(mousePoint))
- {
- Draw();
- if ((int)mousePoint.X != (int)mouseDownPoint.X || (int)mousePoint.Y != (int)mouseDownPoint.Y)
- {
- InvokeDataChangEvent(false);
- }
- }
- }
- }
- public Cursor GetCursor(Point downPoint, Cursor cursor)
- {
- if (isMouseDown)
- {
- return cursor;
- }
- hitControlType = GetHitControlIndex(downPoint);
- switch (hitControlType)
- {
- case PointControlType.LeftTop:
- case PointControlType.RightBottom:
- return Cursors.SizeNWSE;
- case PointControlType.LeftMiddle:
- case PointControlType.RightMiddle:
- return Cursors.SizeWE;
- case PointControlType.LeftBottom:
- case PointControlType.RightTop:
- return Cursors.SizeNESW;
- case PointControlType.MiddlBottom:
- case PointControlType.MiddleTop:
- return Cursors.SizeNS;
- case PointControlType.Body:
- return Cursors.Arrow;
- case PointControlType.Line:
- return Cursors.SizeAll;
- case PointControlType.Rotate:
- return Cursors.Arrow;
- default:
- return Cursors.Arrow;
- }
- }
- public SelectedRect(DefaultDrawParam defaultDrawParam, SelectedType type) : base()
- {
- DrawParam = defaultDrawParam;
- currentDrawPointType = DrawPointType.Square;
- selectedType = type;
- }
- private void DrawNormal(DrawingContext drawDC)
- {
- Rect currentRect = SetDrawRect;
- switch (currentDrawMoveType)
- {
- case DrawMoveType.kDefault:
- currentRect = drawRect;
- CalcControlPoint(currentRect);
- break;
- case DrawMoveType.kReferenceLine:
- CalcControlPoint(currentRect);
- if (isMouseDown == true)
- {
- SolidColorBrush moveBrush = DrawParam.AnnotMoveBrush;
- Pen movepen = DrawParam.AnnotMovePen;
- GetMoveBrushAndPen(ref moveBrush, ref movepen);
- DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect);
- }
- break;
- default:
- break;
- }
- SolidColorBrush solidColorBrush = DrawParam.AnnotRectFillBrush;
- Pen pen = DrawParam.AnnotRectLinePen;
- GetBrushAndPen(ref solidColorBrush, ref pen);
- drawDC?.DrawRectangle(solidColorBrush, pen, currentRect);
- SolidColorBrush PointBrush = DrawParam.AnnotPointBorderBrush;
- Pen PointPen = DrawParam.AnnotPointPen;
- GetPointBrushAndPen(ref PointBrush, ref PointPen);
- switch (currentDrawPointType)
- {
- case DrawPointType.Circle:
- DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
- break;
- case DrawPointType.Square:
- DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
- break;
- case DrawPointType.Crop:
- DrawCropPoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
- break;
- }
- }
- private void PushRotate(DrawingContext drawDC)
- {
- if (CanRotate)
- {
- RotateTransform transform = new RotateTransform();
- transform.Angle = angle;
- // if(hitControlType==PointControlType.Body || hitControlType==PointControlType.Line)
- {
- rotateCenter.X = drawRect.Left + drawRect.Width / 2;
- rotateCenter.Y = drawRect.Top + drawRect.Height / 2;
- }
- transform.CenterX = rotateCenter.X;
- transform.CenterY = rotateCenter.Y;
- drawDC.PushTransform(transform);
- }
- }
- private void PopRotate(DrawingContext drawDC)
- {
- if (CanRotate)
- {
- drawDC.Pop();
- }
- }
- private void DrawRotate(DrawingContext drawDC)
- {
- if (CanRotate)
- {
- double centerX = drawRect.Left + drawRect.Width / 2;
- double centerY = drawRect.Top + drawRect.Height / 2;
- Pen PointPen = DrawParam.AnnotPointPen;
- drawDC.DrawLine(PointPen, new Point(centerX, drawRect.Top), new Point(centerX, drawRect.Top - rotateline));
- drawDC.DrawEllipse(PointPen.Brush, PointPen, new Point(centerX, drawRect.Top - rotateline), RotateSize, RotateSize);
- }
- }
- public void Draw()
- {
- Dispatcher.Invoke(() =>
- {
- Rect currentRect = SetDrawRect;
- drawDC = RenderOpen();
- PushRotate(drawDC);
- DrawRotate(drawDC);
- DrawNormal(drawDC);
- PopRotate(drawDC);
- drawDC?.Close();
- drawDC = null;
- });
- }
- private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
- {
- switch (selectedType)
- {
- case SelectedType.None:
- break;
- case SelectedType.Annot:
- colorBrush = DrawParam.AnnotMoveBrush;
- pen = DrawParam.AnnotMovePen;
- break;
- case SelectedType.PDFEdit:
- colorBrush = DrawParam.PDFEditMoveBrush;
- pen = DrawParam.PDFEditMovePen;
- break;
- default:
- break;
- }
- }
- private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
- {
- switch (selectedType)
- {
- case SelectedType.None:
- break;
- case SelectedType.Annot:
- colorBrush = DrawParam.AnnotPointBorderBrush;
- pen = DrawParam.AnnotPointPen;
- break;
- case SelectedType.PDFEdit:
- if (isHover)
- {
- colorBrush = DrawParam.PDFEditRectFillHoverBrush;
- pen = DrawParam.PDFEditPointHoverPen;
- }
- else if (currentDrawPointType == DrawPointType.Crop)
- {
- colorBrush = DrawParam.SPDFEditCropBorderBrush;//new SolidColorBrush((DrawParam.SPDFEditPointPen.Brush as SolidColorBrush).Color);
- pen = DrawParam.SPDFEditPointPen.Clone();
- pen.DashStyle = DashStyles.Solid;
- }
- else
- {
- if (isSelected)
- {
- colorBrush = DrawParam.SPDFEditPointBorderBrush;
- pen = DrawParam.SPDFEditPointPen;
- }
- else
- {
- colorBrush = DrawParam.PDFEditPointBorderBrush;
- pen = DrawParam.PDFEditPointPen;
- }
- }
- break;
- default:
- break;
- }
- }
- private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
- {
- switch (selectedType)
- {
- case SelectedType.None:
- break;
- case SelectedType.Annot:
- if (isHover)
- {
- colorBrush = DrawParam.AnnotRectFillBrush;
- pen = DrawParam.AnnotRectHoverPen;
- }
- else
- {
- colorBrush = DrawParam.AnnotRectFillBrush;
- pen = DrawParam.AnnotRectLinePen;
- }
- break;
- case SelectedType.PDFEdit:
- if (isHover)
- {
- colorBrush = DrawParam.PDFEditRectFillHoverBrush;
- pen = editHoverPen;//DrawParam.PDFEditRectLineHoverPen;
- }
- else
- {
- if (isSelected)
- {
- colorBrush = DrawParam.SPDFEditRectFillBrush;
- pen = DrawParam.SPDFEditRectLinePen;
- }
- else
- {
- colorBrush = DrawParam.PDFEditRectFillBrush;
- //init Color
- if (showCreatTextRect)
- {
- pen = DrawParam.PDFEditRectLinePen;
- }
- else
- {
- pen = editPen;
- }
- // editPen; //editPen;//// DrawParam.PDFEditRectLinePen;
- }
- }
- break;
- default:
- break;
- }
- }
- public void SetShowCreatTextRect(bool ShowCreatTextRect)
- {
- showCreatTextRect = ShowCreatTextRect;
- }
- public void SetEditPen(Pen editPen = null, Pen editHoverPen = null)
- {
- if (editPen == null)
- {
- this.editPen = DrawParam.PDFEditRectLinePen;
- }
- else
- {
- this.editPen = new Pen(editPen.Brush, editPen.Thickness);
- }
- if (editHoverPen == null)
- {
- this.editHoverPen = DrawParam.PDFEditRectLineHoverPen;
- }
- else
- {
- this.editHoverPen = editHoverPen;
- }
- }
- public virtual void ClearDraw()
- {
- SetDrawRect = drawRect = new Rect();
- drawDC = RenderOpen();
- drawDC?.Close();
- drawDC = null;
- }
- /// <summary>
- /// Hide the drawing
- /// </summary>
- public virtual void HideDraw()
- {
- drawDC = RenderOpen();
- drawDC?.Close();
- }
- public void SetRect(Rect newRect, double zoom)
- {
- if (newRect == Rect.Empty || newRect == null)
- {
- return;
- }
- newRect = new Rect((int)(newRect.X - rectPadding * zoom), (int)(newRect.Y - rectPadding * zoom), (int)(newRect.Width + 2 * rectPadding * zoom), (int)(newRect.Height + 2 * rectPadding * zoom));
- currentZoom = zoom;
- SetDrawRect = drawRect = newRect;
- drawCenterPoint = new Point(drawRect.Left + drawRect.Width / 2, drawRect.Top + drawRect.Height / 2);
- }
- /// <summary>
- /// Get the original set Rect, not the calculated fill
- /// </summary>
- /// <param name="newRect">
- /// The new rect to set
- /// </param>
- public Rect GetRect()
- {
- Rect rect = new Rect(drawRect.X + rectPadding * currentZoom, drawRect.Y + rectPadding * currentZoom, Math.Max(rectMinWidth, drawRect.Width - 2 * rectPadding * currentZoom), Math.Max(RectMinHeight, drawRect.Height - 2 * rectPadding * currentZoom));
- return rect;
- }
- public void SetRectPadding(double rectPadding)
- {
- this.rectPadding = rectPadding;
- }
- public double GetRectPadding()
- {
- return rectPadding;
- }
- public Rect GetDrawRect()
- {
- return drawRect;
- }
- /// <summary>
- /// Obtain cropped and actual region margin
- /// </summary>
- /// <returns></returns>
- public Thickness GetClipThickness()
- {
- return clipThickness;
- }
- /// <summary>
- /// Get ClipRect
- /// </summary>
- /// <returns></returns>
- public Rect GetClipRect()
- {
- Rect drawrect = new Rect(0, 0, 0, 0);
- drawrect.X = SetDrawRect.X - clipThickness.Left * currentZoom;
- drawrect.Y = SetDrawRect.Y - clipThickness.Top * currentZoom;
- drawrect.Width = SetDrawRect.Width - clipThickness.Right * currentZoom + clipThickness.Left * currentZoom;
- drawrect.Height = SetDrawRect.Height - clipThickness.Bottom * currentZoom + clipThickness.Top * currentZoom;
- return drawrect;
- }
- /// <summary>
- /// Set cropping and actual area margins
- /// </summary>
- /// <returns></returns>
- public void SetClipThickness(Thickness rect)
- {
- try
- {
- Rect drawrect = new Rect(0, 0, 0, 0);
- drawrect.X = SetDrawRect.X - rect.Left * currentZoom;
- drawrect.Y = SetDrawRect.Y - rect.Top * currentZoom;
- drawrect.Width = SetDrawRect.Width - rect.Right * currentZoom + rect.Left * currentZoom;
- drawrect.Height = SetDrawRect.Height - rect.Bottom * currentZoom + rect.Top * currentZoom;
- drawRect = drawrect;
- clipThickness = rect;
- }
- catch { }
- }
- public void SetMaxRect(Rect rect)
- {
- maxRect = rect;
- }
- public Rect GetMaxRect()
- {
- return maxRect;
- }
- public void UpdateAnnotData(AnnotData annotData)
- {
- if (selectedRectData != null)
- {
- selectedRectData.annotData = annotData;
- }
- }
- public void SetAnnotData(AnnotData annotData)
- {
- SetIgnorePoints(new List<PointControlType>());
- SetIsProportionalScaling(false);
- isProportionalScaling = false;
- switch (annotData.AnnotType)
- {
- case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
- case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
- case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
- case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
- case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
- case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
- case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
- DisableAll();
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
- case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
- SetIgnorePointsAll();
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
- SetIsProportionalScaling(true);
- break;
- case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
- SetIgnorePointsAll();
- break;
- default:
- break;
- }
- SetMaxRect(annotData.PaintOffset);
- SetRect(annotData.PaintRect, annotData.CurrentZoom);
- selectedRectData = new SelectedAnnotData();
- selectedRectData.annotData = annotData;
- }
- public void SetIsProportionalScaling(bool isProportionalScaling)
- {
- this.isProportionalScaling = isProportionalScaling;
- ignorePoints.Clear();
- if (isProportionalScaling)
- {
- ignorePoints.Add(PointControlType.LeftMiddle);
- ignorePoints.Add(PointControlType.MiddlBottom);
- ignorePoints.Add(PointControlType.RightMiddle);
- ignorePoints.Add(PointControlType.MiddleTop);
- ignorePoints.Add(PointControlType.Rotate);
- }
- }
- public void SetDrawType(DrawPointType drawType)
- {
- currentDrawPointType = drawType;
- }
- public void SetDrawMoveType(DrawMoveType drawType)
- {
- currentDrawMoveType = drawType;
- }
- /// <summary>
- /// Set the types that need to be ignored
- /// </summary>
- /// <param name="types">
- /// The collection of point types that need to be ignored
- /// </param>
- public void SetIgnorePoints(List<PointControlType> types)
- {
- ignorePoints.Clear();
- foreach (PointControlType type in types)
- {
- ignorePoints.Add(type);
- }
- }
- /// <summary>
- /// Set Edit that need to be ignored
- /// </summary>
- /// <param name="types">
- /// The collection of point types that need to be ignored
- /// </param>
- public void SetEditIgnorePoints(List<PointControlType> ignoreTextPoints, List<PointControlType> ignoreImagePoints, DrawPointType drawEditPointType, bool IsText = true)
- {
- SetCurrentDrawPointType(drawEditPointType);
- if (IsText)
- {
- ignorePoints.Clear();
- foreach (PointControlType type in ignoreTextPoints)
- {
- ignorePoints.Add(type);
- }
- }
- else
- {
- ignorePoints.Clear();
- foreach (PointControlType type in ignoreImagePoints)
- {
- ignorePoints.Add(type);
- }
- }
- }
- /// <summary>
- /// Ignore all points
- /// </summary>
- public void SetIgnorePointsAll()
- {
- ignorePoints.Clear();
- ignorePoints.Add(PointControlType.LeftTop);
- ignorePoints.Add(PointControlType.LeftMiddle);
- ignorePoints.Add(PointControlType.LeftBottom);
- ignorePoints.Add(PointControlType.MiddlBottom);
- ignorePoints.Add(PointControlType.RightBottom);
- ignorePoints.Add(PointControlType.RightMiddle);
- ignorePoints.Add(PointControlType.RightTop);
- ignorePoints.Add(PointControlType.MiddleTop);
- }
- /// <summary>
- /// Disable all functions
- /// </summary>
- public void DisableAll()
- {
- ignorePoints.Clear();
- ignorePoints.Add(PointControlType.LeftTop);
- ignorePoints.Add(PointControlType.LeftMiddle);
- ignorePoints.Add(PointControlType.LeftBottom);
- ignorePoints.Add(PointControlType.MiddlBottom);
- ignorePoints.Add(PointControlType.RightBottom);
- ignorePoints.Add(PointControlType.RightMiddle);
- ignorePoints.Add(PointControlType.RightTop);
- ignorePoints.Add(PointControlType.MiddleTop);
- ignorePoints.Add(PointControlType.Rotate);
- ignorePoints.Add(PointControlType.Body);
- ignorePoints.Add(PointControlType.Line);
- }
- /// <summary>
- /// Set the left alignment in the set maximum rectangle
- /// </summary>
- public virtual void SetAlignLeftForMaxRect()
- {
- DrawAlignRect(AlignmentsHelp.SetAlignLeft(drawRect, maxRect));
- }
- /// <summary>
- /// Set horizontal center alignment in the set maximum rectangle
- /// </summary>
- public virtual void SetAlignHorizonCenterForMaxRect()
- {
- DrawAlignRect(AlignmentsHelp.SetAlignHorizonCenter(drawRect, maxRect));
- }
- /// <summary>
- /// Set horizontal right alignment in the set maximum rectangle
- /// </summary>
- public virtual void SetAlignRightForMaxRect()
- {
- DrawAlignRect(AlignmentsHelp.SetAlignRight(drawRect, maxRect));
- }
- /// <summary>
- /// Set the top alignment in the set maximum rectangle
- /// </summary>
- public virtual void SetAlignTopForMaxRect()
- {
- DrawAlignRect(AlignmentsHelp.SetAlignTop(drawRect, maxRect));
- }
- /// <summary>
- /// Set vertical center alignment in the set maximum rectangle
- /// </summary>
- public virtual void SetAlignVerticalCenterForMaxRect()
- {
- DrawAlignRect(AlignmentsHelp.SetAlignVerticalCenter(drawRect, maxRect));
- }
- /// <summary>
- /// Set vertical center alignment in the set maximum rectangle
- /// </summary>
- public virtual void SetAlignHorizonVerticalCenterForMaxRect()
- {
- DrawAlignRect(AlignmentsHelp.SetAlignHorizonVerticalCenter(drawRect, maxRect));
- }
- /// <summary>
- /// Set the bottom alignment in the set maximum rectangle
- /// </summary>
- public virtual void SetAlignBottomForMaxRect()
- {
- DrawAlignRect(AlignmentsHelp.SetAlignBottom(drawRect, maxRect));
- }
- /// <summary>
- /// Get which control point the coordinate is on
- /// </summary>
- /// <param name="clickPoint">
- /// The point to check
- /// </param>
- /// <returns>
- /// The control point type
- /// </returns>
- public PointControlType GetHitControlIndex(Point point, bool isIgnore = true)
- {
- if (CanRotate)
- {
- return GetRotateHitIndex(point, isIgnore);
- }
- return GetNormalHitIndex(point, isIgnore);
- }
- private PointControlType GetNormalHitIndex(Point point, bool isIgnore = true)
- {
- HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
- if (hitResult != null && hitResult.VisualHit is DrawingVisual)
- {
- List<PointControlType> ignoreList = GetIgnorePoints();
- List<Point> IgnorePointsList = new List<Point>();
- foreach (PointControlType type in ignoreList)
- {
- if ((int)type < controlPoints.Count)
- {
- IgnorePointsList.Add(controlPoints[(int)type]);
- }
- }
- for (int i = 0; i < controlPoints.Count; i++)
- {
- Point checkPoint = controlPoints[i];
- if (isIgnore && IgnorePointsList.Contains(checkPoint))
- {
- continue;
- }
- switch (currentDrawPointType)
- {
- case DrawPointType.Circle:
- if (IgnorePointsList.Contains(checkPoint))
- {
- continue;
- }
- Vector checkVector = checkPoint - point;
- double wlen = drawRect.Width;
- if (wlen > 50)
- {
- wlen = 20;
- }
- else
- {
- wlen = wlen / 3;
- }
- double hlen = drawRect.Height;
- if (hlen > 50)
- {
- hlen = 20;
- }
- else
- {
- hlen = wlen / 3;
- }
- if ((PointControlType)i == PointControlType.RightMiddle)
- {
- if (Math.Abs(point.X - checkPoint.X) < wlen && checkVector.Length < drawRect.Height / 3)
- {
- return (PointControlType)i;
- }
- }
- if ((PointControlType)i == PointControlType.LeftMiddle)
- {
- if (Math.Abs(point.X - checkPoint.X) < wlen && checkVector.Length < drawRect.Height / 3)
- {
- return (PointControlType)i;
- }
- }
- if ((PointControlType)i == PointControlType.MiddleTop)
- {
- if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width / 3)
- {
- return (PointControlType)i;
- }
- }
- if ((PointControlType)i == PointControlType.MiddlBottom)
- {
- if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width / 3)
- {
- return (PointControlType)i;
- }
- }
- if (checkVector.Length < pointSize)
- {
- return (PointControlType)i;
- }
- break;
- case DrawPointType.Square:
- Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
- if (checkRect.Contains(point))
- {
- return (PointControlType)i;
- }
- break;
- case DrawPointType.Crop:
- Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
- if (cropRect.Contains(point))
- {
- return (PointControlType)i;
- }
- break;
- default:
- break;
- }
- }
- if (drawRect.Contains(point))
- {
- double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
- double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
- Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
- if (rect.Contains(point))
- {
- if (!ignoreList.Contains(PointControlType.Body))
- {
- return PointControlType.Body;
- }
- }
- if (!ignoreList.Contains(PointControlType.Body))
- {
- return PointControlType.Line;
- }
- }
- }
- return PointControlType.None;
- }
- private PointControlType GetRotateHitIndex(Point point, bool isIgnore = true)
- {
- HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
- if (hitResult != null && hitResult.VisualHit is DrawingVisual)
- {
- //旋转回去
- Point centerPoint = rotateCenter;
- Vector currentVector = point - centerPoint;
- Matrix matrix = new Matrix();
- matrix.Rotate(-angle);
- Vector transVector = matrix.Transform(currentVector);
- Point checkPos = new Point(centerPoint.X + transVector.X, centerPoint.Y + transVector.Y);
- point = checkPos;
- List<PointControlType> ignoreList = GetIgnorePoints();
- List<Point> IgnorePointsList = new List<Point>();
- foreach (PointControlType type in ignoreList)
- {
- if ((int)type < controlPoints.Count)
- {
- IgnorePointsList.Add(controlPoints[(int)type]);
- }
- }
- for (int i = 0; i < controlPoints.Count; i++)
- {
- Point checkPoint = controlPoints[i];
- if (isIgnore && IgnorePointsList.Contains(checkPoint))
- {
- continue;
- }
- switch (currentDrawPointType)
- {
- case DrawPointType.Circle:
- Vector checkCircle = checkPoint - point;
- if (checkCircle.Length < pointSize)
- {
- return (PointControlType)i;
- }
- break;
- case DrawPointType.Square:
- Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
- if (checkRect.Contains(point))
- {
- return (PointControlType)i;
- }
- break;
- case DrawPointType.Crop:
- Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
- if (cropRect.Contains(point))
- {
- return (PointControlType)i;
- }
- break;
- default:
- break;
- }
- }
- if (drawRect.Contains(point))
- {
- double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
- double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
- Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
- if (rect.Contains(point))
- {
- if (!ignoreList.Contains(PointControlType.Body))
- {
- return PointControlType.Body;
- }
- }
- if (!ignoreList.Contains(PointControlType.Body))
- {
- return PointControlType.Line;
- }
- }
- double centerX = drawRect.Left + drawRect.Width / 2;
- double centerY = drawRect.Top + drawRect.Height / 2;
- Point rotatePoint = new Point(centerX, drawRect.Top - rotateline);
- Vector checkVector = point - rotatePoint;
- if (checkVector.Length <= RotateSize)
- {
- return PointControlType.Rotate;
- }
- }
- return PointControlType.None;
- }
- public void SetRotateAngle(double newAngle)
- {
- if (angle != newAngle)
- {
- angle = newAngle;
- Draw();
- }
- }
- /// <summary>
- /// The position of the points in the cropping box
- /// </summary>
- /// <param name="point"></param>
- /// <param name="isIgnore"></param>
- /// <returns></returns>
- public PointControlType GetHitCropControlIndex(Point point, bool isIgnore = true)
- {
- List<Point> controlCurrentPoints = GetControlPoint(drawRect);
- HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
- if (hitResult != null && hitResult.VisualHit is DrawingVisual)
- {
- List<PointControlType> ignoreList = GetIgnorePoints();
- List<Point> IgnorePointsList = new List<Point>();
- foreach (PointControlType type in ignoreList)
- {
- if ((int)type < controlCurrentPoints.Count)
- {
- IgnorePointsList.Add(controlCurrentPoints[(int)type]);
- }
- }
- for (int i = 0; i < controlCurrentPoints.Count; i++)
- {
- Point checkPoint = controlCurrentPoints[i];
- if (isIgnore && IgnorePointsList.Contains(checkPoint))
- {
- continue;
- }
- switch (currentDrawPointType)
- {
- case DrawPointType.Circle:
- Vector checkVector = checkPoint - point;
- if (checkVector.Length < pointSize)
- {
- return (PointControlType)i;
- }
- break;
- case DrawPointType.Square:
- Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
- if (checkRect.Contains(point))
- {
- return (PointControlType)i;
- }
- break;
- case DrawPointType.Crop:
- Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
- if (cropRect.Contains(point))
- {
- return (PointControlType)i;
- }
- break;
- default:
- break;
- }
- }
- if (drawRect.Contains(point))
- {
- double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
- double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
- Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
- if (rect.Contains(point))
- {
- if (!ignoreList.Contains(PointControlType.Body))
- {
- return PointControlType.Body;
- }
- }
- if (!ignoreList.Contains(PointControlType.Body))
- {
- return PointControlType.Line;
- }
- }
- }
- return PointControlType.None;
- }
- }
- }
|