ViewModelMain.cs 11 KB

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