From f97d3156ae07e44305ad0a5496b8ae69ec4171ec Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Sat, 26 Dec 2020 10:37:13 -0600 Subject: [PATCH] Call DocumentLayers methods directly instead of using LayerManager. --- Pinta.Core/Classes/DocumentLayers.cs | 10 ++ Pinta.Core/Classes/DocumentSelection.cs | 4 +- Pinta.Core/Classes/Layer.cs | 8 +- Pinta.Core/Classes/SelectionModeHandler.cs | 2 +- Pinta.Core/Extensions/CairoExtensions.cs | 8 +- .../HistoryItems/AddLayerHistoryItem.cs | 14 +- .../HistoryItems/DeleteLayerHistoryItem.cs | 12 +- .../HistoryItems/FinishPixelsHistoryItem.cs | 40 +++-- Pinta.Core/HistoryItems/InvertHistoryItem.cs | 32 ++-- .../HistoryItems/MovePixelsHistoryItem.cs | 20 ++- Pinta.Core/HistoryItems/PasteHistoryItem.cs | 4 +- Pinta.Core/HistoryItems/ResizeHistoryItem.cs | 10 +- Pinta.Core/HistoryItems/SimpleHistoryItem.cs | 18 +- .../HistoryItems/SwapLayersHistoryItem.cs | 18 +- .../UpdateLayerPropertiesHistoryItem.cs | 10 +- Pinta.Core/Managers/LayerManager.cs | 167 ------------------ Pinta.Core/Managers/LivePreviewManager.cs | 8 +- Pinta.Effects/Dialogs/Effects.LevelsDialog.cs | 6 +- .../Widgets/Canvas/CanvasRenderer.cs | 4 +- .../Widgets/Layers/LayersListWidget.cs | 16 +- .../Widgets/PointPickerGraphic.cs | 4 +- Pinta/Actions/Layers/LayerPropertiesAction.cs | 10 +- Pinta/Dialogs/LayerPropertiesDialog.cs | 40 +++-- 23 files changed, 192 insertions(+), 273 deletions(-) diff --git a/Pinta.Core/Classes/DocumentLayers.cs b/Pinta.Core/Classes/DocumentLayers.cs index c2dbc3a6a3..450924be1b 100644 --- a/Pinta.Core/Classes/DocumentLayers.cs +++ b/Pinta.Core/Classes/DocumentLayers.cs @@ -114,6 +114,11 @@ internal void Close () selection_layer?.Surface.Dispose (); } + /// + /// Returns the number of user layers. + /// + public int Count () => user_layers.Count; + /// /// Creates a new layer, but does not add it to the layer collection. /// @@ -426,5 +431,10 @@ public void SetCurrentUserLayer (UserLayer layer) { SetCurrentUserLayer (user_layers.IndexOf (layer)); } + + /// + /// Gets the user layer at the specified index. + /// + public UserLayer this[int index] => user_layers[index]; } } diff --git a/Pinta.Core/Classes/DocumentSelection.cs b/Pinta.Core/Classes/DocumentSelection.cs index 91789d5d30..64b45c60d5 100644 --- a/Pinta.Core/Classes/DocumentSelection.cs +++ b/Pinta.Core/Classes/DocumentSelection.cs @@ -62,7 +62,9 @@ public Path SelectionPath { if (selection_path == null) { - using (var g = new Context (PintaCore.Layers.CurrentLayer.Surface)) + var doc = PintaCore.Workspace.ActiveDocument; + + using (var g = new Context (doc.Layers.CurrentUserLayer.Surface)) selection_path = g.CreatePolygonPath (ConvertToPolygonSet (SelectionPolygons)); } diff --git a/Pinta.Core/Classes/Layer.cs b/Pinta.Core/Classes/Layer.cs index a4eba4e035..965008b5a0 100644 --- a/Pinta.Core/Classes/Layer.cs +++ b/Pinta.Core/Classes/Layer.cs @@ -90,7 +90,9 @@ public void Clear () public void FlipHorizontal () { - Layer dest = PintaCore.Layers.CreateLayer (); + var doc = PintaCore.Workspace.ActiveDocument; + + Layer dest = doc.Layers.CreateLayer (); using (Cairo.Context g = new Cairo.Context (dest.Surface)) { g.Matrix = new Matrix (-1, 0, 0, 1, Surface.Width, 0); @@ -106,7 +108,9 @@ public void FlipHorizontal () public void FlipVertical () { - Layer dest = PintaCore.Layers.CreateLayer (); + var doc = PintaCore.Workspace.ActiveDocument; + + Layer dest = doc.Layers.CreateLayer (); using (Cairo.Context g = new Cairo.Context (dest.Surface)) { g.Matrix = new Matrix (1, 0, 0, -1, 0, Surface.Height); diff --git a/Pinta.Core/Classes/SelectionModeHandler.cs b/Pinta.Core/Classes/SelectionModeHandler.cs index 12a0ee834a..0cfc468300 100644 --- a/Pinta.Core/Classes/SelectionModeHandler.cs +++ b/Pinta.Core/Classes/SelectionModeHandler.cs @@ -108,7 +108,7 @@ public static void PerformSelectionMode (CombineMode mode, List> doc.Selection = doc.PreviousSelection.Clone (); doc.Selection.Visible = true; - using (Context g = new Context (PintaCore.Layers.CurrentLayer.Surface)) + using (Context g = new Context (doc.Layers.CurrentUserLayer.Surface)) { //Make sure time isn't wasted if the CombineMode is Replace - Replace is much simpler than the other 4 selection modes. if (mode == CombineMode.Replace) diff --git a/Pinta.Core/Extensions/CairoExtensions.cs b/Pinta.Core/Extensions/CairoExtensions.cs index 144f01131b..2fa7c6ea3c 100644 --- a/Pinta.Core/Extensions/CairoExtensions.cs +++ b/Pinta.Core/Extensions/CairoExtensions.cs @@ -896,9 +896,11 @@ public static unsafe bool ContainsTranslucent (this ImageSurface surf) public static Path Clone (this Path path) { + var doc = PintaCore.Workspace.ActiveDocument; + Path newpath; - using (Context g = new Context (PintaCore.Layers.CurrentLayer.Surface)) { + using (Context g = new Context (doc.Layers.CurrentUserLayer.Surface)) { g.AppendPath (path); newpath = g.CopyPath (); } @@ -927,8 +929,10 @@ public static void Clear (this Context g, Rectangle roi) public static Gdk.Rectangle GetBounds(this Path path) { Rectangle rect; + var doc = PintaCore.Workspace.ActiveDocument; + - using (Context g = new Context(PintaCore.Layers.CurrentLayer.Surface)) + using (Context g = new Context(doc.Layers.CurrentUserLayer.Surface)) { g.AppendPath(path); rect = g.PathExtents(); diff --git a/Pinta.Core/HistoryItems/AddLayerHistoryItem.cs b/Pinta.Core/HistoryItems/AddLayerHistoryItem.cs index 85f5c8f40c..279b1de887 100644 --- a/Pinta.Core/HistoryItems/AddLayerHistoryItem.cs +++ b/Pinta.Core/HistoryItems/AddLayerHistoryItem.cs @@ -40,18 +40,22 @@ public AddLayerHistoryItem (string icon, string text, int newLayerIndex) : base public override void Undo () { + var doc = PintaCore.Workspace.ActiveDocument; + // Store the layer for "redo" - layer = PintaCore.Layers[layer_index]; - - PintaCore.Layers.DeleteLayer (layer_index, false); + layer = doc.Layers[layer_index]; + + doc.Layers.DeleteLayer (layer_index, false); } public override void Redo () { - PintaCore.Layers.Insert (layer!, layer_index); // NRT - layer is set by Undo() + var doc = PintaCore.Workspace.ActiveDocument; + + doc.Layers.Insert (layer!, layer_index); // NRT - layer is set by Undo() // Make new layer the current layer - PintaCore.Layers.SetCurrentLayer (layer!); + doc.Layers.SetCurrentUserLayer (layer!); layer = null; } diff --git a/Pinta.Core/HistoryItems/DeleteLayerHistoryItem.cs b/Pinta.Core/HistoryItems/DeleteLayerHistoryItem.cs index 3d2161585c..0ee6d07a5e 100644 --- a/Pinta.Core/HistoryItems/DeleteLayerHistoryItem.cs +++ b/Pinta.Core/HistoryItems/DeleteLayerHistoryItem.cs @@ -41,20 +41,24 @@ public DeleteLayerHistoryItem(string icon, string text, UserLayer layer, int lay public override void Undo () { - PintaCore.Layers.Insert (layer!, layer_index); // NRT - layer is set by constructor + var doc = PintaCore.Workspace.ActiveDocument; + + doc.Layers.Insert (layer!, layer_index); // NRT - layer is set by constructor // Make new layer the current layer - PintaCore.Layers.SetCurrentLayer (layer!); + doc.Layers.SetCurrentUserLayer (layer!); layer = null; } public override void Redo () { + var doc = PintaCore.Workspace.ActiveDocument; + // Store the layer for "undo" - layer = PintaCore.Layers[layer_index]; + layer = doc.Layers[layer_index]; - PintaCore.Layers.DeleteLayer (layer_index, false); + doc.Layers.DeleteLayer (layer_index, false); } public override void Dispose () diff --git a/Pinta.Core/HistoryItems/FinishPixelsHistoryItem.cs b/Pinta.Core/HistoryItems/FinishPixelsHistoryItem.cs index 5f96d7fa58..0931345567 100644 --- a/Pinta.Core/HistoryItems/FinishPixelsHistoryItem.cs +++ b/Pinta.Core/HistoryItems/FinishPixelsHistoryItem.cs @@ -45,16 +45,18 @@ public FinishPixelsHistoryItem () public override void Undo () { - PintaCore.Layers.ShowSelectionLayer = true; + var doc = PintaCore.Workspace.ActiveDocument; + + doc.Layers.ShowSelectionLayer = true; Matrix swap_transfrom = new Matrix(); - swap_transfrom.InitMatrix(PintaCore.Layers.SelectionLayer.Transform); - ImageSurface swap_surf = PintaCore.Layers.CurrentLayer.Surface; - ImageSurface swap_sel = PintaCore.Layers.SelectionLayer.Surface; + swap_transfrom.InitMatrix(doc.Layers.SelectionLayer.Transform); + ImageSurface swap_surf = doc.Layers.CurrentUserLayer.Surface; + ImageSurface swap_sel = doc.Layers.SelectionLayer.Surface; - PintaCore.Layers.SelectionLayer.Surface = old_selection_layer!; // NRT - Set in TakeSnapshot - PintaCore.Layers.SelectionLayer.Transform.InitMatrix(old_transform); - PintaCore.Layers.CurrentLayer.Surface = old_surface!; + doc.Layers.SelectionLayer.Surface = old_selection_layer!; // NRT - Set in TakeSnapshot + doc.Layers.SelectionLayer.Transform.InitMatrix(old_transform); + doc.Layers.CurrentUserLayer.Surface = old_surface!; old_transform.InitMatrix(swap_transfrom); old_surface = swap_surf; @@ -66,20 +68,22 @@ public override void Undo () public override void Redo () { + var doc = PintaCore.Workspace.ActiveDocument; + Matrix swap_transfrom = new Matrix(); - swap_transfrom.InitMatrix(PintaCore.Layers.SelectionLayer.Transform); - ImageSurface swap_surf = PintaCore.Layers.CurrentLayer.Surface.Clone (); - ImageSurface swap_sel = PintaCore.Layers.SelectionLayer.Surface; + swap_transfrom.InitMatrix(doc.Layers.SelectionLayer.Transform); + ImageSurface swap_surf = doc.Layers.CurrentUserLayer.Surface.Clone (); + ImageSurface swap_sel = doc.Layers.SelectionLayer.Surface; - PintaCore.Layers.CurrentLayer.Surface = old_surface!; // NRT - Set in TakeSnapshot - PintaCore.Layers.SelectionLayer.Surface = old_selection_layer!; - PintaCore.Layers.SelectionLayer.Transform.InitMatrix(old_transform); + doc.Layers.CurrentUserLayer.Surface = old_surface!; // NRT - Set in TakeSnapshot + doc.Layers.SelectionLayer.Surface = old_selection_layer!; + doc.Layers.SelectionLayer.Transform.InitMatrix(old_transform); old_surface = swap_surf; old_selection_layer = swap_sel; old_transform.InitMatrix(swap_transfrom); - PintaCore.Layers.DestroySelectionLayer (); + doc.Layers.DestroySelectionLayer (); PintaCore.Workspace.Invalidate (); } @@ -93,9 +97,11 @@ public override void Dispose () public void TakeSnapshot () { - old_selection_layer = PintaCore.Layers.SelectionLayer.Surface.Clone (); - old_surface = PintaCore.Layers.CurrentLayer.Surface.Clone (); - old_transform.InitMatrix(PintaCore.Layers.SelectionLayer.Transform); + var doc = PintaCore.Workspace.ActiveDocument; + + old_selection_layer = doc.Layers.SelectionLayer.Surface.Clone (); + old_surface = doc.Layers.CurrentUserLayer.Surface.Clone (); + old_transform.InitMatrix(doc.Layers.SelectionLayer.Transform); } } } diff --git a/Pinta.Core/HistoryItems/InvertHistoryItem.cs b/Pinta.Core/HistoryItems/InvertHistoryItem.cs index a10b763550..8e06462a04 100644 --- a/Pinta.Core/HistoryItems/InvertHistoryItem.cs +++ b/Pinta.Core/HistoryItems/InvertHistoryItem.cs @@ -87,31 +87,33 @@ public InvertHistoryItem (InvertType type, int layerIndex) public override void Undo () { + var doc = PintaCore.Workspace.ActiveDocument; + switch (type) { //case InvertType.InvertColors: // PintaCore.Actions.Adjustments.InvertColors.Activate (); // break; case InvertType.Rotate180: - PintaCore.Layers.RotateImage180 (); + doc.RotateImage180 (); break; case InvertType.FlipHorizontal: - PintaCore.Layers.FlipImageHorizontal (); + doc.FlipImageHorizontal (); break; case InvertType.FlipVertical: - PintaCore.Layers.FlipImageVertical (); + doc.FlipImageVertical (); break; case InvertType.Rotate90CW: - PintaCore.Layers.RotateImageCCW (); + doc.RotateImageCCW (); break; case InvertType.Rotate90CCW: - PintaCore.Layers.RotateImageCW (); + doc.RotateImageCW (); break; case InvertType.FlipLayerHorizontal: - PintaCore.Workspace.ActiveDocument.Layers.UserLayers[layer_index].FlipHorizontal (); + doc.Layers[layer_index].FlipHorizontal (); PintaCore.Workspace.Invalidate (); break; case InvertType.FlipLayerVertical: - PintaCore.Workspace.ActiveDocument.Layers.UserLayers[layer_index].FlipVertical (); + doc.Layers[layer_index].FlipVertical (); PintaCore.Workspace.Invalidate (); break; } @@ -119,31 +121,33 @@ public override void Undo () public override void Redo () { + var doc = PintaCore.Workspace.ActiveDocument; + switch (type) { //case InvertType.InvertColors: // PintaCore.Actions.Adjustments.InvertColors.Activate (); // break; case InvertType.Rotate180: - PintaCore.Layers.RotateImage180 (); + doc.RotateImage180 (); break; case InvertType.FlipHorizontal: - PintaCore.Layers.FlipImageHorizontal (); + doc.FlipImageHorizontal (); break; case InvertType.FlipVertical: - PintaCore.Layers.FlipImageVertical (); + doc.FlipImageVertical (); break; case InvertType.Rotate90CW: - PintaCore.Layers.RotateImageCW (); + doc.RotateImageCW (); break; case InvertType.Rotate90CCW: - PintaCore.Layers.RotateImageCCW (); + doc.RotateImageCCW (); break; case InvertType.FlipLayerHorizontal: - PintaCore.Workspace.ActiveDocument.Layers.UserLayers[layer_index].FlipHorizontal (); + doc.Layers[layer_index].FlipHorizontal (); PintaCore.Workspace.Invalidate (); break; case InvertType.FlipLayerVertical: - PintaCore.Workspace.ActiveDocument.Layers.UserLayers[layer_index].FlipVertical (); + doc.Layers[layer_index].FlipVertical (); PintaCore.Workspace.Invalidate (); break; } diff --git a/Pinta.Core/HistoryItems/MovePixelsHistoryItem.cs b/Pinta.Core/HistoryItems/MovePixelsHistoryItem.cs index 891903a704..7fac3e3c31 100644 --- a/Pinta.Core/HistoryItems/MovePixelsHistoryItem.cs +++ b/Pinta.Core/HistoryItems/MovePixelsHistoryItem.cs @@ -69,21 +69,23 @@ public override void Dispose () private void Swap () { - DocumentSelection swap_selection = PintaCore.Workspace.ActiveDocument.Selection; - PintaCore.Workspace.ActiveDocument.Selection = old_selection!; // NRT - Set in TakeSnapshot + var doc = PintaCore.Workspace.ActiveDocument; + + DocumentSelection swap_selection = doc.Selection; + doc.Selection = old_selection!; // NRT - Set in TakeSnapshot old_selection = swap_selection; Matrix swap_transform = new Matrix(); - swap_transform.InitMatrix(PintaCore.Layers.SelectionLayer.Transform); - PintaCore.Layers.SelectionLayer.Transform.InitMatrix(old_transform); + swap_transform.InitMatrix(doc.Layers.SelectionLayer.Transform); + doc.Layers.SelectionLayer.Transform.InitMatrix(old_transform); old_transform.InitMatrix(swap_transform); if (lifted) { // Grab the original surface - ImageSurface surf = PintaCore.Layers[layer_index].Surface; + ImageSurface surf = doc.Layers[layer_index].Surface; // Undo to the "old" surface - PintaCore.Layers[layer_index].Surface = old_surface!; // NRT - Set in TakeSnapshot + doc.Layers[layer_index].Surface = old_surface!; // NRT - Set in TakeSnapshot // Store the original surface for Redo old_surface = surf; @@ -97,6 +99,8 @@ private void Swap () public void TakeSnapshot (bool lift) { + var doc = PintaCore.Workspace.ActiveDocument; + lifted = lift; is_lifted = true; @@ -105,8 +109,8 @@ public void TakeSnapshot (bool lift) old_surface = doc.Layers.CurrentUserLayer.Surface.Clone (); } - old_selection = PintaCore.Workspace.ActiveDocument.Selection.Clone (); - old_transform.InitMatrix(PintaCore.Layers.SelectionLayer.Transform); + old_selection = doc.Selection.Clone (); + old_transform.InitMatrix(doc.Layers.SelectionLayer.Transform); } } } diff --git a/Pinta.Core/HistoryItems/PasteHistoryItem.cs b/Pinta.Core/HistoryItems/PasteHistoryItem.cs index e302d7cd19..7d5b540d94 100644 --- a/Pinta.Core/HistoryItems/PasteHistoryItem.cs +++ b/Pinta.Core/HistoryItems/PasteHistoryItem.cs @@ -66,9 +66,11 @@ public override void Redo () public override void Undo () { + var doc = PintaCore.Workspace.ActiveDocument; + Swap (); - PintaCore.Layers.DestroySelectionLayer (); + doc.Layers.DestroySelectionLayer (); PintaCore.Workspace.Invalidate (); } diff --git a/Pinta.Core/HistoryItems/ResizeHistoryItem.cs b/Pinta.Core/HistoryItems/ResizeHistoryItem.cs index 00b4bd93eb..0f21fcb493 100644 --- a/Pinta.Core/HistoryItems/ResizeHistoryItem.cs +++ b/Pinta.Core/HistoryItems/ResizeHistoryItem.cs @@ -46,6 +46,8 @@ public ResizeHistoryItem (Size oldSize) : base () public override void Undo () { + var doc = PintaCore.Workspace.ActiveDocument; + // maintain the current scaling setting after the operation double scale = PintaCore.Workspace.Scale; @@ -63,13 +65,13 @@ public override void Undo () if (RestoreSelection != null) { DocumentSelection old = PintaCore.Workspace.ActiveDocument.Selection; - PintaCore.Workspace.ActiveDocument.Selection = RestoreSelection.Clone(); + doc.Selection = RestoreSelection.Clone(); if (old != null) { old.Dispose (); } } else { - PintaCore.Layers.ResetSelectionPath (); + doc.ResetSelectionPaths (); } PintaCore.Workspace.Invalidate (); @@ -82,6 +84,8 @@ public override void Undo () public override void Redo () { + var doc = PintaCore.Workspace.ActiveDocument; + // maintain the current scaling setting after the operation double scale = PintaCore.Workspace.Scale; @@ -97,7 +101,7 @@ public override void Redo () base.Redo (); - PintaCore.Layers.ResetSelectionPath (); + doc.ResetSelectionPaths (); PintaCore.Workspace.Invalidate (); PintaCore.Workspace.Scale = scale; diff --git a/Pinta.Core/HistoryItems/SimpleHistoryItem.cs b/Pinta.Core/HistoryItems/SimpleHistoryItem.cs index 71f1d6a665..a68d786b47 100644 --- a/Pinta.Core/HistoryItems/SimpleHistoryItem.cs +++ b/Pinta.Core/HistoryItems/SimpleHistoryItem.cs @@ -37,8 +37,10 @@ public class SimpleHistoryItem : BaseHistoryItem public SimpleHistoryItem (string icon, string text, ImageSurface oldSurface, int layerIndex) : base (icon, text) { + var doc = PintaCore.Workspace.ActiveDocument; + layer_index = layerIndex; - surface_diff = SurfaceDiff.Create (oldSurface, PintaCore.Layers[layer_index].Surface); + surface_diff = SurfaceDiff.Create (oldSurface, doc.Layers[layer_index].Surface); // If the diff was too big, store the original surface, else, dispose it if (surface_diff == null) @@ -63,15 +65,17 @@ public override void Redo () private void Swap () { + var doc = PintaCore.Workspace.ActiveDocument; + // Grab the original surface - ImageSurface surf = PintaCore.Layers[layer_index].Surface; + ImageSurface surf = doc.Layers[layer_index].Surface; if (surface_diff != null) { surface_diff.ApplyAndSwap (surf); PintaCore.Workspace.Invalidate (surface_diff.GetBounds ()); } else { // Undo to the "old" surface - PintaCore.Layers[layer_index].Surface = old_surface!; // NRT - Will be not-null if surface_diff is null + doc.Layers[layer_index].Surface = old_surface!; // NRT - Will be not-null if surface_diff is null // Store the original surface for Redo old_surface = surf; @@ -89,13 +93,17 @@ public override void Dispose () public void TakeSnapshotOfLayer (int layerIndex) { + var doc = PintaCore.Workspace.ActiveDocument; + layer_index = layerIndex; - old_surface = PintaCore.Layers[layerIndex].Surface.Clone (); + old_surface = doc.Layers[layerIndex].Surface.Clone (); } public void TakeSnapshotOfLayer(UserLayer layer) { - layer_index = PintaCore.Layers.IndexOf (layer); + var doc = PintaCore.Workspace.ActiveDocument; + + layer_index = doc.Layers.IndexOf (layer); old_surface = layer.Surface.Clone (); } } diff --git a/Pinta.Core/HistoryItems/SwapLayersHistoryItem.cs b/Pinta.Core/HistoryItems/SwapLayersHistoryItem.cs index 77d9c5790f..3a2f49f713 100644 --- a/Pinta.Core/HistoryItems/SwapLayersHistoryItem.cs +++ b/Pinta.Core/HistoryItems/SwapLayersHistoryItem.cs @@ -53,21 +53,23 @@ public override void Redo () private void Swap () { - int selected = PintaCore.Layers.CurrentLayerIndex; + var doc = PintaCore.Workspace.ActiveDocument; + + int selected = doc.Layers.CurrentUserLayerIndex; int l1 = Math.Min (layer_index_1, layer_index_2); int l2 = Math.Max (layer_index_1, layer_index_2); - UserLayer layer1 = PintaCore.Layers[l1]; - UserLayer layer2 = PintaCore.Layers[l2]; + UserLayer layer1 = doc.Layers[l1]; + UserLayer layer2 = doc.Layers[l2]; - PintaCore.Layers.DeleteLayer (l1, false); - PintaCore.Layers.DeleteLayer (l2 - 1, false); + doc.Layers.DeleteLayer (l1, false); + doc.Layers.DeleteLayer (l2 - 1, false); - PintaCore.Layers.Insert (layer2, l1); - PintaCore.Layers.Insert (layer1, l2); + doc.Layers.Insert (layer2, l1); + doc.Layers.Insert (layer1, l2); - PintaCore.Layers.SetCurrentLayer (selected); + doc.Layers.SetCurrentUserLayer (selected); } } } diff --git a/Pinta.Core/HistoryItems/UpdateLayerPropertiesHistoryItem.cs b/Pinta.Core/HistoryItems/UpdateLayerPropertiesHistoryItem.cs index d54195261f..26480eca07 100644 --- a/Pinta.Core/HistoryItems/UpdateLayerPropertiesHistoryItem.cs +++ b/Pinta.Core/HistoryItems/UpdateLayerPropertiesHistoryItem.cs @@ -48,8 +48,10 @@ public UpdateLayerPropertiesHistoryItem ( } public override void Undo () - { - var layer = PintaCore.Layers[layer_index]; + { + var doc = PintaCore.Workspace.ActiveDocument; + + var layer = doc.Layers[layer_index]; layer.Opacity = initial_properties.Opacity; layer.Hidden = initial_properties.Hidden; layer.Name = initial_properties.Name; @@ -58,7 +60,9 @@ public override void Undo () public override void Redo () { - var layer = PintaCore.Layers[layer_index]; + var doc = PintaCore.Workspace.ActiveDocument; + + var layer = doc.Layers[layer_index]; layer.Opacity = updated_properties.Opacity; layer.Hidden = updated_properties.Hidden; layer.Name = updated_properties.Name; diff --git a/Pinta.Core/Managers/LayerManager.cs b/Pinta.Core/Managers/LayerManager.cs index 724f0157a8..b634959ed7 100644 --- a/Pinta.Core/Managers/LayerManager.cs +++ b/Pinta.Core/Managers/LayerManager.cs @@ -35,163 +35,6 @@ namespace Pinta.Core { public class LayerManager { - #region Public Properties - public UserLayer this[int index] - { - get { return PintaCore.Workspace.ActiveDocument.Layers.UserLayers[index]; } - } - - public UserLayer CurrentLayer - { - get { return PintaCore.Workspace.ActiveDocument.Layers.CurrentUserLayer; } - } - - public int Count { - get { return PintaCore.Workspace.ActiveDocument.Layers.UserLayers.Count; } - } - - public Layer ToolLayer { - get { return PintaCore.Workspace.ActiveDocument.Layers.ToolLayer; } - } - - public Layer SelectionLayer { - get { return PintaCore.Workspace.ActiveDocument.Layers.SelectionLayer; } - } - - public int CurrentLayerIndex { - get { return PintaCore.Workspace.ActiveDocument.Layers.CurrentUserLayerIndex; } - } - - public bool ShowSelectionLayer { - get { return PintaCore.Workspace.ActiveDocument.Layers.ShowSelectionLayer; } - set { PintaCore.Workspace.ActiveDocument.Layers.ShowSelectionLayer = value; } - } - #endregion - - #region Public Methods - public List GetLayersToPaint () - { - return PintaCore.Workspace.ActiveDocument.Layers.GetLayersToPaint (); - } - - public void SetCurrentLayer (int i) - { - PintaCore.Workspace.ActiveDocument.Layers.SetCurrentUserLayer (i); - } - - public void SetCurrentLayer(UserLayer layer) - { - PintaCore.Workspace.ActiveDocument.Layers.SetCurrentUserLayer (layer); - } - - public void FinishSelection () - { - PintaCore.Workspace.ActiveDocument.FinishSelection (); - } - - // Adds a new layer above the current one - public UserLayer AddNewLayer(string name) - { - return PintaCore.Workspace.ActiveDocument.Layers.AddNewLayer (name); - } - - // Adds a new layer above the current one - public void Insert(UserLayer layer, int index) - { - PintaCore.Workspace.ActiveDocument.Layers.Insert (layer, index); - } - - public int IndexOf(UserLayer layer) - { - return PintaCore.Workspace.ActiveDocument.Layers.IndexOf (layer); - } - - // Delete the current layer - public void DeleteCurrentLayer () - { - PintaCore.Workspace.ActiveDocument.Layers.DeleteCurrentLayer (); - } - - // Delete the layer - public void DeleteLayer (int index, bool dispose) - { - PintaCore.Workspace.ActiveDocument.Layers.DeleteLayer (index, dispose); - } - - // Duplicate current layer - public Layer DuplicateCurrentLayer () - { - return PintaCore.Workspace.ActiveDocument.Layers.DuplicateCurrentLayer (); - } - - // Flatten current layer - public void MergeCurrentLayerDown () - { - PintaCore.Workspace.ActiveDocument.Layers.MergeCurrentLayerDown (); - } - - // Move current layer up - public void MoveCurrentLayerUp () - { - PintaCore.Workspace.ActiveDocument.Layers.MoveCurrentLayerUp (); - } - - // Move current layer down - public void MoveCurrentLayerDown () - { - PintaCore.Workspace.ActiveDocument.Layers.MoveCurrentLayerDown (); - } - - // Flip image horizontally - public void FlipImageHorizontal () - { - PintaCore.Workspace.ActiveDocument.FlipImageHorizontal (); - } - - // Flip image vertically - public void FlipImageVertical () - { - PintaCore.Workspace.ActiveDocument.FlipImageVertical (); - } - - // Rotate image 180 degrees (flip H+V) - public void RotateImage180 () - { - PintaCore.Workspace.ActiveDocument.RotateImage180 (); - } - - public void RotateImageCW () - { - PintaCore.Workspace.ActiveDocument.RotateImageCW (); - } - - public void RotateImageCCW () - { - PintaCore.Workspace.ActiveDocument.RotateImageCCW (); - } - - // Flatten image - public void FlattenImage () - { - PintaCore.Workspace.ActiveDocument.Layers.FlattenLayers (); - } - - public void CreateSelectionLayer () - { - PintaCore.Workspace.ActiveDocument.Layers.CreateSelectionLayer (); - } - - public void DestroySelectionLayer () - { - PintaCore.Workspace.ActiveDocument.Layers.DestroySelectionLayer (); - } - - public void ResetSelectionPath () - { - PintaCore.Workspace.ActiveDocument.ResetSelectionPaths (); - } - #endregion - #region Protected Methods protected internal void OnLayerAdded () { @@ -213,16 +56,6 @@ protected internal void OnSelectedLayerChanged () #endregion #region Private Methods - public Layer CreateLayer () - { - return PintaCore.Workspace.ActiveDocument.Layers.CreateLayer (); - } - - public Layer CreateLayer (string name, int width, int height) - { - return PintaCore.Workspace.ActiveDocument.Layers.CreateLayer (name, width, height); - } - internal void RaiseLayerPropertyChangedEvent (object? sender, PropertyChangedEventArgs e) { if (LayerPropertyChanged != null) diff --git a/Pinta.Core/Managers/LivePreviewManager.cs b/Pinta.Core/Managers/LivePreviewManager.cs index cc38e4bf32..2ac4da4a2b 100644 --- a/Pinta.Core/Managers/LivePreviewManager.cs +++ b/Pinta.Core/Managers/LivePreviewManager.cs @@ -78,11 +78,13 @@ public void Start (BaseEffect effect) // Start rendering. // Listen for changes to effectConfiguration object, and restart render if needed. + var doc = PintaCore.Workspace.ActiveDocument; + live_preview_enabled = true; apply_live_preview_flag = false; cancel_live_preview_flag = false; - layer = PintaCore.Layers.CurrentLayer; + layer = doc.Layers.CurrentUserLayer; this.effect = effect; //TODO Use the current tool layer instead. @@ -92,13 +94,13 @@ public void Start (BaseEffect effect) // Handle selection path. PintaCore.Tools.Commit (); - var selection = PintaCore.Workspace.ActiveDocument.Selection; + var selection = doc.Selection; selection_path = (selection.Visible) ? selection.SelectionPath : null; render_bounds = (selection_path != null) ? selection_path.GetBounds () : live_preview_surface.GetBounds (); render_bounds = PintaCore.Workspace.ClampToImageSize (render_bounds); history_item = new SimpleHistoryItem (effect.Icon, effect.Name); - history_item.TakeSnapshotOfLayer (PintaCore.Layers.CurrentLayerIndex); + history_item.TakeSnapshotOfLayer (doc.Layers.CurrentUserLayerIndex); // Paint the pre-effect layer surface into into the working surface. using (var ctx = new Cairo.Context (live_preview_surface)) { diff --git a/Pinta.Effects/Dialogs/Effects.LevelsDialog.cs b/Pinta.Effects/Dialogs/Effects.LevelsDialog.cs index ca1ade0b99..c3068ca0ab 100644 --- a/Pinta.Effects/Dialogs/Effects.LevelsDialog.cs +++ b/Pinta.Effects/Dialogs/Effects.LevelsDialog.cs @@ -135,8 +135,10 @@ private void UpdateLivePreview () private void UpdateInputHistogram () { - ImageSurface surface = PintaCore.Layers.CurrentLayer.Surface; - Gdk.Rectangle rect = PintaCore.Workspace.ActiveDocument.Selection.SelectionPath.GetBounds (); + var doc = PintaCore.Workspace.ActiveDocument; + + ImageSurface surface = doc.Layers.CurrentUserLayer.Surface; + Gdk.Rectangle rect = doc.Selection.SelectionPath.GetBounds (); histogramInput.Histogram.UpdateHistogram (surface, rect); UpdateOutputHistogram (); } diff --git a/Pinta.Gui.Widgets/Widgets/Canvas/CanvasRenderer.cs b/Pinta.Gui.Widgets/Widgets/Canvas/CanvasRenderer.cs index af06594e3f..be4a62e1c8 100644 --- a/Pinta.Gui.Widgets/Widgets/Canvas/CanvasRenderer.cs +++ b/Pinta.Gui.Widgets/Widgets/Canvas/CanvasRenderer.cs @@ -60,6 +60,8 @@ public void Render (List layers, Cairo.ImageSurface dst, Gdk.Point offset // Our rectangle of interest var r = new Gdk.Rectangle (offset, dst.GetBounds ().Size).ToCairoRectangle (); + var doc = PintaCore.Workspace.ActiveDocument; + using (var g = new Cairo.Context (dst)) { // Create the transparent checkerboard background @@ -70,7 +72,7 @@ public void Render (List layers, Cairo.ImageSurface dst, Gdk.Point offset var layer = layers[i]; // If we're in LivePreview, substitute current layer with the preview layer - if (layer == PintaCore.Layers.CurrentLayer && PintaCore.LivePreview.IsEnabled) + if (layer == doc.Layers.CurrentUserLayer && PintaCore.LivePreview.IsEnabled) layer = CreateLivePreviewLayer (layer); // If the layer is offset, handle it here diff --git a/Pinta.Gui.Widgets/Widgets/Layers/LayersListWidget.cs b/Pinta.Gui.Widgets/Widgets/Layers/LayersListWidget.cs index 16c0c2a636..f554160fed 100644 --- a/Pinta.Gui.Widgets/Widgets/Layers/LayersListWidget.cs +++ b/Pinta.Gui.Widgets/Widgets/Layers/LayersListWidget.cs @@ -141,10 +141,12 @@ private void SelectLayerInTreeView (int layerIndex) } private void HandleLayerSelected (object? o, EventArgs e) - { + { + var doc = PintaCore.Workspace.ActiveDocument; var layer = GetSelectedLayerInTreeView (); - if (PintaCore.Layers.CurrentLayer != layer && layer != null) - PintaCore.Layers.SetCurrentLayer (layer); + + if (doc.Layers.CurrentUserLayer != layer && layer != null) + doc.Layers.SetCurrentUserLayer (layer); } private void LayerVisibilityToggled (object? o, ToggledArgs args) @@ -247,11 +249,13 @@ public void Reset () store.AppendValues (surf, layer.Name, !layer.Hidden, layer); } - SelectLayerInTreeView (PintaCore.Layers.Count - PintaCore.Layers.CurrentLayerIndex - 1); + SelectLayerInTreeView (doc.Layers.Count () - doc.Layers.CurrentUserLayerIndex - 1); } private void SetLayerVisibility(UserLayer layer, bool visibility) { + var doc = PintaCore.Workspace.ActiveDocument; + layer.Hidden = !visibility; var initial = new LayerProperties(layer.Name, visibility, layer.Opacity, layer.BlendMode); @@ -260,11 +264,11 @@ private void SetLayerVisibility(UserLayer layer, bool visibility) var historyItem = new UpdateLayerPropertiesHistoryItem ( Resources.Icons.LayerProperties, (visibility) ? Translations.GetString ("Layer Shown") : Translations.GetString ("Layer Hidden"), - PintaCore.Layers.IndexOf (layer), + doc.Layers.IndexOf (layer), initial, updated); - PintaCore.Workspace.ActiveDocument.History.PushNewItem (historyItem); + doc.History.PushNewItem (historyItem); //TODO Call this automatically when the layer visibility changes. PintaCore.Workspace.Invalidate (); diff --git a/Pinta.Gui.Widgets/Widgets/PointPickerGraphic.cs b/Pinta.Gui.Widgets/Widgets/PointPickerGraphic.cs index 6a7c59aa74..2224176286 100644 --- a/Pinta.Gui.Widgets/Widgets/PointPickerGraphic.cs +++ b/Pinta.Gui.Widgets/Widgets/PointPickerGraphic.cs @@ -49,6 +49,8 @@ public PointPickerGraphic () private void UpdateThumbnail () { + var doc = PintaCore.Workspace.ActiveDocument; + double scalex = (double)Allocation.Width / (double)PintaCore.Workspace.ImageSize.Width; double scaley = (double)Allocation.Height / (double)PintaCore.Workspace.ImageSize.Height; @@ -56,7 +58,7 @@ private void UpdateThumbnail () using (Cairo.Context g = new Cairo.Context (thumbnail)) { g.Scale (scalex, scaley); - foreach (Layer layer in PintaCore.Layers.GetLayersToPaint ()) { + foreach (Layer layer in doc.Layers.GetLayersToPaint ()) { layer.Draw(g); } } diff --git a/Pinta/Actions/Layers/LayerPropertiesAction.cs b/Pinta/Actions/Layers/LayerPropertiesAction.cs index 35500bab3b..4097b972d0 100644 --- a/Pinta/Actions/Layers/LayerPropertiesAction.cs +++ b/Pinta/Actions/Layers/LayerPropertiesAction.cs @@ -46,6 +46,8 @@ public void Uninitialize () private void Activated (object sender, EventArgs e) { + var doc = PintaCore.Workspace.ActiveDocument; + using var dialog = new LayerPropertiesDialog (); int response = dialog.Run (); @@ -60,18 +62,18 @@ private void Activated (object sender, EventArgs e) var historyItem = new UpdateLayerPropertiesHistoryItem ( Resources.Icons.LayerProperties, historyMessage, - PintaCore.Layers.CurrentLayerIndex, + doc.Layers.CurrentUserLayerIndex, dialog.InitialLayerProperties, dialog.UpdatedLayerProperties); - PintaCore.Workspace.ActiveWorkspace.History.PushNewItem (historyItem); + doc.History.PushNewItem (historyItem); PintaCore.Workspace.ActiveWorkspace.Invalidate (); } else { - var layer = PintaCore.Workspace.ActiveDocument.Layers.CurrentUserLayer; - var selectionLayer = PintaCore.Workspace.ActiveDocument.Layers.SelectionLayer; + var layer = doc.Layers.CurrentUserLayer; + var selectionLayer = doc.Layers.SelectionLayer; var initial = dialog.InitialLayerProperties; initial.SetProperties (layer); if (selectionLayer != null) diff --git a/Pinta/Dialogs/LayerPropertiesDialog.cs b/Pinta/Dialogs/LayerPropertiesDialog.cs index f1c9524432..857cbc004e 100644 --- a/Pinta/Dialogs/LayerPropertiesDialog.cs +++ b/Pinta/Dialogs/LayerPropertiesDialog.cs @@ -52,14 +52,16 @@ public LayerPropertiesDialog () : base (Translations.GetString ("Layer Propertie PintaCore.Chrome.MainWindow, DialogFlags.Modal, Core.GtkExtensions.DialogButtonsCancelOk()) { - Build (); + var doc = PintaCore.Workspace.ActiveDocument; + + Build (); IconName = Resources.Icons.LayerProperties; - name = PintaCore.Layers.CurrentLayer.Name; - hidden = PintaCore.Layers.CurrentLayer.Hidden; - opacity = PintaCore.Layers.CurrentLayer.Opacity; - blendmode = PintaCore.Layers.CurrentLayer.BlendMode; + name = doc.Layers.CurrentUserLayer.Name; + hidden = doc.Layers.CurrentUserLayer.Hidden; + opacity = doc.Layers.CurrentUserLayer.Opacity; + blendmode = doc.Layers.CurrentUserLayer.BlendMode; initial_properties = new LayerProperties( name, @@ -112,17 +114,21 @@ public LayerProperties UpdatedLayerProperties { #region Private Methods private void OnLayerNameChanged (object? sender, EventArgs e) { + var doc = PintaCore.Workspace.ActiveDocument; + name = layerNameEntry.Text; - PintaCore.Layers.CurrentLayer.Name = name; + doc.Layers.CurrentUserLayer.Name = name; } private void OnVisibilityToggled (object? sender, EventArgs e) { + var doc = PintaCore.Workspace.ActiveDocument; + hidden = !visibilityCheckbox.Active; - PintaCore.Layers.CurrentLayer.Hidden = hidden; - if (PintaCore.Layers.SelectionLayer != null) { + doc.Layers.CurrentUserLayer.Hidden = hidden; + if (doc.Layers.SelectionLayer != null) { //Update Visiblity for SelectionLayer and force redraw - PintaCore.Layers.SelectionLayer.Hidden = PintaCore.Layers.CurrentLayer.Hidden; + doc.Layers.SelectionLayer.Hidden = doc.Layers.CurrentUserLayer.Hidden; } PintaCore.Workspace.Invalidate (); } @@ -141,23 +147,27 @@ private void OnOpacitySpinnerChanged (object? sender, EventArgs e) private void UpdateOpacity () { + var doc = PintaCore.Workspace.ActiveDocument; + //TODO check redraws are being throttled. opacity = opacitySpinner.Value / 100d; - PintaCore.Layers.CurrentLayer.Opacity = opacity; - if (PintaCore.Layers.SelectionLayer != null) { + doc.Layers.CurrentUserLayer.Opacity = opacity; + if (doc.Layers.SelectionLayer != null) { //Update Opacity for SelectionLayer and force redraw - PintaCore.Layers.SelectionLayer.Opacity = PintaCore.Layers.CurrentLayer.Opacity; + doc.Layers.SelectionLayer.Opacity = doc.Layers.CurrentUserLayer.Opacity; } PintaCore.Workspace.Invalidate (); } private void OnBlendModeChanged (object? sender, EventArgs e) { + var doc = PintaCore.Workspace.ActiveDocument; + blendmode = UserBlendOps.GetBlendModeByName (blendComboBox.ActiveText); - PintaCore.Layers.CurrentLayer.BlendMode = blendmode; - if (PintaCore.Layers.SelectionLayer != null) { + doc.Layers.CurrentUserLayer.BlendMode = blendmode; + if (doc.Layers.SelectionLayer != null) { //Update BlendMode for SelectionLayer and force redraw - PintaCore.Layers.SelectionLayer.BlendMode = PintaCore.Layers.CurrentLayer.BlendMode; + doc.Layers.SelectionLayer.BlendMode = doc.Layers.CurrentUserLayer.BlendMode; } PintaCore.Workspace.Invalidate (); }