ViewModelMain.cs 12 KB

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