Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

SimulationControlUserControl.xaml.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator.Renderers;
2 using Microscopic_Traffic_Simulator.ViewModels;
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
29 public SimulationControlUserControl()
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;
54 (viewModel.ForwardCommand as ObservableRelayCommand)
55 .CommandExecuted += SimulationControlUserControl_StartTimer;
56 (viewModel.StopCommand as ObservableRelayCommand)
57 .CommandExecuted += SimulationControlUserControl_StopCommandExecuted;
58 (viewModel.PauseCommand as ObservableRelayCommand)
59 .CommandExecuted += SimulationControlUserControl_PauseCommandExecuted;
60 (viewModel.SwitchToConstructionModeCommand as ObservableRelayCommand)
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 {
78 (viewModel.ForwardCommand as ObservableRelayCommand)
79 .CommandExecuted -= SimulationControlUserControl_StartTimer;
80 (viewModel.StopCommand as ObservableRelayCommand)
81 .CommandExecuted -= SimulationControlUserControl_StopCommandExecuted;
82 (viewModel.PauseCommand as ObservableRelayCommand)
83 .CommandExecuted -= SimulationControlUserControl_PauseCommandExecuted;
84 (viewModel.SwitchToConstructionModeCommand as ObservableRelayCommand)
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 }
Microscopic_Traffic_Simulator.ViewModels.SimulationControlViewModel.SwitchToConstructionModeCommand
ICommand SwitchToConstructionModeCommand
Command to switch to construction mode.
Definition: SimulationControlViewModel.cs:238
Microscopic_Traffic_Simulator.ViewModels.SimulationControlViewModel.RestartCommand
ICommand RestartCommand
Command for restarting simulation.
Definition: SimulationControlViewModel.cs:405
System
Microscopic_Traffic_Simulator.Views.MainWindowView.SimulationControlUserControl.SimulationControlUserControl
SimulationControlUserControl()
Initialize control.
Definition: SimulationControlUserControl.xaml.cs:29
Microscopic_Traffic_Simulator.Renderers.SimulationTrafficRenderer
Simulation traffic renderer.
Definition: SimulationTrafficRenderer.cs:22
Microscopic_Traffic_Simulator.ViewModels
Definition: CanvasViewModel.cs:8
Microscopic_Traffic_Simulator.Views.MainWindowView.MainWindow
MainWindow
Definition: MainWindow.g.cs:43
Microscopic_Traffic_Simulator.ViewModels.SimulationControlViewModel.ForwardCommand
ICommand ForwardCommand
Command to run simulation.
Definition: SimulationControlViewModel.cs:258
Microscopic_Traffic_Simulator.ViewModels.SimulationControlViewModel.StopCommand
ICommand StopCommand
Command to stop simulation.
Definition: SimulationControlViewModel.cs:296
Microscopic_Traffic_Simulator.ViewModels.SimulationControlViewModel.PauseCommand
ICommand PauseCommand
Command to pause simulation.
Definition: SimulationControlViewModel.cs:315
Microscopic_Traffic_Simulator
Definition: App.xaml.cs:12
Microscopic_Traffic_Simulator.ObservableRelayCommand
Extended relay command class by adding event after executing command. Useful for performing additiona...
Definition: ObservableRelayCommand.cs:9
Microscopic_Traffic_Simulator.Renderers
Definition: BezierCurveGeometryCreator.cs:5
Microscopic_Traffic_Simulator.Renderers.Rendering
Rendering
Enumeration type containing values representing rendering of one single simulation step and rendering...
Definition: SimulationTrafficRenderer.cs:17
Microscopic_Traffic_Simulator.ViewModels.SimulationControlViewModel
Viewmodel for simulation control.
Definition: SimulationControlViewModel.cs:14