ViewModelMain.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. private WriteableBitmap _lastBlendedBitmap;
  131. public ViewModelMain()
  132. {
  133. PixiFilesManager.InitializeTempDirectories();
  134. Layers = new ObservableCollection<Layer>();
  135. SelectToolCommand = new RelayCommand(RecognizeTool);
  136. GenerateDrawAreaCommand = new RelayCommand(GenerateDrawArea);
  137. MouseMoveOrClickCommand = new RelayCommand(MouseMoveOrClick);
  138. SaveFileCommand = new RelayCommand(SaveFile, CanSave);
  139. UndoCommand = new RelayCommand(Undo, CanUndo);
  140. RedoCommand = new RelayCommand(Redo, CanRedo);
  141. MouseUpCommand = new RelayCommand(MouseUp);
  142. RecenterZoomboxCommand = new RelayCommand(RecenterZoombox);
  143. OpenFileCommand = new RelayCommand(OpenFile);
  144. SetActiveLayerCommand = new RelayCommand(SetActiveLayer);
  145. NewLayerCommand = new RelayCommand(NewLayer, CanCreateNewLayer);
  146. ReloadImageCommand = new RelayCommand(ReloadImage);
  147. primaryToolSet = new ToolsManager(new List<Tool> { new PixiTools.PenTool(), new PixiTools.FloodFill(), new PixiTools.LineTool(),
  148. new PixiTools.CircleTool(), new PixiTools.RectangleTool(), new PixiTools.EarserTool(), new PixiTools.BrightnessTool()});
  149. UndoManager.SetMainRoot(this);
  150. primaryToolSet.SetTool(SelectedTool);
  151. }
  152. public WriteableBitmap BlendLayersBitmaps()
  153. {
  154. Rect size = new Rect(new Size(ActiveLayer.Width, ActiveLayer.Height));
  155. Layer[] visibleLayers = Layers.Where(x => x.IsVisible).ToArray();
  156. if (visibleLayers.Length == 0)
  157. {
  158. return BitmapFactory.New(0,0);
  159. }
  160. WriteableBitmap bitmap = visibleLayers[0].LayerBitmap.Clone();
  161. for (int i = 1; i < visibleLayers.Length; i++)
  162. {
  163. bitmap.Blit(size, visibleLayers[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 ReloadImage(object property=null)
  266. {
  267. if (ActiveLayer != null)
  268. {
  269. _lastBlendedBitmap = BlendLayersBitmaps();
  270. ActiveImage.Source = _lastBlendedBitmap;
  271. }
  272. }
  273. private void RefreshImage()
  274. {
  275. if (ActiveLayer != null)
  276. {
  277. Rect size = new Rect(new Size(ActiveLayer.Height, ActiveLayer.Width));
  278. _lastBlendedBitmap.Blit(size, ActiveLayer.LayerBitmap,
  279. size);
  280. ActiveImage.Source = _lastBlendedBitmap;
  281. }
  282. }
  283. /// <summary>
  284. /// Generates new Layer and sets it as active one
  285. /// </summary>
  286. /// <param name="parameter"></param>
  287. private void GenerateDrawArea(object parameter)
  288. {
  289. NewFileDialog newFile = new NewFileDialog();
  290. if (newFile.ShowDialog())
  291. {
  292. Layers.Clear();
  293. Layers.Add(new Layer("Base Layer",newFile.Width, newFile.Height));
  294. ActiveImage = ImageGenerator.GenerateForPixelArts(newFile.Width, newFile.Height);
  295. ActiveLayer = Layers[0];
  296. }
  297. }
  298. #region SaveFile
  299. /// <summary>
  300. /// Generates export dialog or saves directly if save data is known.
  301. /// </summary>
  302. /// <param name="parameter"></param>
  303. private void SaveFile(object parameter)
  304. {
  305. ReloadImage();
  306. if (Exporter._savePath == null)
  307. {
  308. Exporter.Export(FileType.PNG, ActiveImage, new Size(ActiveLayer.Width, ActiveLayer.Height));
  309. }
  310. else
  311. {
  312. Exporter.ExportWithoutDialog(FileType.PNG, ActiveImage);
  313. }
  314. }
  315. /// <summary>
  316. /// Returns true if file save is possible.
  317. /// </summary>
  318. /// <param name="property"></param>
  319. /// <returns></returns>
  320. private bool CanSave(object property)
  321. {
  322. return ActiveLayer != null;
  323. }
  324. #endregion
  325. /// <summary>
  326. /// Opens file from path.
  327. /// </summary>
  328. /// <param name="parameter"></param>
  329. public void OpenFile(object parameter)
  330. {
  331. ImportFileDialog dialog = new ImportFileDialog();
  332. if (dialog.ShowDialog())
  333. {
  334. Layers.Clear();
  335. Layers.Add(new Layer("Base Layer",dialog.FileWidth, dialog.FileHeight));
  336. ActiveImage = ImageGenerator.GenerateForPixelArts(dialog.FileWidth, dialog.FileHeight);
  337. SetActiveLayer(0);
  338. ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
  339. ReloadImage();
  340. }
  341. }
  342. /// <summary>
  343. /// For now, shows not implemented info, lol.
  344. /// </summary>
  345. /// <param name="parameter"></param>
  346. public void RecenterZoombox(object parameter)
  347. {
  348. MessageBox.Show("This feature is not implemented yet.", "Feature not implemented", MessageBoxButton.OK, MessageBoxImage.Information);
  349. }
  350. public void SetActiveLayer(object parameter)
  351. {
  352. UndoManager.AddUndoChange("ActiveLayer", ActiveLayer, $"Changed layer to {Layers[(int)parameter].Name}");
  353. ActiveLayer = Layers[(int) parameter];
  354. ReloadImage();
  355. }
  356. public void NewLayer(object parameter)
  357. {
  358. Layers.Add(new Layer("New Layer", Layers[0].Width, Layers[0].Height));
  359. Layers.Move(Layers.Count - 1, 0);
  360. ActiveLayer = Layers[0];
  361. }
  362. public bool CanCreateNewLayer(object parameter)
  363. {
  364. return Layers.Count > 0;
  365. }
  366. }
  367. }