ViewModelMain.cs 12 KB

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