2
0

ViewModelMain.cs 11 KB

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