ViewModelMain.cs 12 KB

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