ViewModelMain.cs 14 KB

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