Forráskód Böngészése

Fixed reference layer

flabbet 10 hónapja
szülő
commit
5d7565b667

+ 7 - 7
src/PixiEditor/Models/DocumentModels/Public/DocumentOperationsModule.cs

@@ -260,7 +260,7 @@ internal class DocumentOperationsModule : IDocumentOperations
 
         Internals.ChangeController.TryStopActiveExecutor();
 
-        if (Document.ReferenceLayerHandler.ReferenceBitmap is not null)
+        if (Document.ReferenceLayerHandler.ReferenceTexture is not null)
         {
             VecI offset = anchor.FindOffsetFor(Document.SizeBindable, newSize);
             ShapeCorners curShape = Document.ReferenceLayerHandler.ReferenceShapeBindable;
@@ -292,7 +292,7 @@ internal class DocumentOperationsModule : IDocumentOperations
 
         Internals.ChangeController.TryStopActiveExecutor();
 
-        if (Document.ReferenceLayerHandler.ReferenceBitmap is not null)
+        if (Document.ReferenceLayerHandler.ReferenceTexture is not null)
         {
             VecD scale = ((VecD)newSize).Divide(Document.SizeBindable);
             ShapeCorners curShape = Document.ReferenceLayerHandler.ReferenceShapeBindable;
@@ -693,7 +693,7 @@ internal class DocumentOperationsModule : IDocumentOperations
     /// </summary>
     public void DeleteReferenceLayer()
     {
-        if (Internals.ChangeController.IsBlockingChangeActive || Document.ReferenceLayerHandler.ReferenceBitmap is null)
+        if (Internals.ChangeController.IsBlockingChangeActive || Document.ReferenceLayerHandler.ReferenceTexture is null)
             return;
 
         Internals.ChangeController.TryStopActiveExecutor();
@@ -706,7 +706,7 @@ internal class DocumentOperationsModule : IDocumentOperations
     /// </summary>
     public void TransformReferenceLayer()
     {
-        if (Document.ReferenceLayerHandler.ReferenceBitmap is null || Internals.ChangeController.IsBlockingChangeActive)
+        if (Document.ReferenceLayerHandler.ReferenceTexture is null || Internals.ChangeController.IsBlockingChangeActive)
             return;
         
         Internals.ChangeController.TryStopActiveExecutor();
@@ -719,13 +719,13 @@ internal class DocumentOperationsModule : IDocumentOperations
     /// </summary>
     public void ResetReferenceLayerPosition()
     {
-        if (Document.ReferenceLayerHandler.ReferenceBitmap is null || Internals.ChangeController.IsBlockingChangeActive)
+        if (Document.ReferenceLayerHandler.ReferenceTexture is null || Internals.ChangeController.IsBlockingChangeActive)
             return;
         
         Internals.ChangeController.TryStopActiveExecutor();
 
-        VecD size = new(Document.ReferenceLayerHandler.ReferenceBitmap.Size.X,
-            Document.ReferenceLayerHandler.ReferenceBitmap.Size.Y);
+        VecD size = new(Document.ReferenceLayerHandler.ReferenceTexture.Size.X,
+            Document.ReferenceLayerHandler.ReferenceTexture.Size.Y);
         RectD referenceImageRect = new RectD(VecD.Zero, Document.SizeBindable).AspectFit(new RectD(VecD.Zero, size));
         ShapeCorners corners = new ShapeCorners(referenceImageRect);
         Internals.ActionAccumulator.AddFinishedActions(

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

@@ -23,7 +23,7 @@ internal class ColorPickerToolExecutor : UpdateableChangeExecutor
             return ExecutionState.Error;
 
         scope = tool.Mode;
-        includeReference = tool.PickFromReferenceLayer && document!.ReferenceLayerHandler.ReferenceBitmap is not null;
+        includeReference = tool.PickFromReferenceLayer && document!.ReferenceLayerHandler.ReferenceTexture is not null;
         includeCanvas = tool.PickFromCanvas;
         
         colorsViewModel.PrimaryColor = document.PickColor(controller.LastPrecisePosition, scope, includeReference, includeCanvas, document.AnimationHandler.ActiveFrameBindable, document.ReferenceLayerHandler.IsTopMost);

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

@@ -10,7 +10,7 @@ internal class TransformReferenceLayerExecutor : UpdateableChangeExecutor, ITran
 {
     public override ExecutionState Start()
     {
-        if (document!.ReferenceLayerHandler.ReferenceBitmap is null)
+        if (document!.ReferenceLayerHandler.ReferenceTexture is null)
             return ExecutionState.Error;
 
         ShapeCorners corners = document.ReferenceLayerHandler.ReferenceShapeBindable;

+ 3 - 2
src/PixiEditor/Models/Handlers/IReferenceLayerHandler.cs

@@ -11,11 +11,12 @@ namespace PixiEditor.Models.Handlers;
 
 public interface IReferenceLayerHandler : IHandler
 {
-    public Texture? ReferenceBitmap { get; }
+    public Texture? ReferenceTexture { get; }
     public ShapeCorners ReferenceShapeBindable { get; set; }
     public bool IsTopMost { get; set; }
+    public bool IsVisible { get;  }
     public bool IsTransforming { get; set; }
-    public Matrix ReferenceTransformMatrix { get; }
+    public Matrix3X3 ReferenceTransformMatrix { get; }
     public void SetReferenceLayerIsVisible(bool infoIsVisible);
     public void TransformReferenceLayer(ShapeCorners infoCorners);
     public void DeleteReferenceLayer();

+ 3 - 0
src/PixiEditor/Models/Rendering/SceneRenderer.cs

@@ -1,14 +1,17 @@
 using ChunkyImageLib.DataHolders;
+using Drawie.Backend.Core.ColorsImpl;
 using PixiEditor.ChangeableDocument.Changeables.Animations;
 using PixiEditor.ChangeableDocument.Changeables.Interfaces;
 using PixiEditor.ChangeableDocument.Rendering;
 using Drawie.Backend.Core.Surfaces;
+using Drawie.Backend.Core.Surfaces.PaintImpl;
 using PixiEditor.Models.Handlers;
 
 namespace PixiEditor.Models.Rendering;
 
 internal class SceneRenderer
 {
+    
     public IReadOnlyDocument Document { get; }
     public IDocument DocumentViewModel { get; }
 

+ 3 - 3
src/PixiEditor/ViewModels/Document/DocumentViewModel.cs

@@ -676,13 +676,13 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
 
     public Color? PickColorFromReferenceLayer(VecD pos)
     {
-        Texture? bitmap = ReferenceLayerViewModel.ReferenceBitmap;
+        Texture? bitmap = ReferenceLayerViewModel.ReferenceTexture;
         if (bitmap is null)
             return null;
 
-        Matrix matrix = ReferenceLayerViewModel.ReferenceTransformMatrix;
+        Matrix3X3 matrix = ReferenceLayerViewModel.ReferenceTransformMatrix;
         matrix = matrix.Invert();
-        var transformed = matrix.Transform(new Point(pos.X, pos.Y));
+        var transformed = matrix.MapPoint(pos);
 
         if (transformed.X < 0 || transformed.Y < 0 || transformed.X >= bitmap.Size.X || transformed.Y >= bitmap.Size.Y)
             return null;

+ 11 - 10
src/PixiEditor/ViewModels/Document/ReferenceLayerViewModel.cs

@@ -26,7 +26,7 @@ internal class ReferenceLayerViewModel : ObservableObject, IReferenceLayerHandle
 
     public const double TopMostOpacity = 0.6;
     
-    public Texture? ReferenceBitmap { get; private set; }
+    public Texture? ReferenceTexture { get; private set; }
 
     private ShapeCorners referenceShape;
     public ShapeCorners ReferenceShapeBindable 
@@ -39,15 +39,14 @@ internal class ReferenceLayerViewModel : ObservableObject, IReferenceLayerHandle
         }
     }
     
-    public Matrix ReferenceTransformMatrix
+    public Matrix3X3 ReferenceTransformMatrix
     {
         get
         {
-            if (ReferenceBitmap is null)
-                return Matrix.Identity;
+            if (ReferenceTexture is null)
+                return Matrix3X3.Identity;
 
-            Matrix3X3 skiaMatrix = OperationHelper.CreateMatrixFromPoints(ReferenceShapeBindable, new VecD(ReferenceBitmap.Size.X, ReferenceBitmap.Size.Y));
-            return new Matrix(skiaMatrix.ScaleX, skiaMatrix.SkewY, skiaMatrix.SkewX, skiaMatrix.ScaleY, skiaMatrix.TransX, skiaMatrix.TransY);
+            return OperationHelper.CreateMatrixFromPoints(ReferenceShapeBindable, new VecD(ReferenceTexture.Size.X, ReferenceTexture.Size.Y));
         }
     }
 
@@ -63,6 +62,8 @@ internal class ReferenceLayerViewModel : ObservableObject, IReferenceLayerHandle
     }
 
     private bool isTransforming;
+    bool IReferenceLayerHandler.IsVisible => isVisible;
+
     public bool IsTransforming
     {
         get => isTransforming;
@@ -114,12 +115,12 @@ internal class ReferenceLayerViewModel : ObservableObject, IReferenceLayerHandle
     
     public void SetReferenceLayer(ImmutableArray<byte> imageBgra8888Bytes, VecI imageSize, ShapeCorners shape)
     {
-        ReferenceBitmap = Texture.Load(imageBgra8888Bytes.ToArray(), ColorType.Bgra8888, imageSize); 
+        ReferenceTexture = Texture.Load(imageBgra8888Bytes.ToArray(), ColorType.Bgra8888, imageSize); 
         referenceShape = shape;
         isVisible = true;
         isTransforming = false;
         isTopMost = false;
-        OnPropertyChanged(nameof(ReferenceBitmap));
+        OnPropertyChanged(nameof(ReferenceTexture));
         OnPropertyChanged(nameof(ReferenceShapeBindable));
         OnPropertyChanged(nameof(ReferenceTransformMatrix));
         OnPropertyChanged(nameof(IsVisibleBindable));
@@ -129,9 +130,9 @@ internal class ReferenceLayerViewModel : ObservableObject, IReferenceLayerHandle
 
     public void DeleteReferenceLayer()
     {
-        ReferenceBitmap = null;
+        ReferenceTexture = null;
         isVisible = false;
-        OnPropertyChanged(nameof(ReferenceBitmap));
+        OnPropertyChanged(nameof(ReferenceTexture));
         OnPropertyChanged(nameof(ReferenceTransformMatrix));
         OnPropertyChanged(nameof(IsVisibleBindable));
     }

+ 2 - 2
src/PixiEditor/ViewModels/SubViewModels/LayersViewModel.cs

@@ -376,12 +376,12 @@ internal class LayersViewModel : SubViewModel<ViewModelMain>
 
     [Evaluator.CanExecute("PixiEditor.Layer.ReferenceLayerExists")]
     public bool ReferenceLayerExists() =>
-        Owner.DocumentManagerSubViewModel.ActiveDocument?.ReferenceLayerViewModel.ReferenceBitmap is not null;
+        Owner.DocumentManagerSubViewModel.ActiveDocument?.ReferenceLayerViewModel.ReferenceTexture is not null;
 
     [Evaluator.CanExecute("PixiEditor.Layer.ReferenceLayerDoesntExist")]
     public bool ReferenceLayerDoesntExist() =>
         Owner.DocumentManagerSubViewModel.ActiveDocument is not null &&
-        Owner.DocumentManagerSubViewModel.ActiveDocument.ReferenceLayerViewModel.ReferenceBitmap is null;
+        Owner.DocumentManagerSubViewModel.ActiveDocument.ReferenceLayerViewModel.ReferenceTexture is null;
 
     [Command.Basic("PixiEditor.Layer.ImportReferenceLayer", "ADD_REFERENCE_LAYER", "ADD_REFERENCE_LAYER",
         CanExecute = "PixiEditor.Layer.ReferenceLayerDoesntExist",

+ 2 - 2
src/PixiEditor/ViewModels/Tools/Tools/ColorPickerToolViewModel.cs

@@ -103,7 +103,7 @@ internal class ColorPickerToolViewModel : ToolViewModel, IColorPickerHandler
 
     private void ReferenceLayerChanged(object sender, PropertyChangedEventArgs e)
     {
-        if (e.PropertyName is nameof(ReferenceLayerViewModel.ReferenceBitmap)
+        if (e.PropertyName is nameof(ReferenceLayerViewModel.ReferenceTexture)
             or nameof(ReferenceLayerViewModel.ReferenceShapeBindable))
         {
             UpdateActionDisplay();
@@ -131,7 +131,7 @@ internal class ColorPickerToolViewModel : ToolViewModel, IColorPickerHandler
         var documentBounds = new RectD(default, document.SizeBindable);
         var referenceLayer = document.ReferenceLayerViewModel;
 
-        if (referenceLayer.ReferenceBitmap == null || document.TransformViewModel.TransformActive ||
+        if (referenceLayer.ReferenceTexture == null || document.TransformViewModel.TransformActive ||
             !referenceLayer.ReferenceShapeBindable.Intersects(documentBounds))
         {
             PickFromCanvas = true;

+ 4 - 4
src/PixiEditor/Views/Layers/ReferenceLayer.axaml

@@ -34,13 +34,13 @@
             <controls:Shelf>
                 <Grid Height="40" x:Name="mainDockPanel">
                     <Grid
-                        IsVisible="{Binding Document.ReferenceLayerViewModel.ReferenceBitmap, ElementName=uc, Converter={converters:NullToVisibilityConverter}}"
+                        IsVisible="{Binding Document.ReferenceLayerViewModel.ReferenceTexture, ElementName=uc, Converter={converters:NullToVisibilityConverter}}"
                         Panel.ZIndex="5">
                         <Grid Cursor="Hand" IsVisible="{Binding ElementName=visibilityCheckbox, Path=!IsChecked}"
                               Background="Transparent">
                             <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" DockPanel.Dock="Left">
                                 <TextBlock Margin="5 0 5 0" VerticalAlignment="Center" FontSize="24" Text="{DynamicResource icon-add-reference}" Classes="pixi-icon"
-                                       IsVisible="{Binding Document.ReferenceLayerViewModel.ReferenceBitmap, ElementName=uc, Converter={converters:NullToVisibilityConverter}}" />
+                                       IsVisible="{Binding Document.ReferenceLayerViewModel.ReferenceTexture, ElementName=uc, Converter={converters:NullToVisibilityConverter}}" />
 
                                 <TextBlock IsEnabled="{Binding ElementName=uc, Path=IsEnabled}"
                                            Margin="0 0 5 0" Foreground="{DynamicResource ThemeForegroundBrush}"
@@ -57,7 +57,7 @@
                     </Grid>
 
                     <DockPanel Grid.Row="0" VerticalAlignment="Center" Height="40"
-                               IsVisible="{Binding Document.ReferenceLayerViewModel.ReferenceBitmap, ElementName=uc, Converter={converters:NotNullToVisibilityConverter}}">
+                               IsVisible="{Binding Document.ReferenceLayerViewModel.ReferenceTexture, ElementName=uc, Converter={converters:NotNullToVisibilityConverter}}">
                         <Grid Height="16" Name="layerVisibilityCheckboxGrid" DockPanel.Dock="Left" Margin="10,0,5,0">
                             <CheckBox
                                 Classes="ImageCheckBox"
@@ -80,7 +80,7 @@
                             BorderBrush="Black"
                             Background="{DynamicResource ThemeBackgroundBrush}"
                             Margin="5, 0, 10, 0">
-                            <visuals:TextureControl Texture="{Binding Document.ReferenceLayerViewModel.ReferenceBitmap,ElementName=uc}"
+                            <visuals:TextureControl Texture="{Binding Document.ReferenceLayerViewModel.ReferenceTexture,ElementName=uc}"
                                    Stretch="Uniform" Width="26" Height="26"
                                    RenderOptions.BitmapInterpolationMode="HighQuality" IsHitTestVisible="False" />
                         </Border>

+ 20 - 25
src/PixiEditor/Views/Overlays/ReferenceLayerOverlay.cs

@@ -19,6 +19,7 @@ using PixiEditor.ViewModels.Document;
 using PixiEditor.Views.Visuals;
 using Canvas = Drawie.Backend.Core.Surfaces.Canvas;
 using Color = Drawie.Backend.Core.ColorsImpl.Color;
+using Colors = Drawie.Backend.Core.ColorsImpl.Colors;
 
 namespace PixiEditor.Views.Overlays;
 
@@ -56,15 +57,15 @@ internal class ReferenceLayerOverlay : Overlay
         set => SetValue(FadeOutProperty, value);
     }
 
-    public double ReferenceLayerScale => ((ReferenceLayer.ReferenceBitmap != null && ReferenceShape != null)
-        ? (ReferenceShape.RectSize.X / (double)ReferenceLayer.ReferenceBitmap.Size.X)
+    public double ReferenceLayerScale => ((ReferenceLayer.ReferenceTexture != null && ReferenceShape != null)
+        ? (ReferenceShape.RectSize.X / (double)ReferenceLayer.ReferenceTexture.Size.X)
         : 1);
 
     public override OverlayRenderSorting OverlayRenderSorting => ReferenceLayer.IsTopMost
         ? OverlayRenderSorting.Foreground
         : OverlayRenderSorting.Background;
 
-    private Pen borderBen = new Pen(Brushes.Black, 2);
+    private Paint borderBen = new Paint() { Color = Colors.Black, StrokeWidth = 2, Style = PaintStyle.Stroke };
     
     private Paint overlayPaint = new Paint
     {
@@ -80,39 +81,33 @@ internal class ReferenceLayerOverlay : Overlay
 
     public override void RenderOverlay(Canvas context, RectD dirtyCanvasBounds)
     {
-        /*if (ReferenceLayer is { ReferenceBitmap: not null })
+        if (ReferenceLayer is { ReferenceTexture: not null })
         {
-            using var renderOptions = context.PushRenderOptions(new RenderOptions
-            {
-                BitmapInterpolationMode = ScaleToBitmapScalingModeConverter.Calculate(ReferenceLayerScale)
-            });
+            var interpolationMode = ScaleToBitmapScalingModeConverter.Calculate(ReferenceLayerScale);
 
-            var matrix = context.PushTransform(ReferenceLayer.ReferenceTransformMatrix);
+            int saved = context.Save();
+            
+            context.SetMatrix(context.TotalMatrix.Concat(ReferenceLayer.ReferenceTransformMatrix));
 
-            RectD dirty = new RectD(0, 0, ReferenceLayer.ReferenceBitmap.Size.X, ReferenceLayer.ReferenceBitmap.Size.Y);
+            RectD dirty = new RectD(0, 0, ReferenceLayer.ReferenceTexture.Size.X, ReferenceLayer.ReferenceTexture.Size.Y);
             Rect dirtyRect = new Rect(dirty.X, dirty.Y, dirty.Width, dirty.Height);
 
             double opacity = Opacity;
-            var referenceBitmap = ReferenceLayer.ReferenceBitmap;
-
-            referenceBitmap.DrawingSurface.Flush();
-            overlayPaint.Color = new Color(255, 255, 255, (byte)(opacity * 255)); 
-            
-            // TODO: Implement this
-            /*DrawTextureOperation drawOperation =
-                new DrawTextureOperation(dirtyRect, Stretch.None, referenceBitmap, overlayPaint);#1#
-
-            //context.Custom(drawOperation);
+            var referenceBitmap = ReferenceLayer.ReferenceTexture;
 
-            matrix.Dispose();
+            overlayPaint.Color = new Color(255, 255, 255, (byte)(opacity * 255));
 
+            context.DrawSurface(referenceBitmap.DrawingSurface, 0, 0, overlayPaint); 
+            
+            context.RestoreToCount(saved);
+            
             DrawBorder(context, dirtyCanvasBounds);
-        }*/
+        }
     }
 
-    private void DrawBorder(DrawingContext context, RectD dirtyCanvasBounds)
+    private void DrawBorder(Canvas context, RectD dirtyCanvasBounds)
     {
-        context.DrawRectangle(borderBen, new Rect(0, 0, dirtyCanvasBounds.Width, dirtyCanvasBounds.Height));
+        context.DrawRect(new RectD(0, 0, dirtyCanvasBounds.Width, dirtyCanvasBounds.Height), borderBen);
     }
 
     private static void ReferenceLayerChanged(AvaloniaPropertyChangedEventArgs<ReferenceLayerViewModel> obj)
@@ -131,7 +126,7 @@ internal class ReferenceLayerOverlay : Overlay
 
     protected override void ZoomChanged(double newZoom)
     {
-        borderBen.Thickness = 2 / newZoom;
+        borderBen.StrokeWidth = 2 / (float)newZoom;
     }
 
     private void ToggleFadeOut(bool toggle)

+ 0 - 1
src/PixiEditor/Views/Rendering/Scene.cs

@@ -236,7 +236,6 @@ internal class Scene : Zoombox.Zoombox, ICustomHitTest
         renderTexture.Canvas.SetMatrix(matrix.ToSKMatrix().ToMatrix3X3());
 
         RectD dirtyBounds = new RectD(0, 0, Document.Width, Document.Height);
-        //Cursor = DefaultCursor;
         RenderScene(dirtyBounds);
 
         renderTexture.Canvas.Restore();