Microscopic Traffic Simulator
GeneratorBuildingRenderer.cs
Go to the documentation of this file.
2 using System;
3 using System.Collections.Generic;
4 using System.Collections.ObjectModel;
5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8 using System.Windows;
9 using System.Windows.Media;
10 
11 namespace Microscopic_Traffic_Simulator.Renderers
12 {
14  {
15  private const double PenThickness = 1.0;
16  private const double BoldPenThickness = 2.0;
17  private const double CircleRadius = 4.0;
18  private const double CircleRadiusSquared = CircleRadius * CircleRadius;
19 
20  private Pen pen = new Pen(Brushes.Blue, PenThickness);
21  private Pen boldPen = new Pen(Brushes.Blue, BoldPenThickness);
22 
23  private ReadOnlyCollection<Lane> lanes;
24  internal ReadOnlyCollection<Lane> Lanes { set { lanes = value; } }
25 
26  private Lane laneWithNearestStartPointToCursor = null;
27 
28  internal GeneratorPossibleLocationsRenderer(DrawingVisual visual)
29  : base(visual)
30  {
31  pen.Freeze();
32  }
33 
34  protected override void Render(Point currentMouseLocation)
35  {
36  RenderPossibleLocations(currentMouseLocation);
37  }
38 
39  internal void MouseMove(Point currentMouseLocation)
40  {
41  Render(currentMouseLocation);
42  }
43 
44  internal Lane MouseLeftButtonUp()
45  {
46  if (laneWithNearestStartPointToCursor == null)
47  {
48  return null;
49  }
50  else
51  {
52  Lane laneWithNearestStartPointToCursorToReturn = laneWithNearestStartPointToCursor;
53  return laneWithNearestStartPointToCursorToReturn;
54  }
55  }
56 
57  internal void Clear()
58  {
59  laneWithNearestStartPointToCursor = null;
60  using (DrawingContext dc = visual.RenderOpen())
61  {
62 
63  }
64  }
65 
66  internal void RenderPossibleLocations(Point? currentMouseLocation = null)
67  {
68  using (DrawingContext dc = visual.RenderOpen())
69  {
70  dynamic laneWithNearestStartPointToCursor = null;
71  foreach (Lane lane in lanes)
72  {
73  if (!lane.StartNode.ContainsGenerator)
74  {
75  Point startPointOnCanvas = TransformRealWorldPoint(lane.StartNode.Location);
76  dc.DrawEllipse(null, pen, startPointOnCanvas, CircleRadius, CircleRadius);
77 
78  if (currentMouseLocation.HasValue)
79  {
80  double distanceFromStartPointToCursorSquared =
81  (startPointOnCanvas - currentMouseLocation.Value).LengthSquared;
82 
83  if (distanceFromStartPointToCursorSquared < CircleRadiusSquared &&
84  (laneWithNearestStartPointToCursor == null ||
85  distanceFromStartPointToCursorSquared <
86  laneWithNearestStartPointToCursor.DistanceToStartPointSquared))
87  {
88  laneWithNearestStartPointToCursor = new
89  {
90  Lane = lane,
91  DistanceToStartPointSquared = distanceFromStartPointToCursorSquared
92  };
93  }
94  }
95  }
96  }
97  if (laneWithNearestStartPointToCursor != null)
98  {
99  dc.DrawEllipse(null, boldPen,
100  TransformRealWorldPoint(laneWithNearestStartPointToCursor.Lane.StartNode.Location),
101  CircleRadius, CircleRadius);
102  this.laneWithNearestStartPointToCursor = laneWithNearestStartPointToCursor.Lane;
103  }
104  }
105  }
106  }
107 }
override void Render(Point currentMouseLocation)
Method defining render method to be implemented for all renderers. Current mouse location
StartNode StartNode
Node on the input end of lane.
Definition: Lane.cs:23
Lane(Point startPoint, Point endPoint)
Lane constructor.
Definition: Lane.cs:48