ViewModelMain.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. if (_activeLayer != null)
  49. {
  50. UndoManager.AddUndoChange("ActiveLightLayer",
  51. new LightLayer(_activeLayer.LayerBitmap.ToByteArray(), ActiveLayer.Height, ActiveLayer.Width),
  52. "Layer Changed");
  53. }
  54. _activeLayer = value;
  55. RefreshImage();
  56. RaisePropertyChanged("ActiveLayer");
  57. }
  58. }
  59. public LightLayer ActiveLightLayer
  60. {
  61. get
  62. {
  63. if (_activeLayer != null)
  64. return new LightLayer(_activeLayer.LayerBitmap.ToByteArray(), ActiveLayer.Height, ActiveLayer.Width);
  65. else
  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 { return _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 { return _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 { return _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 { return _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. primaryToolSet.ExecuteTool(ActiveLayer, cords, SelectedColor, ToolSize);
  220. RefreshImage();
  221. }
  222. else
  223. {
  224. ExecuteColorPicker(cords);
  225. }
  226. }
  227. private void ExecuteColorPicker(Coordinates cords)
  228. {
  229. if (Mouse.LeftButton == MouseButtonState.Pressed)
  230. {
  231. PrimaryColor = ActiveLayer.LayerBitmap.GetPixel(cords.X, cords.Y);
  232. }
  233. else
  234. {
  235. SecondaryColor = ActiveLayer.LayerBitmap.GetPixel(cords.X, cords.Y);
  236. }
  237. }
  238. private void RefreshImage()
  239. {
  240. //If it won't work with layers, bug may occur here
  241. if (ActiveLayer != null)
  242. {
  243. ActiveImage.Source = ActiveLayer.LayerBitmap;
  244. }
  245. }
  246. /// <summary>
  247. /// Generates new Layer and sets it as active one
  248. /// </summary>
  249. /// <param name="parameter"></param>
  250. private void GenerateDrawArea(object parameter)
  251. {
  252. NewFileDialog newFile = new NewFileDialog();
  253. if (newFile.ShowDialog() == true)
  254. {
  255. Layers.Clear();
  256. Layers.Add(new Layer(newFile.Width, newFile.Height));
  257. ActiveImage = ImageGenerator.GenerateForPixelArts(newFile.Width, newFile.Height);
  258. ActiveLayer = Layers[0];
  259. }
  260. }
  261. #region SaveFile
  262. /// <summary>
  263. /// Generates export dialog or saves directly if save data is known.
  264. /// </summary>
  265. /// <param name="parameter"></param>
  266. private void SaveFile(object parameter)
  267. {
  268. if (Exporter._savePath == null)
  269. {
  270. Exporter.Export(FileType.PNG, ActiveImage, new Size(ActiveLayer.Width, ActiveLayer.Height));
  271. }
  272. else
  273. {
  274. Exporter.ExportWithoutDialog(FileType.PNG, ActiveImage);
  275. }
  276. }
  277. /// <summary>
  278. /// Returns true if file save is possible.
  279. /// </summary>
  280. /// <param name="property"></param>
  281. /// <returns></returns>
  282. private bool CanSave(object property)
  283. {
  284. return ActiveLayer != null;
  285. }
  286. #endregion
  287. /// <summary>
  288. /// Opens file from path.
  289. /// </summary>
  290. /// <param name="parameter"></param>
  291. public void OpenFile(object parameter)
  292. {
  293. ImportFileDialog dialog = new ImportFileDialog();
  294. if (dialog.ShowDialog() == true)
  295. {
  296. Layers.Clear();
  297. Layers.Add(new Layer(dialog.FileWidth, dialog.FileHeight));
  298. ActiveImage = ImageGenerator.GenerateForPixelArts(dialog.FileWidth, dialog.FileHeight);
  299. ActiveLayer = Layers[0];
  300. ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
  301. RefreshImage();
  302. }
  303. }
  304. /// <summary>
  305. /// For now, shows not implemented info, lol.
  306. /// </summary>
  307. /// <param name="parameter"></param>
  308. public void RecenterZoombox(object parameter)
  309. {
  310. MessageBox.Show("This feature is not implemented yet.", "Feature not implemented", MessageBoxButton.OK, MessageBoxImage.Information);
  311. }
  312. }
  313. }