Skip to content

Commit aa40c75

Browse files
committed
factor out duplicate discrete cab control code
1 parent a271215 commit aa40c75

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

Source/Orts.Simulation/Simulation/RollingStocks/MSTSLocomotive.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4864,6 +4864,9 @@ public virtual float GetDataOf(CabViewControl cvc)
48644864
case CABViewControlTypes.ORTS_POWERKEY:
48654865
data = PowerKey ? 1 : 0;
48664866
break;
4867+
case CABViewControlTypes.ORTS_2DEXTERNALWIPERS:
4868+
data = Wiper ? 1 : 0;
4869+
break;
48674870
case CABViewControlTypes.ORTS_HOURDIAL:
48684871
float hour = (float)(Simulator.ClockTime / 3600) % 12;
48694872
if (hour < 0)

Source/RunActivity/Viewer3D/RollingStock/MSTSLocomotiveViewer.cs

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ public override void Draw(GraphicsDevice graphicsDevice)
17451745
/// </summary>
17461746
public class CabViewDiscreteRenderer : CabViewControlRenderer
17471747
{
1748-
protected readonly CVCWithFrames ControlDiscrete;
1748+
readonly CVCWithFrames ControlDiscrete;
17491749
readonly Rectangle SourceRectangle;
17501750
Rectangle DestinationRectangle = new Rectangle();
17511751
public readonly float CVCFlashTimeOn = 0.75f;
@@ -1802,6 +1802,12 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
18021802
if ((mS.MSStyles.Count > index) && (mS.MSStyles[index] == 1) && (CumulativeTime > CVCFlashTimeOn))
18031803
return;
18041804
}
1805+
1806+
PrepareFrameForIndex(frame, elapsedTime, index);
1807+
}
1808+
1809+
protected void PrepareFrameForIndex(RenderFrame frame, ElapsedTime elapsedTime, int index)
1810+
{
18051811
var dark = Viewer.MaterialManager.sunDirection.Y <= -0.085f || Viewer.Camera.IsUnderground;
18061812

18071813
Texture = CABTextureManager.GetTextureByIndexes(Control.ACEFile, index, dark, Locomotive.CabLightOn, out IsNightTexture, HasCabLightDirectory);
@@ -1833,7 +1839,7 @@ public override void Draw(GraphicsDevice graphicsDevice)
18331839
/// Determines the index of the Texture to be drawn
18341840
/// </summary>
18351841
/// <returns>index of the Texture</returns>
1836-
public virtual int GetDrawIndex()
1842+
public int GetDrawIndex()
18371843
{
18381844
var data = Locomotive.GetDataOf(Control);
18391845

@@ -2274,59 +2280,42 @@ protected int PercentToIndex(float percent)
22742280
/// </summary>
22752281
public class CabViewAnimationsRenderer : CabViewDiscreteRenderer
22762282
{
2277-
float CumulativeTime;
2278-
CVCAnimatedDisplay ControlAnimated;
2279-
float HalfCycleTimeS;
2280-
bool wiperOn = false;
2283+
private float CumulativeTime;
2284+
private readonly float CycleTimeS;
2285+
private bool AnimationOn = false;
22812286

2282-
public CabViewAnimationsRenderer(Viewer viewer, MSTSLocomotive locomotive, CVCWithFrames control, CabShader shader)
2287+
public CabViewAnimationsRenderer(Viewer viewer, MSTSLocomotive locomotive, CVCAnimatedDisplay control, CabShader shader)
22832288
: base(viewer, locomotive, control, shader)
22842289
{
2285-
ControlAnimated = (CVCAnimatedDisplay)ControlDiscrete;
2286-
HalfCycleTimeS = ControlAnimated.CycleTimeS * 0.5f;
2287-
wiperOn = Locomotive.Wiper;
2290+
CycleTimeS = control.CycleTimeS;
22882291
}
22892292

22902293
public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
22912294
{
2292-
if (Locomotive.Wiper) wiperOn = true;
2293-
if (wiperOn)
2295+
var animate = Locomotive.GetDataOf(Control) != 0;
2296+
if (animate)
2297+
AnimationOn = true;
2298+
2299+
int index;
2300+
var halfCycleS = CycleTimeS / 2f;
2301+
if (AnimationOn)
22942302
{
22952303
CumulativeTime += elapsedTime.ClockSeconds;
2296-
if (CumulativeTime > ControlAnimated.CycleTimeS && !Locomotive.Wiper)
2297-
wiperOn = false;
2298-
while (CumulativeTime > ControlAnimated.CycleTimeS)
2299-
CumulativeTime -= ControlAnimated.CycleTimeS;
2300-
}
2301-
base.PrepareFrame(frame, elapsedTime);
2302-
}
2303-
2304+
if (CumulativeTime > CycleTimeS && !animate)
2305+
AnimationOn = false;
2306+
CumulativeTime %= CycleTimeS;
23042307

2305-
public override int GetDrawIndex()
2306-
{
2307-
var data = Locomotive.GetDataOf(Control);
2308-
2309-
var index = 0;
2310-
switch (ControlDiscrete.ControlType)
2308+
if (CumulativeTime < halfCycleS)
2309+
index = PercentToIndex(CumulativeTime / halfCycleS);
2310+
else
2311+
index = PercentToIndex((CycleTimeS - CumulativeTime) / halfCycleS);
2312+
}
2313+
else
23112314
{
2312-
case CABViewControlTypes.ORTS_2DEXTERNALWIPERS:
2313-
if (wiperOn)
2314-
{
2315-
if (CumulativeTime < HalfCycleTimeS)
2316-
index = PercentToIndex(CumulativeTime / HalfCycleTimeS);
2317-
else
2318-
index = PercentToIndex((ControlAnimated.CycleTimeS - CumulativeTime) / HalfCycleTimeS);
2319-
}
2320-
break;
2315+
index = 0;
23212316
}
2322-
// If it is a control with NumPositions and NumValues, the index becomes the reference to the Positions entry, which in turn is the frame index within the .ace file
2323-
if (ControlDiscrete is CVCDiscrete && !(ControlDiscrete is CVCSignal) && (ControlDiscrete as CVCDiscrete).Positions.Count > index &&
2324-
(ControlDiscrete as CVCDiscrete).Positions.Count == ControlDiscrete.Values.Count && index >= 0)
2325-
index = (ControlDiscrete as CVCDiscrete).Positions[index];
23262317

2327-
if (index >= ControlDiscrete.FramesCount) index = ControlDiscrete.FramesCount - 1;
2328-
if (index < 0) index = 0;
2329-
return index;
2318+
PrepareFrameForIndex(frame, elapsedTime, index);
23302319
}
23312320
}
23322321

0 commit comments

Comments
 (0)