Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Equbuxu committed Sep 16, 2021
1 parent 34c6f90 commit 4049155
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using PixiEditor.Models.DataHolders;
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace PixiEditor.Helpers.Converters
{
class DockingManagerActiveContentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return DependencyProperty.UnsetValue;
if (value is Document document)
return document;
return DependencyProperty.UnsetValue;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Document document)
return document;
return DependencyProperty.UnsetValue;
}
}
}
12 changes: 6 additions & 6 deletions PixiEditor/Helpers/Converters/IndexOfConverter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using PixiEditor.Models.Layers;
using PixiEditor.ViewModels;
using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using PixiEditor.Models.Layers;
using PixiEditor.ViewModels;

namespace PixiEditor.Helpers.Converters
{
Expand All @@ -13,7 +12,8 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
{
if (value is Layer layer && ViewModelMain.Current.BitmapManager.ActiveDocument != null)
{
return ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.IndexOf(layer);
int index = ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.IndexOf(layer);
return index;
}

return Binding.DoNothing;
Expand All @@ -24,4 +24,4 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
throw new NotImplementedException();
}
}
}
}
31 changes: 25 additions & 6 deletions PixiEditor/Helpers/Converters/LayersToStructuredLayersConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace PixiEditor.Helpers.Converters
Expand All @@ -12,7 +13,8 @@ namespace PixiEditor.Helpers.Converters
public class LayersToStructuredLayersConverter : IMultiValueConverter
{
private static StructuredLayerTree cachedTree;
private List<Guid> lastLayers = new List<Guid>();
private List<Guid> lastLayerGuids = new List<Guid>();
private IList<Layer> lastLayers = new List<Layer>();
private ObservableCollection<GuidStructureItem> lastStructure = new ObservableCollection<GuidStructureItem>();

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
Expand All @@ -24,18 +26,21 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
cachedTree = new StructuredLayerTree(layers, structure);
}

if (TryFindStructureDifferences(structure) || lastLayers.Count != layers.Count || LayerOrderIsDifferent(layers))
if (TryFindStructureDifferences(structure) ||
lastLayerGuids.Count != layers.Count ||
LayerOrderIsDifferent(layers) ||
LayersAreDifferentObjects(layers, lastLayers))
{
cachedTree = new StructuredLayerTree(layers, structure);

lastLayers = layers.Select(x => x.LayerGuid).ToList();
lastLayers = layers;
lastLayerGuids = layers.Select(x => x.LayerGuid).ToList();
lastStructure = structure.CloneGroups();
}

return cachedTree.RootDirectoryItems;
}

return new StructuredLayerTree(null, null);
return DependencyProperty.UnsetValue;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
Expand All @@ -44,7 +49,21 @@ public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
private bool LayerOrderIsDifferent(IList<Layer> layers)
{
var guids = layers.Select(x => x.LayerGuid).ToArray();
return !guids.SequenceEqual(lastLayers);
return !guids.SequenceEqual(lastLayerGuids);
}

/// <summary>
/// This should trigger if you open and close the same files twice.
/// Even though the layers are technically the same, having two different objects screws things up down the line.
/// </summary>
private bool LayersAreDifferentObjects(IList<Layer> layers, IList<Layer> lastLayers)
{
for (int i = 0; i < layers.Count; i++)
{
if (layers[i] != lastLayers[i])
return true;
}
return false;
}
private bool TryFindStructureDifferences(LayerStructure structure)
{
Expand Down
31 changes: 31 additions & 0 deletions PixiEditor/Helpers/Extensions/Int32RectEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Windows;

namespace PixiEditor.Helpers.Extensions
{
static class Int32RectEx
{
public static Int32Rect Intersect(this Int32Rect rect, Int32Rect other)
{
int rectX2 = rect.X + rect.Width;
int rectY2 = rect.Y + rect.Height;

int otherX2 = other.X + other.Width;
int otherY2 = other.Y + other.Height;

int maxX1 = Math.Max(rect.X, other.X);
int maxY1 = Math.Max(rect.Y, other.Y);

int minX2 = Math.Min(rectX2, otherX2);
int minY2 = Math.Min(rectY2, otherY2);

int width = minX2 - maxX1;
int height = minY2 - maxY1;

if (width <= 0 || height <= 0)
return Int32Rect.Empty;

return new Int32Rect(maxX1, maxY1, width, height);
}
}
}
25 changes: 24 additions & 1 deletion PixiEditor/Models/Controllers/LayerStackRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PixiEditor.Models.DataHolders;
using PixiEditor.Helpers.Extensions;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Layers;
using PixiEditor.Models.Layers.Utils;
using SkiaSharp;
Expand Down Expand Up @@ -39,6 +40,7 @@ public LayerStackRenderer(ObservableCollection<Layer> layers, LayerStructure str
this.layers = layers;
this.structure = structure;
layers.CollectionChanged += OnLayersChanged;
SubscribeToAllLayers(layers);
Resize(width, height);
}

Expand All @@ -56,7 +58,9 @@ public void Resize(int newWidth, int newHeight)
public void SetNewLayersCollection(ObservableCollection<Layer> layers)
{
layers.CollectionChanged -= OnLayersChanged;
UnsubscribeFromAllLayers(this.layers);
this.layers = layers;
SubscribeToAllLayers(layers);
layers.CollectionChanged += OnLayersChanged;
Update(new Int32Rect(0, 0, finalSurface.Width, finalSurface.Height));
}
Expand All @@ -69,6 +73,22 @@ public void Dispose()
layers.CollectionChanged -= OnLayersChanged;
}

private void SubscribeToAllLayers(ObservableCollection<Layer> layers)
{
foreach (var layer in layers)
{
layer.LayerBitmapChanged += OnLayerBitmapChanged;
}
}

private void UnsubscribeFromAllLayers(ObservableCollection<Layer> layers)
{
foreach (var layer in layers)
{
layer.LayerBitmapChanged -= OnLayerBitmapChanged;
}
}

private void Update(Int32Rect dirtyRectangle)
{
finalSurface.SkiaSurface.Canvas.Clear();
Expand All @@ -85,6 +105,9 @@ private void Update(Int32Rect dirtyRectangle)
}
finalBitmap.Lock();
finalSurface.SkiaSurface.Draw(backingSurface.Canvas, 0, 0, Surface.ReplacingPaint);

dirtyRectangle = dirtyRectangle.Intersect(new Int32Rect(0, 0, finalBitmap.PixelWidth, finalBitmap.PixelHeight));

finalBitmap.AddDirtyRect(dirtyRectangle);
finalBitmap.Unlock();
}
Expand Down
5 changes: 4 additions & 1 deletion PixiEditor/Models/Controllers/SingleLayerRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PixiEditor.Models.Layers;
using PixiEditor.Helpers.Extensions;
using PixiEditor.Models.Layers;
using SkiaSharp;
using System;
using System.ComponentModel;
Expand Down Expand Up @@ -63,6 +64,8 @@ private void Update(Int32Rect dirtyRectangle)
layer.OffsetY,
BlendingPaint);
}
dirtyRectangle = dirtyRectangle.Intersect(new Int32Rect(0, 0, finalBitmap.PixelWidth, finalBitmap.PixelHeight));

finalBitmap.AddDirtyRect(dirtyRectangle);
finalBitmap.Unlock();
}
Expand Down
8 changes: 4 additions & 4 deletions PixiEditor/Models/DataHolders/Document/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ private int GetOffsetXForAnchor(int srcWidth, int destWidth, AnchorPoint anchor)
{
if (anchor.HasFlag(AnchorPoint.Center))
{
return Math.Abs((destWidth / 2) - (srcWidth / 2));
return (destWidth / 2) - (srcWidth / 2);
}

if (anchor.HasFlag(AnchorPoint.Right))
{
return Math.Abs(destWidth - srcWidth);
return destWidth - srcWidth;
}

return 0;
Expand All @@ -219,12 +219,12 @@ private int GetOffsetYForAnchor(int srcHeight, int destHeight, AnchorPoint ancho
{
if (anchor.HasFlag(AnchorPoint.Middle))
{
return Math.Abs((destHeight / 2) - (srcHeight / 2));
return (destHeight / 2) - (srcHeight / 2);
}

if (anchor.HasFlag(AnchorPoint.Bottom))
{
return Math.Abs(destHeight - srcHeight);
return destHeight - srcHeight;
}

return 0;
Expand Down
3 changes: 2 additions & 1 deletion PixiEditor/Models/Layers/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace PixiEditor.Models.Layers
[DebuggerDisplay("'{name,nq}' {width}x{height}")]
public class Layer : BasicLayer
{
private const int SizeOfArgb = 4;
private bool clipRequested;

private bool isActive;
Expand Down Expand Up @@ -151,6 +150,7 @@ public float Opacity
get => opacity;
set
{
opacity = value;
RaisePropertyChanged(nameof(OpacityUndoTriggerable));
ViewModelMain.Current?.ToolsSubViewModel?.TriggerCacheOutdated();
InvokeLayerBitmapChange();
Expand Down Expand Up @@ -189,6 +189,7 @@ public Thickness Offset
{
offset = value;
RaisePropertyChanged("Offset");
InvokeLayerBitmapChange();
}
}

Expand Down
3 changes: 2 additions & 1 deletion PixiEditor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<converters:IsSpecifiedTypeConverter SpecifiedType="{x:Type tools:ZoomTool}" x:Key="IsZoomToolConverter"/>
<converters:IsSpecifiedTypeConverter SpecifiedType="{x:Type tools:MoveViewportTool}" x:Key="IsMoveViewportToolConverter"/>
<converters:SKColorToMediaColorConverter x:Key="SKColorToMediaColorConverter"/>
<converters:DockingManagerActiveContentConverter x:Key="DockingManagerActiveContentConverter"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ColorPicker;component/Styles/DefaultColorPickerStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
Expand Down Expand Up @@ -240,7 +241,7 @@
</StackPanel>
<Grid Grid.Column="1" Grid.Row="2" Background="#303030">
<Grid AllowDrop="True" Drop="MainWindow_Drop">
<DockingManager ActiveContent="{Binding BitmapManager.ActiveDocument, Mode=TwoWay}"
<DockingManager ActiveContent="{Binding BitmapManager.ActiveDocument, Mode=TwoWay, Converter={StaticResource DockingManagerActiveContentConverter}}"
DocumentsSource="{Binding BitmapManager.Documents}">
<DockingManager.Theme>
<avalonDockTheme:PixiEditorDockTheme />
Expand Down
13 changes: 13 additions & 0 deletions PixiEditorTests/ModelsTests/DataHoldersTests/SurfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,18 @@ public void TestSurfaceToWriteableBitmap()
int offset = (30 * 5 + 5) * 4;
Assert.Equal(greenColor, new SKColor(pixels[2 + offset], pixels[1 + offset], pixels[0 + offset], pixels[3 + offset]));
}

[Fact]
public void TestSurfaceFromWriteableBitmap()
{
using Surface original = new Surface(30, 30);
original.SkiaSurface.Canvas.Clear(SKColors.Transparent);
original.SkiaSurface.Canvas.DrawRect(5, 5, 20, 20, redPaint);
original.SkiaSurface.Canvas.DrawRect(10, 10, 20, 20, greenPaint);
using Surface fromWriteable = new Surface(original.ToWriteableBitmap());
Assert.Equal(original.GetSRGBPixel(0, 0), fromWriteable.GetSRGBPixel(0, 0));
Assert.Equal(original.GetSRGBPixel(6, 6), fromWriteable.GetSRGBPixel(6, 6));
Assert.Equal(original.GetSRGBPixel(15, 15), fromWriteable.GetSRGBPixel(15, 15));
}
}
}

0 comments on commit 4049155

Please sign in to comment.