Skip to content

Commit d6438b0

Browse files
authored
Merge pull request #449 from kvakvs/446-show-unlimited-over-140kmph
Speed limits: Unlimited speed display fix; Unlimited default speed fix
2 parents ef4fc55 + 541ae96 commit d6438b0

File tree

2 files changed

+79
-52
lines changed

2 files changed

+79
-52
lines changed

TLM/TLM/Traffic/Data/SpeedLimit.cs

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -122,44 +122,73 @@ public static ushort ToKmphPrecise(float speed) {
122122
return (ushort)Mathf.Round(speed * SPEED_TO_KMPH);
123123
}
124124

125-
/// <summary>
126-
/// Based on the MPH/KMPH settings round the current speed to the nearest STEP and
127-
/// then decrease by STEP.
128-
/// </summary>
129-
/// <param name="speed">Ingame speed</param>
130-
/// <returns>Ingame speed decreased by the increment for MPH or KMPH</returns>
131-
public static float GetPrevious(float speed) {
132-
if (speed < 0f) {
133-
return -1f;
134-
}
135-
if (GlobalConfig.Instance.Main.DisplaySpeedLimitsMph) {
136-
var rounded = ToMphRounded(speed);
137-
return (rounded > LOWER_MPH ? rounded - MPH_STEP : LOWER_MPH) / SPEED_TO_MPH;
138-
} else {
139-
var rounded = ToKmphRounded(speed);
140-
return (rounded > LOWER_KMPH ? rounded - KMPH_STEP : LOWER_KMPH) / SPEED_TO_KMPH;
141-
}
142-
}
143-
144-
/// <summary>
145-
/// Based on the MPH/KMPH settings round the current speed to the nearest STEP and
146-
/// then increase by STEP.
147-
/// </summary>
148-
/// <param name="speed">Ingame speed</param>
149-
/// <returns>Ingame speed increased by the increment for MPH or KMPH</returns>
150-
public static float GetNext(float speed) {
151-
if (speed < 0f) {
152-
return -1f;
153-
}
154-
if (GlobalConfig.Instance.Main.DisplaySpeedLimitsMph) {
155-
var rounded = ToMphRounded(speed);
156-
return (rounded < UPPER_MPH ? rounded + MPH_STEP : UPPER_MPH) / SPEED_TO_MPH;
157-
}
158-
else {
159-
var rounded = ToKmphRounded(speed);
160-
return (rounded < UPPER_KMPH ? rounded + KMPH_STEP : UPPER_KMPH) / SPEED_TO_KMPH;
161-
}
162-
}
125+
/// <summary>
126+
/// Based on the MPH/KMPH settings round the current speed to the nearest STEP and
127+
/// then decrease by STEP.
128+
/// </summary>
129+
/// <param name="speed">Ingame speed</param>
130+
/// <returns>Ingame speed decreased by the increment for MPH or KMPH</returns>
131+
public static float GetPrevious(float speed) {
132+
if (speed < 0f) {
133+
return -1f;
134+
}
135+
136+
if (GlobalConfig.Instance.Main.DisplaySpeedLimitsMph) {
137+
ushort rounded = ToMphRounded(speed);
138+
if (rounded == LOWER_MPH) {
139+
return 0;
140+
}
141+
142+
if (rounded == 0) {
143+
return UPPER_MPH / SPEED_TO_MPH;
144+
}
145+
146+
return (rounded > LOWER_MPH ? rounded - MPH_STEP : LOWER_MPH) / SPEED_TO_MPH;
147+
} else {
148+
ushort rounded = ToKmphRounded(speed);
149+
if (rounded == LOWER_KMPH) {
150+
return 0;
151+
}
152+
153+
if (rounded == 0) {
154+
return UPPER_KMPH / SPEED_TO_KMPH;
155+
}
156+
157+
return (rounded > LOWER_KMPH ? rounded - KMPH_STEP : LOWER_KMPH) / SPEED_TO_KMPH;
158+
}
159+
}
160+
161+
/// <summary>
162+
/// Based on the MPH/KMPH settings round the current speed to the nearest STEP and
163+
/// then increase by STEP.
164+
/// </summary>
165+
/// <param name="speed">Ingame speed</param>
166+
/// <returns>Ingame speed increased by the increment for MPH or KMPH</returns>
167+
public static float GetNext(float speed) {
168+
if (speed < 0f) {
169+
return -1f;
170+
}
171+
172+
if (GlobalConfig.Instance.Main.DisplaySpeedLimitsMph) {
173+
ushort rounded = ToMphRounded(speed);
174+
rounded += MPH_STEP;
175+
176+
if (rounded > UPPER_MPH) {
177+
rounded = 0;
178+
}
179+
180+
return rounded / SPEED_TO_MPH;
181+
} else {
182+
ushort rounded = ToKmphRounded(speed);
183+
rounded += KMPH_STEP;
184+
185+
if (rounded > UPPER_KMPH) {
186+
rounded = 0;
187+
}
188+
189+
return rounded / SPEED_TO_KMPH;
190+
}
191+
}
163192

164193
/// <summary>
165194
/// For US signs and MPH enabled, scale textures vertically by 1.25f.

TLM/TLM/UI/TextureResources.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ public static Texture2D GetSpeedLimitTexture(float speedLimit) {
241241
/// <returns></returns>
242242
public static Texture2D GetSpeedLimitTexture(float speedLimit, MphSignStyle mphStyle, SpeedUnit unit) {
243243
// Select the source for the textures based on unit and the theme
244-
var mph = unit == SpeedUnit.Mph;
245-
var textures = SpeedLimitTexturesKmph;
244+
bool mph = unit == SpeedUnit.Mph;
245+
IDictionary<int, Texture2D> textures = SpeedLimitTexturesKmph;
246246
if (mph) {
247247
switch (mphStyle) {
248248
case MphSignStyle.SquareUS:
@@ -257,22 +257,20 @@ public static Texture2D GetSpeedLimitTexture(float speedLimit, MphSignStyle mphS
257257
}
258258
}
259259

260-
// Trim the range
261-
if (speedLimit > SpeedLimitManager.MAX_SPEED * 0.95f) {
262-
return textures[0];
263-
}
264-
265260
// Round to nearest 5 MPH or nearest 10 km/h
266-
var index = mph ? SpeedLimit.ToMphRounded(speedLimit) : SpeedLimit.ToKmphRounded(speedLimit);
261+
ushort index = mph ? SpeedLimit.ToMphRounded(speedLimit) : SpeedLimit.ToKmphRounded(speedLimit);
267262

268263
// Trim the index since 140 km/h / 90 MPH is the max sign we have
269-
var upper = mph ? SpeedLimit.UPPER_MPH : SpeedLimit.UPPER_KMPH;
270-
#if DEBUG
271-
if (index > upper) {
272-
Log.Info($"Trimming speed={speedLimit} index={index} to {upper}");
264+
ushort upper = mph ? SpeedLimit.UPPER_MPH : SpeedLimit.UPPER_KMPH;
265+
266+
// Show unlimited if the speed cannot be represented by the available sign textures
267+
if (index == 0 || index > upper) {
268+
// Log._Debug($"Trimming speed={speedLimit} index={index} to {upper}");
269+
return textures[0];
273270
}
274-
#endif
275-
var trimIndex = Math.Min(upper, Math.Max((ushort)0, index));
271+
272+
// Trim from below to not go below index 5 (5 kmph or 5 mph)
273+
ushort trimIndex = Math.Max((ushort)5, index);
276274
return textures[trimIndex];
277275
}
278276

0 commit comments

Comments
 (0)