Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

CanvasUserControl.xaml.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator.Renderers;
2 using Microscopic_Traffic_Simulator.ViewModels;
3 using Microscopic_Traffic_Simulator___Model.GeometricObjects.Lanes;
4 using System.Collections.Generic;
5 using System.ComponentModel;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Input;
9
10 namespace Microscopic_Traffic_Simulator.Views.MainWindowView
11 {
15 public partial class CanvasUserControl : UserControl
16 {
20 private TopologyRenderer topologyRenderer;
24 internal TopologyRenderer TopologyRenderer { get { return topologyRenderer; } }
25
29 private SimulationTrafficRenderer simulationTrafficRenderer;
33 internal SimulationTrafficRenderer SimulationTrafficRenderer { get { return simulationTrafficRenderer; } }
34
38 private LaneBuildingRenderer straightLaneBuildingRenderer;
42 internal LaneBuildingRenderer StraightLaneBuildingRenderer
43 {
44 get { return straightLaneBuildingRenderer; }
45 }
46
50 private BezierLaneBuildingRenderer bezierLaneBuildingRenderer;
54 internal BezierLaneBuildingRenderer BezierLaneBuildingRenderer { get { return bezierLaneBuildingRenderer; } }
55
59 private CanvasViewModel CanvasViewModel { get { return DataContext as CanvasViewModel; } }
60
64 private CanvasMover mover;
68 internal CanvasMover Mover { get { return mover; } }
69
73 private CanvasZoomer zoomer;
77 internal CanvasZoomer Zoomer { get { return zoomer; } }
78
82 private CanvasRoadPreviewer roadPreviewer;
86 internal CanvasRoadPreviewer RoadPreviewer { get { return roadPreviewer; } }
87
91 private GeneratorPossibleLocationsRenderer generatorPossibleLocationsRenderer;
95 internal GeneratorPossibleLocationsRenderer GeneratorPossibleLocationsRenderer
96 {
97 get { return generatorPossibleLocationsRenderer; }
98 }
99
103 public CanvasUserControl()
104 {
105 InitializeComponent();
106 List<VisualCanvasRenderer> renderers = new List<VisualCanvasRenderer>();
107 topologyRenderer = new TopologyRenderer(topologyCanvas.CreateNewVisual());
108 renderers.Add(topologyRenderer);
109 simulationTrafficRenderer = new SimulationTrafficRenderer(topologyCanvas.CreateNewVisual());
110 renderers.Add(simulationTrafficRenderer);
111 straightLaneBuildingRenderer = new LaneBuildingRenderer(topologyCanvas.CreateNewVisual());
112 renderers.Add(straightLaneBuildingRenderer);
113 bezierLaneBuildingRenderer = new BezierLaneBuildingRenderer(topologyCanvas.CreateNewVisual());
114 renderers.Add(bezierLaneBuildingRenderer);
115 generatorPossibleLocationsRenderer = new GeneratorPossibleLocationsRenderer(
116 topologyCanvas.CreateNewVisual());
117 renderers.Add(generatorPossibleLocationsRenderer);
118 mover = new CanvasMover(renderers);
119 zoomer = new CanvasZoomer(renderers);
120 roadPreviewer = new CanvasRoadPreviewer(new Dictionary<LaneType, LaneBuildingRenderer>
121 {
122 { LaneType.Straight, straightLaneBuildingRenderer },
123 { LaneType.Bezier, bezierLaneBuildingRenderer }
124 }, topologyRenderer);
125 }
126
132 private void UserControl_Loaded(object sender, RoutedEventArgs e)
133 {
134 if (!DesignerProperties.GetIsInDesignMode(this))
135 {
136 roadPreviewer.SetCanvasViewModel(CanvasViewModel);
137 roadPreviewer.LaneTypeWasChanged();
138 generatorPossibleLocationsRenderer.Lanes = CanvasViewModel.GeometricTopology.Lanes;
139 Window.GetWindow(this).KeyDown += CanvasUserControl_KeyDown;
140 topologyRenderer.GeometricTopology = CanvasViewModel.GeometricTopology;
141 topologyRenderer.RenderTopology();
142 }
143 }
144
150 private void CanvasUserControl_KeyDown(object sender, KeyEventArgs e)
151 {
152 if (e.Key == Key.Escape)
153 {
154 roadPreviewer.ClearPreviewOfBuildingOfAnyRoads();
155 }
156 }
157
163 private void topologyCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
164 {
165 if (CanvasViewModel.BuildingMode == BuildingMode.Lane)
166 {
167 roadPreviewer.MouseLeftButtonUp(e.GetPosition(topologyCanvas));
168 }
169 else if (CanvasViewModel.BuildingMode == BuildingMode.Generator)
170 {
171 Lane laneWhereGeneratorToAdd = generatorPossibleLocationsRenderer.MouseLeftButtonUp();
172 if (laneWhereGeneratorToAdd != null)
173 {
174 CanvasViewModel.CreateGeneratorForLane(laneWhereGeneratorToAdd);
175 topologyRenderer.RenderTopology();
176 generatorPossibleLocationsRenderer.MouseMove(e.GetPosition(topologyCanvas));
177 }
178 }
179 }
180
186 private void topologyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
187 {
188 if (CanvasViewModel.BuildingMode == BuildingMode.Lane)
189 {
190 roadPreviewer.MouseLeftButtonDown(e.GetPosition(topologyCanvas));
191 }
192 }
193
199 private void topologyCanvas_MouseMove(object sender, MouseEventArgs e)
200 {
201 Point currentMouseLocationOnCanvas = e.GetPosition(topologyCanvas);
202
203 if (e.RightButton == MouseButtonState.Pressed)
204 {
205 mover.SetLastMousePositionToRenderersForMovingCanvas(currentMouseLocationOnCanvas);
206 }
207 else if (e.LeftButton == MouseButtonState.Pressed)
208 {
209 if (CanvasViewModel.BuildingMode == BuildingMode.Lane)
210 {
211 roadPreviewer.MovePointOfLaneIfCursorIsNear(currentMouseLocationOnCanvas);
212 }
213 }
214 else
215 {
216 mover.ResetLastMousePositionToRenderersForMovingCanvas();
217 if (CanvasViewModel.BuildingMode == BuildingMode.Lane)
218 {
219 roadPreviewer.RenderBuildingLane(currentMouseLocationOnCanvas);
220 }
221 else if (CanvasViewModel.BuildingMode == BuildingMode.Generator)
222 {
223 generatorPossibleLocationsRenderer.MouseMove(currentMouseLocationOnCanvas);
224 }
225 }
226 }
227
233 private void topologyCanvas_MouseWheel(object sender, MouseWheelEventArgs e)
234 {
235 Slider zoomSlider = (Window.GetWindow(this) as MainWindow).bottomPanelUserControl.zoomSlider;
236 zoomer.SetCurrentCursorPositionAsZoomPoint(e.GetPosition(topologyCanvas));
237 if (e.Delta > 0)
238 zoomSlider.Value++;
239 else
240 zoomSlider.Value--;
241 }
242
248 private void topologyCanvas_KeyUp(object sender, KeyEventArgs e)
249 {
250 if (e.Key == Key.Escape)
251 {
252 roadPreviewer.ClearPreviewOfBuildingOfAnyRoads();
253 }
254 }
255
261 private void topologyCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
262 {
263 mover.SetLastMousePositionToRenderersForMovingCanvas(e.GetPosition(topologyCanvas));
264 }
265 }
266 }
Microscopic_Traffic_Simulator.Renderers.TopologyRenderer
Geometric topology renderer.
Definition: TopologyRenderer.cs:12
Microscopic_Traffic_Simulator.Renderers.BezierLaneBuildingRenderer
Renderer for drawing bezier lanes during their building.
Definition: BezierLaneBuildingRenderer.cs:12
Microscopic_Traffic_Simulator.Views.MainWindowView.CanvasRoadPreviewer
Previewer of building roads on canvas.
Definition: CanvasRoadPreviewer.cs:11
System
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.CanvasViewModel
Viewmodel for canvas panel.
Definition: CanvasViewModel.cs:13
Microscopic_Traffic_Simulator.Views.MainWindowView.CanvasMover
Actions related to moving with canvas.
Definition: CanvasMover.cs:14
Microscopic_Traffic_Simulator___Model.GeometricObjects.Lanes.Lane
Represent single lane.
Definition: Lane.cs:14
Microscopic_Traffic_Simulator.Renderers.LaneBuildingRenderer
Renderer for drawing lanes during their building.
Definition: LaneBuildingRenderer.cs:12
Microscopic_Traffic_Simulator
Definition: App.xaml.cs:12
Microscopic_Traffic_Simulator.Renderers
Definition: BezierCurveGeometryCreator.cs:5
Microscopic_Traffic_Simulator.Views.MainWindowView.CanvasZoomer
Contains actions related to zooming.
Definition: CanvasZoomer.cs:10
Microscopic_Traffic_Simulator.Renderers.GeneratorPossibleLocationsRenderer
Definition: GeneratorBuildingRenderer.cs:13
Microscopic_Traffic_Simulator___Model.GeometricObjects.Lanes
Definition: BezierLane.cs:5
Microscopic_Traffic_Simulator___Model.GeometricObjects
Definition: GeometricTopology.cs:8
Microscopic_Traffic_Simulator___Model
Microscopic_Traffic_Simulator.Views.MainWindowView.CanvasUserControl.CanvasUserControl
CanvasUserControl()
Initialize canvas control.
Definition: CanvasUserControl.xaml.cs:103