|
@@ -1,5 +1,7 @@
|
|
|
-using System.Collections.Generic;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
+using System.Windows;
|
|
|
using System.Windows.Input;
|
|
|
using System.Windows.Media;
|
|
|
using PixiEditor.Helpers.Extensions;
|
|
@@ -25,6 +27,7 @@ namespace PixiEditor.Models.Tools.Tools
|
|
|
private Coordinates _lastMouseMove;
|
|
|
private Coordinates _lastStartMousePos;
|
|
|
private Dictionary<Layer, Color[]> _startPixelColors;
|
|
|
+ private Dictionary<Layer, Thickness> _startingOffsets;
|
|
|
private Coordinates[] _startSelection;
|
|
|
private bool _updateViewModelSelection = true;
|
|
|
|
|
@@ -39,38 +42,57 @@ namespace PixiEditor.Models.Tools.Tools
|
|
|
|
|
|
public override void AfterAddedUndo()
|
|
|
{
|
|
|
- //Inject to default undo system change custom changes made by this tool
|
|
|
- foreach (var item in _startPixelColors)
|
|
|
+ if (_currentSelection.Length != 0)
|
|
|
{
|
|
|
- BitmapPixelChanges beforeMovePixels = BitmapPixelChanges.FromArrays(_startSelection, item.Value);
|
|
|
- Change changes = UndoManager.UndoStack.Peek();
|
|
|
- int layerIndex = ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.IndexOf(item.Key);
|
|
|
+ //Inject to default undo system change custom changes made by this tool
|
|
|
+ foreach (var item in _startPixelColors)
|
|
|
+ {
|
|
|
+ BitmapPixelChanges beforeMovePixels = BitmapPixelChanges.FromArrays(_startSelection, item.Value);
|
|
|
+ Change changes = UndoManager.UndoStack.Peek();
|
|
|
+ int layerIndex = ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.IndexOf(item.Key);
|
|
|
|
|
|
- ((LayerChange[]) changes.OldValue)[layerIndex].PixelChanges.ChangedPixels
|
|
|
- .AddRangeOverride(beforeMovePixels.ChangedPixels);
|
|
|
+ ((LayerChange[]) changes.OldValue).First(x=> x.LayerIndex == layerIndex).PixelChanges.ChangedPixels
|
|
|
+ .AddRangeOverride(beforeMovePixels.ChangedPixels);
|
|
|
|
|
|
- ((LayerChange[]) changes.NewValue)[layerIndex].PixelChanges.ChangedPixels
|
|
|
- .AddRangeNewOnly(BitmapPixelChanges.FromSingleColoredArray(_startSelection, System.Windows.Media.Colors.Transparent).ChangedPixels);
|
|
|
+ ((LayerChange[]) changes.NewValue).First(x => x.LayerIndex == layerIndex).PixelChanges.ChangedPixels
|
|
|
+ .AddRangeNewOnly(BitmapPixelChanges
|
|
|
+ .FromSingleColoredArray(_startSelection, System.Windows.Media.Colors.Transparent)
|
|
|
+ .ChangedPixels);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void OnMouseUp() //This adds undo if there is no selection, reason why this isn't in AfterUndoAdded,
|
|
|
+ { //is because it doesn't fire if no pixel changes were made.
|
|
|
+ if (_currentSelection.Length == 0)
|
|
|
+ {
|
|
|
+ UndoManager.AddUndoChange(new Change("Offset", ApplyOffsets, new object[]{_startingOffsets},
|
|
|
+ ApplyOffsets, new object[] { GetOffsets(_affectedLayers)}, "Move layers"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ApplyOffsets(object[] parameters)
|
|
|
+ {
|
|
|
+ Dictionary<Layer, Thickness> offsets = (Dictionary<Layer, Thickness>)parameters[0];
|
|
|
+ foreach (var offset in offsets)
|
|
|
+ {
|
|
|
+ offset.Key.Offset = offset.Value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public override LayerChange[] Use(Layer layer, Coordinates[] mouseMove, Color color)
|
|
|
{
|
|
|
Coordinates start = mouseMove[^1];
|
|
|
- if (_lastStartMousePos != start
|
|
|
- ) //I am aware that this could be moved to OnMouseDown, but it is executed before Use, so I didn't want to complicate for now
|
|
|
+ if (_lastStartMousePos != start) //I am aware that this could be moved to OnMouseDown, but it is executed before Use, so I didn't want to complicate for now
|
|
|
{
|
|
|
ResetSelectionValues(start);
|
|
|
- if (ViewModelMain.Current.ActiveSelection.SelectedPoints.Count == 0
|
|
|
- ) //Move every pixel if none is selected
|
|
|
+ if (ViewModelMain.Current.ActiveSelection.SelectedPoints.Count > 0) //Move offset if no selection
|
|
|
{
|
|
|
- SelectTool select = new SelectTool();
|
|
|
- _currentSelection = select.GetAllSelection();
|
|
|
- _updateViewModelSelection = false;
|
|
|
+ _currentSelection = ViewModelMain.Current.ActiveSelection.SelectedPoints.ToArray();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- _currentSelection = ViewModelMain.Current.ActiveSelection.SelectedPoints.ToArray();
|
|
|
+ _currentSelection = Array.Empty<Coordinates>();
|
|
|
}
|
|
|
|
|
|
if (Keyboard.IsKeyDown(Key.LeftCtrl) || MoveAll)
|
|
@@ -81,21 +103,41 @@ namespace PixiEditor.Models.Tools.Tools
|
|
|
|
|
|
_startSelection = _currentSelection;
|
|
|
_startPixelColors = BitmapUtils.GetPixelsForSelection(_affectedLayers, _startSelection);
|
|
|
+ _startingOffsets = GetOffsets(_affectedLayers);
|
|
|
}
|
|
|
|
|
|
LayerChange[] result = new LayerChange[_affectedLayers.Length];
|
|
|
-
|
|
|
+ var end = mouseMove[0];
|
|
|
for (int i = 0; i < _affectedLayers.Length; i++)
|
|
|
{
|
|
|
- var changes = MoveSelection(_affectedLayers[i], mouseMove);
|
|
|
- changes = RemoveTransparentPixels(changes);
|
|
|
+ if (_currentSelection.Length > 0)
|
|
|
+ {
|
|
|
+ var changes = MoveSelection(_affectedLayers[i], mouseMove);
|
|
|
+ changes = RemoveTransparentPixels(changes);
|
|
|
|
|
|
- result[i] = new LayerChange(changes, _affectedLayers[i]);
|
|
|
+ result[i] = new LayerChange(changes, _affectedLayers[i]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var vector = Transform.GetTranslation(_lastMouseMove, end);
|
|
|
+ _affectedLayers[i].Offset = new Thickness(_affectedLayers[i].OffsetX + vector.X, _affectedLayers[i].OffsetY + vector.Y, 0, 0);
|
|
|
+ result[i] = new LayerChange(BitmapPixelChanges.Empty, _affectedLayers[i]);
|
|
|
+ }
|
|
|
}
|
|
|
+ _lastMouseMove = end;
|
|
|
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
+ private Dictionary<Layer, Thickness> GetOffsets(Layer[] layers)
|
|
|
+ {
|
|
|
+ Dictionary<Layer, Thickness> dict = new Dictionary<Layer, Thickness>();
|
|
|
+ for (int i = 0; i < layers.Length; i++)
|
|
|
+ {
|
|
|
+ dict.Add(layers[i], layers[i].Offset);
|
|
|
+ }
|
|
|
|
|
|
- return result;
|
|
|
+ return dict;
|
|
|
}
|
|
|
|
|
|
private BitmapPixelChanges RemoveTransparentPixels(BitmapPixelChanges pixels)
|
|
@@ -125,6 +167,8 @@ namespace PixiEditor.Models.Tools.Tools
|
|
|
_lastMouseMove = start;
|
|
|
_clearedPixels = new Dictionary<Layer, bool>();
|
|
|
_updateViewModelSelection = true;
|
|
|
+ _startPixelColors = null;
|
|
|
+ _startSelection = null;
|
|
|
}
|
|
|
|
|
|
private Coordinates[] TranslateSelection(Coordinates end, out Coordinates[] previousSelection)
|