Skip to content

Commit

Permalink
Now tabs are unique to documents, they don't depend on active one
Browse files Browse the repository at this point in the history
  • Loading branch information
flabbet committed Dec 20, 2020
1 parent 8fa06d1 commit 8996dec
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 166 deletions.
42 changes: 11 additions & 31 deletions PixiEditor/Models/Controllers/BitmapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace PixiEditor.Models.Controllers
public class BitmapManager : NotifyableObject
{
private Document activeDocument;
private Layer previewLayer;
private Tool selectedTool;

public BitmapManager()
Expand Down Expand Up @@ -50,16 +49,6 @@ private set
}
}

public Layer PreviewLayer
{
get => previewLayer;
set
{
previewLayer = value;
RaisePropertyChanged("PreviewLayer");
}
}

public Layer ActiveLayer => ActiveDocument.ActiveLayer;

public Color PrimaryColor { get; set; }
Expand Down Expand Up @@ -132,18 +121,6 @@ public void ExecuteTool(Coordinates newPosition, bool clickedOnCanvas)
}
}

public void GeneratePreviewLayer()
{
if (ActiveDocument != null)
{
PreviewLayer = new Layer("_previewLayer")
{
MaxWidth = ActiveDocument.Width,
MaxHeight = ActiveDocument.Height
};
}
}

public WriteableBitmap GetCombinedLayersBitmap()
{
return BitmapUtils.CombineLayers(ActiveDocument.Layers.Where(x => x.IsVisible).ToArray(), ActiveDocument.Width, ActiveDocument.Height);
Expand All @@ -159,7 +136,10 @@ public bool IsOperationTool()

public void SetActiveTool(Tool tool)
{
PreviewLayer = null;
if (ActiveDocument != null)
{
ActiveDocument.PreviewLayer = null;
}
SelectedTool?.Toolbar.SaveToolbarSettings();
SelectedTool = tool;
SelectedTool.Toolbar.LoadSharedSettings();
Expand Down Expand Up @@ -196,7 +176,7 @@ private bool IsDraggingViewport()
private void MouseController_StartedRecordingChanges(object sender, EventArgs e)
{
SelectedTool.OnRecordingLeftMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
PreviewLayer = null;
ActiveDocument.PreviewLayer = null;
}

private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
Expand All @@ -220,25 +200,25 @@ private void HighlightPixels(Coordinates newPosition)
if (CanChangeHighlightOffset(highlightArea))
{
Coordinates start = highlightArea.First();
PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
ActiveDocument.PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
}
else if (!IsInsideBounds(highlightArea))
{
PreviewLayer = null;
ActiveDocument.PreviewLayer = null;
}
else
{
GeneratePreviewLayer();
PreviewLayer.SetPixels(
ActiveDocument.GeneratePreviewLayer();
ActiveDocument.PreviewLayer.SetPixels(
BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77, 0, 0, 0)));
}
}

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

private bool IsInsideBounds(IEnumerable<Coordinates> highlightArea)
Expand Down
6 changes: 3 additions & 3 deletions PixiEditor/Models/Controllers/BitmapOperationsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void ApplyPreviewLayer()
lastModifiedLayers[i].PixelChanges,
oldValues,
lastModifiedLayers[i].LayerIndex));
Manager.PreviewLayer = null;
Manager.ActiveDocument.GeneratePreviewLayer();
}
}

Expand Down Expand Up @@ -184,13 +184,13 @@ private void UseToolOnPreviewLayer(List<Coordinates> mouseMove)
LayerChange[] modifiedLayers;
if (mouseMove.Count > 0 && mouseMove[0] != lastMousePos)
{
Manager.GeneratePreviewLayer();
Manager.ActiveDocument.GeneratePreviewLayer();
modifiedLayers = ((BitmapOperationTool)Manager.SelectedTool).Use(
Manager.ActiveDocument.ActiveLayer,
mouseMove.ToArray(),
Manager.PrimaryColor);
BitmapPixelChanges[] changes = modifiedLayers.Select(x => x.PixelChanges).ToArray();
Manager.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
Manager.ActiveDocument.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
lastModifiedLayers = modifiedLayers;
}
}
Expand Down
103 changes: 103 additions & 0 deletions PixiEditor/Models/DataHolders/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public Document(int width, int height)
Height = height;
RequestCloseDocumentCommand = new RelayCommand(RequestCloseDocument);
UndoManager = new UndoManager();
XamlAccesibleViewModel = ViewModelMain.Current ?? null;
GeneratePreviewLayer();
DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(0, 0, width, height));
}

Expand All @@ -37,6 +39,18 @@ public Document(int width, int height)

public RelayCommand RequestCloseDocumentCommand { get; set; }

private ViewModelMain xamlAccesibleViewModel = null;

public ViewModelMain XamlAccesibleViewModel // Used to access ViewModelMain, without changing DataContext in XAML
{
get => xamlAccesibleViewModel;
set
{
xamlAccesibleViewModel = value;
RaisePropertyChanged(nameof(XamlAccesibleViewModel));
}
}

private string documentFilePath = string.Empty;

public string DocumentFilePath
Expand Down Expand Up @@ -101,6 +115,78 @@ public Selection ActiveSelection
}
}

private Layer previewLayer;

public Layer PreviewLayer
{
get => previewLayer;
set
{
previewLayer = value;
RaisePropertyChanged("PreviewLayer");
}
}

private double mouseXonCanvas;

private double mouseYonCanvas;

public double MouseXOnCanvas // Mouse X coordinate relative to canvas
{
get => mouseXonCanvas;
set
{
mouseXonCanvas = value;
RaisePropertyChanged(nameof(MouseXOnCanvas));
}
}

public double MouseYOnCanvas // Mouse Y coordinate relative to canvas
{
get => mouseYonCanvas;
set
{
mouseYonCanvas = value;
RaisePropertyChanged(nameof(MouseYOnCanvas));
}
}

private double zoomPercentage = 100;

public double ZoomPercentage
{
get => zoomPercentage;
set
{
zoomPercentage = value;
RaisePropertyChanged(nameof(ZoomPercentage));
}
}

private Point viewPortPosition;

public Point ViewportPosition
{
get => viewPortPosition;
set
{
viewPortPosition = value;
RaisePropertyChanged(nameof(ViewportPosition));
}
}

private bool recenterZoombox = true;

public bool RecenterZoombox
{
get => recenterZoombox;
set
{
recenterZoombox = value;
RaisePropertyChanged(nameof(RecenterZoombox));
}
}

public UndoManager UndoManager { get; set; }

public ObservableCollection<Layer> Layers { get; set; } = new ObservableCollection<Layer>();
Expand All @@ -118,6 +204,23 @@ public int ActiveLayerIndex
}
}

public void GeneratePreviewLayer()
{
PreviewLayer = new Layer("_previewLayer")
{
MaxWidth = Width,
MaxHeight = Height
};
}

public void CenterViewport()
{
RecenterZoombox = false; // It's a trick to trigger change in UserControl
RecenterZoombox = true;
ViewportPosition = default;
ZoomPercentage = default;
}

public void SaveWithDialog()
{
bool savedSuccessfully = Exporter.SaveAsEditableFileWithDialog(this, out string path);
Expand Down
1 change: 1 addition & 0 deletions PixiEditor/Models/Layers/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ public void ClipCanvas()
public void Clear()
{
LayerBitmap.Clear();
ClipCanvas();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/Models/Tools/Tools/MoveViewportTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void OnMouseMove(MouseEventArgs e)
if (e.LeftButton == MouseButtonState.Pressed || e.MiddleButton == MouseButtonState.Pressed)
{
var point = MousePositionConverter.GetCursorPosition();
ViewModelMain.Current.ViewportSubViewModel.ViewportPosition = new System.Windows.Point(
ViewModelMain.Current.BitmapManager.ActiveDocument.ViewportPosition = new System.Windows.Point(
point.X - clickPoint.X,
point.Y - clickPoint.Y);
}
Expand Down
4 changes: 2 additions & 2 deletions PixiEditor/Models/Tools/Tools/ZoomTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override void OnKeyUp(KeyEventArgs e)
public override void OnRecordingLeftMouseDown(MouseEventArgs e)
{
startingX = MousePositionConverter.GetCursorPosition().X;
ViewModelMain.Current.ViewportSubViewModel.ZoomPercentage = 100; // This resest the value, so callback in MainDrawingPanel can fire again later
ViewModelMain.Current.BitmapManager.ActiveDocument.ZoomPercentage = 100; // This resest the value, so callback in MainDrawingPanel can fire again later
}

public override void OnMouseMove(MouseEventArgs e)
Expand Down Expand Up @@ -78,7 +78,7 @@ public override void OnStoppedRecordingMouseUp(MouseEventArgs e)

public void Zoom(double percentage)
{
ViewModelMain.Current.ViewportSubViewModel.ZoomPercentage = percentage;
ViewModelMain.Current.BitmapManager.ActiveDocument.ZoomPercentage = percentage;
}

public override void Use(Coordinates[] pixels)
Expand Down
36 changes: 7 additions & 29 deletions PixiEditor/ViewModels/SubViewModels/Main/IoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,6 @@ public class IoViewModel : SubViewModel<ViewModelMain>

public RelayCommand KeyUpCommand { get; set; }

private double mouseXonCanvas;

private double mouseYonCanvas;

public double MouseXOnCanvas // Mouse X coordinate relative to canvas
{
get => mouseXonCanvas;
set
{
mouseXonCanvas = value;
RaisePropertyChanged(nameof(MouseXOnCanvas));
}
}

public double MouseYOnCanvas // Mouse Y coordinate relative to canvas
{
get => mouseYonCanvas;
set
{
mouseYonCanvas = value;
RaisePropertyChanged(nameof(MouseYOnCanvas));
}
}

private bool restoreToolOnKeyUp = false;

public IoViewModel(ViewModelMain owner)
Expand Down Expand Up @@ -90,10 +66,10 @@ private void MouseDown(object parameter)
{
if (!Owner.BitmapManager.MouseController.IsRecordingChanges)
{
bool clickedOnCanvas = MouseXOnCanvas >= 0 &&
MouseXOnCanvas <= Owner.BitmapManager.ActiveDocument.Width &&
MouseYOnCanvas >= 0 &&
MouseYOnCanvas <= Owner.BitmapManager.ActiveDocument.Height;
bool clickedOnCanvas = Owner.BitmapManager.ActiveDocument.MouseXOnCanvas >= 0 &&
Owner.BitmapManager.ActiveDocument.MouseXOnCanvas <= Owner.BitmapManager.ActiveDocument.Width &&
Owner.BitmapManager.ActiveDocument.MouseYOnCanvas >= 0 &&
Owner.BitmapManager.ActiveDocument.MouseYOnCanvas <= Owner.BitmapManager.ActiveDocument.Height;
Owner.BitmapManager.MouseController.StartRecordingMouseMovementChanges(clickedOnCanvas);
Owner.BitmapManager.MouseController.RecordMouseMovementChange(MousePositionConverter.CurrentCoordinates);
}
Expand All @@ -115,7 +91,9 @@ private void MouseDown(object parameter)
/// <param name="parameter">CommandParameter.</param>
private void MouseMove(object parameter)
{
Coordinates cords = new Coordinates((int)MouseXOnCanvas, (int)MouseYOnCanvas);
Coordinates cords = new Coordinates(
(int)Owner.BitmapManager.ActiveDocument.MouseXOnCanvas,
(int)Owner.BitmapManager.ActiveDocument.MouseYOnCanvas);
MousePositionConverter.CurrentCoordinates = cords;

if (Owner.BitmapManager.MouseController.IsRecordingChanges && Mouse.LeftButton == MouseButtonState.Pressed)
Expand Down
Loading

0 comments on commit 8996dec

Please sign in to comment.