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> Items { get; set; } = new Dictionary>(); 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 allowType = new List() { 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 list = new List() { 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 { 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; } } }