Browse Source

Line overlay transformed fix wip

flabbet 11 months ago
parent
commit
dc1573541c

+ 2 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Interfaces/Shapes/IReadOnlyLineData.cs

@@ -6,4 +6,6 @@ public interface IReadOnlyLineData : IReadOnlyShapeVectorData
 {
     public VecD Start { get; }
     public VecD End { get; }
+    public VecD TransformedStart { get; set; }
+    public VecD TransformedEnd { get; set; }
 }

+ 13 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Shapes/Data/LineVectorData.cs

@@ -11,7 +11,19 @@ public class LineVectorData(VecD startPos, VecD pos) : ShapeVectorData, IReadOnl
 {
     public VecD Start { get; set; } = startPos;
     public VecD End { get; set; } = pos;
-    
+
+    public VecD TransformedStart
+    {
+        get => TransformationMatrix.MapPoint(Start);
+        set => Start = TransformationMatrix.Invert().MapPoint(value);
+    }
+
+    public VecD TransformedEnd
+    {
+        get => TransformationMatrix.MapPoint(End);
+        set => End = TransformationMatrix.Invert().MapPoint(value);
+    }
+
     public override RectD GeometryAABB
     {
         get

+ 3 - 3
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/LineExecutor.cs

@@ -3,6 +3,7 @@ using PixiEditor.ChangeableDocument.Actions.Generated;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces.Shapes;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.DrawingApi.Core.Surfaces.PaintImpl;
 using PixiEditor.Extensions.CommonApi.Palettes;
 using PixiEditor.Helpers.Extensions;
@@ -106,7 +107,7 @@ internal abstract class LineExecutor<T> : UpdateableChangeExecutor where T : ILi
             return;
         }
 
-        document!.LineToolOverlayHandler.Show(startPos + new VecD(0.5), curPos + new VecD(0.5), true);
+        document!.LineToolOverlayHandler.Show(startPos + new VecD(0.5), curPos + new VecD(0.5), Matrix3X3.Identity, true);
         transforming = true;
     }
 
@@ -114,8 +115,7 @@ internal abstract class LineExecutor<T> : UpdateableChangeExecutor where T : ILi
     {
         if (!transforming)
             return;
-
-
+        
         var moveOverlayAction = TransformOverlayMoved(start, end);
         internals!.ActionAccumulator.AddActions(moveOverlayAction);
 

+ 1 - 1
src/PixiEditor/Models/DocumentModels/UpdateableChangeExecutors/VectorLineToolExecutor.cs

@@ -40,7 +40,7 @@ internal class VectorLineToolExecutor : LineExecutor<IVectorLineToolHandler>
 
     protected override IAction TransformOverlayMoved(VecD start, VecD end)
     {
-        LineVectorData data = new LineVectorData((VecD)start, (VecD)end)
+        LineVectorData data = new LineVectorData(start, end)
         {
             StrokeColor = StrokeColor,
             StrokeWidth = StrokeWidth,

+ 1 - 1
src/PixiEditor/Models/Handlers/ILineOverlayHandler.cs

@@ -9,5 +9,5 @@ internal interface ILineOverlayHandler
     public bool Nudge(VecD distance);
     public bool Undo();
     public bool Redo();
-    public void Show(VecD startPos, VecD curPos, bool showApplyButton);
+    public void Show(VecD startPos, VecD endPos, Matrix3X3 transformation, bool showApplyButton);
 }

+ 12 - 3
src/PixiEditor/ViewModels/Document/TransformOverlays/LineToolOverlayViewModel.cs

@@ -14,6 +14,7 @@ internal class LineToolOverlayViewModel : ObservableObject, ILineOverlayHandler
     private TransformOverlayUndoStack<(VecD, VecD)>? undoStack = null;
 
     private VecD lineStart;
+    private Matrix3X3 transformationMatrix;
 
     public VecD LineStart
     {
@@ -37,6 +38,12 @@ internal class LineToolOverlayViewModel : ObservableObject, ILineOverlayHandler
         }
     }
 
+    public Matrix3X3 TransformationMatrix
+    {
+        get => transformationMatrix;
+        set => SetProperty(ref transformationMatrix, value);
+    }
+    
     private bool isEnabled;
 
     public bool IsEnabled
@@ -67,15 +74,17 @@ internal class LineToolOverlayViewModel : ObservableObject, ILineOverlayHandler
             new RelayCommand(() => undoStack?.AddState((LineStart, LineEnd), TransformOverlayStateType.Move));
     }
 
-    public void Show(VecD lineStart, VecD lineEnd, bool showApplyButton)
+    public void Show(VecD lineStart, VecD endPos, Matrix3X3 transformationMatrix, bool showApplyButton)
     {
         if (undoStack is not null)
             return;
         undoStack = new();
-        undoStack.AddState((lineStart, lineEnd), TransformOverlayStateType.Initial);
+        
+        undoStack.AddState((lineStart, endPos), TransformOverlayStateType.Initial);
 
         LineStart = lineStart;
-        LineEnd = lineEnd;
+        LineEnd = endPos; 
+        TransformationMatrix = transformationMatrix;
         IsEnabled = true;
         ShowApplyButton = showApplyButton;
     }

+ 1 - 1
src/PixiEditor/ViewModels/Tools/Tools/VectorLineToolViewModel.cs

@@ -68,7 +68,7 @@ internal class VectorLineToolViewModel : ShapeTool, IVectorLineToolHandler
             IReadOnlyLineData? lineVectorData = vectorLayer.GetShapeData(document.AnimationDataViewModel.ActiveFrameTime) as IReadOnlyLineData;
             if (lineVectorData is null) return;
             
-            document.LineToolOverlayViewModel.Show(lineVectorData.Start, lineVectorData.End, false);
+            document.LineToolOverlayViewModel.Show(lineVectorData.TransformedStart, lineVectorData.TransformedEnd, lineVectorData.TransformationMatrix, false);
         }
 
         document.Tools.UseVectorLineTool();

+ 8 - 0
src/PixiEditor/Views/Main/ViewportControls/ViewportOverlays.cs

@@ -216,12 +216,20 @@ internal class ViewportOverlays
             Path = "Document.LineToolOverlayViewModel.LineEnd",
             Mode = BindingMode.TwoWay
         };
+        
+        Binding transformationMatrixBinding = new()
+        {
+            Source = Viewport,
+            Path = "Document.LineToolOverlayViewModel.TransformationMatrix",
+            Mode = BindingMode.TwoWay
+        };
 
         lineToolOverlay.Bind(Visual.IsVisibleProperty, isVisibleBinding);
         lineToolOverlay.Bind(LineToolOverlay.SnappingControllerProperty, snappingBinding);
         lineToolOverlay.Bind(LineToolOverlay.ActionCompletedProperty, actionCompletedBinding);
         lineToolOverlay.Bind(LineToolOverlay.LineStartProperty, lineStartBinding);
         lineToolOverlay.Bind(LineToolOverlay.LineEndProperty, lineEndBinding);
+        lineToolOverlay.Bind(LineToolOverlay.TransformationMatrixProperty, transformationMatrixBinding);
     }
 
     private void BindTransformOverlay()

+ 32 - 11
src/PixiEditor/Views/Overlays/LineToolOverlay/LineToolOverlay.cs

@@ -1,10 +1,8 @@
 using System.Windows.Input;
 using Avalonia;
 using Avalonia.Input;
-using Avalonia.Interactivity;
 using Avalonia.Media;
 using ChunkyImageLib.DataHolders;
-using PixiEditor.Helpers;
 using PixiEditor.Models.Controllers.InputDevice;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.Extensions.UI.Overlays;
@@ -34,6 +32,15 @@ internal class LineToolOverlay : Overlay
         set => SetValue(LineEndProperty, value);
     }
 
+    public static readonly StyledProperty<Matrix3X3> TransformationMatrixProperty = AvaloniaProperty.Register<LineToolOverlay, Matrix3X3>(
+        nameof(TransformationMatrix));
+
+    public Matrix3X3 TransformationMatrix
+    {
+        get => GetValue(TransformationMatrixProperty);
+        set => SetValue(TransformationMatrixProperty, value);
+    }
+
     public static readonly StyledProperty<ICommand?> ActionCompletedProperty =
         AvaloniaProperty.Register<LineToolOverlay, ICommand?>(nameof(ActionCompleted));
 
@@ -72,6 +79,9 @@ internal class LineToolOverlay : Overlay
     private RectangleHandle endHandle;
     private TransformHandle moveHandle;
 
+    private VecD startPos;
+    private VecD endPos;
+
     public LineToolOverlay()
     {
         Cursor = new Cursor(StandardCursorType.Arrow);
@@ -114,14 +124,20 @@ internal class LineToolOverlay : Overlay
 
     public override void RenderOverlay(DrawingContext context, RectD canvasBounds)
     {
-        startHandle.Position = LineStart;
-        endHandle.Position = LineEnd;
-        VecD center = (LineStart + LineEnd) / 2;
-        VecD size = LineEnd - LineStart;
+        VecD mappedStart = LineStart; //TransformationMatrix.MapPoint(LineStart); 
+        VecD mappedEnd = LineEnd; //TransformationMatrix.MapPoint(LineEnd);
+        
+        startHandle.Position = mappedStart;
+        endHandle.Position = mappedEnd; 
+        
+        VecD center = (mappedStart + mappedEnd) / 2;
+        VecD size = mappedEnd - mappedStart;
+        
         moveHandle.Position = TransformHelper.GetHandlePos(new ShapeCorners(center, size), ZoomScale, moveHandle.Size);
 
-        context.DrawLine(blackDashedPen, new Point(LineStart.X, LineStart.Y), new Point(LineEnd.X, LineEnd.Y));
-        context.DrawLine(whiteDashedPen, new Point(LineStart.X, LineStart.Y), new Point(LineEnd.X, LineEnd.Y));
+        context.DrawLine(blackDashedPen, new Point(mappedStart.X, mappedStart.Y), new Point(mappedEnd.X, mappedEnd.Y));
+        context.DrawLine(whiteDashedPen, new Point(mappedStart.X, mappedStart.Y), new Point(mappedEnd.X, mappedEnd.Y));
+        
         startHandle.Draw(context);
         endHandle.Draw(context);
         moveHandle.Draw(context);
@@ -142,13 +158,15 @@ internal class LineToolOverlay : Overlay
 
     private void StartHandleOnDrag(Handle source, VecD position)
     {
-        LineStart = SnapAndHighlight(position);
+        VecD delta = position - mouseDownPos;
+        LineStart = SnapAndHighlight(lineStartOnMouseDown + delta);
         movedWhileMouseDown = true;
     }
 
     private void EndHandleOnDrag(Handle source, VecD position)
     {
-        var final = SnapAndHighlight(position);
+        VecD delta = position - mouseDownPos;
+        VecD final = SnapAndHighlight(lineEndOnMouseDown + delta);
         
         LineEnd = final;
         movedWhileMouseDown = true;
@@ -182,8 +200,11 @@ internal class LineToolOverlay : Overlay
     private void MoveHandleOnDrag(Handle source, VecD position)
     {
         var delta = position - mouseDownPos;
+        
+        VecD mappedStart = LineStart;
+        VecD mappedEnd = LineEnd;
 
-        ((string, string), VecD) snapDeltaResult = TrySnapLine(LineStart, LineEnd, delta);
+        ((string, string), VecD) snapDeltaResult = TrySnapLine(mappedStart, mappedEnd, delta);
 
         if (SnappingController != null)
         {