1 using System;
2 using System.Drawing;
3 using OpenBveApi.Colors;
4 using OpenBveApi.Graphics;
5 using OpenBveApi.Math;
6 using OpenBveApi.Textures;
7 
8 namespace OpenBve.Graphics.Renderers
9 {
10 	internal partial class Overlays
11 	{
12 		/// <summary>Renders the ATS lamp overlay</summary>
13 		/// <param name="Element">The HUD element these are to be rendererd onto</param>
14 		/// <param name="TimeElapsed">The time elapsed</param>
RenderATSLamps(HUD.Element Element, double TimeElapsed)15 		private void RenderATSLamps(HUD.Element Element, double TimeElapsed)
16 		{
17 			if (TrainManager.PlayerTrain == null)
18 			{
19 				return;
20 			}
21 			// ats lamps
22 			if (CurrentLampCollection == null)
23 			{
24 				CurrentLampCollection = new LampCollection(TrainManager.PlayerTrain);
25 			}
26 			double lcrh, lw, rw;
27 			CalculateViewingPlaneSize(Element, out lw, out rw, out lcrh);
28 			// start
29 
30 			// ReSharper disable once PossibleNullReferenceException
31 			int n = CurrentLampCollection.Lamps.Length;
32 			double w = CurrentLampCollection.Width + lw + rw;
33 			double h = Element.Value2 * n;
34 			double x = Element.Alignment.X < 0 ? 0.0 : Element.Alignment.X > 0 ? renderer.Screen.Width - w : 0.5 * (renderer.Screen.Width - w);
35 			double y = Element.Alignment.Y < 0 ? 0.0 : Element.Alignment.Y > 0 ? renderer.Screen.Height - h : 0.5 * (renderer.Screen.Height - h);
36 			x += Element.Position.X;
37 			y += Element.Position.Y;
38 			for (int j = 0; j < n; j++)
39 			{
40 				if (CurrentLampCollection.Lamps[j].Type != LampType.None)
41 				{
42 					int o;
43 					if (j == 0)
44 					{
45 						o = -1;
46 					}
47 					else if (CurrentLampCollection.Lamps[j - 1].Type == LampType.None)
48 					{
49 						o = -1;
50 					}
51 					else if (j < n - 1 && CurrentLampCollection.Lamps[j + 1].Type == LampType.None)
52 					{
53 						o = 1;
54 					}
55 					else if (j == n - 1)
56 					{
57 						o = 1;
58 					}
59 					else
60 					{
61 						o = 0;
62 					}
63 					HUD.Image Left = o < 0 ? Element.TopLeft : o == 0 ? Element.CenterLeft : Element.BottomLeft;
64 					HUD.Image Middle = o < 0 ? Element.TopMiddle : o == 0 ? Element.CenterMiddle : Element.BottomMiddle;
65 					HUD.Image Right = o < 0 ? Element.TopRight : o == 0 ? Element.CenterRight : Element.BottomRight;
66 					MessageColor sc = MessageColor.Gray;
67 					if (TrainManager.PlayerTrain.Plugin.Panel.Length >= 272)
68 					{
69 						switch (CurrentLampCollection.Lamps[j].Type)
70 						{
71 							case LampType.Ats:
72 								if (TrainManager.PlayerTrain.Plugin.Panel[256] != 0)
73 								{
74 									sc = MessageColor.Orange;
75 								}
76 								break;
77 							case LampType.AtsOperation:
78 								if (TrainManager.PlayerTrain.Plugin.Panel[258] != 0)
79 								{
80 									sc = MessageColor.Red;
81 								}
82 								break;
83 							case LampType.AtsPPower:
84 								if (TrainManager.PlayerTrain.Plugin.Panel[259] != 0)
85 								{
86 									sc = MessageColor.Green;
87 								}
88 								break;
89 							case LampType.AtsPPattern:
90 								if (TrainManager.PlayerTrain.Plugin.Panel[260] != 0)
91 								{
92 									sc = MessageColor.Orange;
93 								}
94 								break;
95 							case LampType.AtsPBrakeOverride:
96 								if (TrainManager.PlayerTrain.Plugin.Panel[261] != 0)
97 								{
98 									sc = MessageColor.Orange;
99 								}
100 								break;
101 							case LampType.AtsPBrakeOperation:
102 								if (TrainManager.PlayerTrain.Plugin.Panel[262] != 0)
103 								{
104 									sc = MessageColor.Orange;
105 								}
106 								break;
107 							case LampType.AtsP:
108 								if (TrainManager.PlayerTrain.Plugin.Panel[263] != 0)
109 								{
110 									sc = MessageColor.Green;
111 								}
112 								break;
113 							case LampType.AtsPFailure:
114 								if (TrainManager.PlayerTrain.Plugin.Panel[264] != 0)
115 								{
116 									sc = MessageColor.Red;
117 								}
118 								break;
119 							case LampType.Atc:
120 								if (TrainManager.PlayerTrain.Plugin.Panel[265] != 0)
121 								{
122 									sc = MessageColor.Orange;
123 								}
124 								break;
125 							case LampType.AtcPower:
126 								if (TrainManager.PlayerTrain.Plugin.Panel[266] != 0)
127 								{
128 									sc = MessageColor.Orange;
129 								}
130 								break;
131 							case LampType.AtcUse:
132 								if (TrainManager.PlayerTrain.Plugin.Panel[267] != 0)
133 								{
134 									sc = MessageColor.Orange;
135 								}
136 								break;
137 							case LampType.AtcEmergency:
138 								if (TrainManager.PlayerTrain.Plugin.Panel[268] != 0)
139 								{
140 									sc = MessageColor.Red;
141 								}
142 								break;
143 							case LampType.Eb:
144 								if (TrainManager.PlayerTrain.Plugin.Panel[270] != 0)
145 								{
146 									sc = MessageColor.Green;
147 								}
148 								break;
149 							case LampType.ConstSpeed:
150 								if (TrainManager.PlayerTrain.Plugin.Panel[269] != 0)
151 								{
152 									sc = MessageColor.Orange;
153 								}
154 								break;
155 						}
156 					}
157 					// colors
158 					Color128 bc = Element.BackgroundColor.CreateBackColor(sc, 1.0f);
159 					Color128 tc = Element.TextColor.CreateTextColor(sc, 1.0f);
160 					Color128 oc = Element.OverlayColor.CreateBackColor(sc, 1.0f);
161 					// left background
162 					if (Left.BackgroundTexture != null)
163 					{
164 						if (Program.CurrentHost.LoadTexture(ref Left.BackgroundTexture, OpenGlTextureWrapMode.ClampClamp))
165 						{
166 							double u = Left.BackgroundTexture.Width;
167 							double v = Left.BackgroundTexture.Height;
168 							renderer.Rectangle.Draw(Left.BackgroundTexture, new Vector2(x, y), new Vector2(u, v), bc);
169 						}
170 					}
171 					// right background
172 					if (Right.BackgroundTexture != null)
173 					{
174 						if (Program.CurrentHost.LoadTexture(ref Right.BackgroundTexture, OpenGlTextureWrapMode.ClampClamp))
175 						{
176 							double u = Right.BackgroundTexture.Width;
177 							double v = Right.BackgroundTexture.Height;
178 							renderer.Rectangle.Draw(Right.BackgroundTexture, new Vector2(x + w - u, y), new Vector2(u, v), bc);
179 						}
180 					}
181 					// middle background
182 					if (Middle.BackgroundTexture != null)
183 					{
184 						if (Program.CurrentHost.LoadTexture(ref Middle.BackgroundTexture, OpenGlTextureWrapMode.ClampClamp))
185 						{
186 							double v = Middle.BackgroundTexture.Height;
187 							renderer.Rectangle.Draw(Middle.BackgroundTexture, new Vector2(x + lw, y), new Vector2(w - lw - rw, v), bc);
188 						}
189 					}
190 					{ // text
191 						string t = CurrentLampCollection.Lamps[j].Text;
192 						double u = CurrentLampCollection.Lamps[j].Width;
193 						double v = CurrentLampCollection.Lamps[j].Height;
194 						double p = Math.Round(Element.TextAlignment.X < 0 ? x : Element.TextAlignment.X > 0 ? x + w - u : x + 0.5 * (w - u));
195 						double q = Math.Round(Element.TextAlignment.Y < 0 ? y : Element.TextAlignment.Y > 0 ? y + lcrh - v : y + 0.5 * (lcrh - v));
196 						p += Element.TextPosition.X;
197 						q += Element.TextPosition.Y;
198 						renderer.OpenGlString.Draw(Element.Font, t, new Vector2(p, q), TextAlignment.TopLeft, tc, Element.TextShadow);
199 					}
200 					// left overlay
201 					if (Left.OverlayTexture != null)
202 					{
203 						if (Program.CurrentHost.LoadTexture(ref Left.OverlayTexture, OpenGlTextureWrapMode.ClampClamp))
204 						{
205 							double u = Left.OverlayTexture.Width;
206 							double v = Left.OverlayTexture.Height;
207 							renderer.Rectangle.Draw(Left.OverlayTexture, new Vector2(x, y), new Vector2(u, v), oc);
208 						}
209 					}
210 					// right overlay
211 					if (Right.OverlayTexture != null)
212 					{
213 						if (Program.CurrentHost.LoadTexture(ref Right.OverlayTexture, OpenGlTextureWrapMode.ClampClamp))
214 						{
215 							double u = Right.OverlayTexture.Width;
216 							double v = Right.OverlayTexture.Height;
217 							renderer.Rectangle.Draw(Right.OverlayTexture, new Vector2(x + w - u, y), new Vector2(u, v), oc);
218 						}
219 					}
220 					// middle overlay
221 					if (Middle.OverlayTexture != null)
222 					{
223 						if (Program.CurrentHost.LoadTexture(ref Middle.OverlayTexture, OpenGlTextureWrapMode.ClampClamp))
224 						{
225 							double v = Middle.OverlayTexture.Height;
226 							renderer.Rectangle.Draw(Middle.OverlayTexture, new Vector2(x + lw, y), new Vector2(w - lw - rw, v), oc);
227 						}
228 					}
229 				}
230 				y += Element.Value2;
231 			}
232 		}
233 	}
234 }
235