123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using System;
- using System.Linq;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using PixiEditor.Helpers;
- using PixiEditor.Models.DataHolders;
- using PixiEditor.Models.Enums;
- using PixiEditor.Models.Events;
- using PixiEditor.Models.ImageManipulation;
- using PixiEditor.Models.Layers;
- using PixiEditor.Models.Position;
- using PixiEditor.Models.Tools;
- using PixiEditor.Models.Tools.ToolSettings;
- namespace PixiEditor.Models.Controllers
- {
- public class BitmapManager : NotifyableObject
- {
- public MouseMovementController MouseController { get; set; }
- public Tool SelectedTool
- {
- get => _selectedTool;
- private set
- {
- _selectedTool = value;
- RaisePropertyChanged("SelectedTool");
- }
- }
- public Layer PreviewLayer
- {
- get => _previewLayer;
- set
- {
- _previewLayer = value;
- RaisePropertyChanged("PreviewLayer");
- }
- }
- public Layer ActiveLayer => ActiveDocument.ActiveLayer;
- public Color PrimaryColor { get; set; }
- public int ToolSize
- {
- get => SelectedTool.Toolbar.GetSetting("ToolSize") != null
- ? (int)SelectedTool.Toolbar.GetSetting("ToolSize").Value
- : 1;
- set
- {
- if (SelectedTool.Toolbar.GetSetting("ToolSize") is Setting toolSize)
- {
- toolSize.Value = value;
- }
- }
- }
- public BitmapOperationsUtility BitmapOperations { get; set; }
- public ReadonlyToolUtility ReadonlyToolUtility { get; set; }
- public Document ActiveDocument
- {
- get => _activeDocument;
- set
- {
- _activeDocument = value;
- RaisePropertyChanged("ActiveDocument");
- DocumentChanged?.Invoke(this, new DocumentChangedEventArgs(value));
- }
- }
- private Document _activeDocument;
- private Layer _previewLayer;
- private Tool _selectedTool;
- public BitmapManager()
- {
- MouseController = new MouseMovementController();
- MouseController.StartedRecordingChanges += MouseController_StartedRecordingChanges;
- MouseController.MousePositionChanged += Controller_MousePositionChanged;
- MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
- BitmapOperations = new BitmapOperationsUtility(this);
- ReadonlyToolUtility = new ReadonlyToolUtility();
- }
- public event EventHandler<LayersChangedEventArgs> LayersChanged;
- public event EventHandler<DocumentChangedEventArgs> DocumentChanged;
- public void SetActiveTool(Tool tool)
- {
- PreviewLayer = null;
- SelectedTool?.Toolbar.SaveToolbarSettings();
- SelectedTool = tool;
- SelectedTool.Toolbar.LoadSharedSettings();
- }
- public void SetActiveLayer(int index)
- {
- if (ActiveDocument.ActiveLayerIndex <= ActiveDocument.Layers.Count - 1)
- ActiveDocument.ActiveLayer.IsActive = false;
- ActiveDocument.ActiveLayerIndex = index;
- ActiveDocument.ActiveLayer.IsActive = true;
- LayersChanged?.Invoke(this, new LayersChangedEventArgs(index, LayerAction.SetActive));
- }
- public void AddNewLayer(string name, WriteableBitmap bitmap, bool setAsActive = true)
- {
- AddNewLayer(name, bitmap.PixelWidth, bitmap.PixelHeight, setAsActive);
- ActiveDocument.Layers.Last().LayerBitmap = bitmap;
- }
- public void AddNewLayer(string name, bool setAsActive = true)
- {
- AddNewLayer(name, 0, 0, setAsActive);
- }
- public void AddNewLayer(string name, int width, int height, bool setAsActive = true)
- {
- ActiveDocument.Layers.Add(new Layer(name, width, height)
- {
- MaxHeight = ActiveDocument.Height,
- MaxWidth = ActiveDocument.Width
- });
- if (setAsActive) SetActiveLayer(ActiveDocument.Layers.Count - 1);
- LayersChanged?.Invoke(this, new LayersChangedEventArgs(0, LayerAction.Add));
- }
- public void RemoveLayer(int layerIndex)
- {
- if (ActiveDocument.Layers.Count == 0) return;
- bool wasActive = ActiveDocument.Layers[layerIndex].IsActive;
- ActiveDocument.Layers.RemoveAt(layerIndex);
- if (wasActive)
- SetActiveLayer(0);
- else if (ActiveDocument.ActiveLayerIndex > ActiveDocument.Layers.Count - 1)
- SetActiveLayer(ActiveDocument.Layers.Count - 1);
- }
- private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
- {
- SelectedTool.OnMouseMove(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
- if (Mouse.LeftButton == MouseButtonState.Pressed && !IsDraggingViewport() && ActiveDocument != null)
- {
- ExecuteTool(e.NewPosition, MouseController.ClickedOnCanvas);
- }
- else if (Mouse.LeftButton == MouseButtonState.Released)
- {
- HighlightPixels(e.NewPosition);
- }
- }
- public void ExecuteTool(Coordinates newPosition, bool clickedOnCanvas)
- {
- if (SelectedTool.CanStartOutsideCanvas || clickedOnCanvas)
- {
- if (IsOperationTool(SelectedTool))
- {
- BitmapOperations.ExecuteTool(newPosition,
- MouseController.LastMouseMoveCoordinates.ToList(), (BitmapOperationTool)SelectedTool);
- }
- else
- {
- ReadonlyToolUtility.ExecuteTool(MouseController.LastMouseMoveCoordinates.ToArray(),
- (ReadonlyTool)SelectedTool);
- }
- }
- }
- private bool IsDraggingViewport()
- {
- return Keyboard.IsKeyDown(Key.LeftShift) && !(SelectedTool is ShapeTool);
- }
- private void MouseController_StartedRecordingChanges(object sender, EventArgs e)
- {
- SelectedTool.OnMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
- PreviewLayer = null;
- }
- private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
- {
- SelectedTool.OnMouseUp(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
- if (IsOperationTool(SelectedTool) && ((BitmapOperationTool) SelectedTool).RequiresPreviewLayer)
- BitmapOperations.StopAction();
- }
- public void GeneratePreviewLayer()
- {
- if (ActiveDocument != null)
- PreviewLayer = new Layer("_previewLayer")
- {
- MaxWidth = ActiveDocument.Width,
- MaxHeight = ActiveDocument.Height
- };
- }
- private void HighlightPixels(Coordinates newPosition)
- {
- if (ActiveDocument == null || ActiveDocument.Layers.Count == 0 || SelectedTool.HideHighlight) return;
- Coordinates[] highlightArea = CoordinatesCalculator.RectangleToCoordinates(
- CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));
- if (CanChangeHighlightOffset(highlightArea))
- {
- PreviewLayer.Offset = new Thickness(highlightArea[0].X, highlightArea[0].Y,0,0);
- }
- else if (!IsInsideBounds(highlightArea))
- {
- PreviewLayer = null;
- }
- else
- {
- GeneratePreviewLayer();
- PreviewLayer.SetPixels(
- BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77, 0, 0, 0)));
- }
- }
- private bool CanChangeHighlightOffset(Coordinates[] highlightArea)
- {
- return highlightArea.Length > 0 && PreviewLayer != null &&
- IsInsideBounds(highlightArea) && highlightArea.Length == PreviewLayer.Width * PreviewLayer.Height;
- }
- private bool IsInsideBounds(Coordinates[] highlightArea)
- {
- return highlightArea[0].X <= ActiveDocument.Width - 1 &&
- highlightArea[0].Y <= ActiveDocument.Height - 1 &&
- highlightArea[^1].X >= 0 && highlightArea[^1].Y >= 0;
- }
- public WriteableBitmap GetCombinedLayersBitmap()
- {
- return BitmapUtils.CombineLayers(ActiveDocument.Layers.Where(x => x.IsVisible).ToArray(), ActiveDocument.Width, ActiveDocument.Height);
- }
- /// <summary>
- /// Returns if selected tool is BitmapOperationTool
- /// </summary>
- /// <returns></returns>
- public bool IsOperationTool()
- {
- return IsOperationTool(SelectedTool);
- }
- /// <summary>
- /// Returns if tool is BitmapOperationTool
- /// </summary>
- /// <param name="tool"></param>
- /// <returns></returns>
- public static bool IsOperationTool(Tool tool)
- {
- return tool is BitmapOperationTool;
- }
- }
- public class LayersChangedEventArgs : EventArgs
- {
- public int LayerAffected { get; set; }
- public LayerAction LayerChangeType { get; set; }
- public LayersChangedEventArgs(int layerAffected, LayerAction layerChangeType)
- {
- LayerAffected = layerAffected;
- LayerChangeType = layerChangeType;
- }
- }
- }
|