123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996 |
- 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,
- 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 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 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;
- }
- }
- }
- 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;
- default:
- return Cursors.Arrow;
- }
- }
- public SelectedRect(DefaultDrawParam defaultDrawParam, SelectedType type) : base()
- {
- DrawParam = defaultDrawParam;
- currentDrawPointType = DrawPointType.Square;
- selectedType = type;
- }
- public void Draw()
- {
- Dispatcher.Invoke(() =>
- {
- Rect currentRect = SetDrawRect;
- drawDC = RenderOpen();
- 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);
- if (selectedType == SelectedType.PDFEdit)
- {
- DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect, DrawParam.PDFEditMoveRectPen);
- }
- else
- {
- 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:
- if (selectedType == SelectedType.PDFEdit)
- {
- DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
- //DrawEditSelectionBox(drawDC, PointPen);
- }
- else
- {
- 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;
- }
- 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)
- {
- 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;
- }
- /// <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;
- }
- }
- }
|