Skip to content

Commit

Permalink
rotate layer wip
Browse files Browse the repository at this point in the history
  • Loading branch information
flabbet committed Nov 4, 2021
1 parent b8aa6b8 commit 7e49f95
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
36 changes: 36 additions & 0 deletions PixiEditor/Models/DataHolders/Document/Document.Operations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using PixiEditor.Models.Enums;
using PixiEditor.Models.Layers;
using PixiEditor.Models.Undo;
using SkiaSharp;
using System;
using System.Linq;
using System.Windows;
Expand Down Expand Up @@ -46,6 +47,41 @@ public void ResizeCanvas(int width, int height, AnchorPoint anchor)
DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(oldWidth, oldHeight, width, height));
}

public void RotateActiveLayer(float degrees)
{
Guid layerGuid = ActiveLayer.LayerGuid;

object[] processArgs = { layerGuid, degrees };
object[] reverseProcessArgs = { layerGuid, -degrees };

RotateLayerProcess(processArgs);

UndoManager.AddUndoChange(new Change(
RotateLayerProcess,
reverseProcessArgs,
RotateLayerProcess,
processArgs,
"Rotate layer"));
}

private void RotateLayerProcess(object[] parameters)
{
Layer layer = Layers.First(x => x.LayerGuid == (Guid)parameters[0]);
float degrees = (float)parameters[1];

using (new SKAutoCanvasRestore(layer.LayerBitmap.SkiaSurface.Canvas, true))
{
var copy = layer.LayerBitmap.SkiaSurface.Snapshot();
var canvas = layer.LayerBitmap.SkiaSurface.Canvas;
canvas.Clear();
canvas.RotateDegrees(degrees, layer.MaxWidth / 2, layer.MaxHeight / 2);
canvas.DrawImage(copy, new SKPoint(0, 0));
copy.Dispose();
}

layer.InvokeLayerBitmapChange();
}

/// <summary>
/// Resizes all document layers using NearestNeighbor interpolation.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions PixiEditor/ViewModels/SubViewModels/Main/DocumentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ public class DocumentViewModel : SubViewModel<ViewModelMain>

public RelayCommand OpenResizePopupCommand { get; set; }

public RelayCommand RotateToRightCommand { get; set; }

public DocumentViewModel(ViewModelMain owner)
: base(owner)
{
CenterContentCommand = new RelayCommand(CenterContent, Owner.DocumentIsNotNull);
ClipCanvasCommand = new RelayCommand(ClipCanvas, Owner.DocumentIsNotNull);
DeletePixelsCommand = new RelayCommand(DeletePixels, Owner.SelectionSubViewModel.SelectionIsNotEmpty);
OpenResizePopupCommand = new RelayCommand(OpenResizePopup, Owner.DocumentIsNotNull);
RotateToRightCommand = new RelayCommand(RotateLayer, Owner.DocumentIsNotNull);
}

public void RotateLayer(object parameter)
{
if (parameter is double angle)
{
Owner.BitmapManager.ActiveDocument?.RotateActiveLayer((float)angle);
}
}

public void ClipCanvas(object parameter)
Expand Down
13 changes: 12 additions & 1 deletion PixiEditor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
xmlns:cmd="http://www.galasoft.ch/mvvmlight"
xmlns:avalondock="https://github.com/Dirkster99/AvalonDock"
xmlns:colorpicker="clr-namespace:ColorPicker;assembly=ColorPicker" xmlns:usercontrols="clr-namespace:PixiEditor.Views.UserControls" xmlns:behaviours="clr-namespace:PixiEditor.Helpers.Behaviours"
xmlns:avalonDockTheme="clr-namespace:PixiEditor.Styles.AvalonDock" xmlns:layerUserControls="clr-namespace:PixiEditor.Views.UserControls.Layers" d:DataContext="{d:DesignInstance Type=vm:ViewModelMain}"
xmlns:avalonDockTheme="clr-namespace:PixiEditor.Styles.AvalonDock" xmlns:layerUserControls="clr-namespace:PixiEditor.Views.UserControls.Layers" xmlns:sys="clr-namespace:System;assembly=System.Runtime" d:DataContext="{d:DesignInstance Type=vm:ViewModelMain}"
mc:Ignorable="d" WindowStyle="None" Initialized="MainWindow_Initialized"
Title="PixiEditor" Name="mainWindow" Height="1000" Width="1600" Background="{StaticResource MainColor}"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
Expand Down Expand Up @@ -134,6 +134,17 @@
<MenuItem Header="_Clip Canvas" Command="{Binding DocumentSubViewModel.ClipCanvasCommand}" />
<Separator/>
<MenuItem Header="_Center Content" Command="{Binding DocumentSubViewModel.CenterContentCommand}" />
<Separator/>
<MenuItem Header="_Rotate to right 90&#186;" Command="{Binding DocumentSubViewModel.RotateToRightCommand}">
<MenuItem.CommandParameter>
<sys:Double>90</sys:Double>
</MenuItem.CommandParameter>
</MenuItem>
<MenuItem Header="_Rotate to left 90&#186;" Command="{Binding DocumentSubViewModel.RotateToRightCommand}">
<MenuItem.CommandParameter>
<sys:Double>-90</sys:Double>
</MenuItem.CommandParameter>
</MenuItem>
</MenuItem>
<MenuItem Header="_View">
<MenuItem Header="_Show Grid Lines" IsChecked="{Binding ViewportSubViewModel.GridLinesEnabled, Mode=TwoWay}"
Expand Down

0 comments on commit 7e49f95

Please sign in to comment.