-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrafficLight.cs
171 lines (147 loc) · 5.31 KB
/
TrafficLight.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Diagnostics;
using System.Linq;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using static Crossroad.RoadSizes;
namespace Crossroad
{
public class TrafficLight
{
public TrafficLight()
{
SetView();
}
public static int TLTime { set; get; } = TRAFFIC_LIGHT_DEF_TIME;
public Grid TLight { set; get; } = new();
public Color PrevLight { set; get; } = Colors.Green;
public static int TimeFromLasReset { set; get; } = 0;
private Color currentLight;
public Color CurrentLight {
get
{
return currentLight;
}
set
{
currentLight = value;
int index = 0;
switch (value.ToString())
{
case "#FFFF0000": index = 0; break;
case "#FFFFFF00": index = 1; break;
case "#FF008000": index = 2; break;
}
if (index!=1) PrevLight = value;
Application.Current.Dispatcher.Invoke(() =>
{
for (int i = 0; i < 3; i++)
{
TLight.Children.Cast<Ellipse>().ElementAt(i).Fill = new SolidColorBrush(Colors.Gray);
}
TLight.Children.Cast<Ellipse>().ElementAt(index).Fill = new SolidColorBrush(value);
});
}
}
public static Timer SwapTimer { set; get; } = new();
public static Timer StartTimer { set; get; } = new();
public static Timer YellowTimer { set; get; } = new();
public void SwapColors()
{
if (PrevLight == Colors.Green)
CurrentLight = Colors.Red;
else CurrentLight = Colors.Green;
}
public void SetView()
{
TLight = new Grid()
{
Width = LIGHT_SIZE,
Background = new SolidColorBrush(Colors.DarkGray),
Margin = new Thickness(0, 10, 0, 0),
};
for(int i=0; i<TIMERS_COUNT; i++)
{
var gridRow = new RowDefinition()
{
Height = new GridLength(LIGHT_SIZE)
};
TLight.RowDefinitions.Add(gridRow);
var circle = new Ellipse()
{
Width = LIGHT_SIZE * LIGHT_SIZE_COEF,
Height = LIGHT_SIZE * LIGHT_SIZE_COEF,
Fill = new SolidColorBrush(Colors.Gray),
};
Grid.SetRow(circle, i);
TLight.Children.Add(circle);
}
}
public static int GetRemainigTime()
{
return TLTime - YELLOW_TIME - (((int)DateTime.Now.Ticks) - TimeFromLasReset) / 10000;
}
public static void BuildLightMode()
{
var road = Road.GetRoad();
if (SwapTimer.Enabled)
{
StartTimer.Stop();
SwapTimer.Stop();
YellowTimer.Stop();
}
for (int i = 0; i < ROADS_COUNT; i++)
road.LightsSet[i].CurrentLight = Colors.Yellow;
road.Axis = Axes.Undef;
SwapTimer.Interval = TLTime;
YellowTimer.Interval = TLTime;
if (!road.Reset)
{
SwapTimer.Elapsed += (s, e) =>
{
Application.Current.Dispatcher.Invoke(() =>
{
TimeFromLasReset = (int)DateTime.Now.Ticks;
for (int i = 0; i < ROADS_COUNT; i++)
{
road.LightsSet[i].SwapColors();
}
if (road.LightsSet[0].CurrentLight == Colors.Green)
road.Axis = Axes.Vertical;
else road.Axis = Axes.Horizontal;
});
};
StartTimer.Interval = YELLOW_TIME;
StartTimer.Elapsed += (s, e) =>
{
Application.Current.Dispatcher.Invoke(() =>
{
TimeFromLasReset = (int)DateTime.Now.Ticks;
for (int i = 0; i < ROADS_COUNT; i++)
{
if (i % 2 == 0) road.LightsSet[i].CurrentLight = Colors.Red;
else road.LightsSet[i].CurrentLight = Colors.Green;
}
road.Axis = Axes.Horizontal;
SwapTimer.Start();
StartTimer.Stop();
});
};
YellowTimer.Elapsed += (s, e) =>
{
Application.Current.Dispatcher.Invoke(() =>
{
for (int i = 0; i < ROADS_COUNT; i++)
road.LightsSet[i].CurrentLight = Colors.Yellow;
road.Axis = Axes.Undef;
});
};
}
YellowTimer.Start();
StartTimer.Start();
}
}
}