MainWindow.xaml.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. bool InDebugMode = false;
  31. String[] commArgs = Environment.GetCommandLineArgs();
  32. InitializeComponent();
  33. if (commArgs.Length > 1)
  34. {
  35. if (commArgs[1].Equals("-debug"))
  36. {
  37. InDebugMode = true;
  38. }
  39. }
  40. if(!InDebugMode)
  41. {
  42. DebugMode.Visibility = Visibility.Collapsed;
  43. }
  44. ProgramPresenter = new MVP_Presenter(this);
  45. // DispatcherTimer setup
  46. dispatcherTimer = new DispatcherTimer(DispatcherPriority.Render);
  47. dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
  48. dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
  49. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.Width, (int)LeftCanvas.Height),
  50. new Tuple<int, int>((int)RightCanvas.Width, (int)RightCanvas.Height));
  51. }
  52. public enum ButtonState
  53. {
  54. Enabled,
  55. Disabled,
  56. Active
  57. }
  58. DispatcherTimer dispatcherTimer;
  59. /// <summary>
  60. /// Dialog to select a file.
  61. /// </summary>
  62. OpenFileDialog openFileDialog = new OpenFileDialog();
  63. /// <summary>
  64. /// All Lines in the current session
  65. /// </summary>
  66. List<Tuple<bool, InternalLine>> rightLineList = new List<Tuple<bool, InternalLine>>();
  67. /// <summary>
  68. /// Queue for the cursorPositions
  69. /// </summary>
  70. Queue<Point> cursorPositions = new Queue<Point>();
  71. /// <summary>
  72. /// The Presenter Component of the MVP-Model
  73. /// </summary>
  74. MVP_Presenter ProgramPresenter;
  75. /// <summary>
  76. /// The line currently being drawn
  77. /// </summary>
  78. Polyline currentLine;
  79. /// <summary>
  80. /// If the debug function is running.
  81. /// </summary>
  82. bool debugRunning = false;
  83. /// <summary>
  84. /// Point collections for debugging.
  85. /// </summary>
  86. DebugData debugDat = new DebugData();
  87. /********************************************/
  88. /*** WINDOW SPECIFIC FUNCTIONS START HERE ***/
  89. /********************************************/
  90. /// <summary>
  91. /// Resize Function connected to the form resize event, will refresh the form when it is resized
  92. /// </summary>
  93. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  94. {
  95. ProgramPresenter.Resize(new Tuple<int, int>((int)LeftCanvas.ActualWidth, (int)LeftCanvas.ActualHeight),
  96. new Tuple<int, int>((int)RightCanvas.ActualWidth, (int)RightCanvas.ActualHeight));
  97. }
  98. /// <summary>
  99. /// Redo an Action.
  100. /// </summary>
  101. private void RedoButton_Click(object sender, RoutedEventArgs e)
  102. {
  103. ProgramPresenter.Redo();
  104. }
  105. /// <summary>
  106. /// Undo an Action.
  107. /// </summary>
  108. private void UndoButton_Click(object sender, RoutedEventArgs e)
  109. {
  110. ProgramPresenter.Undo();
  111. }
  112. /// <summary>
  113. /// Changes the state of the program to deletion
  114. /// </summary>
  115. private void DeleteButton_Click(object sender, RoutedEventArgs e)
  116. {
  117. ProgramPresenter.ChangeState(false);
  118. RightCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
  119. }
  120. /// <summary>
  121. /// Changes the state of the program to drawing
  122. /// </summary>
  123. private void DrawButton_Click(object sender, RoutedEventArgs e)
  124. {
  125. ProgramPresenter.ChangeState(true);
  126. RightCanvas.EditingMode = InkCanvasEditingMode.Ink;
  127. }
  128. /// <summary>
  129. /// Hold left mouse button to start drawing.
  130. /// </summary>
  131. private void RightCanvas_MouseDown(object sender, MouseButtonEventArgs e)
  132. {
  133. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  134. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);");
  135. }
  136. /// <summary>
  137. /// Lift left mouse button to stop drawing and add a new Line.
  138. /// </summary>
  139. private void RightCanvas_MouseUp(object sender, MouseButtonEventArgs e)
  140. {
  141. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  142. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);");
  143. }
  144. /// <summary>
  145. /// If the mouse leaves the canvas, it is treated as if the mouse was released.
  146. /// </summary>
  147. private void RightCanvas_MouseLeave(object sender, MouseEventArgs e)
  148. {
  149. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  150. }
  151. /// <summary>
  152. /// If the cursor enters the canvas, it is treated as if the cursor was just pressed if the cursor is pressed.
  153. /// </summary>
  154. private void RightCanvas_MouseEnter(object sender, MouseEventArgs e)
  155. {
  156. if (IsMousePressed())
  157. {
  158. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, Mouse.GetPosition(RightCanvas));
  159. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  160. }
  161. }
  162. /// <summary>
  163. /// If the finger enters the canvas, it is treated as if the finger was just pressed if the finger is pressed.
  164. /// </summary>
  165. private void RightCanvas_TouchEnter(object sender, TouchEventArgs e)
  166. {
  167. if (IsMousePressed())
  168. {
  169. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, Mouse.GetPosition(RightCanvas));
  170. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down);
  171. }
  172. }
  173. /// <summary>
  174. /// If the finger leaves the canvas, it is treated as if the finger was released.
  175. /// </summary>
  176. private void RightCanvas_TouchLeave(object sender, TouchEventArgs e)
  177. {
  178. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up);
  179. }
  180. /// <summary>
  181. /// Get current Mouse positon within the right picture box.
  182. /// </summary>
  183. private void RightCanvas_MouseMove(object sender, MouseEventArgs e)
  184. {
  185. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, e.GetPosition(RightCanvas));
  186. //System.Diagnostics.Debug.WriteLine("new Point(" + ((int)e.GetPosition(RightCanvas).X).ToString() + "," + ((int)e.GetPosition(RightCanvas).Y).ToString() + "), ");
  187. }
  188. /// <summary>
  189. /// Button to create a new Canvas. Will create an empty image
  190. /// which is the size of the left image, if there is one.
  191. /// If there is no image loaded the canvas will be the size of the right picture box
  192. /// </summary>
  193. private void CanvasButton_Click(object sender, RoutedEventArgs e)
  194. {
  195. ProgramPresenter.NewCanvas();
  196. RightCanvas.Strokes.Clear();
  197. }
  198. /// <summary>
  199. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  200. /// Takes 7000ms
  201. /// </summary>
  202. private void DebugOne_Click(object sender, RoutedEventArgs e)
  203. {
  204. Debug(1);
  205. }
  206. /// <summary>
  207. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  208. /// Takes 24000ms
  209. /// </summary>
  210. private void DebugTwo_Click(object sender, RoutedEventArgs e)
  211. {
  212. Debug(2);
  213. }
  214. /// <summary>
  215. /// Sends inputs to the presenter simulating drawing, used for testing and debugging.
  216. /// Takes 7000ms
  217. /// </summary>
  218. private void DebugThree_Click(object sender, RoutedEventArgs e)
  219. {
  220. Debug(3);
  221. }
  222. /// <summary>
  223. /// Ticks the Presenter.
  224. /// </summary>
  225. private void dispatcherTimer_Tick(object sender, EventArgs e)
  226. {
  227. ProgramPresenter.Tick();
  228. //System.Diagnostics.Debug.WriteLine("ProgramPresenter.Tick();");
  229. }
  230. /// <summary>
  231. /// Import button for .isad drawing, will open an OpenFileDialog
  232. /// </summary>
  233. private void ISADMenuItem_Click(object sender, RoutedEventArgs e)
  234. {
  235. ProgramPresenter.ExamplePictureToolStripMenuItemClick();
  236. }
  237. /// <summary>
  238. /// Import button for .svg file, will open an OpenFileDialog
  239. /// </summary>
  240. private void SVGMenuItem_Click(object sender, RoutedEventArgs e)
  241. {
  242. ProgramPresenter.SVGToolStripMenuItemClick();
  243. }
  244. /*************************/
  245. /*** PRESENTER -> VIEW ***/
  246. /*************************/
  247. /// <summary>
  248. /// Returns the cursor position.
  249. /// </summary>
  250. /// <returns>The cursor Position</returns>
  251. public Point GetCursorPosition()
  252. {
  253. return Mouse.GetPosition(RightCanvas);
  254. }
  255. /// <summary>
  256. /// If the mouse is pressed or not.
  257. /// </summary>
  258. /// <returns>Whether or not the mouse is pressed.</returns>
  259. public bool IsMousePressed()
  260. {
  261. if (!debugRunning) {
  262. return (Mouse.LeftButton.Equals(MouseButtonState.Pressed) || Mouse.RightButton.Equals(MouseButtonState.Pressed)); }
  263. else return true;
  264. }
  265. /// <summary>
  266. /// Remove the current line.
  267. /// </summary>
  268. public void RemoveCurrLine()
  269. {
  270. RightCanvas.Children.Remove(currentLine);
  271. }
  272. /// <summary>
  273. /// Display the current line.
  274. /// </summary>
  275. /// <param name="line">The current line to display</param>
  276. public void DisplayCurrLine(Polyline line)
  277. {
  278. if (RightCanvas.Children.Contains(currentLine))
  279. {
  280. RemoveCurrLine();
  281. }
  282. RightCanvas.Children.Add(line);
  283. currentLine = line;
  284. }
  285. /// <summary>
  286. /// Removes all Lines from the left canvas.
  287. /// </summary>
  288. public void RemoveAllLeftLines()
  289. {
  290. LeftCanvas.Children.Clear();
  291. }
  292. /// <summary>
  293. /// Removes all lines in the right canvas.
  294. /// </summary>
  295. public void RemoveAllRightLines()
  296. {
  297. RightCanvas.Children.Clear();
  298. }
  299. /// <summary>
  300. /// Adds another Line that will be displayed in the left display.
  301. /// </summary>
  302. /// <param name="newLine">The new Polyline to be added displayed.</param>
  303. public void AddNewLineLeft(Polyline newLine)
  304. {
  305. newLine.Stroke = Brushes.Black;
  306. newLine.StrokeThickness = 2;
  307. LeftCanvas.Children.Add(newLine);
  308. }
  309. /// <summary>
  310. /// Adds another Line that will be displayed in the right display.
  311. /// </summary>
  312. /// <param name="newLine">The new Polyline to be added displayed.</param>
  313. public void AddNewLineRight(Polyline newLine)
  314. {
  315. newLine.Stroke = Brushes.Black;
  316. newLine.StrokeThickness = 2;
  317. RightCanvas.Children.Add(newLine);
  318. }
  319. /// <summary>
  320. /// Adds a point to the right canvas
  321. /// </summary>
  322. /// <param name="newPoint">The point</param>
  323. public void AddNewPointRight(Ellipse newPoint)
  324. {
  325. newPoint.Height = 3; newPoint.Width = 3;
  326. newPoint.Fill = Brushes.Black;
  327. RightCanvas.Children.Add(newPoint);
  328. }
  329. /// <summary>
  330. /// Adds a point to the left canvas
  331. /// </summary>
  332. /// <param name="newPoint">The point</param>
  333. public void AddNewPointLeft(Ellipse newPoint)
  334. {
  335. newPoint.Height = 3; newPoint.Width = 3;
  336. newPoint.Fill = Brushes.Black;
  337. LeftCanvas.Children.Add(newPoint);
  338. }
  339. /// <summary>
  340. /// Enables the timer of the View, which will tick the Presenter.
  341. /// </summary>
  342. public void EnableTimer()
  343. {
  344. dispatcherTimer.Start();
  345. }
  346. /// <summary>
  347. /// A function that opens a file dialog and returns the filename.
  348. /// </summary>
  349. /// <param name="Filter">The filter that should be applied to the new Dialog.</param>
  350. /// <returns>Returns the FileName and the SafeFileName if the user correctly selects a file,
  351. /// else returns a tuple with empty strigns</returns>
  352. public Tuple<string, string> openNewDialog(string Filter)
  353. {
  354. openFileDialog.Filter = Filter;
  355. if (openFileDialog.ShowDialog() == true)
  356. {
  357. return new Tuple<string, string>(openFileDialog.FileName, openFileDialog.SafeFileName);
  358. }
  359. else
  360. {
  361. return new Tuple<string, string>("", "");
  362. }
  363. }
  364. /// <summary>
  365. /// Sets the contents of the last action taken indicator label.
  366. /// </summary>
  367. /// <param name="message">The new contents</param>
  368. public void SetLastActionTakenText(string message)
  369. {
  370. LastActionBox.Text = message;
  371. }
  372. /// <summary>
  373. /// Changes the states of a tool strip button.
  374. /// </summary>
  375. /// <param name="buttonName">The name of the button.</param>
  376. /// <param name="state">The new state of the button.</param>
  377. public void SetToolStripButtonStatus(string buttonName, MainWindow.ButtonState state)
  378. {
  379. ButtonBase buttonToChange;
  380. bool isToggleable = false;
  381. switch (buttonName)
  382. {
  383. case "canvasButton":
  384. buttonToChange = CanvasButton;
  385. break;
  386. case "drawButton":
  387. buttonToChange = DrawButton;
  388. isToggleable = true;
  389. break;
  390. case "deleteButton":
  391. buttonToChange = DeleteButton;
  392. isToggleable = true;
  393. break;
  394. case "undoButton":
  395. buttonToChange = UndoButton;
  396. break;
  397. case "redoButton":
  398. buttonToChange = RedoButton;
  399. break;
  400. default:
  401. Console.WriteLine("Invalid Button was given to SetToolStripButton. \nMaybe you forgot to add a case?");
  402. return;
  403. }
  404. if (isToggleable)
  405. {
  406. switch (state)
  407. {
  408. case ButtonState.Active:
  409. ((ToggleButton)buttonToChange).IsEnabled = true;
  410. ((ToggleButton)buttonToChange).IsChecked = true;
  411. ((ToggleButton)buttonToChange).Opacity = 1;
  412. ((ToggleButton)buttonToChange).Background = Brushes.SkyBlue;
  413. break;
  414. case ButtonState.Disabled:
  415. ((ToggleButton)buttonToChange).IsEnabled = false;
  416. ((ToggleButton)buttonToChange).IsChecked = false;
  417. ((ToggleButton)buttonToChange).Opacity = 0.5;
  418. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  419. break;
  420. case ButtonState.Enabled:
  421. ((ToggleButton)buttonToChange).IsEnabled = true;
  422. ((ToggleButton)buttonToChange).IsChecked = false;
  423. ((ToggleButton)buttonToChange).Opacity = 1;
  424. ((ToggleButton)buttonToChange).Background = Brushes.LightGray;
  425. break;
  426. }
  427. }
  428. else
  429. {
  430. switch (state)
  431. {
  432. case ButtonState.Disabled:
  433. ((Button)buttonToChange).IsEnabled = false;
  434. ((Button)buttonToChange).Opacity = 0.5;
  435. break;
  436. default:
  437. ((Button)buttonToChange).IsEnabled = true;
  438. ((Button)buttonToChange).Opacity = 1;
  439. break;
  440. }
  441. }
  442. }
  443. /// <summary>
  444. /// Sets the contents of the load status indicator label.
  445. /// </summary>
  446. /// <param name="message">The new contents</param>
  447. public void SetToolStripLoadStatus(string message)
  448. {
  449. LoadStatusBox.Text = message;
  450. }
  451. /// <summary>
  452. /// shows the given info message in a popup and asks the user to aknowledge it
  453. /// </summary>
  454. /// <param name="message">the message to show</param>
  455. public void ShowInfoMessage(string message)
  456. {
  457. MessageBox.Show(message);
  458. }
  459. /// <summary>
  460. /// Shows a warning box with the given message (Yes/No Buttons)and returns true if the user aknowledges it.
  461. /// </summary>
  462. /// <param name="message">The message of the warning.</param>
  463. /// <returns>True if the user confirms (Yes), negative if he doesn't (No)</returns>
  464. public bool ShowWarning(string message)
  465. {
  466. MessageBoxResult result = MessageBox.Show(message, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
  467. return (result.Equals(MessageBoxResult.Yes));
  468. }
  469. /// <summary>
  470. /// Updates the colour of a canvas.
  471. /// </summary>
  472. /// <param name="canvasName">The name of the canvas to be updated.</param>
  473. /// <param name="active">Whether or not the canvas is active.</param>
  474. public void SetCanvasState(string canvasName, bool active)
  475. {
  476. Canvas canvas;
  477. InkCanvas inkCanvas;
  478. switch (canvasName)
  479. {
  480. case ("LeftCanvas"):
  481. canvas = LeftCanvas;
  482. if (active)
  483. {
  484. canvas.Background = Brushes.White;
  485. }
  486. else
  487. {
  488. canvas.Background = Brushes.SlateGray;
  489. }
  490. break;
  491. case ("RightCanvas"):
  492. inkCanvas = RightCanvas;
  493. if (active)
  494. {
  495. inkCanvas.Background = Brushes.White;
  496. }
  497. else
  498. {
  499. inkCanvas.Background = Brushes.SlateGray;
  500. }
  501. break;
  502. default:
  503. throw new InvalidOperationException("Unknown canvas name, Check that the canvas passed is either LeftCanvas or RightCanvas");
  504. }
  505. }
  506. /************************/
  507. /*** HELPING FUNCTION ***/
  508. /************************/
  509. /// <summary>
  510. /// A function which simulates canvas input for debugging.
  511. /// </summary>
  512. /// <param name="option"></param>
  513. private async void Debug(int option)
  514. {
  515. Point[] points;
  516. switch (option)
  517. {
  518. case 1:
  519. points = debugDat.debugPoints1;
  520. break;
  521. case 2:
  522. points = debugDat.debugPoints2;
  523. break;
  524. default:
  525. return;
  526. }
  527. dispatcherTimer.Stop();
  528. debugRunning = true;
  529. ProgramPresenter.Tick(); await Task.Delay(10);
  530. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, new Point(50, 50));
  531. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Down); await Task.Delay(10);
  532. for (int x = 0; x < points.Length; x++)
  533. {
  534. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Move, points[x]);
  535. await Task.Delay(1);
  536. if (x % 5 == 0)
  537. {
  538. ProgramPresenter.Tick();
  539. await Task.Delay(1);
  540. }
  541. }
  542. ProgramPresenter.MouseEvent(MVP_Presenter.MouseAction.Up); await Task.Delay(1);
  543. debugRunning = false;
  544. dispatcherTimer.Start();
  545. }
  546. }
  547. }