ViewModelMain.cs 12 KB

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