MainWindow.xaml.cs 21 KB

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