MainWindow.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Timers;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Controls.Primitives;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. using System.Windows.Threading;
  21. namespace SketchAssistantWPF
  22. {
  23. /// <summary>
  24. /// Interaction logic for MainWindow.xaml
  25. /// </summary>
  26. public partial class MainWindow : Window, MVP_View
  27. {
  28. public MainWindow()
  29. {
  30. InitializeComponent();
  31. ProgramPresenter = new MVP_Presenter(this);
  32. // DispatcherTimer setup
  33. dispatcherTimer = new DispatcherTimer(DispatcherPriority.Render);
  34. dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
  35. dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
  36. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.Width, (int)LeftCanvas.Height),
  37. new Tuple<int, int>((int)RightCanvas.Width, (int)RightCanvas.Height));
  38. }
  39. public enum ButtonState
  40. {
  41. Enabled,
  42. Disabled,
  43. Active
  44. }
  45. DispatcherTimer dispatcherTimer;
  46. /// <summary>
  47. /// Dialog to select a file.
  48. /// </summary>
  49. OpenFileDialog openFileDialog = new OpenFileDialog();
  50. /// <summary>
  51. /// All Lines in the current session
  52. /// </summary>
  53. List<Tuple<bool, InternalLine>> rightLineList = new List<Tuple<bool, InternalLine>>();
  54. /// <summary>
  55. /// Queue for the cursorPositions
  56. /// </summary>
  57. Queue<Point> cursorPositions = new Queue<Point>();
  58. /// <summary>
  59. /// The Presenter Component of the MVP-Model
  60. /// </summary>
  61. MVP_Presenter ProgramPresenter;
  62. /// <summary>
  63. /// The line currently being drawn
  64. /// </summary>
  65. Polyline currentLine;
  66. /********************************************/
  67. /*** WINDOW SPECIFIC FUNCTIONS START HERE ***/
  68. /********************************************/
  69. /// <summary>
  70. /// Resize Function connected to the form resize event, will refresh the form when it is resized
  71. /// </summary>
  72. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  73. {
  74. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.ActualWidth, (int)LeftCanvas.ActualHeight),
  75. new Tuple<int, int>((int)RightCanvas.ActualWidth, (int)RightCanvas.ActualHeight));
  76. }
  77. /// <summary>
  78. /// Redo an Action.
  79. /// </summary>
  80. private void RedoButton_Click(object sender, RoutedEventArgs e)
  81. {
  82. ProgramPresenter.Redo();
  83. }
  84. /// <summary>
  85. /// Undo an Action.
  86. /// </summary>
  87. private void UndoButton_Click(object sender, RoutedEventArgs e)
  88. {
  89. ProgramPresenter.Undo();
  90. }
  91. /// <summary>
  92. /// Changes the state of the program to deletion
  93. /// </summary>
  94. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  95. {
  96. ProgramPresenter.ChangeState(false);
  97. }
  98. /// <summary>
  99. /// Changes the state of the program to drawing
  100. /// </summary>
  101. private void DrawButton_Click(object sender, RoutedEventArgs e)
  102. {
  103. ProgramPresenter.ChangeState(true);
  104. }
  105. /// <summary>
  106. /// Hold left mouse button to start drawing.
  107. /// </summary>
  108. private void RightCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  109. {
  110. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  111. }
  112. /// <summary>
  113. /// Lift left mouse button to stop drawing and add a new Line.
  114. /// </summary>
  115. private void RightCanvas_MouseUp(object sender, MouseButtonEventArgs e)
  116. {
  117. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  118. }
  119. /// <summary>
  120. /// If the mouse leaves the canvas, it is treated as if the mouse was released.
  121. /// </summary>
  122. private void RightCanvas_MouseLeave(object sender, MouseEventArgs e)
  123. {
  124. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  125. }
  126. /// <summary>
  127. /// If the finger leaves the canvas, it is treated as if the finger was released.
  128. /// </summary>
  129. private void RightCanvas_TouchLeave(object sender, TouchEventArgs e)
  130. {
  131. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  132. }
  133. /// <summary>
  134. /// Get current Mouse positon within the right picture box.
  135. /// </summary>
  136. private void RightCanvas_MouseMove(object sender, MouseEventArgs e)
  137. {
  138. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, e.GetPosition(RightCanvas));
  139. }
  140. /// <summary>
  141. /// Button to create a new Canvas. Will create an empty image
  142. /// which is the size of the left image, if there is one.
  143. /// If there is no image loaded the canvas will be the size of the right picture box
  144. /// </summary>
  145. private void CanvasButton_Click(object sender, RoutedEventArgs e)
  146. {
  147. ProgramPresenter.NewCanvas();
  148. }
  149. /// <summary>
  150. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  151. /// </summary>
  152. private void DebugButton(object sender, RoutedEventArgs e)
  153. {
  154. dispatcherTimer.Stop();
  155. ProgramPresenter.NewCanvas(); Thread.Sleep(50); ProgramPresenter.Tick(); Thread.Sleep(200);
  156. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, new Point(20,20)); Thread.Sleep(50); ProgramPresenter.Tick(); Thread.Sleep(50);
  157. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down); Thread.Sleep(50); ProgramPresenter.Tick(); Thread.Sleep(50);
  158. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, new Point(200, 200)); Thread.Sleep(50); ProgramPresenter.Tick(); Thread.Sleep(50);
  159. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, new Point(30, 80)); Thread.Sleep(50); ProgramPresenter.Tick(); Thread.Sleep(50);
  160. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up); Thread.Sleep(50); ProgramPresenter.Tick(); Thread.Sleep(50);
  161. dispatcherTimer.Start();
  162. }
  163. /// <summary>
  164. /// Ticks the Presenter.
  165. /// </summary>
  166. private void dispatcherTimer_Tick(object sender, EventArgs e)
  167. {
  168. ProgramPresenter.Tick();
  169. }
  170. /// <summary>
  171. /// Import button, will open an OpenFileDialog
  172. /// </summary>
  173. private void MenuItem_Click(object sender, RoutedEventArgs e)
  174. {
  175. ProgramPresenter.ExamplePictureToolStripMenuItemClick();
  176. }
  177. /*************************/
  178. /*** PRESENTER -> VIEW ***/
  179. /*************************/
  180. /// <summary>
  181. /// Returns the cursor position.
  182. /// </summary>
  183. /// <returns>The cursor Position</returns>
  184. public Point GetCursorPosition()
  185. {
  186. return Mouse.GetPosition(RightCanvas);
  187. }
  188. /// <summary>
  189. /// If the mouse is pressed or not.
  190. /// </summary>
  191. /// <returns>Whether or not the mouse is pressed.</returns>
  192. public bool IsMousePressed()
  193. {
  194. return (Mouse.LeftButton.Equals(MouseButtonState.Pressed) || Mouse.RightButton.Equals(MouseButtonState.Pressed));
  195. }
  196. /// <summary>
  197. /// Remove the current line.
  198. /// </summary>
  199. public void RemoveCurrLine()
  200. {
  201. RightCanvas.Children.Remove(currentLine);
  202. }
  203. /// <summary>
  204. /// Display the current line.
  205. /// </summary>
  206. /// <param name="line">The current line to display</param>
  207. public void DisplayCurrLine(Polyline line)
  208. {
  209. if (RightCanvas.Children.Contains(currentLine))
  210. {
  211. RemoveCurrLine();
  212. }
  213. RightCanvas.Children.Add(line);
  214. currentLine = line;
  215. }
  216. /// <summary>
  217. /// Removes all Lines from the left canvas.
  218. /// </summary>
  219. public void RemoveAllLeftLines()
  220. {
  221. LeftCanvas.Children.Clear();
  222. }
  223. /// <summary>
  224. /// Removes all lines in the right canvas.
  225. /// </summary>
  226. public void RemoveAllRightLines()
  227. {
  228. RightCanvas.Children.Clear();
  229. }
  230. /// <summary>
  231. /// Adds another Line that will be displayed in the left display.
  232. /// </summary>
  233. /// <param name="newLine">The new Polyline to be added displayed.</param>
  234. public void AddNewLineLeft(Polyline newLine)
  235. {
  236. newLine.Stroke = Brushes.Black;
  237. newLine.StrokeThickness = 2;
  238. LeftCanvas.Children.Add(newLine);
  239. }
  240. /// <summary>
  241. /// Adds another Line that will be displayed in the right display.
  242. /// </summary>
  243. /// <param name="newLine">The new Polyline to be added displayed.</param>
  244. public void AddNewLineRight(Polyline newLine)
  245. {
  246. newLine.Stroke = Brushes.Black;
  247. newLine.StrokeThickness = 2;
  248. RightCanvas.Children.Add(newLine);
  249. }
  250. /// <summary>
  251. /// Adds a point to the right canvas
  252. /// </summary>
  253. /// <param name="newPoint">The point</param>
  254. public void AddNewPointRight(Ellipse newPoint)
  255. {
  256. newPoint.Height = 3; newPoint.Width = 3;
  257. newPoint.Fill = Brushes.Black;
  258. RightCanvas.Children.Add(newPoint);
  259. }
  260. /// <summary>
  261. /// Adds a point to the left canvas
  262. /// </summary>
  263. /// <param name="newPoint">The point</param>
  264. public void AddNewPointLeft(Ellipse newPoint)
  265. {
  266. newPoint.Height = 3; newPoint.Width = 3;
  267. newPoint.Fill = Brushes.Black;
  268. LeftCanvas.Children.Add(newPoint);
  269. }
  270. /// <summary>
  271. /// Enables the timer of the View, which will tick the Presenter.
  272. /// </summary>
  273. public void EnableTimer()
  274. {
  275. dispatcherTimer.Start();
  276. }
  277. /// <summary>
  278. /// A function that opens a file dialog and returns the filename.
  279. /// </summary>
  280. /// <param name="Filter">The filter that should be applied to the new Dialog.</param>
  281. /// <returns>Returns the FileName and the SafeFileName if the user correctly selects a file,
  282. /// else returns a tuple with empty strigns</returns>
  283. public Tuple<string, string> openNewDialog(string Filter)
  284. {
  285. openFileDialog.Filter = Filter;
  286. if (openFileDialog.ShowDialog() == true)
  287. {
  288. return new Tuple<string, string>(openFileDialog.FileName, openFileDialog.SafeFileName);
  289. }
  290. else
  291. {
  292. return new Tuple<string, string>("", "");
  293. }
  294. }
  295. /// <summary>
  296. /// Sets the contents of the last action taken indicator label.
  297. /// </summary>
  298. /// <param name="message">The new contents</param>
  299. public void SetLastActionTakenText(string message)
  300. {
  301. LastActionBox.Text = message;
  302. }
  303. /// <summary>
  304. /// Changes the states of a tool strip button.
  305. /// </summary>
  306. /// <param name="buttonName">The name of the button.</param>
  307. /// <param name="state">The new state of the button.</param>
  308. public void SetToolStripButtonStatus(string buttonName, MainWindow.ButtonState state)
  309. {
  310. ButtonBase buttonToChange;
  311. bool isToggleable = false;
  312. switch (buttonName)
  313. {
  314. case "canvasButton":
  315. buttonToChange = CanvasButton;
  316. break;
  317. case "drawButton":
  318. buttonToChange = DrawButton;
  319. isToggleable = true;
  320. break;
  321. case "deleteButton":
  322. buttonToChange = DeleteButton;
  323. isToggleable = true;
  324. break;
  325. case "undoButton":
  326. buttonToChange = UndoButton;
  327. break;
  328. case "redoButton":
  329. buttonToChange = RedoButton;
  330. break;
  331. default:
  332. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  333. return;
  334. }
  335. if (isToggleable)
  336. {
  337. switch (state)
  338. {
  339. case ButtonState.Active:
  340. ((ToggleButton)buttonToChange).IsEnabled = true;
  341. ((ToggleButton)buttonToChange).IsChecked = true;
  342. ((ToggleButton)buttonToChange).Opacity = 1;
  343. ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
  344. break;
  345. case ButtonState.Disabled:
  346. ((ToggleButton)buttonToChange).IsEnabled = false;
  347. ((ToggleButton)buttonToChange).IsChecked = false;
  348. ((ToggleButton)buttonToChange).Opacity = 0.5;
  349. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  350. break;
  351. case ButtonState.Enabled:
  352. ((ToggleButton)buttonToChange).IsEnabled = true;
  353. ((ToggleButton)buttonToChange).IsChecked = false;
  354. ((ToggleButton)buttonToChange).Opacity = 1;
  355. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  356. break;
  357. }
  358. }
  359. else
  360. {
  361. switch (state)
  362. {
  363. case ButtonState.Disabled:
  364. ((Button)buttonToChange).IsEnabled = false;
  365. ((Button)buttonToChange).Opacity = 0.5;
  366. break;
  367. default:
  368. ((Button)buttonToChange).IsEnabled = true;
  369. ((Button)buttonToChange).Opacity = 1;
  370. break;
  371. }
  372. }
  373. }
  374. /// <summary>
  375. /// Sets the contents of the load status indicator label.
  376. /// </summary>
  377. /// <param name="message">The new contents</param>
  378. public void SetToolStripLoadStatus(string message)
  379. {
  380. LoadStatusBox.Text = message;
  381. }
  382. /// <summary>
  383. /// shows the given info message in a popup and asks the user to aknowledge it
  384. /// </summary>
  385. /// <param name="message">the message to show</param>
  386. public void ShowInfoMessage(string message)
  387. {
  388. MessageBox.Show(message);
  389. }
  390. /// <summary>
  391. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  392. /// </summary>
  393. /// <param name="message">The message of the warning.</param>
  394. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  395. public bool ShowWarning(string message)
  396. {
  397. MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  398. return (result.Equals(MessageBoxResult.Yes));
  399. }
  400. /// <summary>
  401. /// Updates the colour of a canvas.
  402. /// </summary>
  403. /// <param name="canvasName">The name of the canvas to be updated.</param>
  404. /// <param name="active">Whether or not the canvas is active.</param>
  405. public void SetCanvasState(string canvasName, bool active)
  406. {
  407. Canvas canvas;
  408. switch (canvasName){
  409. case ("LeftCanvas"):
  410. canvas = LeftCanvas;
  411. break;
  412. case ("RightCanvas"):
  413. canvas = RightCanvas;
  414. break;
  415. default:
  416. throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
  417. }
  418. if (active)
  419. {
  420. canvas.Background = Brushes.White;
  421. }
  422. else
  423. {
  424. canvas.Background = Brushes.SlateGray;
  425. }
  426. }
  427. }
  428. }