|
@@ -1,46 +1,46 @@
|
|
|
-using PixiEditor.Helpers;
|
|
|
-using PixiEditor.Models.DataHolders;
|
|
|
-using PixiEditor.Models.Events;
|
|
|
-using PixiEditor.Models.Layers;
|
|
|
-using PixiEditor.Models.Position;
|
|
|
+using PixiEditor.Helpers;
|
|
|
+using PixiEditor.Models.DataHolders;
|
|
|
+using PixiEditor.Models.Events;
|
|
|
+using PixiEditor.Models.Layers;
|
|
|
+using PixiEditor.Models.Position;
|
|
|
using PixiEditor.Models.Tools;
|
|
|
-using PixiEditor.Models.Tools.Tools;
|
|
|
+using PixiEditor.Models.Tools.Tools;
|
|
|
using PixiEditor.ViewModels.SubViewModels.Main;
|
|
|
using SkiaSharp;
|
|
|
-using System;
|
|
|
+using System;
|
|
|
using System.Collections.ObjectModel;
|
|
|
using System.Diagnostics;
|
|
|
using System.Windows;
|
|
|
|
|
|
-namespace PixiEditor.Models.Controllers
|
|
|
-{
|
|
|
- [DebuggerDisplay("{Documents.Count} Document(s)")]
|
|
|
- public class BitmapManager : NotifyableObject
|
|
|
+namespace PixiEditor.Models.Controllers
|
|
|
+{
|
|
|
+ [DebuggerDisplay("{Documents.Count} Document(s)")]
|
|
|
+ public class BitmapManager : NotifyableObject
|
|
|
{
|
|
|
private ToolSessionController ToolSessionController { get; set; }
|
|
|
public ICanvasInputTarget InputTarget => ToolSessionController;
|
|
|
public BitmapOperationsUtility BitmapOperations { get; set; }
|
|
|
|
|
|
- public ObservableCollection<Document> Documents { get; set; } = new ObservableCollection<Document>();
|
|
|
-
|
|
|
- private Document activeDocument;
|
|
|
- public Document ActiveDocument
|
|
|
- {
|
|
|
- get => activeDocument;
|
|
|
- set
|
|
|
- {
|
|
|
- if (activeDocument == value)
|
|
|
- return;
|
|
|
- activeDocument?.UpdatePreviewImage();
|
|
|
- Document oldDoc = activeDocument;
|
|
|
- activeDocument = value;
|
|
|
- RaisePropertyChanged(nameof(ActiveDocument));
|
|
|
- ActiveWindow = value;
|
|
|
- DocumentChanged?.Invoke(this, new DocumentChangedEventArgs(value, oldDoc));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private object activeWindow;
|
|
|
+ public System.Collections.ObjectModel.ObservableCollection<Document> Documents { get; set; } = new System.Collections.ObjectModel.ObservableCollection<Document>();
|
|
|
+
|
|
|
+ private Document activeDocument;
|
|
|
+ public Document ActiveDocument
|
|
|
+ {
|
|
|
+ get => activeDocument;
|
|
|
+ set
|
|
|
+ {
|
|
|
+ if (activeDocument == value)
|
|
|
+ return;
|
|
|
+ activeDocument?.UpdatePreviewImage();
|
|
|
+ Document oldDoc = activeDocument;
|
|
|
+ activeDocument = value;
|
|
|
+ RaisePropertyChanged(nameof(ActiveDocument));
|
|
|
+ ActiveWindow = value;
|
|
|
+ DocumentChanged?.Invoke(this, new DocumentChangedEventArgs(value, oldDoc));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private object activeWindow;
|
|
|
public object ActiveWindow
|
|
|
{
|
|
|
get => activeWindow;
|
|
@@ -61,8 +61,8 @@ namespace PixiEditor.Models.Controllers
|
|
|
public Layer ActiveLayer => ActiveDocument.ActiveLayer;
|
|
|
|
|
|
public SKColor PrimaryColor { get; set; }
|
|
|
-
|
|
|
- private bool hideReferenceLayer;
|
|
|
+
|
|
|
+ private bool hideReferenceLayer;
|
|
|
public bool HideReferenceLayer
|
|
|
{
|
|
|
get => hideReferenceLayer;
|
|
@@ -74,8 +74,8 @@ namespace PixiEditor.Models.Controllers
|
|
|
{
|
|
|
get => onlyReferenceLayer;
|
|
|
set => SetProperty(ref onlyReferenceLayer, value);
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
private readonly ToolsViewModel _tools;
|
|
|
|
|
|
private int previewLayerSize;
|
|
@@ -86,16 +86,16 @@ namespace PixiEditor.Models.Controllers
|
|
|
private ToolSession activeSession = null;
|
|
|
|
|
|
|
|
|
- public BitmapManager(ToolsViewModel tools, UndoViewModel undo)
|
|
|
- {
|
|
|
- _tools = tools;
|
|
|
-
|
|
|
- ToolSessionController = new ToolSessionController();
|
|
|
- ToolSessionController.SessionStarted += OnSessionStart;
|
|
|
- ToolSessionController.SessionEnded += OnSessionEnd;
|
|
|
- ToolSessionController.PixelMousePositionChanged += OnPixelMousePositionChange;
|
|
|
- ToolSessionController.PreciseMousePositionChanged += OnPreciseMousePositionChange;
|
|
|
- ToolSessionController.KeyStateChanged += (_, _) => UpdateActionDisplay(_tools.ActiveTool);
|
|
|
+ public BitmapManager(ToolsViewModel tools, UndoViewModel undo)
|
|
|
+ {
|
|
|
+ _tools = tools;
|
|
|
+
|
|
|
+ ToolSessionController = new ToolSessionController();
|
|
|
+ ToolSessionController.SessionStarted += OnSessionStart;
|
|
|
+ ToolSessionController.SessionEnded += OnSessionEnd;
|
|
|
+ ToolSessionController.PixelMousePositionChanged += OnPixelMousePositionChange;
|
|
|
+ ToolSessionController.PreciseMousePositionChanged += OnPreciseMousePositionChange;
|
|
|
+ ToolSessionController.KeyStateChanged += (_, _) => UpdateActionDisplay(_tools.ActiveTool);
|
|
|
BitmapOperations = new BitmapOperationsUtility(this, tools);
|
|
|
|
|
|
undo.UndoRedoCalled += (_, _) => ToolSessionController.ForceStopActiveSessionIfAny();
|
|
@@ -103,10 +103,10 @@ namespace PixiEditor.Models.Controllers
|
|
|
DocumentChanged += BitmapManager_DocumentChanged;
|
|
|
|
|
|
_highlightPen = new PenTool(this)
|
|
|
- {
|
|
|
- AutomaticallyResizeCanvas = false
|
|
|
- };
|
|
|
- _highlightColor = new SKColor(0, 0, 0, 77);
|
|
|
+ {
|
|
|
+ AutomaticallyResizeCanvas = false
|
|
|
+ };
|
|
|
+ _highlightColor = new SKColor(0, 0, 0, 77);
|
|
|
}
|
|
|
|
|
|
public void CloseDocument(Document document)
|
|
@@ -134,8 +134,8 @@ namespace PixiEditor.Models.Controllers
|
|
|
|
|
|
ActiveDocument.PreviewLayer.Reset();
|
|
|
ExecuteTool();
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
private void OnSessionEnd(object sender, ToolSession e)
|
|
|
{
|
|
|
activeSession = null;
|
|
@@ -148,8 +148,8 @@ namespace PixiEditor.Models.Controllers
|
|
|
ActiveDocument.PreviewLayer.Reset();
|
|
|
HighlightPixels(ToolSessionController.LastPixelPosition);
|
|
|
StopUsingTool?.Invoke(this, EventArgs.Empty);
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
private void OnPreciseMousePositionChange(object sender, (double, double) e)
|
|
|
{
|
|
|
if (activeSession == null || !activeSession.Tool.RequiresPreciseMouseData)
|
|
@@ -165,16 +165,16 @@ namespace PixiEditor.Models.Controllers
|
|
|
return;
|
|
|
ExecuteTool();
|
|
|
return;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- HighlightPixels(e.NewPosition);
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- private void ExecuteTool()
|
|
|
- {
|
|
|
- if (activeSession == null)
|
|
|
+ else
|
|
|
+ {
|
|
|
+ HighlightPixels(e.NewPosition);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ExecuteTool()
|
|
|
+ {
|
|
|
+ if (activeSession == null)
|
|
|
throw new Exception("Can't execute tool's Use outside a session");
|
|
|
|
|
|
if (activeSession.Tool is BitmapOperationTool operationTool)
|
|
@@ -189,25 +189,25 @@ namespace PixiEditor.Models.Controllers
|
|
|
{
|
|
|
throw new InvalidOperationException($"'{activeSession.Tool.GetType().Name}' is either not a Tool or can't inherit '{nameof(Tool)}' directly.\nChanges the base type to either '{nameof(BitmapOperationTool)}' or '{nameof(ReadonlyTool)}'");
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventArgs e)
|
|
|
{
|
|
|
e.NewDocument?.GeneratePreviewLayer();
|
|
|
if (e.OldDocument != e.NewDocument)
|
|
|
ToolSessionController.ForceStopActiveSessionIfAny();
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
public void UpdateHighlightIfNecessary(bool forceHide = false)
|
|
|
{
|
|
|
if (activeSession != null)
|
|
|
return;
|
|
|
|
|
|
HighlightPixels(forceHide ? new(-1, -1) : ToolSessionController.LastPixelPosition);
|
|
|
- }
|
|
|
-
|
|
|
- private void HighlightPixels(Coordinates newPosition)
|
|
|
- {
|
|
|
+ }
|
|
|
+
|
|
|
+ private void HighlightPixels(Coordinates newPosition)
|
|
|
+ {
|
|
|
if (ActiveDocument == null || ActiveDocument.Layers.Count == 0)
|
|
|
{
|
|
|
return;
|
|
@@ -215,14 +215,14 @@ namespace PixiEditor.Models.Controllers
|
|
|
|
|
|
var previewLayer = ActiveDocument.PreviewLayer;
|
|
|
|
|
|
- if (newPosition.X > ActiveDocument.Width
|
|
|
- || newPosition.Y > ActiveDocument.Height
|
|
|
- || newPosition.X < 0 || newPosition.Y < 0
|
|
|
- || _tools.ActiveTool.HideHighlight)
|
|
|
- {
|
|
|
- previewLayer.Reset();
|
|
|
- previewLayerSize = -1;
|
|
|
- return;
|
|
|
+ if (newPosition.X > ActiveDocument.Width
|
|
|
+ || newPosition.Y > ActiveDocument.Height
|
|
|
+ || newPosition.X < 0 || newPosition.Y < 0
|
|
|
+ || _tools.ActiveTool.HideHighlight)
|
|
|
+ {
|
|
|
+ previewLayer.Reset();
|
|
|
+ previewLayerSize = -1;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
if (_tools.ToolSize != previewLayerSize || previewLayer.IsReset)
|
|
@@ -236,15 +236,15 @@ namespace PixiEditor.Models.Controllers
|
|
|
previewLayer.Offset = new Thickness(0, 0, 0, 0);
|
|
|
_highlightPen.Draw(previewLayer, cords, cords, _highlightColor, _tools.ToolSize);
|
|
|
}
|
|
|
- AdjustOffset(newPosition, previewLayer);
|
|
|
+ AdjustOffset(newPosition, previewLayer);
|
|
|
+
|
|
|
+ previewLayer.InvokeLayerBitmapChange();
|
|
|
+ }
|
|
|
|
|
|
- previewLayer.InvokeLayerBitmapChange();
|
|
|
- }
|
|
|
-
|
|
|
private void AdjustOffset(Coordinates newPosition, Layer previewLayer)
|
|
|
{
|
|
|
- Coordinates start = newPosition - halfSize;
|
|
|
+ Coordinates start = newPosition - halfSize;
|
|
|
previewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
|
|
|
}
|
|
|
- }
|
|
|
-}
|
|
|
+ }
|
|
|
+}
|