ViewModelMain.cs 11 KB

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