ViewModelMain.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using PixiEditor.Helpers;
  2. using PixiEditor.Models.Controllers;
  3. using PixiEditor.Models.DataHolders;
  4. using PixiEditor.Models.Dialogs;
  5. using PixiEditor.Models.Enums;
  6. using PixiEditor.Models.Images;
  7. using PixiEditor.Models.IO;
  8. using PixiEditor.Models.Layers;
  9. using PixiEditor.Models.Position;
  10. using PixiEditor.Models.Tools;
  11. using PixiEditor.Models.Tools.Tools;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Security.Cryptography.X509Certificates;
  17. using System.Windows;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. namespace PixiEditor.ViewModels
  22. {
  23. class ViewModelMain : ViewModelBase
  24. {
  25. public static ViewModelMain Current { get; set; } = null;
  26. public RelayCommand SelectToolCommand { get; set; } //Command that handles tool switching
  27. public RelayCommand OpenNewFilePopupCommand { get; set; } //Command that generates draw area
  28. public RelayCommand MouseMoveCommand { get; set; } //Command that is used to draw
  29. public RelayCommand MouseDownCommand { get; set; }
  30. public RelayCommand KeyDownCommand { get; set; }
  31. public RelayCommand SaveFileCommand { get; set; } //Command that is used to save file
  32. public RelayCommand UndoCommand { get; set; }
  33. public RelayCommand RedoCommand { get; set; }
  34. public RelayCommand MouseUpCommand { get; set; }
  35. public RelayCommand OpenFileCommand { get; set; }
  36. public RelayCommand SetActiveLayerCommand { get; set; }
  37. public RelayCommand NewLayerCommand { get; set; }
  38. public RelayCommand ReloadImageCommand { get; set; }
  39. public RelayCommand DeleteLayerCommand { get; set; }
  40. public RelayCommand RenameLayerCommand { get; set; }
  41. public RelayCommand MoveToBackCommand { get; set; }
  42. public RelayCommand MoveToFrontCommand { get; set; }
  43. public RelayCommand SwapColorsCommand { get; set; }
  44. public RelayCommand DeselectCommand { get; set; }
  45. public RelayCommand SelectAllCommand { get; set; }
  46. public RelayCommand CopyCommand { get; set; }
  47. public RelayCommand DuplicateCommand { get; set; }
  48. public RelayCommand CutCommand { get; set; }
  49. public RelayCommand PasteCommand { get; set; }
  50. public RelayCommand ClipCanvasCommand { get; set; }
  51. public RelayCommand DeletePixelsCommand { get; set; }
  52. public RelayCommand OpenResizePopupCommand { get; set; }
  53. public RelayCommand SelectColorCommand { get; set; }
  54. public RelayCommand RemoveSwatchCommand { get; set; }
  55. private double _mouseXonCanvas;
  56. public double MouseXOnCanvas //Mouse X coordinate relative to canvas
  57. {
  58. get => _mouseXonCanvas;
  59. set { _mouseXonCanvas = value; RaisePropertyChanged("MouseXonCanvas"); }
  60. }
  61. private double _mouseYonCanvas;
  62. public double MouseYOnCanvas //Mouse Y coordinate relative to canvas
  63. {
  64. get => _mouseYonCanvas;
  65. set { _mouseYonCanvas = value; RaisePropertyChanged("MouseYonCanvas"); }
  66. }
  67. private bool _recenterZoombox;
  68. public bool RecenterZoombox
  69. {
  70. get => _recenterZoombox;
  71. set { _recenterZoombox = value; RaisePropertyChanged("RecenterZoombox"); }
  72. }
  73. private Color _primaryColor = Colors.Black;
  74. public Color PrimaryColor //Primary color, hooked with left mouse button
  75. {
  76. get => _primaryColor;
  77. set
  78. {
  79. if (_primaryColor != value)
  80. {
  81. _primaryColor = value;
  82. BitmapManager.PrimaryColor = value;
  83. RaisePropertyChanged("PrimaryColor");
  84. }
  85. }
  86. }
  87. private Color _secondaryColor = Colors.White;
  88. public Color SecondaryColor
  89. {
  90. get => _secondaryColor;
  91. set { if (_secondaryColor != value) { _secondaryColor = value; RaisePropertyChanged("SecondaryColor"); } }
  92. }
  93. private ToolType _selectedTool;
  94. public ToolType SelectedTool
  95. {
  96. get { return _selectedTool; }
  97. set
  98. {
  99. if (_selectedTool != value)
  100. {
  101. _selectedTool = value;
  102. SetActiveTool(value);
  103. RaisePropertyChanged("SelectedTool");
  104. }
  105. }
  106. }
  107. public ObservableCollection<Tool> ToolSet { get; set; }
  108. public ObservableCollection<Color> Swatches { get; set; } = new ObservableCollection<Color>();
  109. private LayerChange[] _undoChanges;
  110. public LayerChange[] UndoChanges
  111. {
  112. get { return _undoChanges; }
  113. set
  114. {
  115. _undoChanges = value;
  116. for (int i = 0; i < value.Length; i++)
  117. {
  118. BitmapManager.ActiveDocument.Layers[value[i].LayerIndex].ApplyPixels(value[i].PixelChanges);
  119. }
  120. }
  121. }
  122. private Cursor _toolCursor;
  123. public Cursor ToolCursor
  124. {
  125. get { return _toolCursor; }
  126. set
  127. {
  128. _toolCursor = value;
  129. RaisePropertyChanged("ToolCursor");
  130. }
  131. }
  132. public BitmapManager BitmapManager { get; set; }
  133. public PixelChangesController ChangesController { get; set; }
  134. public ShortcutController ShortcutController { get; set; }
  135. private Selection _selection = null;
  136. public Selection ActiveSelection
  137. {
  138. get => _selection;
  139. set
  140. {
  141. _selection = value;
  142. RaisePropertyChanged("ActiveSelection");
  143. }
  144. }
  145. public ClipboardController ClipboardController { get; set; }
  146. public ViewModelMain()
  147. {
  148. FilesManager.InitializeTempDirectories();
  149. BitmapManager = new BitmapManager();
  150. BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
  151. BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
  152. ChangesController = new PixelChangesController();
  153. SelectToolCommand = new RelayCommand(SetTool, DocumentIsNotNull);
  154. OpenNewFilePopupCommand = new RelayCommand(OpenNewFilePopup);
  155. MouseMoveCommand = new RelayCommand(MouseMove);
  156. MouseDownCommand = new RelayCommand(MouseDown);
  157. SaveFileCommand = new RelayCommand(SaveFile, CanSave);
  158. UndoCommand = new RelayCommand(Undo, CanUndo);
  159. RedoCommand = new RelayCommand(Redo, CanRedo);
  160. MouseUpCommand = new RelayCommand(MouseUp);
  161. OpenFileCommand = new RelayCommand(OpenFile);
  162. SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
  163. NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
  164. DeleteLayerCommand = new RelayCommand(DeleteLayer, CanDeleteLayer);
  165. MoveToBackCommand = new RelayCommand(MoveLayerToBack, CanMoveToBack);
  166. MoveToFrontCommand = new RelayCommand(MoveLayerToFront, CanMoveToFront);
  167. SwapColorsCommand = new RelayCommand(SwapColors);
  168. KeyDownCommand = new RelayCommand(KeyDown);
  169. RenameLayerCommand = new RelayCommand(RenameLayer);
  170. DeselectCommand = new RelayCommand(Deselect, SelectionIsNotEmpty);
  171. SelectAllCommand = new RelayCommand(SelectAll, CanSelectAll);
  172. CopyCommand = new RelayCommand(Copy, SelectionIsNotEmpty);
  173. DuplicateCommand = new RelayCommand(Duplicate, SelectionIsNotEmpty);
  174. CutCommand = new RelayCommand(Cut, SelectionIsNotEmpty);
  175. PasteCommand = new RelayCommand(Paste, CanPaste);
  176. ClipCanvasCommand = new RelayCommand(ClipCanvas, DocumentIsNotNull);
  177. DeletePixelsCommand = new RelayCommand(DeletePixels, SelectionIsNotEmpty);
  178. OpenResizePopupCommand = new RelayCommand(OpenResizePopup, DocumentIsNotNull);
  179. SelectColorCommand = new RelayCommand(SelectColor);
  180. RemoveSwatchCommand = new RelayCommand(RemoveSwatch);
  181. ToolSet = new ObservableCollection<Tool> {new MoveTool(), new PenTool(), new SelectTool(), new FloodFill(), new LineTool(),
  182. new CircleTool(), new RectangleTool(), new EarserTool(), new ColorPickerTool(), new BrightnessTool()};
  183. ShortcutController = new ShortcutController
  184. {
  185. Shortcuts = new List<Shortcut> {
  186. new Shortcut(Key.B, SelectToolCommand, ToolType.Pen),
  187. new Shortcut(Key.X, SwapColorsCommand),
  188. new Shortcut(Key.O, OpenFileCommand, modifier: ModifierKeys.Control),
  189. new Shortcut(Key.E, SelectToolCommand, ToolType.Earser),
  190. new Shortcut(Key.O, SelectToolCommand, ToolType.ColorPicker),
  191. new Shortcut(Key.R, SelectToolCommand, ToolType.Rectangle),
  192. new Shortcut(Key.C, SelectToolCommand, ToolType.Circle),
  193. new Shortcut(Key.L, SelectToolCommand, ToolType.Line),
  194. new Shortcut(Key.G, SelectToolCommand, ToolType.Bucket),
  195. new Shortcut(Key.U, SelectToolCommand, ToolType.Brightness),
  196. new Shortcut(Key.V, SelectToolCommand, ToolType.Move),
  197. new Shortcut(Key.M, SelectToolCommand, ToolType.Select),
  198. new Shortcut(Key.Y, RedoCommand, modifier: ModifierKeys.Control),
  199. new Shortcut(Key.Z, UndoCommand),
  200. new Shortcut(Key.S, SaveFileCommand, modifier: ModifierKeys.Control),
  201. new Shortcut(Key.N, OpenNewFilePopupCommand, modifier: ModifierKeys.Control),
  202. new Shortcut(Key.S, SaveFileCommand, "AsNew", ModifierKeys.Control | ModifierKeys.Shift),
  203. new Shortcut(Key.D, DeselectCommand, modifier: ModifierKeys.Control),
  204. new Shortcut(Key.A, SelectAllCommand, modifier: ModifierKeys.Control),
  205. new Shortcut(Key.C, CopyCommand, modifier: ModifierKeys.Control),
  206. new Shortcut(Key.V, PasteCommand, modifier: ModifierKeys.Control),
  207. new Shortcut(Key.J, DuplicateCommand, modifier: ModifierKeys.Control),
  208. new Shortcut(Key.X, CutCommand, modifier: ModifierKeys.Control),
  209. new Shortcut(Key.Delete, DeletePixelsCommand),
  210. new Shortcut(Key.I, OpenResizePopupCommand, modifier: ModifierKeys.Control | ModifierKeys.Shift),
  211. new Shortcut(Key.C, OpenResizePopupCommand, "canvas" ,ModifierKeys.Control | ModifierKeys.Shift),
  212. }
  213. };
  214. UndoManager.SetMainRoot(this);
  215. ClipboardController = new ClipboardController();
  216. SetActiveTool(ToolType.Move);
  217. BitmapManager.PrimaryColor = PrimaryColor;
  218. Current = this;
  219. }
  220. private void RemoveSwatch(object parameter)
  221. {
  222. if (!(parameter is Color))
  223. {
  224. throw new ArgumentException();
  225. }
  226. Color color = (Color)parameter;
  227. if (Swatches.Contains(color))
  228. {
  229. Swatches.Remove(color);
  230. }
  231. }
  232. private void SelectColor(object parameter)
  233. {
  234. if(!(parameter is Color))
  235. {
  236. throw new ArgumentException();
  237. }
  238. PrimaryColor = (Color)parameter;
  239. }
  240. private void ActiveDocument_DocumentSizeChanged(object sender, DocumentSizeChangedEventArgs e)
  241. {
  242. ActiveSelection = new Selection(Array.Empty<Coordinates>());
  243. RecenterZoombox = true;
  244. }
  245. public void AddSwatch(Color color)
  246. {
  247. if (!Swatches.Contains(color))
  248. {
  249. Swatches.Add(color);
  250. }
  251. }
  252. private void OpenResizePopup(object parameter)
  253. {
  254. bool isCanvasDialog = (string)parameter == "canvas";
  255. ResizeDocumentDialog dialog = new ResizeDocumentDialog(BitmapManager.ActiveDocument.Width, BitmapManager.ActiveDocument.Height, isCanvasDialog);
  256. if (dialog.ShowDialog())
  257. {
  258. if (isCanvasDialog)
  259. {
  260. BitmapManager.ActiveDocument.ResizeCanvas(dialog.Width, dialog.Height, dialog.ResizeAnchor);
  261. }
  262. else
  263. {
  264. BitmapManager.ActiveDocument.Resize(dialog.Width, dialog.Height);
  265. }
  266. }
  267. }
  268. private void DeletePixels(object parameter)
  269. {
  270. BitmapManager.BitmapOperations.DeletePixels(new Layer[] { BitmapManager.ActiveLayer },
  271. ActiveSelection.SelectedPoints.ToArray());
  272. }
  273. public void ClipCanvas(object parameter)
  274. {
  275. if (BitmapManager.ActiveDocument != null)
  276. {
  277. BitmapManager.ActiveDocument.ClipCanvas();
  278. }
  279. }
  280. public void Duplicate(object parameter)
  281. {
  282. Copy(null);
  283. Paste(null);
  284. }
  285. public void Cut(object parameter)
  286. {
  287. Copy(null);
  288. BitmapManager.ActiveLayer.
  289. ApplyPixels(BitmapPixelChanges.FromSingleColoredArray(ActiveSelection.SelectedPoints.ToArray(), Colors.Transparent));
  290. }
  291. public void Paste(object parameter)
  292. {
  293. ClipboardController.PasteFromClipboard();
  294. }
  295. private bool CanPaste(object property)
  296. {
  297. return DocumentIsNotNull(null) && ClipboardController.IsImageInClipboard();
  298. }
  299. private void Copy(object parameter)
  300. {
  301. ClipboardController.CopyToClipboard(BitmapManager.ActiveDocument.Layers.ToArray(),
  302. ActiveSelection.SelectedPoints.ToArray());
  303. }
  304. public void SelectAll(object parameter)
  305. {
  306. SelectTool select = new SelectTool();
  307. select.Use(select.GetAllSelection());
  308. }
  309. private bool CanSelectAll(object property)
  310. {
  311. return BitmapManager.ActiveDocument != null && BitmapManager.ActiveDocument.Layers.Count > 0;
  312. }
  313. private bool DocumentIsNotNull(object property)
  314. {
  315. return BitmapManager.ActiveDocument != null;
  316. }
  317. public void Deselect(object parameter)
  318. {
  319. ActiveSelection.Clear();
  320. }
  321. private bool SelectionIsNotEmpty(object property)
  322. {
  323. return ActiveSelection != null && ActiveSelection.SelectedPoints != null && ActiveSelection.SelectedPoints.Count > 0;
  324. }
  325. public void SetTool(object parameter)
  326. {
  327. SetActiveTool((ToolType)parameter);
  328. }
  329. public void RenameLayer(object parameter)
  330. {
  331. BitmapManager.ActiveDocument.Layers[(int)parameter].IsRenaming = true;
  332. }
  333. public void KeyDown(object parameter)
  334. {
  335. ShortcutController.KeyPressed(((KeyEventArgs)parameter).Key);
  336. }
  337. private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
  338. {
  339. if (BitmapManager.IsOperationTool(BitmapManager.SelectedTool)
  340. && (BitmapManager.SelectedTool as BitmapOperationTool).UseDefaultUndoMethod)
  341. {
  342. Tuple<LayerChange, LayerChange>[] changes = ChangesController.PopChanges();
  343. if (changes != null && changes.Length > 0)
  344. {
  345. LayerChange[] newValues = changes.Select(x => x.Item1).ToArray();
  346. LayerChange[] oldValues = changes.Select(x => x.Item2).ToArray();
  347. UndoManager.AddUndoChange(new Change("UndoChanges", oldValues, newValues));
  348. BitmapManager.SelectedTool.AfterAddedUndo();
  349. }
  350. }
  351. }
  352. private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
  353. {
  354. ChangesController.AddChanges(new LayerChange(e.PixelsChanged, e.ChangedLayerIndex),
  355. new LayerChange(e.OldPixelsValues, e.ChangedLayerIndex));
  356. }
  357. public void SwapColors(object parameter)
  358. {
  359. var tmp = PrimaryColor;
  360. PrimaryColor = SecondaryColor;
  361. SecondaryColor = tmp;
  362. }
  363. public void MoveLayerToFront(object parameter)
  364. {
  365. int oldIndex = (int)parameter;
  366. BitmapManager.ActiveDocument.Layers.Move(oldIndex, oldIndex + 1);
  367. if (BitmapManager.ActiveDocument.ActiveLayerIndex == oldIndex)
  368. {
  369. BitmapManager.SetActiveLayer(oldIndex + 1);
  370. }
  371. }
  372. public void MoveLayerToBack(object parameter)
  373. {
  374. int oldIndex = (int)parameter;
  375. BitmapManager.ActiveDocument.Layers.Move(oldIndex, oldIndex - 1);
  376. if (BitmapManager.ActiveDocument.ActiveLayerIndex == oldIndex)
  377. {
  378. BitmapManager.SetActiveLayer(oldIndex - 1);
  379. }
  380. }
  381. public bool CanMoveToFront(object property)
  382. {
  383. return DocumentIsNotNull(null) && BitmapManager.ActiveDocument.Layers.Count - 1 > (int)property;
  384. }
  385. public bool CanMoveToBack(object property)
  386. {
  387. return (int)property > 0;
  388. }
  389. public void SetActiveLayer(object parameter)
  390. {
  391. BitmapManager.SetActiveLayer((int)parameter);
  392. }
  393. public void DeleteLayer(object parameter)
  394. {
  395. BitmapManager.RemoveLayer((int)parameter);
  396. }
  397. public bool CanDeleteLayer(object property)
  398. {
  399. return BitmapManager.ActiveDocument != null && BitmapManager.ActiveDocument.Layers.Count > 1;
  400. }
  401. #region Undo/Redo
  402. /// <summary>
  403. /// Undo last action
  404. /// </summary>
  405. /// <param name="parameter"></param>
  406. public void Undo(object parameter)
  407. {
  408. Deselect(null);
  409. UndoManager.Undo();
  410. }
  411. /// <summary>
  412. /// Returns true if undo can be done.
  413. /// </summary>
  414. /// <param name="property"></param>
  415. /// <returns></returns>
  416. private bool CanUndo(object property)
  417. {
  418. return UndoManager.CanUndo;
  419. }
  420. /// <summary>
  421. /// Redo last action
  422. /// </summary>
  423. /// <param name="parameter"></param>
  424. public void Redo(object parameter)
  425. {
  426. UndoManager.Redo();
  427. }
  428. /// <summary>
  429. /// Returns true if redo can be done.
  430. /// </summary>
  431. /// <param name="property"></param>
  432. /// <returns></returns>
  433. private bool CanRedo(object property)
  434. {
  435. return UndoManager.CanRedo;
  436. }
  437. #endregion
  438. private void SetActiveTool(ToolType tool)
  439. {
  440. Tool foundTool = ToolSet.First(x => x.ToolType == tool);
  441. Tool activeTool = ToolSet.FirstOrDefault(x => x.IsActive);
  442. if (activeTool != null)
  443. {
  444. activeTool.IsActive = false;
  445. }
  446. foundTool.IsActive = true;
  447. BitmapManager.SetActiveTool(foundTool);
  448. SetToolCursor(tool);
  449. }
  450. private void SetToolCursor(ToolType tool)
  451. {
  452. if (tool != ToolType.None)
  453. {
  454. ToolCursor = BitmapManager.SelectedTool.Cursor;
  455. }
  456. else
  457. {
  458. ToolCursor = Cursors.Arrow;
  459. }
  460. }
  461. /// <summary>
  462. /// When mouse is up stops recording changes.
  463. /// </summary>
  464. /// <param name="parameter"></param>
  465. private void MouseUp(object parameter)
  466. {
  467. BitmapManager.MouseController.StopRecordingMouseMovementChanges();
  468. }
  469. private void MouseDown(object parameter)
  470. {
  471. if (BitmapManager.ActiveDocument.Layers.Count == 0) return;
  472. if (Mouse.LeftButton == MouseButtonState.Pressed)
  473. {
  474. if (!BitmapManager.MouseController.IsRecordingChanges)
  475. {
  476. BitmapManager.MouseController.StartRecordingMouseMovementChanges();
  477. BitmapManager.MouseController.RecordMouseMovementChange(MousePositionConverter.CurrentCoordinates);
  478. AddSwatch(PrimaryColor);
  479. }
  480. }
  481. }
  482. /// <summary>
  483. /// Method connected with command, it executes tool "activity"
  484. /// </summary>
  485. /// <param name="parameter"></param>
  486. private void MouseMove(object parameter)
  487. {
  488. Coordinates cords = new Coordinates((int)MouseXOnCanvas, (int)MouseYOnCanvas);
  489. MousePositionConverter.CurrentCoordinates = cords;
  490. if (BitmapManager.MouseController.IsRecordingChanges && Mouse.LeftButton == MouseButtonState.Pressed)
  491. {
  492. BitmapManager.MouseController.RecordMouseMovementChange(cords);
  493. }
  494. else
  495. {
  496. BitmapManager.MouseController.MouseMoved(cords);
  497. }
  498. }
  499. /// <summary>
  500. /// Generates new Layer and sets it as active one
  501. /// </summary>
  502. /// <param name="parameter"></param>
  503. public void OpenNewFilePopup(object parameter)
  504. {
  505. NewFileDialog newFile = new NewFileDialog();
  506. if (newFile.ShowDialog())
  507. {
  508. NewDocument(newFile.Width, newFile.Height);
  509. }
  510. }
  511. #region SaveFile
  512. /// <summary>
  513. /// Generates export dialog or saves directly if save data is known.
  514. /// </summary>
  515. /// <param name="parameter"></param>
  516. private void SaveFile(object parameter)
  517. {
  518. WriteableBitmap bitmap = BitmapManager.GetCombinedLayersBitmap();
  519. if (Exporter.SavePath == null || (string)parameter == "AsNew")
  520. {
  521. Exporter.Export(FileType.PNG, bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
  522. }
  523. else
  524. {
  525. Exporter.ExportWithoutDialog(FileType.PNG, bitmap);
  526. }
  527. }
  528. /// <summary>
  529. /// Returns true if file save is possible.
  530. /// </summary>
  531. /// <param name="property"></param>
  532. /// <returns></returns>
  533. private bool CanSave(object property)
  534. {
  535. return BitmapManager.ActiveDocument != null;
  536. }
  537. #endregion
  538. /// <summary>
  539. /// Opens file from path.
  540. /// </summary>
  541. /// <param name="parameter"></param>
  542. public void OpenFile(object parameter)
  543. {
  544. ImportFileDialog dialog = new ImportFileDialog();
  545. if (dialog.ShowDialog())
  546. {
  547. NewDocument(dialog.FileWidth, dialog.FileHeight);
  548. BitmapManager.ActiveDocument.ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
  549. }
  550. }
  551. private void NewDocument(int width, int height)
  552. {
  553. BitmapManager.ActiveDocument = new Models.DataHolders.Document(width, height);
  554. BitmapManager.AddNewLayer("Base Layer", width, height, true);
  555. BitmapManager.PreviewLayer = null;
  556. UndoManager.UndoStack.Clear();
  557. UndoManager.RedoStack.Clear();
  558. ActiveSelection = new Selection(Array.Empty<Coordinates>());
  559. BitmapManager.ActiveDocument.DocumentSizeChanged += ActiveDocument_DocumentSizeChanged;
  560. }
  561. public void NewLayer(object parameter)
  562. {
  563. BitmapManager.AddNewLayer($"New Layer {BitmapManager.ActiveDocument.Layers.Count}", BitmapManager.ActiveDocument.Width, BitmapManager.ActiveDocument.Height);
  564. }
  565. public bool CanCreateNewLayer(object parameter)
  566. {
  567. return BitmapManager.ActiveDocument != null && BitmapManager.ActiveDocument.Layers.Count > 0;
  568. }
  569. }
  570. }