ViewModelMain.cs 11 KB

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