MainWindow.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using MultiAgentSystemPCL;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. using System.Windows.Shapes;
  9. using System.Windows.Threading;
  10. namespace WasteSorting
  11. {
  12. public partial class MainWindow : Window
  13. {
  14. WasteEnvironment environment;
  15. bool play = false;
  16. DispatcherTimer updateTimer;
  17. public MainWindow()
  18. {
  19. InitializeComponent();
  20. Loaded += MainWindow_Loaded;
  21. environmentCanvas.MouseDown += environmentCanvas_MouseDown;
  22. updateTimer = new DispatcherTimer();
  23. updateTimer.Tick += dispatcherTimer_Tick;
  24. updateTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
  25. }
  26. void MainWindow_Loaded(object _sender, RoutedEventArgs _e)
  27. {
  28. environment = new WasteEnvironment(50, 30, environmentCanvas.ActualWidth, environmentCanvas.ActualHeight, 3);
  29. environment.environmentUpdatedEvent += environment_environmentUpdatedEvent;
  30. environment.Update();
  31. }
  32. void environmentCanvas_MouseDown(object _sender, MouseButtonEventArgs _mouseEvent)
  33. {
  34. if (play)
  35. {
  36. updateTimer.Stop();
  37. }
  38. else
  39. {
  40. updateTimer.Start();
  41. }
  42. play = !play;
  43. }
  44. void environment_environmentUpdatedEvent(List<Waste> _wasteList, List<WasteAgent> _agentList)
  45. {
  46. int nbWaste = _wasteList.Count();
  47. int nbAgentsLoaded = _agentList.Where(x => x.isLoaded()).Count();
  48. environmentCanvas.Children.Clear();
  49. foreach (Waste waste in _wasteList)
  50. {
  51. DrawWaste(waste);
  52. }
  53. foreach (WasteAgent agent in _agentList)
  54. {
  55. DrawAgent(agent);
  56. }
  57. Console.Out.WriteLine(nbWaste + " - " + nbAgentsLoaded);
  58. if (nbWaste == 3 && nbAgentsLoaded == 0)
  59. {
  60. updateTimer.Stop();
  61. }
  62. }
  63. private void DrawAgent(WasteAgent _agent)
  64. {
  65. Rectangle rect = new Rectangle();
  66. rect.Width = 3;
  67. rect.Height = 3;
  68. rect.Margin = new Thickness(_agent.PosX - 1, _agent.PosY - 1, 0, 0);
  69. if (_agent.isLoaded())
  70. {
  71. rect.Stroke = Brushes.Gray;
  72. rect.Fill = Brushes.Gray;
  73. }
  74. else
  75. {
  76. rect.Stroke = Brushes.Black;
  77. rect.Fill = Brushes.Black;
  78. }
  79. environmentCanvas.Children.Add(rect);
  80. }
  81. private void DrawWaste(Waste _waste)
  82. {
  83. Rectangle rect = new Rectangle();
  84. rect.Width = 3;
  85. rect.Height = 3;
  86. rect.Margin = new Thickness(_waste.PosX - 1, _waste.PosY - 1, 0, 0);
  87. Brush strokeAndFill = Brushes.Transparent;
  88. switch (_waste.Type)
  89. {
  90. case 0:
  91. strokeAndFill = Brushes.Red;
  92. break;
  93. case 1:
  94. strokeAndFill = Brushes.Blue;
  95. break;
  96. case 2:
  97. strokeAndFill = Brushes.ForestGreen;
  98. break;
  99. }
  100. rect.Stroke = strokeAndFill;
  101. rect.Fill = strokeAndFill;
  102. environmentCanvas.Children.Add(rect);
  103. Ellipse zone = new Ellipse();
  104. zone.Width = 2 * _waste.Zone;
  105. zone.Height = 2 * _waste.Zone;
  106. zone.Margin = new Thickness(_waste.PosX - _waste.Zone, _waste.PosY - _waste.Zone, 0, 0);
  107. zone.Fill = strokeAndFill;
  108. zone.Opacity = 0.3;
  109. environmentCanvas.Children.Add(zone);
  110. }
  111. void dispatcherTimer_Tick(object _sender, EventArgs _e)
  112. {
  113. environment.Update();
  114. }
  115. }
  116. }