Browse Source

Somewhat fixed FixedViewport

Krzysztof Krysiński 1 year ago
parent
commit
8a4f101d16

+ 0 - 2
src/PixiEditor.AvaloniaUI/Views/Main/ViewportControls/FixedViewport.axaml

@@ -17,8 +17,6 @@
         x:Name="mainImage"
         x:Name="mainImage"
         Focusable="True"
         Focusable="True"
         Surface="{Binding TargetBitmap, ElementName=uc}"
         Surface="{Binding TargetBitmap, ElementName=uc}"
-        Width="{Binding Document.Width, ElementName=uc}"
-        Height="{Binding Document.Height, ElementName=uc}"
         Stretch="Uniform"
         Stretch="Uniform"
         SizeChanged="OnImageSizeChanged">
         SizeChanged="OnImageSizeChanged">
         <ui:RenderOptionsBindable.BitmapInterpolationMode>
         <ui:RenderOptionsBindable.BitmapInterpolationMode>

+ 2 - 2
src/PixiEditor.AvaloniaUI/Views/Main/ViewportControls/FixedViewport.axaml.cs

@@ -10,7 +10,6 @@ using ChunkyImageLib.DataHolders;
 using PixiEditor.AvaloniaUI.Models.Position;
 using PixiEditor.AvaloniaUI.Models.Position;
 using PixiEditor.AvaloniaUI.ViewModels.Document;
 using PixiEditor.AvaloniaUI.ViewModels.Document;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.DrawingApi.Core.Numerics;
-using PixiEditor.DrawingApi.Core.Surface;
 
 
 namespace PixiEditor.AvaloniaUI.Views.Main.ViewportControls;
 namespace PixiEditor.AvaloniaUI.Views.Main.ViewportControls;
 
 
@@ -98,7 +97,7 @@ internal partial class FixedViewport : UserControl, INotifyPropertyChanged
 
 
     private void ForceRefreshFinalImage()
     private void ForceRefreshFinalImage()
     {
     {
-        mainImage.InvalidateVisual();
+        mainImage.RequestNextFrameRendering();
     }
     }
 
 
     private ViewportInfo GetLocation()
     private ViewportInfo GetLocation()
@@ -125,6 +124,7 @@ internal partial class FixedViewport : UserControl, INotifyPropertyChanged
         FixedViewport? viewport = (FixedViewport)args.Sender;
         FixedViewport? viewport = (FixedViewport)args.Sender;
         oldDoc?.Operations.RemoveViewport(viewport.GuidValue);
         oldDoc?.Operations.RemoveViewport(viewport.GuidValue);
         newDoc?.Operations.AddOrUpdateViewport(viewport.GetLocation());
         newDoc?.Operations.AddOrUpdateViewport(viewport.GetLocation());
+        viewport.PropertyChanged?.Invoke(viewport, new(nameof(TargetBitmap)));
     }
     }
 
 
     private static void OnBitmapsChange(AvaloniaPropertyChangedEventArgs<Dictionary<ChunkResolution, WriteableBitmap>> args)
     private static void OnBitmapsChange(AvaloniaPropertyChangedEventArgs<Dictionary<ChunkResolution, WriteableBitmap>> args)

+ 36 - 0
src/PixiEditor.AvaloniaUI/Views/Visuals/SurfaceControl.cs

@@ -36,6 +36,7 @@ public class SurfaceControl : OpenGlControlBase
     static SurfaceControl()
     static SurfaceControl()
     {
     {
         AffectsRender<SurfaceControl>(StretchProperty, SurfaceProperty);
         AffectsRender<SurfaceControl>(StretchProperty, SurfaceProperty);
+        AffectsMeasure<SurfaceControl>(StretchProperty, SurfaceProperty);
         BoundsProperty.Changed.AddClassHandler<SurfaceControl>(BoundsChanged);
         BoundsProperty.Changed.AddClassHandler<SurfaceControl>(BoundsChanged);
         SurfaceProperty.Changed.AddClassHandler<SurfaceControl>(Rerender);
         SurfaceProperty.Changed.AddClassHandler<SurfaceControl>(Rerender);
         StretchProperty.Changed.AddClassHandler<SurfaceControl>(Rerender);
         StretchProperty.Changed.AddClassHandler<SurfaceControl>(Rerender);
@@ -46,6 +47,41 @@ public class SurfaceControl : OpenGlControlBase
         ClipToBounds = true;
         ClipToBounds = true;
     }
     }
 
 
+    /// <summary>
+    /// Measures the control.
+    /// </summary>
+    /// <param name="availableSize">The available size.</param>
+    /// <returns>The desired size of the control.</returns>
+    protected override Size MeasureOverride(Size availableSize)
+    {
+        var source = Surface;
+        var result = new Size();
+
+        if (source != null)
+        {
+            result = Stretch.CalculateSize(availableSize, new Size(source.Size.X, source.Size.Y));
+        }
+
+        return result;
+    }
+
+    /// <inheritdoc/>
+    protected override Size ArrangeOverride(Size finalSize)
+    {
+        var source = Surface;
+
+        if (source != null)
+        {
+            var sourceSize = source.Size;
+            var result = Stretch.CalculateSize(finalSize, new Size(sourceSize.X, sourceSize.Y));
+            return result;
+        }
+        else
+        {
+            return new Size();
+        }
+    }
+
     protected override void OnOpenGlInit(GlInterface gl)
     protected override void OnOpenGlInit(GlInterface gl)
     {
     {
         gr = GRContext.CreateGl(GRGlInterface.Create(gl.GetProcAddress));
         gr = GRContext.CreateGl(GRGlInterface.Create(gl.GetProcAddress));

+ 2 - 3
src/PixiEditor.AvaloniaUI/Views/Windows/HelloTherePopup.axaml

@@ -116,14 +116,13 @@
                                                     Padding="0"
                                                     Padding="0"
                                                     Command="{Binding DataContext.OpenRecentCommand, RelativeSource={RelativeSource AncestorType=panels:AlignableWrapPanel}}"
                                                     Command="{Binding DataContext.OpenRecentCommand, RelativeSource={RelativeSource AncestorType=panels:AlignableWrapPanel}}"
                                                     CommandParameter="{Binding FilePath}"
                                                     CommandParameter="{Binding FilePath}"
-                                                    
                                                     x:Name="fileButton">
                                                     x:Name="fileButton">
                                                 <Grid Width="100" Height="100">
                                                 <Grid Width="100" Height="100">
                                                     <visuals:SurfaceControl
                                                     <visuals:SurfaceControl
                                                         Surface="{Binding PreviewBitmap}"
                                                         Surface="{Binding PreviewBitmap}"
+                                                        Margin="10"
                                                         Stretch="Uniform"
                                                         Stretch="Uniform"
-                                                        x:Name="image"
-                                                        Margin="20">
+                                                        x:Name="image">
                                                         <ui1:RenderOptionsBindable.BitmapInterpolationMode>
                                                         <ui1:RenderOptionsBindable.BitmapInterpolationMode>
                                                             <MultiBinding Converter="{converters:WidthToBitmapScalingModeConverter}">
                                                             <MultiBinding Converter="{converters:WidthToBitmapScalingModeConverter}">
                                                                 <Binding Path="PreviewBitmap.Size.X"/>
                                                                 <Binding Path="PreviewBitmap.Size.X"/>