Microscopic Traffic Simulator
SimulationControlUserControl.xaml.cs
Go to the documentation of this file.
3 using System;
4 using System.Windows;
5 using System.Windows.Controls;
6 using System.Windows.Input;
7 using System.Windows.Threading;
8 
9 namespace Microscopic_Traffic_Simulator.Views.MainWindowView
10 {
14  public partial class SimulationControlUserControl : UserControl
15  {
19  private DispatcherTimer timer;
20 
24  private SimulationControlViewModel viewModel;
25 
30  {
31  InitializeComponent();
32  }
33 
39  private void timer_Tick(object sender, EventArgs e)
40  {
41  (Window.GetWindow(this) as MainWindow).canvasUserControl
42  .SimulationTrafficRenderer.RenderSimulationTraffic(Rendering.Continuous);
43  }
44 
51  private void UserControl_Loaded(object sender, RoutedEventArgs e)
52  {
53  viewModel = DataContext as SimulationControlViewModel;
55  .CommandExecuted += SimulationControlUserControl_StartTimer;
56  (viewModel.StopCommand as ObservableRelayCommand)
57  .CommandExecuted += SimulationControlUserControl_StopCommandExecuted;
59  .CommandExecuted += SimulationControlUserControl_PauseCommandExecuted;
61  .CommandExecuted += SimulationControlUserControl_StopCommandExecuted;
62  (viewModel.RestartCommand as ObservableRelayCommand).CommandExecuted +=
63  SimulationControlUserControl_RestartCommandExecuted;
64  viewModel.CellularTopology.Simulation.SimulationPaused += Simulation_SimulationPaused;
65  timer = new DispatcherTimer(DispatcherPriority.Render);
66  timer.Tick += timer_Tick;
67  timer.Interval = TimeSpan.FromMilliseconds(1000.0 / sliderGraphicsTimer.Value);
68  }
69 
76  private void UserControl_Unloaded(object sender, RoutedEventArgs e)
77  {
79  .CommandExecuted -= SimulationControlUserControl_StartTimer;
80  (viewModel.StopCommand as ObservableRelayCommand)
81  .CommandExecuted -= SimulationControlUserControl_StopCommandExecuted;
83  .CommandExecuted -= SimulationControlUserControl_PauseCommandExecuted;
85  .CommandExecuted -= SimulationControlUserControl_StopCommandExecuted;
86  (viewModel.RestartCommand as ObservableRelayCommand).CommandExecuted -=
87  SimulationControlUserControl_RestartCommandExecuted;
88  viewModel.CellularTopology.Simulation.SimulationPaused -= Simulation_SimulationPaused;
89  timer.Tick -= timer_Tick;
90  }
91 
97  private void Simulation_SimulationPaused(object sender, EventArgs e)
98  {
99  timer.Stop();
100  Application.Current.Dispatcher.Invoke(() => CommandManager.InvalidateRequerySuggested(),
101  DispatcherPriority.Render);
102  }
103 
110  private void SimulationControlUserControl_StartTimer(object sender, EventArgs e)
111  {
112  timer.Start();
113  }
114 
122  private void SimulationControlUserControl_PauseCommandExecuted(object sender, EventArgs e)
123  {
124  timer.Stop();
125  (Window.GetWindow(this) as MainWindow).canvasUserControl.SimulationTrafficRenderer.Pause();
126  }
127 
133  private void SimulationControlUserControl_StopCommandExecuted(object sender, EventArgs e)
134  {
135  timer.Stop();
136  (Window.GetWindow(this) as MainWindow).canvasUserControl.SimulationTrafficRenderer.Stop();
137  }
138 
144  private void sliderGraphicsTimer_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
145  {
146  if (timer != null)
147  {
148  timer.Stop();
149  timer.Interval = TimeSpan.FromMilliseconds(1000.0 / sliderGraphicsTimer.Value);
150  timer.Start();
151  }
152  }
153 
159  private void ApplyAlarmButton_Click(object sender, RoutedEventArgs e)
160  {
161  btnAlarm.IsChecked = false;
162  }
163 
170  private void SwitchToConstructionModeButton_Click(object sender, RoutedEventArgs e)
171  {
172  (Window.GetWindow(this) as MainWindow).canvasUserControl.SimulationTrafficRenderer
173  .DetachEventHandlerFromCellularTopology();
174  }
175 
181  private void ForwardButton_Click(object sender, RoutedEventArgs e)
182  {
183  (Window.GetWindow(this) as MainWindow).canvasUserControl.SimulationTrafficRenderer.Run();
184  }
185 
191  private void ForwardStepButton_Click(object sender, RoutedEventArgs e)
192  {
193  (Window.GetWindow(this) as MainWindow).canvasUserControl.SimulationTrafficRenderer.StepForward();
194  }
195 
201  private void SimulationControlUserControl_RestartCommandExecuted(object sender, EventArgs e)
202  {
203  SimulationTrafficRenderer renderer =
204  (Window.GetWindow(this) as MainWindow).canvasUserControl.SimulationTrafficRenderer;
205  renderer.Stop();
206  renderer.Run();
207  if (!timer.IsEnabled)
208  {
209  timer.Start();
210  }
211  }
212  }
213 }
ICommand SwitchToConstructionModeCommand
Command to switch to construction mode.
Extended relay command class by adding event after executing command. Useful for performing additiona...
Rendering
Enumeration type containing values representing rendering of one single simulation step and rendering...