MainWindow.xaml.cs 22 KB

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