ViewModelMain.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using PixiEditor.Helpers;
  2. using PixiEditorDotNetCore3.Models.Enums;
  3. using PixiEditorDotNetCore3.Models.Tools;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Drawing.Drawing2D;
  8. using System.Linq;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using PixiTools = PixiEditorDotNetCore3.Models.Tools.Tools;
  15. using PixiEditorDotNetCore3.Models.Controllers;
  16. using PixiEditorDotNetCore3.Models.Dialogs;
  17. using PixiEditorDotNetCore3.Models.Images;
  18. using PixiEditorDotNetCore3.Models.IO;
  19. using PixiEditorDotNetCore3.Models.Layers;
  20. using PixiEditorDotNetCore3.Models.Position;
  21. namespace PixiEditor.ViewModels
  22. {
  23. class ViewModelMain : ViewModelBase
  24. {
  25. private ObservableCollection<Layer> _layers;
  26. public ObservableCollection<Layer> Layers
  27. {
  28. get => _layers;
  29. set { if (_layers != value) { _layers = value;} }
  30. }
  31. public RelayCommand SelectToolCommand { get; set; } //Command that handles tool switching
  32. public RelayCommand GenerateDrawAreaCommand { get; set; } //Command that generates draw area
  33. public RelayCommand MouseMoveOrClickCommand { get; set; } //Command that is used to draw
  34. public RelayCommand SaveFileCommand { get; set; } //Command that is used to save file
  35. public RelayCommand UndoCommand { get; set; }
  36. public RelayCommand RedoCommand { get; set; }
  37. public RelayCommand MouseUpCommand { get; set; }
  38. public RelayCommand RecenterZoomboxCommand { get; set; }
  39. public RelayCommand OpenFileCommand { get; set; }
  40. public RelayCommand SetActiveLayerCommand { get; set; }
  41. public RelayCommand NewLayerCommand { get; set; }
  42. private Image _activeImage;
  43. public Image ActiveImage
  44. {
  45. get => _activeImage;
  46. set
  47. {
  48. _activeImage = BuildFinalImage(value);
  49. RaisePropertyChanged("ActiveImage");
  50. }
  51. }
  52. private Layer _activeLayer;
  53. public Layer ActiveLayer //Active drawing layer
  54. {
  55. get => _activeLayer;
  56. set {
  57. _activeLayer = value;
  58. RefreshImage();
  59. RaisePropertyChanged("ActiveLayer");
  60. }
  61. }
  62. public LightLayer ActiveLightLayer
  63. {
  64. get
  65. {
  66. if (_activeLayer != null)
  67. return new LightLayer(
  68. _activeLayer.ConvertBitmapToBytes(),
  69. ActiveLayer.Height, ActiveLayer.Width);
  70. return null;
  71. }
  72. set => ActiveLayer = new Layer(BitmapConverter.BytesToWriteableBitmap(ActiveLayer.Width, ActiveLayer.Height,value.LayerBytes));
  73. }
  74. private double _mouseXonCanvas;
  75. public double MouseXOnCanvas //Mouse X coordinate relative to canvas
  76. {
  77. get => _mouseXonCanvas;
  78. set { _mouseXonCanvas = value; RaisePropertyChanged("MouseXonCanvas"); }
  79. }
  80. private double _mouseYonCanvas;
  81. public double MouseYOnCanvas //Mouse Y coordinate relative to canvas
  82. {
  83. get => _mouseYonCanvas;
  84. set { _mouseYonCanvas = value; RaisePropertyChanged("MouseYonCanvas"); }
  85. }
  86. private Color _primaryColor = Colors.White;
  87. public Color PrimaryColor //Primary color, hooked with left mouse button
  88. {
  89. get => _primaryColor;
  90. set
  91. {
  92. if (_primaryColor != value)
  93. {
  94. _primaryColor = value;
  95. RaisePropertyChanged("PrimaryColor");
  96. }
  97. }
  98. }
  99. private Color _secondaryColor = Colors.Black;
  100. public Color SecondaryColor //Secondary color, hooked with right mouse button
  101. {
  102. get => _secondaryColor;
  103. set { if (_secondaryColor != value) { _secondaryColor = value; RaisePropertyChanged("SecondaryColor"); } }
  104. }
  105. private Color _selectedColor = Colors.White;
  106. public Color SelectedColor
  107. {
  108. get { return _selectedColor; }
  109. set
  110. {
  111. if(_selectedColor != value)
  112. _selectedColor = value;
  113. RaisePropertyChanged("SelectedColor");
  114. }
  115. }
  116. private ToolType _selectedTool = ToolType.Pen;
  117. public ToolType SelectedTool
  118. {
  119. get { return _selectedTool; }
  120. set { if (_selectedTool != value) { _selectedTool = value; primaryToolSet.SetTool(SelectedTool); RaisePropertyChanged("SelectedTool"); } }
  121. }
  122. private int _toolSize = 1;
  123. public int ToolSize
  124. {
  125. get { return _toolSize; }
  126. set { if (_toolSize != value) { _toolSize = value; RaisePropertyChanged("ToolSize"); } }
  127. }
  128. private ToolsManager primaryToolSet;
  129. public ViewModelMain()
  130. {
  131. PixiFilesManager.InitializeTempDirectories();
  132. Layers = new ObservableCollection<Layer>();
  133. SelectToolCommand = new RelayCommand(RecognizeTool);
  134. GenerateDrawAreaCommand = new RelayCommand(GenerateDrawArea);
  135. MouseMoveOrClickCommand = new RelayCommand(MouseMoveOrClick);
  136. SaveFileCommand = new RelayCommand(SaveFile, CanSave);
  137. UndoCommand = new RelayCommand(Undo, CanUndo);
  138. RedoCommand = new RelayCommand(Redo, CanRedo);
  139. MouseUpCommand = new RelayCommand(MouseUp);
  140. RecenterZoomboxCommand = new RelayCommand(RecenterZoombox);
  141. OpenFileCommand = new RelayCommand(OpenFile);
  142. SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
  143. NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
  144. primaryToolSet = new ToolsManager(new List<Tool> { new PixiTools.PenTool(), new PixiTools.FloodFill(), new PixiTools.LineTool(),
  145. new PixiTools.CircleTool(), new PixiTools.RectangleTool(), new PixiTools.EarserTool(), new PixiTools.BrightnessTool()});
  146. UndoManager.SetMainRoot(this);
  147. primaryToolSet.SetTool(SelectedTool);
  148. }
  149. public Image BuildFinalImage(Image image)
  150. {
  151. if (ActiveLayer == null) return image;
  152. WriteableBitmap bitmap = BlendLayersBitmaps();
  153. Image finalImage = image;
  154. image.Source = bitmap;
  155. return finalImage;
  156. }
  157. public WriteableBitmap BlendLayersBitmaps()
  158. {
  159. Rect size = new Rect(new Size(ActiveLayer.Width, ActiveLayer.Height));
  160. WriteableBitmap bitmap = Layers[0].LayerBitmap;
  161. for (int i = 1; i < Layers.Count; i++)
  162. {
  163. bitmap.Blit(size, Layers[i].LayerBitmap,
  164. size, WriteableBitmapExtensions.BlendMode.Additive);
  165. }
  166. return bitmap;
  167. }
  168. #region Undo/Redo
  169. /// <summary>
  170. /// Undo last action
  171. /// </summary>
  172. /// <param name="parameter"></param>
  173. public void Undo(object parameter)
  174. {
  175. UndoManager.Undo();
  176. }
  177. /// <summary>
  178. /// Returns true if undo can be done.
  179. /// </summary>
  180. /// <param name="property"></param>
  181. /// <returns></returns>
  182. private bool CanUndo(object property)
  183. {
  184. return UndoManager.CanUndo;
  185. }
  186. /// <summary>
  187. /// Redo last action
  188. /// </summary>
  189. /// <param name="parameter"></param>
  190. public void Redo(object parameter)
  191. {
  192. UndoManager.Redo();
  193. }
  194. /// <summary>
  195. /// Returns true if redo can be done.
  196. /// </summary>
  197. /// <param name="property"></param>
  198. /// <returns></returns>
  199. private bool CanRedo(object property)
  200. {
  201. return UndoManager.CanRedo;
  202. }
  203. #endregion
  204. /// <summary>
  205. /// Recognizes selected tool from UI
  206. /// </summary>
  207. /// <param name="parameter"></param>
  208. private void RecognizeTool(object parameter)
  209. {
  210. ToolType tool = (ToolType)Enum.Parse(typeof(ToolType), parameter.ToString());
  211. SelectedTool = tool;
  212. }
  213. /// <summary>
  214. /// When mouse is up stops recording changes.
  215. /// </summary>
  216. /// <param name="parameter"></param>
  217. private void MouseUp(object parameter)
  218. {
  219. UndoManager.StopRecording();
  220. primaryToolSet.StopExectuingTool();
  221. }
  222. /// <summary>
  223. /// Method connected with command, it executes tool "activity"
  224. /// </summary>
  225. /// <param name="parameter"></param>
  226. private void MouseMoveOrClick(object parameter)
  227. {
  228. Coordinates cords = new Coordinates((int)MouseXOnCanvas, (int)MouseYOnCanvas);
  229. MousePositionConverter.CurrentCoordinates = cords;
  230. if (Mouse.LeftButton == MouseButtonState.Pressed)
  231. {
  232. SelectedColor = PrimaryColor;
  233. }
  234. else if(Mouse.RightButton == MouseButtonState.Pressed)
  235. {
  236. SelectedColor = SecondaryColor;
  237. }
  238. else
  239. {
  240. return;
  241. }
  242. if (SelectedTool != ToolType.ColorPicker)
  243. {
  244. UndoManager.RecordChanges("ActiveLightLayer", new LightLayer(ActiveLayer.ConvertBitmapToBytes(), (int)ActiveLayer.LayerBitmap.Height,
  245. (int)ActiveLayer.LayerBitmap.Width), $"Used {SelectedTool.ToString()}");
  246. primaryToolSet.ExecuteTool(ActiveLayer, cords, SelectedColor, ToolSize);
  247. RefreshImage();
  248. }
  249. else
  250. {
  251. ExecuteColorPicker(cords);
  252. }
  253. }
  254. private void ExecuteColorPicker(Coordinates cords)
  255. {
  256. if (Mouse.LeftButton == MouseButtonState.Pressed)
  257. {
  258. PrimaryColor = ActiveLayer.LayerBitmap.GetPixel(cords.X, cords.Y);
  259. }
  260. else
  261. {
  262. SecondaryColor = ActiveLayer.LayerBitmap.GetPixel(cords.X, cords.Y);
  263. }
  264. }
  265. private void RefreshImage()
  266. {
  267. //If it won't work with layers, bug may occur here
  268. if (ActiveLayer != null)
  269. {
  270. Image activeImage = ActiveImage;
  271. activeImage.Source = ActiveLayer.LayerBitmap;
  272. ActiveImage = activeImage;
  273. }
  274. }
  275. /// <summary>
  276. /// Generates new Layer and sets it as active one
  277. /// </summary>
  278. /// <param name="parameter"></param>
  279. private void GenerateDrawArea(object parameter)
  280. {
  281. NewFileDialog newFile = new NewFileDialog();
  282. if (newFile.ShowDialog() == true)
  283. {
  284. Layers.Clear();
  285. Layers.Add(new Layer("Base Layer",newFile.Width, newFile.Height));
  286. ActiveImage = ImageGenerator.GenerateForPixelArts(newFile.Width, newFile.Height);
  287. ActiveLayer = Layers[0];
  288. }
  289. }
  290. #region SaveFile
  291. /// <summary>
  292. /// Generates export dialog or saves directly if save data is known.
  293. /// </summary>
  294. /// <param name="parameter"></param>
  295. private void SaveFile(object parameter)
  296. {
  297. if (Exporter._savePath == null)
  298. {
  299. Exporter.Export(FileType.PNG, ActiveImage, new Size(ActiveLayer.Width, ActiveLayer.Height));
  300. }
  301. else
  302. {
  303. Exporter.ExportWithoutDialog(FileType.PNG, ActiveImage);
  304. }
  305. }
  306. /// <summary>
  307. /// Returns true if file save is possible.
  308. /// </summary>
  309. /// <param name="property"></param>
  310. /// <returns></returns>
  311. private bool CanSave(object property)
  312. {
  313. return ActiveLayer != null;
  314. }
  315. #endregion
  316. /// <summary>
  317. /// Opens file from path.
  318. /// </summary>
  319. /// <param name="parameter"></param>
  320. public void OpenFile(object parameter)
  321. {
  322. ImportFileDialog dialog = new ImportFileDialog();
  323. if (dialog.ShowDialog() == true)
  324. {
  325. Layers.Clear();
  326. Layers.Add(new Layer("Base Layer",dialog.FileWidth, dialog.FileHeight));
  327. ActiveImage = ImageGenerator.GenerateForPixelArts(dialog.FileWidth, dialog.FileHeight);
  328. ActiveLayer = Layers[0];
  329. ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
  330. RefreshImage();
  331. }
  332. }
  333. /// <summary>
  334. /// For now, shows not implemented info, lol.
  335. /// </summary>
  336. /// <param name="parameter"></param>
  337. public void RecenterZoombox(object parameter)
  338. {
  339. Layer testLayer = new Layer("Test Layer", Layers[0].Width, Layers[0].Height);
  340. testLayer.LayerBitmap.SetPixel(5, 5, Colors.Black);
  341. Layers.Add(testLayer);
  342. RefreshImage();
  343. MessageBox.Show("This feature is not implemented yet.", "Feature not implemented", MessageBoxButton.OK, MessageBoxImage.Information);
  344. }
  345. public void SetActiveLayer(object parameter)
  346. {
  347. ActiveLayer = Layers[(int) parameter];
  348. }
  349. public void NewLayer(object parameter)
  350. {
  351. Layers.Add(new Layer("New Layer", Layers[0].Width, Layers[0].Height));
  352. Layers.Move(Layers.Count - 1, 0);
  353. }
  354. public bool CanCreateNewLayer(object parameter)
  355. {
  356. return Layers.Count > 0;
  357. }
  358. }
  359. }