Browse Source

Added scaling vector layers and recenter viewport on resize

Krzysztof Krysiński 5 months ago
parent
commit
5603708378

+ 8 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Interfaces/IScalable.cs

@@ -0,0 +1,8 @@
+using Drawie.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+
+public interface IScalable
+{
+    public void Resize(VecD multiplier);
+}

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

@@ -120,4 +120,5 @@ public class LineVectorData : ShapeVectorData, IReadOnlyLineData
 
 
         return path;
         return path;
     }
     }
+
 }
 }

+ 2 - 2
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Shapes/Data/RectangleVectorData.cs

@@ -10,8 +10,8 @@ namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
 
 
 public class RectangleVectorData : ShapeVectorData, IReadOnlyRectangleData
 public class RectangleVectorData : ShapeVectorData, IReadOnlyRectangleData
 {
 {
-    public VecD Center { get; }
-    public VecD Size { get; }
+    public VecD Center { get; set; }
+    public VecD Size { get; set; }
 
 
     public override RectD GeometryAABB => RectD.FromCenterAndSize(Center, Size);
     public override RectD GeometryAABB => RectD.FromCenterAndSize(Center, Size);
 
 

+ 21 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Shapes/Data/TextVectorData.cs

@@ -5,11 +5,12 @@ using Drawie.Backend.Core.Surfaces.PaintImpl;
 using Drawie.Backend.Core.Text;
 using Drawie.Backend.Core.Text;
 using Drawie.Backend.Core.Vector;
 using Drawie.Backend.Core.Vector;
 using Drawie.Numerics;
 using Drawie.Numerics;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces.Shapes;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces.Shapes;
 
 
 namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
 namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
 
 
-public class TextVectorData : ShapeVectorData, IReadOnlyTextData
+public class TextVectorData : ShapeVectorData, IReadOnlyTextData, IScalable
 {
 {
     private string text;
     private string text;
     private Font font = Font.CreateDefault();
     private Font font = Font.CreateDefault();
@@ -237,4 +238,23 @@ public class TextVectorData : ShapeVectorData, IReadOnlyTextData
 
 
         return hash.ToHashCode();
         return hash.ToHashCode();
     }
     }
+
+    public void Resize(VecD multiplier)
+    {
+        // TODO: Resize font size
+        /*Position = Position.Multiply(multiplier);
+        if(Font != null)
+        {
+            Font.Size *= multiplier.Y;
+        }
+
+        if (Spacing.HasValue)
+        {
+            Spacing *= multiplier.Y;
+        }*/
+
+        TransformationMatrix = TransformationMatrix.PostConcat(Matrix3X3.CreateScale((float)multiplier.X, (float)multiplier.Y));
+
+        lastBounds = richText.MeasureBounds(Font);
+    }
 }
 }

+ 20 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/VectorLayerNode.cs

@@ -16,7 +16,7 @@ using Drawie.Numerics;
 namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 
 
 [NodeInfo("VectorLayer")]
 [NodeInfo("VectorLayer")]
-public class VectorLayerNode : LayerNode, ITransformableObject, IReadOnlyVectorNode, IRasterizable
+public class VectorLayerNode : LayerNode, ITransformableObject, IReadOnlyVectorNode, IRasterizable, IScalable
 {
 {
     public OutputProperty<ShapeVectorData> Shape { get; }
     public OutputProperty<ShapeVectorData> Shape { get; }
 
 
@@ -190,4 +190,23 @@ public class VectorLayerNode : LayerNode, ITransformableObject, IReadOnlyVectorN
             AllowHighDpiRendering = this.AllowHighDpiRendering
             AllowHighDpiRendering = this.AllowHighDpiRendering
         };
         };
     }
     }
+
+    public void Resize(VecD multiplier)
+    {
+        if (ShapeData == null)
+        {
+            return;
+        }
+
+        if(ShapeData is IScalable resizable)
+        {
+            resizable.Resize(multiplier);
+        }
+        else
+        {
+            ShapeData.TransformationMatrix =
+                ShapeData.TransformationMatrix.PostConcat(Matrix3X3.CreateScale((float)multiplier.X,
+                    (float)multiplier.Y));
+        }
+    }
 }
 }

+ 11 - 0
src/PixiEditor.ChangeableDocument/Changes/Root/ResizeImage_Change.cs

@@ -6,6 +6,7 @@ using Drawie.Backend.Core.Numerics;
 using Drawie.Backend.Core.Surfaces;
 using Drawie.Backend.Core.Surfaces;
 using Drawie.Backend.Core.Surfaces.PaintImpl;
 using Drawie.Backend.Core.Surfaces.PaintImpl;
 using Drawie.Numerics;
 using Drawie.Numerics;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using BlendMode = Drawie.Backend.Core.Surfaces.BlendMode;
 using BlendMode = Drawie.Backend.Core.Surfaces.BlendMode;
 
 
 namespace PixiEditor.ChangeableDocument.Changes.Root;
 namespace PixiEditor.ChangeableDocument.Changes.Root;
@@ -98,6 +99,11 @@ internal class ResizeImage_Change : Change
                     img.CommitChanges();
                     img.CommitChanges();
                 });
                 });
             }
             }
+            else if (member is IScalable scalableLayer)
+            {
+                VecD multiplier = new VecD(newSize.X / (double)originalSize.X, newSize.Y / (double)originalSize.Y);
+                scalableLayer.Resize(multiplier);
+            }
 
 
             // Add support for different Layer types
             // Add support for different Layer types
 
 
@@ -129,6 +135,11 @@ internal class ResizeImage_Change : Change
                     layerImage.CommitChanges();
                     layerImage.CommitChanges();
                 });
                 });
             }
             }
+            else if (member is IScalable scalableLayer)
+            {
+                VecD multiplier = new VecD(originalSize.X / (double)newSize.X, originalSize.Y / (double)newSize.Y);
+                scalableLayer.Resize(multiplier);
+            }
 
 
             if (member.EmbeddedMask is not null)
             if (member.EmbeddedMask is not null)
             {
             {

+ 15 - 0
src/PixiEditor/Views/Main/ViewportControls/Viewport.axaml.cs

@@ -418,12 +418,27 @@ internal partial class Viewport : UserControl, INotifyPropertyChanged
 
 
         DocumentViewModel? oldDoc = e.OldValue.Value;
         DocumentViewModel? oldDoc = e.OldValue.Value;
 
 
+        if (oldDoc != null)
+        {
+            oldDoc.SizeChanged -= viewport.OnDocumentSizeChanged;
+        }
+
         DocumentViewModel? newDoc = e.NewValue.Value;
         DocumentViewModel? newDoc = e.NewValue.Value;
 
 
+        if (newDoc != null)
+        {
+            newDoc.SizeChanged += viewport.OnDocumentSizeChanged;
+        }
+
         oldDoc?.Operations.RemoveViewport(viewport.GuidValue);
         oldDoc?.Operations.RemoveViewport(viewport.GuidValue);
         newDoc?.Operations.AddOrUpdateViewport(viewport.GetLocation());
         newDoc?.Operations.AddOrUpdateViewport(viewport.GetLocation());
     }
     }
 
 
+    private void OnDocumentSizeChanged(object? sender, DocumentSizeChangedEventArgs documentSizeChangedEventArgs)
+    {
+        scene.CenterContent(documentSizeChangedEventArgs.NewSize);
+    }
+
     private ChunkResolution CalculateResolution()
     private ChunkResolution CalculateResolution()
     {
     {
         VecD densityVec = Dimensions.Divide(RealDimensions);
         VecD densityVec = Dimensions.Divide(RealDimensions);