Browse Source

TransformOverlay almost finished

Krzysztof Krysiński 1 year ago
parent
commit
0b63ee40a0

+ 2 - 2
src/PixiEditor.AvaloniaUI/Styles/PixiEditor.Handles.axaml

@@ -12,8 +12,8 @@
             <SolidColorBrush x:Key="HandleBrush" Color="{DynamicResource GlyphColor}"/>
             <SolidColorBrush x:Key="HandleBrush" Color="{DynamicResource GlyphColor}"/>
             <SolidColorBrush x:Key="HandleBackgroundBrush" Color="{DynamicResource GlyphBackground}"/>
             <SolidColorBrush x:Key="HandleBackgroundBrush" Color="{DynamicResource GlyphBackground}"/>
 
 
-            <system:Double x:Key="HandleSize">24</system:Double>
-            <system:Double x:Key="AnchorHandleSize">14</system:Double>
+            <system:Double x:Key="HandleSize">20</system:Double>
+            <system:Double x:Key="AnchorHandleSize">10</system:Double>
         </ResourceDictionary>
         </ResourceDictionary>
     </Styles.Resources>
     </Styles.Resources>
 
 

+ 3 - 3
src/PixiEditor.AvaloniaUI/Views/Overlays/Handles/Handle.cs

@@ -36,7 +36,7 @@ public abstract class Handle : IHandle
     {
     {
         Owner = owner;
         Owner = owner;
         Position = position;
         Position = position;
-        Size = Application.Current.TryGetResource("HandleSize", out object size) ? new VecD((double)size) : new VecD(24);
+        Size = Application.Current.TryGetResource("HandleSize", out object size) ? new VecD((double)size) : new VecD(16);
 
 
         Owner.PointerPressed += OnPointerPressed;
         Owner.PointerPressed += OnPointerPressed;
         Owner.PointerMoved += OnPointerMoved;
         Owner.PointerMoved += OnPointerMoved;
@@ -112,14 +112,14 @@ public abstract class Handle : IHandle
         if (!isHovered && isWithinHandle)
         if (!isHovered && isWithinHandle)
         {
         {
             isHovered = true;
             isHovered = true;
-            if (Owner.Cursor != null)
+            if (Cursor != null)
             {
             {
                 Owner.Cursor = Cursor;
                 Owner.Cursor = Cursor;
             }
             }
 
 
             OnHover?.Invoke(this);
             OnHover?.Invoke(this);
         }
         }
-        else if (isHovered && isWithinHandle)
+        else if (isHovered && !isWithinHandle)
         {
         {
             isHovered = false;
             isHovered = false;
             Owner.Cursor = null;
             Owner.Cursor = null;

+ 22 - 0
src/PixiEditor.AvaloniaUI/Views/Overlays/Handles/OriginAnchor.cs

@@ -0,0 +1,22 @@
+using Avalonia.Controls;
+using Avalonia.Media;
+using PixiEditor.AvaloniaUI.Views.Overlays.TransformOverlay;
+using PixiEditor.DrawingApi.Core.Numerics;
+
+namespace PixiEditor.AvaloniaUI.Views.Overlays.Handles;
+
+public class OriginAnchor : Handle
+{
+    public IPen? SecondaryHandlePen { get; set; } = new Pen(Brushes.White, 1);
+    public OriginAnchor(Control owner, VecD position) : base(owner, position)
+    {
+
+    }
+
+    public override void Draw(DrawingContext context)
+    {
+        double radius = Size.LongestAxis / ZoomboxScale / 2;
+        context.DrawEllipse(HandleBrush, HandlePen, TransformHelper.ToPoint(Position), radius, radius);
+        context.DrawEllipse(HandleBrush, SecondaryHandlePen, TransformHelper.ToPoint(Position), radius, radius);
+    }
+}

+ 1 - 1
src/PixiEditor.AvaloniaUI/Views/Overlays/LineToolOverlay/LineToolOverlay.cs

@@ -103,7 +103,7 @@ internal class LineToolOverlay : Overlay
     {
     {
         startHandle.Position = LineStart;
         startHandle.Position = LineStart;
         endHandle.Position = LineEnd;
         endHandle.Position = LineEnd;
-        moveHandle.Position = TransformHelper.GetDragHandlePos(new ShapeCorners(LineStart, LineEnd - LineStart), ZoomboxScale);
+        moveHandle.Position = TransformHelper.GetHandlePos(new ShapeCorners(LineStart, LineEnd - LineStart), ZoomboxScale, moveHandle.Size);
 
 
         startHandle.Draw(context);
         startHandle.Draw(context);
         endHandle.Draw(context);
         endHandle.Draw(context);

+ 6 - 9
src/PixiEditor.AvaloniaUI/Views/Overlays/TransformOverlay/TransformHelper.cs

@@ -8,9 +8,6 @@ using PixiEditor.DrawingApi.Core.Numerics;
 namespace PixiEditor.AvaloniaUI.Views.Overlays.TransformOverlay;
 namespace PixiEditor.AvaloniaUI.Views.Overlays.TransformOverlay;
 internal static class TransformHelper
 internal static class TransformHelper
 {
 {
-    public const double AnchorSize = 14;
-    public const double MoveHandleSize = 24;
-
     public static Rect ToHandleRect(VecD pos, VecD size, double zoomboxScale)
     public static Rect ToHandleRect(VecD pos, VecD size, double zoomboxScale)
     {
     {
         double scaledX = size.X / zoomboxScale;
         double scaledX = size.X / zoomboxScale;
@@ -61,17 +58,17 @@ internal static class TransformHelper
             return new Cursor(StandardCursorType.SizeWestEast);
             return new Cursor(StandardCursorType.SizeWestEast);
         }
         }
 
 
-        if (angle is (2 or 6))
+        if (angle is 2 or 6)
         {
         {
             return new Cursor(StandardCursorType.SizeNorthSouth);
             return new Cursor(StandardCursorType.SizeNorthSouth);
         }
         }
 
 
-        if (angle is (1 or 5))
+        if (angle is 1 or 5)
         {
         {
-            return new Cursor(StandardCursorType.SizeAll);
+            return new Cursor(StandardCursorType.BottomRightCorner);
         }
         }
 
 
-        return new Cursor(StandardCursorType.SizeAll);
+        return new Cursor(StandardCursorType.BottomLeftCorner);
     }
     }
 
 
     private static double GetSnappingAngle(double angle)
     private static double GetSnappingAngle(double angle)
@@ -279,11 +276,11 @@ internal static class TransformHelper
         return delta.X < scaled.X && delta.Y < scaled.Y;
         return delta.X < scaled.X && delta.Y < scaled.Y;
     }
     }
 
 
-    public static VecD GetDragHandlePos(ShapeCorners corners, double zoomboxScale)
+    public static VecD GetHandlePos(ShapeCorners corners, double zoomboxScale, VecD size)
     {
     {
         VecD max = new(
         VecD max = new(
             Math.Max(Math.Max(corners.TopLeft.X, corners.TopRight.X), Math.Max(corners.BottomLeft.X, corners.BottomRight.X)),
             Math.Max(Math.Max(corners.TopLeft.X, corners.TopRight.X), Math.Max(corners.BottomLeft.X, corners.BottomRight.X)),
             Math.Max(Math.Max(corners.TopLeft.Y, corners.TopRight.Y), Math.Max(corners.BottomLeft.Y, corners.BottomRight.Y)));
             Math.Max(Math.Max(corners.TopLeft.Y, corners.TopRight.Y), Math.Max(corners.BottomLeft.Y, corners.BottomRight.Y)));
-        return max + new VecD(MoveHandleSize / zoomboxScale, MoveHandleSize / zoomboxScale);
+        return max + new VecD(size.X / zoomboxScale, size.Y / zoomboxScale);
     }
     }
 }
 }

+ 18 - 10
src/PixiEditor.AvaloniaUI/Views/Overlays/TransformOverlay/TransformOverlay.cs

@@ -147,6 +147,7 @@ internal class TransformOverlay : Overlay
     private RectangleHandle bottomHandle;
     private RectangleHandle bottomHandle;
     private RectangleHandle leftHandle;
     private RectangleHandle leftHandle;
     private RectangleHandle rightHandle;
     private RectangleHandle rightHandle;
+    private OriginAnchor originHandle;
 
 
     private Dictionary<Handle, Anchor> anchorMap = new();
     private Dictionary<Handle, Anchor> anchorMap = new();
 
 
@@ -170,6 +171,12 @@ internal class TransformOverlay : Overlay
         leftHandle = new AnchorHandle(this, VecD.Zero);
         leftHandle = new AnchorHandle(this, VecD.Zero);
         rightHandle = new AnchorHandle(this, VecD.Zero);
         rightHandle = new AnchorHandle(this, VecD.Zero);
 
 
+        originHandle = new(this, VecD.Zero)
+        {
+            HandlePen = blackFreqDashedPen, SecondaryHandlePen = whiteFreqDashedPen, HandleBrush = Brushes.Transparent
+        };
+
+        AddHandle(originHandle);
         AddHandle(moveHandle);
         AddHandle(moveHandle);
         AddHandle(topLeftHandle);
         AddHandle(topLeftHandle);
         AddHandle(topRightHandle);
         AddHandle(topRightHandle);
@@ -188,6 +195,7 @@ internal class TransformOverlay : Overlay
         anchorMap.Add(bottomHandle, Anchor.Bottom);
         anchorMap.Add(bottomHandle, Anchor.Bottom);
         anchorMap.Add(leftHandle, Anchor.Left);
         anchorMap.Add(leftHandle, Anchor.Left);
         anchorMap.Add(rightHandle, Anchor.Right);
         anchorMap.Add(rightHandle, Anchor.Right);
+        anchorMap.Add(originHandle, Anchor.Origin);
 
 
         ForAllHandles<AnchorHandle>(x =>
         ForAllHandles<AnchorHandle>(x =>
         {
         {
@@ -195,6 +203,9 @@ internal class TransformOverlay : Overlay
             x.OnRelease += OnAnchorHandleReleased;
             x.OnRelease += OnAnchorHandleReleased;
         });
         });
 
 
+        originHandle.OnPress += OnAnchorHandlePressed;
+        originHandle.OnRelease += OnAnchorHandleReleased;
+
         moveHandle.OnPress += OnMoveHandlePressed;
         moveHandle.OnPress += OnMoveHandlePressed;
     }
     }
 
 
@@ -240,11 +251,11 @@ internal class TransformOverlay : Overlay
             TransformHelper.ToPoint((Corners.BottomRight + Corners.TopRight) / 2),
             TransformHelper.ToPoint((Corners.BottomRight + Corners.TopRight) / 2),
             TransformHelper.ToPoint((Corners.BottomRight + Corners.BottomLeft) / 2),
             TransformHelper.ToPoint((Corners.BottomRight + Corners.BottomLeft) / 2),
         };
         };
-        double ellipseSize = (TransformHelper.AnchorSize * anchorSizeMultiplierForRotation - 2) / (ZoomboxScale * 2);
+        /*double ellipseSize = (TransformHelper.AnchorSize * anchorSizeMultiplierForRotation - 2) / (ZoomboxScale * 2);
         foreach (var point in points)
         foreach (var point in points)
         {
         {
             context.DrawEllipse(Brushes.Transparent, null, point, ellipseSize, ellipseSize);
             context.DrawEllipse(Brushes.Transparent, null, point, ellipseSize, ellipseSize);
-        }
+        }*/
     }
     }
 
 
     private void DrawOverlay
     private void DrawOverlay
@@ -288,6 +299,7 @@ internal class TransformOverlay : Overlay
         bottomHandle.Position = bottom;
         bottomHandle.Position = bottom;
         leftHandle.Position = left;
         leftHandle.Position = left;
         rightHandle.Position = right;
         rightHandle.Position = right;
+        originHandle.Position = origin;
 
 
         topLeftHandle.Draw(context);
         topLeftHandle.Draw(context);
         topRightHandle.Draw(context);
         topRightHandle.Draw(context);
@@ -297,14 +309,10 @@ internal class TransformOverlay : Overlay
         bottomHandle.Draw(context);
         bottomHandle.Draw(context);
         leftHandle.Draw(context);
         leftHandle.Draw(context);
         rightHandle.Draw(context);
         rightHandle.Draw(context);
-
-        // origin
-        double radius = TransformHelper.AnchorSize / zoomboxScale / 2;
-        context.DrawEllipse(Brushes.Transparent, blackFreqDashedPen, TransformHelper.ToPoint(origin), radius, radius);
-        context.DrawEllipse(Brushes.Transparent, whiteFreqDashedPen, TransformHelper.ToPoint(origin), radius, radius);
+        originHandle.Draw(context);
 
 
         // move handle
         // move handle
-        VecD handlePos = TransformHelper.GetDragHandlePos(corners, zoomboxScale);
+        VecD handlePos = TransformHelper.GetHandlePos(corners, zoomboxScale, moveHandle.Size);
         moveHandle.Position = handlePos;
         moveHandle.Position = handlePos;
         moveHandle.Draw(context);
         moveHandle.Draw(context);
 
 
@@ -344,14 +352,14 @@ internal class TransformOverlay : Overlay
         if (!CanRotate(pos))
         if (!CanRotate(pos))
         {
         {
             isMoving = true;
             isMoving = true;
-            mousePosOnStartMove = TransformHelper.ToVecD(e.GetPosition(this));
+            mousePosOnStartMove = pos;
             originOnStartMove = InternalState.Origin;
             originOnStartMove = InternalState.Origin;
             cornersOnStartMove = Corners;
             cornersOnStartMove = Corners;
         }
         }
         else if (!LockRotation)
         else if (!LockRotation)
         {
         {
             isRotating = true;
             isRotating = true;
-            mousePosOnStartRotate = TransformHelper.ToVecD(e.GetPosition(this));
+            mousePosOnStartRotate = pos;
             cornersOnStartRotate = Corners;
             cornersOnStartRotate = Corners;
             propAngle1OnStartRotate = InternalState.ProportionalAngle1;
             propAngle1OnStartRotate = InternalState.ProportionalAngle1;
             propAngle2OnStartRotate = InternalState.ProportionalAngle2;
             propAngle2OnStartRotate = InternalState.ProportionalAngle2;