ViewModelMain.cs 12 KB

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