ViewModelMain.cs 28 KB

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