MainWindow.xaml.cs 14 KB

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