123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.Tool.DrawTool;
- using ComPDFKit.Tool.UndoManger;
- using ComPDFKitViewer;
- using ComPDFKitViewer.BaseObject;
- using ComPDFKitViewer.Widget;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- namespace ComPDFKit.Tool.FillTool
- {
- public enum FillType
- {
- Unknow,
- Circle,
- Square,
- Text,
- Line,
- Check,
- Cross,
- Image
- }
- public class FillItem
- {
- public FillType Type { get; set; }
- public int PageIndex { get; set; }
- public int AnnotIndex { get; set; }
- public AnnotParam Param { get; set; }
- public int Width { get; set; }
- public int Height { get; set; }
- public bool KeepRate { get; set; }
- internal AnnotHistory HistoryItem { get; set; }
- public FillItem Clone()
- {
- FillItem subItem = new FillItem();
- subItem.Type = Type;
- subItem.PageIndex = PageIndex;
- subItem.AnnotIndex = AnnotIndex;
- if (Param != null)
- {
- switch (this.Type)
- {
- case FillType.Square:
- SquareParam squareParam = new SquareParam();
- Param.CopyTo(squareParam);
- subItem.Param = squareParam;
- break;
- case FillType.Circle:
- CircleParam circleParam = new CircleParam();
- Param.CopyTo(circleParam);
- subItem.Param = circleParam;
- break;
- case FillType.Line:
- LineParam lineParam = new LineParam();
- Param.CopyTo(lineParam);
- subItem.Param = lineParam;
- break;
- case FillType.Check:
- InkParam checkParam = new InkParam();
- Param.CopyTo(checkParam);
- subItem.Param = checkParam;
- break;
- case FillType.Cross:
- InkParam crossParam = new InkParam();
- Param.CopyTo(crossParam);
- subItem.Param = crossParam;
- break;
- default:
- break;
- }
- }
- subItem.Width = Width;
- subItem.Height = Height;
- subItem.KeepRate = KeepRate;
- return subItem;
- }
- }
- public class FillManager
- {
- private Dictionary<int, List<FillItem>> Items { get; set; } = new Dictionary<int, List<FillItem>>();
- private CPDFViewerTool ViewTool { get; set; }
- public CPDFToolManager ToolManager { get; set; }
- private FillCreateTool FillCreator { get; set; }
- private int ViewIndex { get; set; } = -1;
- private FillItem FillParam { get; set; }
- public bool IsFillMode { get; private set; }
- public bool AllowFillHit { get; set; }
- public FillManager(CPDFViewerTool tool)
- {
- ViewTool = tool;
- ViewTool.MouseLeave -= ViewTool_MouseLeave;
- ViewTool.MouseLeave += ViewTool_MouseLeave;
- ViewTool.DrawChanged -= ViewTool_DrawChanged;
- ViewTool.DrawChanged += ViewTool_DrawChanged;
- }
- private void ViewTool_DrawChanged(object sender, EventArgs e)
- {
- FillCreator?.Draw();
- }
- private void ViewTool_MouseLeave(object sender, MouseEventArgs e)
- {
- FillCreator?.CleanDraw();
- }
- public FillItem GetFillItem(int pageIndex, int annotIndex)
- {
- if (pageIndex < 0 || annotIndex < 0)
- {
- return null;
- }
- if (Items.ContainsKey(pageIndex))
- {
- foreach (FillItem item in Items[pageIndex])
- {
- if (item.HistoryItem == null)
- {
- continue;
- }
- if (item.HistoryItem.GetAnnotIndex() == annotIndex)
- {
- return item;
- }
- }
- }
- return null;
- }
- internal bool InsertFillLayer()
- {
- if (ViewTool == null && ViewTool.PDFViewer == null)
- {
- return false;
- }
- if (FillCreator != null)
- {
- return true;
- }
- CPDFViewer pdfViewer = ViewTool.PDFViewer;
- if (ViewIndex != -1)
- {
- FillCreator = pdfViewer.GetViewForTag(ViewIndex) as FillCreateTool;
- if (FillCreator != null)
- {
- return true;
- }
- }
- FillCreator = new FillCreateTool();
- int annotViewindex = pdfViewer.GetMaxViewIndex();
- pdfViewer.InsertView(annotViewindex, FillCreator);
- ViewIndex = FillCreator.GetResTag();
- FillCreator.PDFViewer = pdfViewer;
- return true;
- }
- public bool EnableFillMode(bool isFill)
- {
- IsFillMode = false;
- if (ViewTool == null && ViewTool.PDFViewer == null)
- {
- return false;
- }
- if (isFill)
- {
- IsFillMode = InsertFillLayer();
- if (IsFillMode)
- {
- HideCursor();
- }
- FillCreator?.Draw();
- return IsFillMode;
- }
- ShowCursor();
- if (FillCreator == null)
- {
- return true;
- }
- CPDFViewer pdfViewer = ViewTool.PDFViewer;
- pdfViewer.RemoveView(FillCreator);
- FillCreator = null;
- ViewIndex = -1;
- FillParam = null;
- return true;
- }
- public void ClearFillMode()
- {
- EnableFillMode(false);
- ClearFillItem();
- }
- public void ClearFillItem()
- {
- Items?.Clear();
- }
- public void SetFillParam(FillItem param)
- {
- FillParam = param;
- FillCreator?.SetFillParam(FillParam);
- }
- private bool IsNeedProcess()
- {
- if (IsFillMode == false || FillParam == null || FillCreator == null)
- {
- return false;
- }
- List<FillType> allowType = new List<FillType>()
- {
- FillType.Circle,
- FillType.Square,
- FillType.Line,
- FillType.Check,
- FillType.Cross
- };
- if (allowType.Contains(FillParam.Type) == false)
- {
- return false;
- }
- return true;
- }
- private bool IsMouseOnHitAnnot()
- {
- if (ViewTool != null && ViewTool.cacheHitTestAnnot != null)
- {
- BaseAnnot cacheAnnot = ViewTool.cacheHitTestAnnot;
- BaseAnnot baseAnnot = ViewTool.PDFViewer.AnnotHitTest(false);
- if (baseAnnot != null && baseAnnot.CurrentType != C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
- {
- AnnotData cacheData = cacheAnnot.GetAnnotData();
- AnnotData data = baseAnnot.GetAnnotData();
- if (data.PageIndex == cacheData.PageIndex && data.AnnotIndex == cacheData.AnnotIndex)
- {
- return true;
- }
- }
- Point point = Mouse.GetPosition(ViewTool);
- // PointControlType pointControlType = ViewTool.GetLastSelectedRect().GetHitControlIndex(point);
- //if (pointControlType != PointControlType.None)
- //{
- // return true;
- //}
- }
- return false;
- }
- private bool IsFillHitProcess()
- {
- if (IsFillMode)
- {
- BaseAnnot oldAnnot = ViewTool.cacheHitTestAnnot;
- ViewTool.cacheHitTestAnnot = null;
- ViewTool.isDrawSelectRect = false;
- BaseAnnot baseAnnot = ViewTool.PDFViewer.AnnotHitTest(true);
- if (baseAnnot != null && baseAnnot.CurrentType != C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
- {
- ViewTool.cacheHitTestAnnot = baseAnnot;
- ViewTool.isDrawSelectRect = true;
- List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
- {
- C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
- C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
- };
- MouseEventObject eventObj = new MouseEventObject();
- if (list.Contains(baseAnnot.CurrentType))
- {
- ViewTool.SetEditAnnotObject();
- ViewTool.DrawEditAnnotLayer();
- eventObj.IsMersured = baseAnnot.GetAnnotData().Annot.IsMeasured();
- }
- else
- {
- ViewTool.SelectedAnnot();
- }
- eventObj.annotType = baseAnnot.CurrentType;
- eventObj.hitTestType = MouseHitTestType.Annot;
- ViewTool.DrawSelectedLayer();
- ViewTool.InvokeMouseLeftDown(eventObj);
- return true;
- }
- if (oldAnnot != null)
- {
- ViewTool.CleanSelectedRect();
- MouseEventObject eventObj = new MouseEventObject();
- eventObj.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
- eventObj.hitTestType = MouseHitTestType.Unknown;
- ViewTool.InvokeMouseLeftDown(eventObj);
- HideCursor();
- }
- }
- return false;
- }
- private void HideCursor()
- {
- if (ViewTool != null && ViewTool.PDFViewer != null)
- {
- CPDFViewer pdfViewer = ViewTool.PDFViewer;
- if (pdfViewer.Cursor != Cursors.None)
- {
- pdfViewer.Cursor = Cursors.None;
- ViewTool.Cursor = Cursors.None;
- }
- }
- }
- private void ShowCursor()
- {
- if (ViewTool != null && ViewTool.PDFViewer != null)
- {
- CPDFViewer pdfViewer = ViewTool.PDFViewer;
- if (pdfViewer.Cursor == Cursors.None)
- {
- pdfViewer.Cursor = Cursors.Arrow;
- ViewTool.Cursor = Cursors.Arrow;
- }
- }
- }
- internal bool MouseDownHandle(MouseButtonEventArgs e)
- {
- if (ViewTool != null && ViewTool.cacheHitTestAnnot != null && ViewTool.DrawSelectRectDownEvent())
- {
- return false;
- }
- if (IsFillHitProcess())
- {
- FillCreator.CleanDraw();
- return false;
- }
- if (IsNeedProcess())
- {
- ViewTool.PDFViewer.EnableZoom(false);
- ViewTool.PDFViewer.CanHorizontallyScroll = false;
- ViewTool.PDFViewer.CanVerticallyScroll = false;
- ViewTool.PDFViewer.CaptureMouse();
- FillCreator.MouseDownPos = e.GetPosition(ViewTool.PDFViewer);
- FillCreator.CurrentMousePos = FillCreator.MouseDownPos;
- ViewTool.PDFViewer.GetPointPageInfo(FillCreator.MouseDownPos, out int pageIndex, out Rect paintRect, out Rect pageBound);
- FillCreator.PageIndex = pageIndex;
- FillCreator.PageBound = pageBound;
- FillCreator.IsMouseMoved = false;
- return true;
- }
- return false;
- }
- internal bool MouseUpHandle(MouseButtonEventArgs e)
- {
- if (ViewTool != null && ViewTool.cacheHitTestAnnot != null)
- {
- return false;
- }
- if (IsNeedProcess())
- {
- FillItem annotItem = FillCreator.SaveFillItem();
- ViewTool.PDFViewer.EnableZoom(true);
- ViewTool.PDFViewer.CanHorizontallyScroll = true;
- ViewTool.PDFViewer.CanVerticallyScroll = true;
- ViewTool.PDFViewer.ReleaseMouseCapture();
- if (annotItem != null)
- {
- if (Items.ContainsKey(annotItem.PageIndex) == false)
- {
- Items[annotItem.PageIndex] = new List<FillItem> { annotItem };
- }
- Items[annotItem.PageIndex].Add(annotItem);
- ViewTool.PDFViewer.UpdateAnnotFrame();
- FillCreator.MouseDownPos = new Point(-1, -1);
- FillCreator.CurrentMousePos = FillCreator.MouseDownPos;
- FillCreator.PageIndex = -1;
- FillCreator.PageBound = Rect.Empty;
- FillCreator.IsMouseMoved = false;
- FillCreator.CleanDraw();
- }
- return true;
- }
- return false;
- }
- internal bool MouseMoveHandle(MouseEventArgs e)
- {
- if (ViewTool != null && ViewTool.cacheHitTestAnnot != null)
- {
- if (e.LeftButton == MouseButtonState.Pressed || ViewTool.DrawSelectRectDownEvent())
- {
- FillCreator?.CleanDraw();
- return false;
- }
- }
- if (IsNeedProcess())
- {
- HideCursor();
- FillCreator.IsMouseMoved = (e.LeftButton == MouseButtonState.Pressed);
- FillCreator.CurrentMousePos = e.GetPosition(ViewTool.PDFViewer);
- FillCreator.Draw();
- return true;
- }
- return false;
- }
- }
- }
|