Skip to content

Commit

Permalink
Merge pull request PintaProject#164 from jpobst/layer-manager
Browse files Browse the repository at this point in the history
Call DocumentLayers methods directly instead of using LayerManager.
  • Loading branch information
cameronwhite authored Dec 26, 2020
2 parents 136c74f + f97d315 commit 29008c7
Show file tree
Hide file tree
Showing 23 changed files with 192 additions and 273 deletions.
10 changes: 10 additions & 0 deletions Pinta.Core/Classes/DocumentLayers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ internal void Close ()
selection_layer?.Surface.Dispose ();
}

/// <summary>
/// Returns the number of user layers.
/// </summary>
public int Count () => user_layers.Count;

/// <summary>
/// Creates a new layer, but does not add it to the layer collection.
/// </summary>
Expand Down Expand Up @@ -426,5 +431,10 @@ public void SetCurrentUserLayer (UserLayer layer)
{
SetCurrentUserLayer (user_layers.IndexOf (layer));
}

/// <summary>
/// Gets the user layer at the specified index.
/// </summary>
public UserLayer this[int index] => user_layers[index];
}
}
4 changes: 3 additions & 1 deletion Pinta.Core/Classes/DocumentSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
8 changes: 6 additions & 2 deletions Pinta.Core/Classes/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Core/Classes/SelectionModeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static void PerformSelectionMode (CombineMode mode, List<List<IntPoint>>
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)
Expand Down
8 changes: 6 additions & 2 deletions Pinta.Core/Extensions/CairoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 9 additions & 5 deletions Pinta.Core/HistoryItems/AddLayerHistoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 8 additions & 4 deletions Pinta.Core/HistoryItems/DeleteLayerHistoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
40 changes: 23 additions & 17 deletions Pinta.Core/HistoryItems/FinishPixelsHistoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ();
}

Expand All @@ -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);
}
}
}
32 changes: 18 additions & 14 deletions Pinta.Core/HistoryItems/InvertHistoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,63 +87,67 @@ 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;
}
}

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;
}
Expand Down
20 changes: 12 additions & 8 deletions Pinta.Core/HistoryItems/MovePixelsHistoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -97,6 +99,8 @@ private void Swap ()

public void TakeSnapshot (bool lift)
{
var doc = PintaCore.Workspace.ActiveDocument;

lifted = lift;
is_lifted = true;

Expand All @@ -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);
}
}
}
4 changes: 3 additions & 1 deletion Pinta.Core/HistoryItems/PasteHistoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}

Expand Down
Loading

0 comments on commit 29008c7

Please sign in to comment.