Skip to content

Commit

Permalink
Move tool wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Equbuxu committed Dec 4, 2021
1 parent c84716d commit 8e5fbb6
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 121 deletions.
2 changes: 1 addition & 1 deletion PixiEditor/Models/Controllers/ClipboardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class ClipboardController
public static void CopyToClipboard(Layer[] layers, Coordinates[] selection, int originalImageWidth, int originalImageHeight, SerializableDocument document = null)
{
Clipboard.Clear();
using Surface surface = BitmapUtils.CombineLayers(originalImageWidth, originalImageHeight, layers);
using Surface surface = BitmapUtils.CombineLayers(new Int32Rect(0, 0, originalImageWidth, originalImageHeight), layers);
DataObject data = new DataObject();

WriteableBitmap combinedBitmaps = surface.ToWriteableBitmap();
Expand Down
5 changes: 5 additions & 0 deletions PixiEditor/Models/DataHolders/Selection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public void SetSelection(IEnumerable<Coordinates> selection, SelectionType mode)
SelectionLayer.SetPixels(BitmapPixelChanges.FromSingleColoredArray(selection, selectionColor));
}

public void TranslateSelection(int dX, int dY)
{
//TODO implement
}

public void SetSelection(Int32Rect rect, bool isCirclular, SelectionType mode)
{
using SKPaint paint = new()
Expand Down
14 changes: 11 additions & 3 deletions PixiEditor/Models/ImageManipulation/BitmapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PixiEditor.Models.ImageManipulation
{
public static class BitmapUtils
{
public static Surface CombineLayers(int width, int height, IEnumerable<Layer> layers, LayerStructure structure = null)
public static Surface CombineLayers(Int32Rect portion, IEnumerable<Layer> layers, LayerStructure structure = null)
{
Surface finalSurface = new(width, height);
Surface finalSurface = new(portion.Width, portion.Height);
using SKPaint paint = new();

for (int i = 0; i < layers.Count(); i++)
Expand All @@ -34,7 +35,14 @@ public static Surface CombineLayers(int width, int height, IEnumerable<Layer> la
throw new InvalidOperationException("Layers must not extend beyond canvas borders");
}

layer.LayerBitmap.SkiaSurface.Draw(finalSurface.SkiaSurface.Canvas, layer.OffsetX, layer.OffsetY, paint);
using SKImage snapshot = layer.LayerBitmap.SkiaSurface.Snapshot();
int x = portion.X - layer.OffsetX;
int y = portion.Y - layer.OffsetY;
finalSurface.SkiaSurface.Canvas.DrawImage(
snapshot,
new SKRect(x, y, portion.Width + x, portion.Height + y),
new SKRect(0, 0, portion.Width, portion.Height),
paint);
}

return finalSurface;
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/Models/Layers/LayerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static Layer MergeWith(this Layer thisLayer, Layer otherLayer, string new
int height = yCloser.Height + offsetY + yOther.Height;

// Merge both layers into a bitmap
Surface mergedBitmap = BitmapUtils.CombineLayers((int)documentsSize.X, (int)documentsSize.Y, new Layer[] { thisLayer, otherLayer });
Surface mergedBitmap = BitmapUtils.CombineLayers(new Int32Rect(0, 0, (int)documentsSize.X, (int)documentsSize.Y), new Layer[] { thisLayer, otherLayer });
mergedBitmap = mergedBitmap.Crop(xCloser.OffsetX, yCloser.OffsetY, width, height);

// Create the new layer with the merged bitmap
Expand Down
4 changes: 0 additions & 4 deletions PixiEditor/Models/Tools/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ public virtual void OnMouseMove(MouseEventArgs e)
{
}

public virtual void AfterAddedUndo(UndoManager undoManager)
{
}

public virtual void OnSelected()
{
}
Expand Down
6 changes: 5 additions & 1 deletion PixiEditor/Models/Tools/Tools/MagicWandTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using SkiaSharp;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace PixiEditor.Models.Tools.Tools
Expand Down Expand Up @@ -93,7 +94,10 @@ public void DocumentChanged()

private void ValidateCache(Document document)
{
cachedDocument ??= new Layer("_CombinedLayers", BitmapUtils.CombineLayers(document.Width, document.Height, document.Layers, document.LayerStructure));
cachedDocument ??= new Layer("_CombinedLayers", BitmapUtils.CombineLayers(
new Int32Rect(0, 0, document.Width, document.Height),
document.Layers,
document.LayerStructure));
}
}
}

0 comments on commit 8e5fbb6

Please sign in to comment.