Skip to content

Commit

Permalink
Added reference layer picking, improved Dependency Injection and move…
Browse files Browse the repository at this point in the history
…d tool stuff to ToolsViewModel
  • Loading branch information
CPKreu committed Dec 2, 2021
1 parent 6535dd2 commit 55f3377
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 295 deletions.
63 changes: 63 additions & 0 deletions PixiEditor/Helpers/Extensions/ServiceCollectionHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.Extensions.DependencyInjection;
using PixiEditor.Models.Controllers;
using PixiEditor.Models.Controllers.Shortcuts;
using PixiEditor.Models.Services;
using PixiEditor.Models.Tools;
using PixiEditor.Models.Tools.Tools;
using PixiEditor.Models.UserPreferences;
using PixiEditor.ViewModels;
using PixiEditor.ViewModels.SubViewModels.Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PixiEditor.Helpers.Extensions
{
public static class ServiceCollectionHelpers
{
/// <summary>
/// Add's all the services required to fully run PixiEditor's MainWindow
/// </summary>
public static IServiceCollection AddPixiEditor(this IServiceCollection collection) => collection
.AddSingleton<ViewModelMain>()
.AddSingleton<IPreferences, PreferencesSettings>()
// View Models
.AddSingleton<StylusViewModel>()
.AddSingleton<WindowViewModel>()
.AddSingleton<ToolsViewModel>()
.AddSingleton<FileViewModel>()
.AddSingleton<UpdateViewModel>()
.AddSingleton<IoViewModel>()
.AddSingleton<LayersViewModel>()
.AddSingleton<ClipboardViewModel>()
.AddSingleton<UndoViewModel>()
.AddSingleton<SelectionViewModel>()
.AddSingleton<ViewportViewModel>()
.AddSingleton<ColorsViewModel>()
.AddSingleton<DocumentViewModel>()
.AddSingleton<MiscViewModel>()
.AddSingleton(x => new DiscordViewModel(x.GetService<ViewModelMain>(), "764168193685979138"))
.AddSingleton<DebugViewModel>()
// Controllers
.AddSingleton<ShortcutController>()
.AddSingleton<BitmapManager>()
// Tools
.AddSingleton<Tool, MoveViewportTool>()
.AddSingleton<Tool, MoveTool>()
.AddSingleton<Tool, PenTool>()
.AddSingleton<Tool, SelectTool>()
.AddSingleton<Tool, MagicWandTool>()
.AddSingleton<Tool, FloodFillTool>()
.AddSingleton<Tool, LineTool>()
.AddSingleton<Tool, CircleTool>()
.AddSingleton<Tool, RectangleTool>()
.AddSingleton<Tool, EraserTool>()
.AddSingleton<Tool, ColorPickerTool>()
.AddSingleton<Tool, BrightnessTool>()
.AddSingleton<Tool, ZoomTool>()
// Other
.AddSingleton<DocumentProvider>();
}
}
149 changes: 57 additions & 92 deletions PixiEditor/Models/Controllers/BitmapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using PixiEditor.Models.Tools;
using PixiEditor.Models.Tools.Tools;
using PixiEditor.Models.Tools.ToolSettings.Settings;
using PixiEditor.ViewModels;
using PixiEditor.ViewModels.SubViewModels.Main;
using SkiaSharp;
using System;
using System.Collections.Generic;
Expand All @@ -20,24 +22,27 @@ namespace PixiEditor.Models.Controllers
[DebuggerDisplay("{Documents.Count} Document(s)")]
public class BitmapManager : NotifyableObject
{
private readonly ToolsViewModel _tools;

private int previewLayerSize;
private Document activeDocument;
private Tool selectedTool;
private Document activeDocument;
private Coordinates? startPosition = null;
private int halfSize;
private SKColor _highlightColor;
private PenTool _highlightPen;

public BitmapManager()
public BitmapManager(ToolsViewModel tools)
{
_tools = tools;

MouseController = new MouseMovementController();
MouseController.StartedRecordingChanges += MouseController_StartedRecordingChanges;
MouseController.MousePositionChanged += Controller_MousePositionChanged;
MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
MouseController.OnMouseDown += MouseController_OnMouseDown;
MouseController.OnMouseUp += MouseController_OnMouseUp;
MouseController.OnMouseDownCoordinates += MouseController_OnMouseDownCoordinates;
BitmapOperations = new BitmapOperationsUtility(this);
BitmapOperations = new BitmapOperationsUtility(this, tools);
ReadonlyToolUtility = new ReadonlyToolUtility();
DocumentChanged += BitmapManager_DocumentChanged;
_highlightPen = new PenTool(this)
Expand All @@ -53,38 +58,10 @@ public BitmapManager()

public MouseMovementController MouseController { get; set; }

public Tool SelectedTool
{
get => selectedTool;
private set
{
Tool previousTool = selectedTool;
if (SetProperty(ref selectedTool, value))
{
SelectedToolChanged?.Invoke(this, new SelectedToolEventArgs(previousTool, value));
}
}
}

public Layer ActiveLayer => ActiveDocument.ActiveLayer;

public SKColor PrimaryColor { get; set; }

public int ToolSize
{
get => SelectedTool.Toolbar.GetSetting<SizeSetting>("ToolSize") != null
? SelectedTool.Toolbar.GetSetting<SizeSetting>("ToolSize").Value
: 1;
set
{
if (SelectedTool.Toolbar.GetSetting<SizeSetting>("ToolSize") is SizeSetting toolSize)
{
toolSize.Value = value;
HighlightPixels(MousePositionConverter.CurrentCoordinates);
}
}
}

public BitmapOperationsUtility BitmapOperations { get; set; }

public ReadonlyToolUtility ReadonlyToolUtility { get; set; }
Expand All @@ -105,14 +82,6 @@ public Document ActiveDocument

#nullable disable
public ObservableCollection<Document> Documents { get; set; } = new ObservableCollection<Document>();

/// <summary>
/// Returns if tool is BitmapOperationTool.
/// </summary>
public static bool IsOperationTool(Tool tool)
{
return tool is BitmapOperationTool;
}

public void CloseDocument(Document document)
{
Expand All @@ -130,46 +99,34 @@ public void CloseDocument(Document document)

public void ExecuteTool(Coordinates newPosition, bool clickedOnCanvas)
{
if (SelectedTool.CanStartOutsideCanvas || clickedOnCanvas)
{
if (startPosition == null)
{
SelectedTool.OnStart(newPosition);
startPosition = newPosition;
}
Tool activeTool = _tools.ActiveTool;

if (SelectedTool is BitmapOperationTool operationTool)
{
BitmapOperations.ExecuteTool(newPosition, MouseController.LastMouseMoveCoordinates, operationTool);
}
else if (SelectedTool is ReadonlyTool readonlyTool)
{
ReadonlyToolUtility.ExecuteTool(
MouseController.LastMouseMoveCoordinates,
readonlyTool);
}
else
{
throw new InvalidOperationException($"'{SelectedTool.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)}'");
}
}
}

/// <summary>
/// Returns if selected tool is BitmapOperationTool.
/// </summary>
public bool IsOperationTool()
{
return IsOperationTool(SelectedTool);
}
if (activeTool.CanStartOutsideCanvas && !clickedOnCanvas)
{
return;
}

public void SetActiveTool(Tool tool)
{
ActiveDocument?.PreviewLayer?.Reset();
SelectedTool?.Toolbar.SaveToolbarSettings();
SelectedTool = tool;
SelectedTool.Toolbar.LoadSharedSettings();
}
if (startPosition == null)
{
activeTool.OnStart(newPosition);
startPosition = newPosition;
}

if (activeTool is BitmapOperationTool operationTool)
{
BitmapOperations.ExecuteTool(newPosition, MouseController.LastMouseMoveCoordinates, operationTool);
}
else if (activeTool is ReadonlyTool readonlyTool)
{
ReadonlyToolUtility.ExecuteTool(
MouseController.LastMouseMoveCoordinates,
readonlyTool);
}
else
{
throw new InvalidOperationException($"'{activeTool.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)
{
Expand All @@ -178,7 +135,14 @@ private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventAr

private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
{
SelectedTool.OnMouseMove(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
Tool activeTool = _tools.ActiveTool;

if (activeTool == null)
{
return;
}

activeTool.OnMouseMove(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
if (!MaybeExecuteTool(e.NewPosition) && Mouse.LeftButton == MouseButtonState.Released)
{
HighlightPixels(e.NewPosition);
Expand All @@ -187,12 +151,12 @@ private void Controller_MousePositionChanged(object sender, MouseMovementEventAr

private void MouseController_OnMouseDown(object sender, MouseEventArgs e)
{
SelectedTool.OnMouseDown(e);
_tools.ActiveTool.OnMouseDown(e);
}

private void MouseController_OnMouseUp(object sender, MouseEventArgs e)
{
SelectedTool.OnMouseUp(e);
_tools.ActiveTool.OnMouseUp(e);
}
private void MouseController_OnMouseDownCoordinates(object sender, MouseMovementEventArgs e)
{
Expand All @@ -211,12 +175,12 @@ private bool MaybeExecuteTool(Coordinates newPosition)

private bool IsDraggingViewport()
{
return SelectedTool is MoveViewportTool;
return _tools.ActiveTool is MoveViewportTool;
}

private void MouseController_StartedRecordingChanges(object sender, EventArgs e)
{
SelectedTool.OnRecordingLeftMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
_tools.ActiveTool.OnRecordingLeftMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
if (ActiveDocument != null)
{
ActiveDocument.PreviewLayer.Reset();
Expand All @@ -225,8 +189,9 @@ private void MouseController_StartedRecordingChanges(object sender, EventArgs e)

private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
{
SelectedTool.OnStoppedRecordingMouseUp(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
if (IsOperationTool(SelectedTool) && ((BitmapOperationTool)SelectedTool).RequiresPreviewLayer)
Tool selectedTool = _tools.ActiveTool;
selectedTool.OnStoppedRecordingMouseUp(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
if (selectedTool is BitmapOperationTool operationTool && operationTool.RequiresPreviewLayer)
{
BitmapOperations.ApplyPreviewLayer();
}
Expand All @@ -236,25 +201,25 @@ private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
startPosition = null;
}

private void HighlightPixels(Coordinates newPosition)
public void HighlightPixels(Coordinates newPosition)
{
if (ActiveDocument == null || ActiveDocument.Layers.Count == 0 || SelectedTool.HideHighlight)
if (ActiveDocument == null || ActiveDocument.Layers.Count == 0 || _tools.ActiveTool.HideHighlight)
{
return;
}

var previewLayer = ActiveDocument.PreviewLayer;

if (ToolSize != previewLayerSize || previewLayer.IsReset)
if (_tools.ToolSize != previewLayerSize || previewLayer.IsReset)
{
previewLayerSize = ToolSize;
halfSize = (int)Math.Floor(ToolSize / 2f);
previewLayer.CreateNewBitmap(ToolSize, ToolSize);
previewLayerSize = _tools.ToolSize;
halfSize = (int)Math.Floor(_tools.ToolSize / 2f);
previewLayer.CreateNewBitmap(_tools.ToolSize, _tools.ToolSize);

Coordinates cords = new Coordinates(halfSize, halfSize);

previewLayer.Offset = new Thickness(0, 0, 0, 0);
_highlightPen.Draw(previewLayer, cords, cords, _highlightColor, ToolSize);
_highlightPen.Draw(previewLayer, cords, cords, _highlightColor, _tools.ToolSize);

AdjustOffset(newPosition, previewLayer);

Expand Down
8 changes: 6 additions & 2 deletions PixiEditor/Models/Controllers/BitmapOperationsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PixiEditor.Models.Position;
using PixiEditor.Models.Tools;
using PixiEditor.Models.Tools.ToolSettings.Settings;
using PixiEditor.ViewModels.SubViewModels.Main;
using SkiaSharp;
using System;
using System.Collections.Generic;
Expand All @@ -18,15 +19,18 @@ public class BitmapOperationsUtility

private SizeSetting sizeSetting;

public BitmapOperationsUtility(BitmapManager manager)
public BitmapOperationsUtility(BitmapManager manager, ToolsViewModel tools)
{
Manager = manager;
Tools = tools;
}

public event EventHandler<BitmapChangedEventArgs> BitmapChanged;

public BitmapManager Manager { get; set; }

public ToolsViewModel Tools { get; set; }

public void DeletePixels(Layer[] layers, Coordinates[] pixels)
{
if (Manager.ActiveDocument == null)
Expand Down Expand Up @@ -218,7 +222,7 @@ private void UseToolOnPreviewLayer(List<Coordinates> mouseMove, bool clearPrevie
Manager.ActiveDocument.PreviewLayer.ClearCanvas();
}

((BitmapOperationTool)Manager.SelectedTool).Use(
((BitmapOperationTool)Tools.ActiveTool).Use(
Manager.ActiveDocument.PreviewLayer,
mouseMove,
Manager.PrimaryColor);
Expand Down
16 changes: 16 additions & 0 deletions PixiEditor/Models/DataHolders/Document/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public Selection ActiveSelection
}
}

private double mouseXonReference;

public double MouseXOnReference
{
get => mouseXonReference;
set => SetProperty(ref mouseXonReference, value);
}

private double mouseYonReference;

public double MouseYOnReference
{
get => mouseYonReference;
set => SetProperty(ref mouseYonReference, value);
}

public ExecutionTrigger<Size> CenterViewportTrigger { get; } = new();
public ExecutionTrigger<double> ZoomViewportTrigger { get; } = new();

Expand Down

0 comments on commit 55f3377

Please sign in to comment.