ViewModelMain.cs 13 KB

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