Explorar el Código

ComPDFKit.Tool(win) - 旋转注释编辑框支持等比缩放(待完善)

TangJinZhou hace 2 meses
padre
commit
ff4c1ed28d

+ 8 - 17
Demo/Examples/ComPDFKit.Tool/DrawTool/SelectedRect.cs

@@ -1,27 +1,15 @@
-using ComPDFKit.PDFAnnotation;
+using ComPDFKit.Import;
+using ComPDFKit.PDFAnnotation;
 using ComPDFKit.Tool.Help;
 using ComPDFKit.Tool.SettingParam;
-using ComPDFKit.Viewer.Layer;
 using ComPDFKitViewer;
 using System;
 using System.Collections.Generic;
-using System.ComponentModel;
-using System.Data;
-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.Windows.Shapes;
-using System.Xml.Linq;
-using static ComPDFKit.Tool.Help.ImportWin32;
-using static System.Net.Mime.MediaTypeNames;
 
 namespace ComPDFKit.Tool.DrawTool
 {
@@ -48,7 +36,6 @@ namespace ComPDFKit.Tool.DrawTool
         PDFEdit
     }
 
-
     public enum DrawPointType
     {
         Circle,
@@ -192,7 +179,10 @@ namespace ComPDFKit.Tool.DrawTool
                 if (hitControlType != PointControlType.None)
                 {
                     cacheRect = drawRect;
-                    if(hitControlType != PointControlType.Rotate)
+                    rotateRect = drawRect;
+                    rotateControlPoints = controlPoints.ToList();
+
+                    if (hitControlType != PointControlType.Rotate)
                     {
                         isInScaling = true;
                     }
@@ -266,7 +256,7 @@ namespace ComPDFKit.Tool.DrawTool
                 case PointControlType.Line:
                     return Cursors.SizeAll;
                 case PointControlType.Rotate:
-                    // return CommonHelper.RotationCursor;
+                     return CommonHelper.RotationCursor;
                 default:
                     return Cursors.Arrow;
             }
@@ -598,6 +588,7 @@ namespace ComPDFKit.Tool.DrawTool
         {
             maxRect = rect;
         }
+
         public Rect GetMaxRect()
         {
             return maxRect;

+ 124 - 2
Demo/Examples/ComPDFKit.Tool/DrawTool/SelectedRect.protected.cs

@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-using System.Linq;
 using System.Windows;
 using System.Windows.Media;
 
@@ -152,6 +151,9 @@ namespace ComPDFKit.Tool.DrawTool
 
         protected bool disable = false;
 
+        protected List<Point> rotateControlPoints = new List<Point>();
+
+        protected Rect rotateRect = new Rect();
 
         #endregion
 
@@ -272,7 +274,14 @@ namespace ComPDFKit.Tool.DrawTool
 
             if (!isOutSideScaling)
             {
-                return NormalScaling(mousePoint);
+                if (selectedRectData.rotationAngle != 0 && (hitControlType >= PointControlType.LeftTop && hitControlType <= PointControlType.MiddleTop))
+                {
+                    return RotateScaling(mousePoint);
+                }
+                else
+                {
+                    return NormalScaling(mousePoint);
+                }
             }
             else
             {
@@ -608,6 +617,119 @@ namespace ComPDFKit.Tool.DrawTool
             return false;
         }
 
+        protected bool RotateScaling(Point mouseMovePoint)
+        {
+            Point rotatePoint = new Point();
+            Point hitControlUIPos = new Point();
+            if (hitControlType < PointControlType.Body)
+            {
+                hitControlUIPos = rotateControlPoints[(int)hitControlType];
+                //Debug.WriteLine(hitControlUIPos.ToString());
+                //Debug.WriteLine(rotateRect.ToString());
+            }
+
+            hitControlUIPos = GetRotateUIPoint(hitControlUIPos);
+            Point centerPoint = new Point((rotateRect.Left + rotateRect.Right) / 2, (rotateRect.Top + rotateRect.Bottom) / 2);
+            Vector moveVector = mouseMovePoint - mouseDownPoint;
+            Vector hitVector = hitControlUIPos - centerPoint;
+
+            if (hitControlType == PointControlType.LeftTop
+                || hitControlType == PointControlType.LeftBottom
+                || hitControlType == PointControlType.RightTop
+                || hitControlType == PointControlType.RightBottom)
+            {
+                if (isProportionalScaling)
+                {
+                    double vectorAngle = Vector.AngleBetween(moveVector, hitVector);
+                    double newLenght = Math.Cos(Math.PI / 180.0 * vectorAngle) * moveVector.Length;
+
+                    hitVector.Normalize();
+                    hitVector.X *= newLenght;
+                    hitVector.Y *= newLenght;
+                    rotatePoint = new Point(hitControlUIPos.X + hitVector.X, hitControlUIPos.Y + hitVector.Y);
+                }
+            }
+
+            switch (hitControlType)
+            {
+                case PointControlType.LeftTop:
+                    {
+                        Point rightBottomPoint = rotateControlPoints[(int)PointControlType.RightBottom];
+                        Point rightBottomUIPos = GetRotateUIPoint(rightBottomPoint);
+                        centerPoint = new Point((rotatePoint.X + rightBottomUIPos.X) / 2, (rotatePoint.Y + rightBottomUIPos.Y) / 2);
+
+                        Matrix rotateMatrix = new Matrix();
+                        rotateMatrix.RotateAt(-rotateAngle, centerPoint.X, centerPoint.Y);
+
+                        Point leftTopPoint = rotateMatrix.Transform(rotatePoint);
+                        rightBottomPoint = rotateMatrix.Transform(rightBottomUIPos);
+                        drawRect = new Rect(leftTopPoint, rightBottomPoint);
+                    }
+                    break;
+
+                case PointControlType.LeftBottom:
+                    {
+                        Point rightTopPoint = rotateControlPoints[(int)PointControlType.RightTop];
+                        Point rightTopUIPos = GetRotateUIPoint(rightTopPoint);
+                        centerPoint = new Point((rotatePoint.X + rightTopUIPos.X) / 2, (rotatePoint.Y + rightTopUIPos.Y) / 2);
+
+                        Matrix rotateMatrix = new Matrix();
+                        rotateMatrix.RotateAt(-rotateAngle, centerPoint.X, centerPoint.Y);
+
+                        Point leftBottomPoint = rotateMatrix.Transform(rotatePoint);
+                        rightTopPoint = rotateMatrix.Transform(rightTopUIPos);
+                        drawRect = new Rect(leftBottomPoint, rightTopPoint);
+                    }
+                    break;
+
+                case PointControlType.RightTop:
+                    {
+                        Point leftBottomPoint = rotateControlPoints[(int)PointControlType.LeftBottom];
+                        Point leftBottomUIPos = GetRotateUIPoint(leftBottomPoint);
+                        centerPoint = new Point((rotatePoint.X + leftBottomUIPos.X) / 2, (rotatePoint.Y + leftBottomUIPos.Y) / 2);
+
+                        Matrix rotateMatrix = new Matrix();
+                        rotateMatrix.RotateAt(-rotateAngle, centerPoint.X, centerPoint.Y);
+
+                        Point rightTopPoint = rotateMatrix.Transform(rotatePoint);
+                        leftBottomPoint = rotateMatrix.Transform(leftBottomUIPos);
+
+                        drawRect = new Rect(leftBottomPoint, rightTopPoint);
+                    }
+                    break;
+
+                case PointControlType.RightBottom:
+                    {
+                        Point leftTopPoint = rotateControlPoints[(int)PointControlType.LeftTop];
+                        Point leftTopUIPos = GetRotateUIPoint(leftTopPoint);
+                        centerPoint = new Point((rotatePoint.X + leftTopUIPos.X) / 2, (rotatePoint.Y + leftTopUIPos.Y) / 2);
+
+                        Matrix rotateMatrix = new Matrix();
+                        rotateMatrix.RotateAt(-rotateAngle, centerPoint.X, centerPoint.Y);
+
+                        Point rightBottomPoint = rotateMatrix.Transform(rotatePoint);
+                        leftTopPoint = rotateMatrix.Transform(leftTopUIPos);
+
+                        drawRect = new Rect(leftTopPoint, rightBottomPoint);
+                    }
+                    break;
+
+                default:
+                    break;
+            }
+
+            moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
+            return true;
+        }
+
+        private Point GetRotateUIPoint(Point point)
+        {
+            Point centerPoint = new Point((rotateRect.Left + rotateRect.Right) / 2, (rotateRect.Top + rotateRect.Bottom) / 2);
+            Matrix rotateMatrix = new Matrix();
+            rotateMatrix.RotateAt(rotateAngle, centerPoint.X, centerPoint.Y);
+            return rotateMatrix.Transform(point);
+        }
+
         /// <summary>
         /// Provisional logic, to be further improved, not yet used: Draw the algorithm in the form of normal scaling (drag a point, only scale in one direction).
         /// </summary>