ViewModelMain.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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(
  64. _activeLayer.ConvertBitmapToBytes(),
  65. ActiveLayer.Height, ActiveLayer.Width);
  66. else
  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 { return _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 { return _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 { return _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 { return _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. #region Undo/Redo
  145. /// <summary>
  146. /// Undo last action
  147. /// </summary>
  148. /// <param name="parameter"></param>
  149. public void Undo(object parameter)
  150. {
  151. UndoManager.Undo();
  152. }
  153. /// <summary>
  154. /// Returns true if undo can be done.
  155. /// </summary>
  156. /// <param name="property"></param>
  157. /// <returns></returns>
  158. private bool CanUndo(object property)
  159. {
  160. return UndoManager.CanUndo;
  161. }
  162. /// <summary>
  163. /// Redo last action
  164. /// </summary>
  165. /// <param name="parameter"></param>
  166. public void Redo(object parameter)
  167. {
  168. UndoManager.Redo();
  169. }
  170. /// <summary>
  171. /// Returns true if redo can be done.
  172. /// </summary>
  173. /// <param name="property"></param>
  174. /// <returns></returns>
  175. private bool CanRedo(object property)
  176. {
  177. return UndoManager.CanRedo;
  178. }
  179. #endregion
  180. /// <summary>
  181. /// Recognizes selected tool from UI
  182. /// </summary>
  183. /// <param name="parameter"></param>
  184. private void RecognizeTool(object parameter)
  185. {
  186. ToolType tool = (ToolType)Enum.Parse(typeof(ToolType), parameter.ToString());
  187. SelectedTool = tool;
  188. }
  189. /// <summary>
  190. /// When mouse is up stops recording changes.
  191. /// </summary>
  192. /// <param name="parameter"></param>
  193. private void MouseUp(object parameter)
  194. {
  195. UndoManager.StopRecording();
  196. primaryToolSet.StopExectuingTool();
  197. }
  198. /// <summary>
  199. /// Method connected with command, it executes tool "activity"
  200. /// </summary>
  201. /// <param name="parameter"></param>
  202. private void MouseMoveOrClick(object parameter)
  203. {
  204. Coordinates cords = new Coordinates((int)MouseXOnCanvas, (int)MouseYOnCanvas);
  205. MousePositionConverter.CurrentCoordinates = cords;
  206. if (Mouse.LeftButton == MouseButtonState.Pressed)
  207. {
  208. SelectedColor = PrimaryColor;
  209. }
  210. else if(Mouse.RightButton == MouseButtonState.Pressed)
  211. {
  212. SelectedColor = SecondaryColor;
  213. }
  214. else
  215. {
  216. return;
  217. }
  218. if (SelectedTool != ToolType.ColorPicker)
  219. {
  220. UndoManager.RecordChanges("ActiveLightLayer", new LightLayer(ActiveLayer.ConvertBitmapToBytes(), (int)ActiveLayer.LayerBitmap.Height,
  221. (int)ActiveLayer.LayerBitmap.Width), $"Used {SelectedTool.ToString()}");
  222. primaryToolSet.ExecuteTool(ActiveLayer, cords, SelectedColor, ToolSize);
  223. RefreshImage();
  224. }
  225. else
  226. {
  227. ExecuteColorPicker(cords);
  228. }
  229. }
  230. private void ExecuteColorPicker(Coordinates cords)
  231. {
  232. if (Mouse.LeftButton == MouseButtonState.Pressed)
  233. {
  234. PrimaryColor = ActiveLayer.LayerBitmap.GetPixel(cords.X, cords.Y);
  235. }
  236. else
  237. {
  238. SecondaryColor = ActiveLayer.LayerBitmap.GetPixel(cords.X, cords.Y);
  239. }
  240. }
  241. private void RefreshImage()
  242. {
  243. //If it won't work with layers, bug may occur here
  244. if (ActiveLayer != null)
  245. {
  246. ActiveImage.Source = ActiveLayer.LayerBitmap;
  247. }
  248. }
  249. /// <summary>
  250. /// Generates new Layer and sets it as active one
  251. /// </summary>
  252. /// <param name="parameter"></param>
  253. private void GenerateDrawArea(object parameter)
  254. {
  255. NewFileDialog newFile = new NewFileDialog();
  256. if (newFile.ShowDialog() == true)
  257. {
  258. Layers.Clear();
  259. Layers.Add(new Layer(newFile.Width, newFile.Height));
  260. ActiveImage = ImageGenerator.GenerateForPixelArts(newFile.Width, newFile.Height);
  261. ActiveLayer = Layers[0];
  262. }
  263. }
  264. #region SaveFile
  265. /// <summary>
  266. /// Generates export dialog or saves directly if save data is known.
  267. /// </summary>
  268. /// <param name="parameter"></param>
  269. private void SaveFile(object parameter)
  270. {
  271. if (Exporter._savePath == null)
  272. {
  273. Exporter.Export(FileType.PNG, ActiveImage, new Size(ActiveLayer.Width, ActiveLayer.Height));
  274. }
  275. else
  276. {
  277. Exporter.ExportWithoutDialog(FileType.PNG, ActiveImage);
  278. }
  279. }
  280. /// <summary>
  281. /// Returns true if file save is possible.
  282. /// </summary>
  283. /// <param name="property"></param>
  284. /// <returns></returns>
  285. private bool CanSave(object property)
  286. {
  287. return ActiveLayer != null;
  288. }
  289. #endregion
  290. /// <summary>
  291. /// Opens file from path.
  292. /// </summary>
  293. /// <param name="parameter"></param>
  294. public void OpenFile(object parameter)
  295. {
  296. ImportFileDialog dialog = new ImportFileDialog();
  297. if (dialog.ShowDialog() == true)
  298. {
  299. Layers.Clear();
  300. Layers.Add(new Layer(dialog.FileWidth, dialog.FileHeight));
  301. ActiveImage = ImageGenerator.GenerateForPixelArts(dialog.FileWidth, dialog.FileHeight);
  302. ActiveLayer = Layers[0];
  303. ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
  304. RefreshImage();
  305. }
  306. }
  307. /// <summary>
  308. /// For now, shows not implemented info, lol.
  309. /// </summary>
  310. /// <param name="parameter"></param>
  311. public void RecenterZoombox(object parameter)
  312. {
  313. MessageBox.Show("This feature is not implemented yet.", "Feature not implemented", MessageBoxButton.OK, MessageBoxImage.Information);
  314. }
  315. }
  316. }