123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750 |
- using Microsoft.Win32;
- using PixiEditor.Helpers;
- using PixiEditor.Models.Controllers;
- using PixiEditor.Models.DataHolders;
- using PixiEditor.Models.Dialogs;
- using PixiEditor.Models.Enums;
- using PixiEditor.Models.IO;
- using PixiEditor.Models.Layers;
- using PixiEditor.Models.Position;
- using PixiEditor.Models.Tools;
- using PixiEditor.Models.Tools.Tools;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace PixiEditor.ViewModels
- {
- class ViewModelMain : ViewModelBase
- {
- public Action CloseAction { get; set; }
- public static ViewModelMain Current { get; set; } = null;
- public RelayCommand SelectToolCommand { get; set; } //Command that handles tool switching
- public RelayCommand OpenNewFilePopupCommand { get; set; } //Command that generates draw area
- public RelayCommand MouseMoveCommand { get; set; } //Command that is used to draw
- public RelayCommand MouseDownCommand { get; set; }
- public RelayCommand KeyDownCommand { get; set; }
- public RelayCommand SaveFileCommand { get; set; } //Command that is used to save file
- public RelayCommand UndoCommand { get; set; }
- public RelayCommand RedoCommand { get; set; }
- public RelayCommand MouseUpCommand { get; set; }
- public RelayCommand OpenFileCommand { get; set; }
- public RelayCommand SetActiveLayerCommand { get; set; }
- public RelayCommand NewLayerCommand { get; set; }
- public RelayCommand ReloadImageCommand { get; set; }
- public RelayCommand DeleteLayerCommand { get; set; }
- public RelayCommand RenameLayerCommand { get; set; }
- public RelayCommand MoveToBackCommand { get; set; }
- public RelayCommand MoveToFrontCommand { get; set; }
- public RelayCommand SwapColorsCommand { get; set; }
- public RelayCommand DeselectCommand { get; set; }
- public RelayCommand SelectAllCommand { get; set; }
- public RelayCommand CopyCommand { get; set; }
- public RelayCommand DuplicateCommand { get; set; }
- public RelayCommand CutCommand { get; set; }
- public RelayCommand PasteCommand { get; set; }
- public RelayCommand ClipCanvasCommand { get; set; }
- public RelayCommand DeletePixelsCommand { get; set; }
- public RelayCommand OpenResizePopupCommand { get; set; }
- public RelayCommand SelectColorCommand { get; set; }
- public RelayCommand RemoveSwatchCommand { get; set; }
- public RelayCommand SaveDocumentCommand { get; set; }
- public RelayCommand OnStartupCommand { get; set; }
- public RelayCommand CloseWindowCommand { get; set; }
- private double _mouseXonCanvas;
- public double MouseXOnCanvas //Mouse X coordinate relative to canvas
- {
- get => _mouseXonCanvas;
- set { _mouseXonCanvas = value; RaisePropertyChanged("MouseXonCanvas"); }
- }
- private double _mouseYonCanvas;
- public double MouseYOnCanvas //Mouse Y coordinate relative to canvas
- {
- get => _mouseYonCanvas;
- set { _mouseYonCanvas = value; RaisePropertyChanged("MouseYonCanvas"); }
- }
- private bool _recenterZoombox;
- public bool RecenterZoombox
- {
- get => _recenterZoombox;
- set { _recenterZoombox = value; RaisePropertyChanged("RecenterZoombox"); }
- }
- private Color _primaryColor = Colors.Black;
- public Color PrimaryColor //Primary color, hooked with left mouse button
- {
- get => _primaryColor;
- set
- {
- if (_primaryColor != value)
- {
- _primaryColor = value;
- BitmapManager.PrimaryColor = value;
- RaisePropertyChanged("PrimaryColor");
- }
- }
- }
- private Color _secondaryColor = Colors.White;
- public Color SecondaryColor
- {
- get => _secondaryColor;
- set { if (_secondaryColor != value) { _secondaryColor = value; RaisePropertyChanged("SecondaryColor"); } }
- }
- private ToolType _selectedTool;
- public ToolType SelectedTool
- {
- get { return _selectedTool; }
- set
- {
- if (_selectedTool != value)
- {
- _selectedTool = value;
- SetActiveTool(value);
- RaisePropertyChanged("SelectedTool");
- }
- }
- }
- public ObservableCollection<Tool> ToolSet { get; set; }
- private LayerChange[] _undoChanges;
- public LayerChange[] UndoChanges
- {
- get { return _undoChanges; }
- set
- {
- _undoChanges = value;
- for (int i = 0; i < value.Length; i++)
- {
- BitmapManager.ActiveDocument.Layers[value[i].LayerIndex].ApplyPixels(value[i].PixelChanges);
- }
- }
- }
- private Cursor _toolCursor;
- public Cursor ToolCursor
- {
- get { return _toolCursor; }
- set
- {
- _toolCursor = value;
- RaisePropertyChanged("ToolCursor");
- }
- }
- public BitmapManager BitmapManager { get; set; }
- public PixelChangesController ChangesController { get; set; }
- public ShortcutController ShortcutController { get; set; }
- private Selection _selection = null;
- public Selection ActiveSelection
- {
- get => _selection;
- set
- {
- _selection = value;
- RaisePropertyChanged("ActiveSelection");
- }
- }
- private bool _unsavedDocumentModified = false;
- public ClipboardController ClipboardController { get; set; }
- public ViewModelMain()
- {
- FilesManager.InitializeTempDirectories();
- BitmapManager = new BitmapManager();
- BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
- BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
- BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
- ChangesController = new PixelChangesController();
- SelectToolCommand = new RelayCommand(SetTool, DocumentIsNotNull);
- OpenNewFilePopupCommand = new RelayCommand(OpenNewFilePopup);
- MouseMoveCommand = new RelayCommand(MouseMove);
- MouseDownCommand = new RelayCommand(MouseDown);
- SaveFileCommand = new RelayCommand(SaveFile, CanSave);
- UndoCommand = new RelayCommand(Undo, CanUndo);
- RedoCommand = new RelayCommand(Redo, CanRedo);
- MouseUpCommand = new RelayCommand(MouseUp);
- OpenFileCommand = new RelayCommand(Open);
- SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
- NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
- DeleteLayerCommand = new RelayCommand(DeleteLayer, CanDeleteLayer);
- MoveToBackCommand = new RelayCommand(MoveLayerToBack, CanMoveToBack);
- MoveToFrontCommand = new RelayCommand(MoveLayerToFront, CanMoveToFront);
- SwapColorsCommand = new RelayCommand(SwapColors);
- KeyDownCommand = new RelayCommand(KeyDown);
- RenameLayerCommand = new RelayCommand(RenameLayer);
- DeselectCommand = new RelayCommand(Deselect, SelectionIsNotEmpty);
- SelectAllCommand = new RelayCommand(SelectAll, CanSelectAll);
- CopyCommand = new RelayCommand(Copy, SelectionIsNotEmpty);
- DuplicateCommand = new RelayCommand(Duplicate, SelectionIsNotEmpty);
- CutCommand = new RelayCommand(Cut, SelectionIsNotEmpty);
- PasteCommand = new RelayCommand(Paste, CanPaste);
- ClipCanvasCommand = new RelayCommand(ClipCanvas, DocumentIsNotNull);
- DeletePixelsCommand = new RelayCommand(DeletePixels, SelectionIsNotEmpty);
- OpenResizePopupCommand = new RelayCommand(OpenResizePopup, DocumentIsNotNull);
- SelectColorCommand = new RelayCommand(SelectColor);
- RemoveSwatchCommand = new RelayCommand(RemoveSwatch);
- SaveDocumentCommand = new RelayCommand(SaveDocument, DocumentIsNotNull);
- OnStartupCommand = new RelayCommand(OnStartup);
- CloseWindowCommand = new RelayCommand(CloseWindow);
- ToolSet = new ObservableCollection<Tool> {new MoveTool(), new PenTool(), new SelectTool(), new FloodFill(), new LineTool(),
- new CircleTool(), new RectangleTool(), new EarserTool(), new ColorPickerTool(), new BrightnessTool()};
- ShortcutController = new ShortcutController
- {
- Shortcuts = new List<Shortcut> {
- new Shortcut(Key.B, SelectToolCommand, ToolType.Pen),
- new Shortcut(Key.X, SwapColorsCommand),
- new Shortcut(Key.O, OpenFileCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.E, SelectToolCommand, ToolType.Earser),
- new Shortcut(Key.O, SelectToolCommand, ToolType.ColorPicker),
- new Shortcut(Key.R, SelectToolCommand, ToolType.Rectangle),
- new Shortcut(Key.C, SelectToolCommand, ToolType.Circle),
- new Shortcut(Key.L, SelectToolCommand, ToolType.Line),
- new Shortcut(Key.G, SelectToolCommand, ToolType.Bucket),
- new Shortcut(Key.U, SelectToolCommand, ToolType.Brightness),
- new Shortcut(Key.V, SelectToolCommand, ToolType.Move),
- new Shortcut(Key.M, SelectToolCommand, ToolType.Select),
- new Shortcut(Key.Y, RedoCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.Z, UndoCommand),
- new Shortcut(Key.S, SaveFileCommand, modifier: ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt),
- new Shortcut(Key.S, SaveDocumentCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.S, SaveDocumentCommand, "AsNew", modifier: ModifierKeys.Control | ModifierKeys.Shift),
- new Shortcut(Key.N, OpenNewFilePopupCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.D, DeselectCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.A, SelectAllCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.C, CopyCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.V, PasteCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.J, DuplicateCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.X, CutCommand, modifier: ModifierKeys.Control),
- new Shortcut(Key.Delete, DeletePixelsCommand),
- new Shortcut(Key.I, OpenResizePopupCommand, modifier: ModifierKeys.Control | ModifierKeys.Shift),
- new Shortcut(Key.C, OpenResizePopupCommand, "canvas" ,ModifierKeys.Control | ModifierKeys.Shift),
- }
- };
- UndoManager.SetMainRoot(this);
- ClipboardController = new ClipboardController();
- SetActiveTool(ToolType.Move);
- BitmapManager.PrimaryColor = PrimaryColor;
- Current = this;
- }
- private void CloseWindow(object property)
- {
- if (!(property is CancelEventArgs)) throw new ArgumentException();
- (property as CancelEventArgs).Cancel = true;
- ConfirmationType result = ConfirmationType.No;
- if (_unsavedDocumentModified)
- {
- result = ConfirmationDialog.Show("Document was modified. Do you want to save changes?");
- if(result == ConfirmationType.Yes)
- {
- SaveDocument(null);
- }
- }
- if (result != ConfirmationType.Canceled)
- {
- (property as CancelEventArgs).Cancel = false;
- }
- }
- private void OnStartup(object parameter)
- {
- var lastArg = Environment.GetCommandLineArgs().Last();
- if (Importer.IsSupportedFile(lastArg) && File.Exists(lastArg))
- {
- Open(lastArg);
- }
- else
- {
- OpenNewFilePopup(null);
- }
- }
- private void BitmapManager_DocumentChanged(object sender, Models.Events.DocumentChangedEventArgs e)
- {
- e.NewDocument.DocumentSizeChanged += ActiveDocument_DocumentSizeChanged;
- }
- private void Open(object property)
- {
- OpenFileDialog dialog = new OpenFileDialog
- {
- Filter = "All Files|*.*|PixiEditor Files | *.pixi|PNG Files|*.png",
- DefaultExt = "pixi"
- };
- if ((bool)dialog.ShowDialog())
- {
- if (dialog.FileName.EndsWith(".png") || dialog.FileName.EndsWith(".jpeg") || dialog.FileName.EndsWith(".jpg"))
- {
- OpenFile(dialog.FileName);
- }
- else if (dialog.FileName.EndsWith(".pixi"))
- {
- OpenDocument(dialog.FileName);
- }
- RecenterZoombox = !RecenterZoombox;
- }
- }
- private void Open(string path)
- {
- if (path.EndsWith(".pixi"))
- {
- OpenDocument(path);
- }
- else
- {
- OpenFile(path);
- }
- RecenterZoombox = !RecenterZoombox;
- }
- private void OpenDocument(string path)
- {
- BitmapManager.ActiveDocument = Importer.ImportDocument(path);
- Exporter.SaveDocumentPath = path;
- _unsavedDocumentModified = false;
- }
- private void SaveDocument(object parameter)
- {
- bool paramIsAsNew = parameter != null && parameter.ToString().ToLower() == "asnew";
- if (paramIsAsNew || Exporter.SaveDocumentPath == null)
- {
- Exporter.SaveAsNewEditableFile(BitmapManager.ActiveDocument, !paramIsAsNew);
- }
- else
- {
- Exporter.SaveAsEditableFile(BitmapManager.ActiveDocument, Exporter.SaveDocumentPath);
- }
- _unsavedDocumentModified = false;
- }
- private void RemoveSwatch(object parameter)
- {
- if (!(parameter is Color))
- {
- throw new ArgumentException();
- }
- Color color = (Color)parameter;
- if (BitmapManager.ActiveDocument.Swatches.Contains(color))
- {
- BitmapManager.ActiveDocument.Swatches.Remove(color);
- }
- }
- private void SelectColor(object parameter)
- {
- if(!(parameter is Color))
- {
- throw new ArgumentException();
- }
- PrimaryColor = (Color)parameter;
- }
- private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
- {
- ActiveSelection = new Selection(Array.Empty<Coordinates>());
- RecenterZoombox = true;
- _unsavedDocumentModified = true;
- }
- public void AddSwatch(Color color)
- {
- if (!BitmapManager.ActiveDocument.Swatches.Contains(color))
- {
- BitmapManager.ActiveDocument.Swatches.Add(color);
- }
- }
- private void OpenResizePopup(object parameter)
- {
- bool isCanvasDialog = (string)parameter == "canvas";
- ResizeDocumentDialog dialog = new ResizeDocumentDialog(BitmapManager.ActiveDocument.Width, BitmapManager.ActiveDocument.Height, isCanvasDialog);
- if (dialog.ShowDialog())
- {
- if (isCanvasDialog)
- {
- BitmapManager.ActiveDocument.ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
- }
- else
- {
- BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
- }
- }
- }
- private void DeletePixels(object parameter)
- {
- BitmapManager.BitmapOperations.DeletePixels(new Layer[] { BitmapManager.ActiveLayer },
- ActiveSelection.SelectedPoints.ToArray());
- }
- public void ClipCanvas(object parameter)
- {
- if (BitmapManager.ActiveDocument != null)
- {
- BitmapManager.ActiveDocument.ClipCanvas();
- }
- }
- public void Duplicate(object parameter)
- {
- Copy(null);
- Paste(null);
- }
- public void Cut(object parameter)
- {
- Copy(null);
- BitmapManager.ActiveLayer.
- ApplyPixels(BitmapPixelChanges.FromSingleColoredArray(ActiveSelection.SelectedPoints.ToArray(), Colors.Transparent));
- }
- public void Paste(object parameter)
- {
- ClipboardController.PasteFromClipboard();
- }
- private bool CanPaste(object property)
- {
- return DocumentIsNotNull(null) && ClipboardController.IsImageInClipboard();
- }
- private void Copy(object parameter)
- {
- ClipboardController.CopyToClipboard(BitmapManager.ActiveDocument.Layers.ToArray(),
- ActiveSelection.SelectedPoints.ToArray());
- }
- public void SelectAll(object parameter)
- {
- SelectTool select = new SelectTool();
- select.Use(select.GetAllSelection());
- }
- private bool CanSelectAll(object property)
- {
- return BitmapManager.ActiveDocument != null && BitmapManager.ActiveDocument.Layers.Count > 0;
- }
- private bool DocumentIsNotNull(object property)
- {
- return BitmapManager.ActiveDocument != null;
- }
- public void Deselect(object parameter)
- {
- if (ActiveSelection != null)
- {
- ActiveSelection.Clear();
- }
- }
- private bool SelectionIsNotEmpty(object property)
- {
- return ActiveSelection != null && ActiveSelection.SelectedPoints != null && ActiveSelection.SelectedPoints.Count > 0;
- }
- public void SetTool(object parameter)
- {
- SetActiveTool((ToolType)parameter);
- }
- public void RenameLayer(object parameter)
- {
- BitmapManager.ActiveDocument.Layers[(int)parameter].IsRenaming = true;
- }
- public void KeyDown(object parameter)
- {
- ShortcutController.KeyPressed(((KeyEventArgs)parameter).Key);
- }
- private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
- {
- if (BitmapManager.IsOperationTool(BitmapManager.SelectedTool)
- && (BitmapManager.SelectedTool as BitmapOperationTool).UseDefaultUndoMethod)
- {
- Tuple<LayerChange, LayerChange>[] changes = ChangesController.PopChanges();
- if (changes != null && changes.Length > 0)
- {
- LayerChange[] newValues = changes.Select(x => x.Item1).ToArray();
- LayerChange[] oldValues = changes.Select(x => x.Item2).ToArray();
- UndoManager.AddUndoChange(new Change("UndoChanges", oldValues, newValues));
- BitmapManager.SelectedTool.AfterAddedUndo();
- }
- }
- }
- private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
- {
- ChangesController.AddChanges(new LayerChange(e.PixelsChanged, e.ChangedLayerIndex),
- new LayerChange(e.OldPixelsValues, e.ChangedLayerIndex));
- _unsavedDocumentModified = true;
- }
- public void SwapColors(object parameter)
- {
- var tmp = PrimaryColor;
- PrimaryColor = SecondaryColor;
- SecondaryColor = tmp;
- }
- public void MoveLayerToFront(object parameter)
- {
- int oldIndex = (int)parameter;
- BitmapManager.ActiveDocument.Layers.Move(oldIndex, oldIndex + 1);
- if (BitmapManager.ActiveDocument.ActiveLayerIndex == oldIndex)
- {
- BitmapManager.SetActiveLayer(oldIndex + 1);
- }
- }
- public void MoveLayerToBack(object parameter)
- {
- int oldIndex = (int)parameter;
- BitmapManager.ActiveDocument.Layers.Move(oldIndex, oldIndex - 1);
- if (BitmapManager.ActiveDocument.ActiveLayerIndex == oldIndex)
- {
- BitmapManager.SetActiveLayer(oldIndex - 1);
- }
- }
- public bool CanMoveToFront(object property)
- {
- return DocumentIsNotNull(null) && BitmapManager.ActiveDocument.Layers.Count - 1 > (int)property;
- }
- public bool CanMoveToBack(object property)
- {
- return (int)property > 0;
- }
- public void SetActiveLayer(object parameter)
- {
- BitmapManager.SetActiveLayer((int)parameter);
- }
- public void DeleteLayer(object parameter)
- {
- BitmapManager.RemoveLayer((int)parameter);
- }
- public bool CanDeleteLayer(object property)
- {
- return BitmapManager.ActiveDocument != null && BitmapManager.ActiveDocument.Layers.Count > 1;
- }
- #region Undo/Redo
- /// <summary>
- /// Undo last action
- /// </summary>
- /// <param name="parameter"></param>
- public void Undo(object parameter)
- {
- Deselect(null);
- UndoManager.Undo();
- }
- /// <summary>
- /// Returns true if undo can be done.
- /// </summary>
- /// <param name="property"></param>
- /// <returns></returns>
- private bool CanUndo(object property)
- {
- return UndoManager.CanUndo;
- }
- /// <summary>
- /// Redo last action
- /// </summary>
- /// <param name="parameter"></param>
- public void Redo(object parameter)
- {
- UndoManager.Redo();
- }
- /// <summary>
- /// Returns true if redo can be done.
- /// </summary>
- /// <param name="property"></param>
- /// <returns></returns>
- private bool CanRedo(object property)
- {
- return UndoManager.CanRedo;
- }
- #endregion
- private void SetActiveTool(ToolType tool)
- {
- Tool foundTool = ToolSet.First(x => x.ToolType == tool);
- Tool activeTool = ToolSet.FirstOrDefault(x => x.IsActive);
- if (activeTool != null)
- {
- activeTool.IsActive = false;
- }
- foundTool.IsActive = true;
- BitmapManager.SetActiveTool(foundTool);
- SetToolCursor(tool);
- }
- private void SetToolCursor(ToolType tool)
- {
- if (tool != ToolType.None)
- {
- ToolCursor = BitmapManager.SelectedTool.Cursor;
- }
- else
- {
- ToolCursor = Cursors.Arrow;
- }
- }
- /// <summary>
- /// When mouse is up stops recording changes.
- /// </summary>
- /// <param name="parameter"></param>
- private void MouseUp(object parameter)
- {
- BitmapManager.MouseController.StopRecordingMouseMovementChanges();
- }
- private void MouseDown(object parameter)
- {
- if (BitmapManager.ActiveDocument.Layers.Count == 0) return;
- if (Mouse.LeftButton == MouseButtonState.Pressed)
- {
- if (!BitmapManager.MouseController.IsRecordingChanges)
- {
- BitmapManager.MouseController.StartRecordingMouseMovementChanges();
- BitmapManager.MouseController.RecordMouseMovementChange(MousePositionConverter.CurrentCoordinates);
- AddSwatch(PrimaryColor);
- }
- }
- }
- /// <summary>
- /// Method connected with command, it executes tool "activity"
- /// </summary>
- /// <param name="parameter"></param>
- private void MouseMove(object parameter)
- {
- Coordinates cords = new Coordinates((int)MouseXOnCanvas, (int)MouseYOnCanvas);
- MousePositionConverter.CurrentCoordinates = cords;
- if (BitmapManager.MouseController.IsRecordingChanges && Mouse.LeftButton == MouseButtonState.Pressed)
- {
- BitmapManager.MouseController.RecordMouseMovementChange(cords);
- }
- else
- {
- BitmapManager.MouseController.MouseMoved(cords);
- }
- }
- /// <summary>
- /// Generates new Layer and sets it as active one
- /// </summary>
- /// <param name="parameter"></param>
- public void OpenNewFilePopup(object parameter)
- {
- NewFileDialog newFile = new NewFileDialog();
- if (newFile.ShowDialog())
- {
- NewDocument(newFile.Width, newFile.Height);
- }
- }
- #region SaveFile
- /// <summary>
- /// Generates export dialog or saves directly if save data is known.
- /// </summary>
- /// <param name="parameter"></param>
- private void SaveFile(object parameter)
- {
- WriteableBitmap bitmap = BitmapManager.GetCombinedLayersBitmap();
- Exporter.Export(bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
- }
- /// <summary>
- /// Returns true if file save is possible.
- /// </summary>
- /// <param name="property"></param>
- /// <returns></returns>
- private bool CanSave(object property)
- {
- return BitmapManager.ActiveDocument != null;
- }
- #endregion
- /// <summary>
- /// Opens file from path.
- /// </summary>
- /// <param name="path"></param>
- public void OpenFile(string path)
- {
- ImportFileDialog dialog = new ImportFileDialog();
- if (path != null && File.Exists(path.ToString()))
- dialog.FilePath = path.ToString();
- if (dialog.ShowDialog())
- {
- NewDocument(dialog.FileWidth, dialog.FileHeight);
- BitmapManager.ActiveDocument.ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
- }
- }
- private void NewDocument(int width, int height)
- {
- BitmapManager.ActiveDocument = new Document(width, height);
- BitmapManager.AddNewLayer("Base Layer", width, height, true);
- BitmapManager.PreviewLayer = null;
- UndoManager.UndoStack.Clear();
- UndoManager.RedoStack.Clear();
- ActiveSelection = new Selection(Array.Empty<Coordinates>());
- RecenterZoombox = !RecenterZoombox;
- Exporter.SaveDocumentPath = null;
- _unsavedDocumentModified = false;
- }
- public void NewLayer(object parameter)
- {
- BitmapManager.AddNewLayer($"New Layer {BitmapManager.ActiveDocument.Layers.Count}", BitmapManager.ActiveDocument.Width, BitmapManager.ActiveDocument.Height);
- }
- public bool CanCreateNewLayer(object parameter)
- {
- return BitmapManager.ActiveDocument != null && BitmapManager.ActiveDocument.Layers.Count > 0;
- }
- }
- }
|