Skip to content

Commit

Permalink
New preview layer and highlight wip
Browse files Browse the repository at this point in the history
  • Loading branch information
flabbet committed Oct 13, 2021
1 parent 2a3a2ea commit d7acc86
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
62 changes: 30 additions & 32 deletions PixiEditor/Models/Controllers/BitmapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ namespace PixiEditor.Models.Controllers
[DebuggerDisplay("{Documents.Count} Document(s)")]
public class BitmapManager : NotifyableObject
{
private int previewLayerSize;
private Document activeDocument;
private Tool selectedTool;
private Coordinates? startPosition = null;
private IEnumerable<Coordinates> cachedHighlight;
private int halfSize;
private BitmapPixelChanges cachedPixels;

public BitmapManager()
{
Expand All @@ -35,6 +39,7 @@ public BitmapManager()
MouseController.OnMouseDownCoordinates += MouseController_OnMouseDownCoordinates;
BitmapOperations = new BitmapOperationsUtility(this);
ReadonlyToolUtility = new ReadonlyToolUtility();
DocumentChanged += BitmapManager_DocumentChanged;
}

public event EventHandler<DocumentChangedEventArgs> DocumentChanged;
Expand Down Expand Up @@ -154,14 +159,15 @@ public bool IsOperationTool()

public void SetActiveTool(Tool tool)
{
if (ActiveDocument != null)
{
ActiveDocument.PreviewLayer = null;
}

ActiveDocument?.PreviewLayer?.Clear();
SelectedTool?.Toolbar.SaveToolbarSettings();
SelectedTool = tool;
SelectedTool.Toolbar.LoadSharedSettings();
}

private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventArgs e)
{
e.NewDocument.GeneratePreviewLayer();
}

private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
Expand Down Expand Up @@ -207,7 +213,7 @@ private void MouseController_StartedRecordingChanges(object sender, EventArgs e)
SelectedTool.OnRecordingLeftMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
if (ActiveDocument != null)
{
ActiveDocument.PreviewLayer = null;
ActiveDocument.PreviewLayer.Clear();
}
}

Expand All @@ -229,38 +235,30 @@ private void HighlightPixels(Coordinates newPosition)
return;
}

IEnumerable<Coordinates> highlightArea = CoordinatesCalculator.RectangleToCoordinates(
CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));
if (CanChangeHighlightOffset(highlightArea))
{
Coordinates start = highlightArea.First();
ActiveDocument.PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
}
else if (!IsInsideBounds(highlightArea))
{
ActiveDocument.PreviewLayer = null;
if (ToolSize != previewLayerSize)
{
cachedHighlight = CoordinatesCalculator.RectangleToCoordinates(
CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));

previewLayerSize = ToolSize;
halfSize = (int)Math.Floor(ToolSize / 2f);

cachedPixels = BitmapPixelChanges.FromSingleColoredArray(cachedHighlight, new SKColor(0, 0, 0, 77));

ActiveDocument.PreviewLayer.SetPixels(cachedPixels);
ActiveDocument.PreviewLayer.ClipCanvas();
}
else if (ActiveDocument.PreviewLayer == null)

Coordinates start = newPosition - halfSize;
ActiveDocument.PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);

if (!IsInsideBounds(cachedHighlight))
{
ActiveDocument.GeneratePreviewLayer();
ActiveDocument.PreviewLayer.SetPixels(
BitmapPixelChanges.FromSingleColoredArray(highlightArea, new SKColor(0, 0, 0, 77)));
}
else
{
ActiveDocument.PreviewLayer.Clear();
ActiveDocument.PreviewLayer.SetPixels(
BitmapPixelChanges.FromSingleColoredArray(highlightArea, new SKColor(0, 0, 0, 77)));
previewLayerSize = -1;
}
}

private bool CanChangeHighlightOffset(IEnumerable<Coordinates> highlightArea)
{
int count = highlightArea.Count();
return count > 0 && ActiveDocument.PreviewLayer != null &&
IsInsideBounds(highlightArea) && count == ActiveDocument.PreviewLayer.Width * ActiveDocument.PreviewLayer.Height;
}

private bool IsInsideBounds(IEnumerable<Coordinates> highlightArea)
{
Coordinates start = highlightArea.First();
Expand Down
4 changes: 2 additions & 2 deletions PixiEditor/Models/Controllers/BitmapOperationsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ private void UseToolOnPreviewLayer(List<Coordinates> mouseMove, bool clearPrevie
{
if (mouseMove.Count > 0 && mouseMove[0] != lastMousePos)
{
if (clearPreviewLayer || Manager.ActiveDocument.PreviewLayer == null)
if (clearPreviewLayer || !Manager.ActiveDocument.PreviewLayer.IsCleared)
{
Manager.ActiveDocument.GeneratePreviewLayer();
Manager.ActiveDocument.PreviewLayer.Clear();
}

// TODO: Use on preview layer
Expand Down
5 changes: 5 additions & 0 deletions PixiEditor/Models/Layers/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,19 @@ public Thickness Offset

public int MaxHeight { get; set; } = int.MaxValue;

public bool IsCleared { get; private set; }

public event EventHandler<Int32Rect> LayerBitmapChanged;

public void InvokeLayerBitmapChange()
{
IsCleared = false;
LayerBitmapChanged?.Invoke(this, new Int32Rect(OffsetX, OffsetY, Width, Height));
}

public void InvokeLayerBitmapChange(Int32Rect dirtyArea)
{
IsCleared = false;
LayerBitmapChanged?.Invoke(this, dirtyArea);
}

Expand Down Expand Up @@ -462,6 +466,7 @@ public void ClipCanvas()
/// </summary>
public void Clear()
{
IsCleared = true;
LayerBitmap.SkiaSurface.Canvas.Clear();
ClipCanvas();
InvokeLayerBitmapChange();
Expand Down
5 changes: 5 additions & 0 deletions PixiEditor/Models/Position/Coordinates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public Coordinates(int x, int y)
return new Coordinates(tuple.width, tuple.height);
}

public static Coordinates operator -(Coordinates coordiantes, int size)
{
return new Coordinates(coordiantes.X - size, coordiantes.Y - size);
}

public static bool operator ==(Coordinates c1, Coordinates c2)
{
return c2.X == c1.X && c2.Y == c1.Y;
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/ViewModels/ViewModelMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void ResetProgramStateValues()
{
foreach (var document in BitmapManager.Documents)
{
document.PreviewLayer = null;
document.PreviewLayer.Clear();
}
}

Expand Down

0 comments on commit d7acc86

Please sign in to comment.