Skip to content

Commit 201ccf0

Browse files
committed
refactor: centralize cab control power supply logic
1 parent ff1e728 commit 201ccf0

File tree

2 files changed

+71
-59
lines changed

2 files changed

+71
-59
lines changed

Source/RunActivity/Viewer3D/RollingStock/MSTSLocomotiveViewer.cs

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ internal void Mark()
14171417
public abstract class CabViewControlRenderer : RenderPrimitive
14181418
{
14191419
protected readonly Viewer Viewer;
1420-
public readonly MSTSLocomotive Locomotive;
1420+
protected readonly MSTSLocomotive Locomotive;
14211421
public readonly CabViewControl Control;
14221422
protected readonly CabShader Shader;
14231423
protected readonly SpriteBatchMaterial ControlView;
@@ -1429,6 +1429,25 @@ public abstract class CabViewControlRenderer : RenderPrimitive
14291429

14301430
Matrix Matrix = Matrix.Identity;
14311431

1432+
/// <summary>
1433+
/// Determines whether or not the control has power given the state of the cab power supply.
1434+
/// </summary>
1435+
/// <remarks>
1436+
/// For controls that do not depend on the power supply, this will always return true.
1437+
/// </remarks>
1438+
public bool IsPowered
1439+
{
1440+
get
1441+
{
1442+
if (Control.DisabledIfLowVoltagePowerSupplyOff)
1443+
return Locomotive.LocomotivePowerSupply.LowVoltagePowerSupplyOn;
1444+
else if (Control.DisabledIfCabPowerSupplyOff)
1445+
return Locomotive.LocomotivePowerSupply.CabPowerSupplyOn;
1446+
else
1447+
return true;
1448+
}
1449+
}
1450+
14321451
public CabViewControlRenderer(Viewer viewer, MSTSLocomotive locomotive, CabViewControl control, CabShader shader)
14331452
{
14341453
Viewer = viewer;
@@ -1471,11 +1490,12 @@ public CABViewControlStyles GetStyle()
14711490
[CallOnThread("Updater")]
14721491
public virtual void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
14731492
{
1474-
if ((!Control.DisabledIfLowVoltagePowerSupplyOff || Locomotive.LocomotivePowerSupply.LowVoltagePowerSupplyOn) &&
1475-
(!Control.DisabledIfCabPowerSupplyOff || Locomotive.LocomotivePowerSupply.CabPowerSupplyOn))
1476-
{
1477-
frame.AddPrimitive(ControlView, this, RenderPrimitiveGroup.Cab, ref Matrix);
1478-
}
1493+
var noPower = (Control.DisabledIfLowVoltagePowerSupplyOff && !Locomotive.LocomotivePowerSupply.LowVoltagePowerSupplyOn)
1494+
|| (Control.DisabledIfCabPowerSupplyOff && !Locomotive.LocomotivePowerSupply.CabPowerSupplyOn);
1495+
if (noPower)
1496+
return;
1497+
1498+
frame.AddPrimitive(ControlView, this, RenderPrimitiveGroup.Cab, ref Matrix);
14791499
}
14801500

14811501
internal void Mark()
@@ -3109,18 +3129,17 @@ static float GetTextureCoordY(char c)
31093129

31103130
public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
31113131
{
3112-
if ((!CVFR.Control.DisabledIfLowVoltagePowerSupplyOff || CVFR.Locomotive.LocomotivePowerSupply.LowVoltagePowerSupplyOn)
3113-
&& (!CVFR.Control.DisabledIfCabPowerSupplyOff || CVFR.Locomotive.LocomotivePowerSupply.CabPowerSupplyOn))
3114-
{
3115-
UpdateDigit();
3116-
Matrix mx = TrainCarShape.Location.XNAMatrix;
3117-
mx.M41 += (TrainCarShape.Location.TileX - Viewer.Camera.TileX) * 2048;
3118-
mx.M43 += (-TrainCarShape.Location.TileZ + Viewer.Camera.TileZ) * 2048;
3119-
Matrix m = XNAMatrix * mx;
3132+
if (!CVFR.IsPowered)
3133+
return;
31203134

3121-
// TODO: Make this use AddAutoPrimitive instead.
3122-
frame.AddPrimitive(this.shapePrimitive.Material, this.shapePrimitive, RenderPrimitiveGroup.Interior, ref m, ShapeFlags.None);
3123-
}
3135+
UpdateDigit();
3136+
Matrix mx = TrainCarShape.Location.XNAMatrix;
3137+
mx.M41 += (TrainCarShape.Location.TileX - Viewer.Camera.TileX) * 2048;
3138+
mx.M43 += (-TrainCarShape.Location.TileZ + Viewer.Camera.TileZ) * 2048;
3139+
Matrix m = XNAMatrix * mx;
3140+
3141+
// TODO: Make this use AddAutoPrimitive instead.
3142+
frame.AddPrimitive(this.shapePrimitive.Material, this.shapePrimitive, RenderPrimitiveGroup.Interior, ref m, ShapeFlags.None);
31243143
}
31253144

31263145
internal void Mark()
@@ -3324,18 +3343,17 @@ private void UpdateShapePrimitive()
33243343

33253344
public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
33263345
{
3327-
if ((!CVFR.Control.DisabledIfLowVoltagePowerSupplyOff || CVFR.Locomotive.LocomotivePowerSupply.LowVoltagePowerSupplyOn)
3328-
&& (!CVFR.Control.DisabledIfCabPowerSupplyOff || CVFR.Locomotive.LocomotivePowerSupply.CabPowerSupplyOn))
3329-
{
3330-
UpdateDigit();
3331-
Matrix mx = TrainCarShape.Location.XNAMatrix;
3332-
mx.M41 += (TrainCarShape.Location.TileX - Viewer.Camera.TileX) * 2048;
3333-
mx.M43 += (-TrainCarShape.Location.TileZ + Viewer.Camera.TileZ) * 2048;
3334-
Matrix m = XNAMatrix * mx;
3346+
if (!CVFR.IsPowered)
3347+
return;
33353348

3336-
// TODO: Make this use AddAutoPrimitive instead.
3337-
frame.AddPrimitive(this.shapePrimitive.Material, this.shapePrimitive, RenderPrimitiveGroup.Interior, ref m, ShapeFlags.None);
3338-
}
3349+
UpdateDigit();
3350+
Matrix mx = TrainCarShape.Location.XNAMatrix;
3351+
mx.M41 += (TrainCarShape.Location.TileX - Viewer.Camera.TileX) * 2048;
3352+
mx.M43 += (-TrainCarShape.Location.TileZ + Viewer.Camera.TileZ) * 2048;
3353+
Matrix m = XNAMatrix * mx;
3354+
3355+
// TODO: Make this use AddAutoPrimitive instead.
3356+
frame.AddPrimitive(this.shapePrimitive.Material, this.shapePrimitive, RenderPrimitiveGroup.Interior, ref m, ShapeFlags.None);
33393357
}
33403358

33413359
internal void Mark()
@@ -3371,12 +3389,7 @@ public void Update(MSTSLocomotiveViewer locoViewer, ElapsedTime elapsedTime)
33713389
{
33723390
if (!locoViewer._has3DCabRenderer) return;
33733391

3374-
float scale = 0f;
3375-
if ((!CVFR.Control.DisabledIfLowVoltagePowerSupplyOff || CVFR.Locomotive.LocomotivePowerSupply.LowVoltagePowerSupplyOn)
3376-
&& (!CVFR.Control.DisabledIfCabPowerSupplyOff || CVFR.Locomotive.LocomotivePowerSupply.CabPowerSupplyOn))
3377-
{
3378-
scale = CVFR.GetRangeFraction();
3379-
}
3392+
var scale = CVFR.IsPowered ? CVFR.GetRangeFraction() : 0f;
33803393

33813394
if (CVFR.GetStyle() == CABViewControlStyles.POINTER)
33823395
{

Source/RunActivity/Viewer3D/RollingStock/SubSystems/ETCS/DriverMachineInterface.cs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -765,32 +765,31 @@ public DriverMachineInterfaceRenderer(Viewer viewer, MSTSLocomotive locomotive,
765765

766766
public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
767767
{
768-
if ((!Control.DisabledIfLowVoltagePowerSupplyOff || Locomotive.LocomotivePowerSupply.LowVoltagePowerSupplyOn) &&
769-
(!Control.DisabledIfCabPowerSupplyOff || Locomotive.LocomotivePowerSupply.CabPowerSupplyOn))
770-
{
771-
base.PrepareFrame(frame, elapsedTime);
772-
var xScale = Viewer.CabWidthPixels / 640f;
773-
var yScale = Viewer.CabHeightPixels / 480f;
774-
DrawPosition.X = (int)(Position.X * xScale) - Viewer.CabXOffsetPixels + Viewer.CabXLetterboxPixels;
775-
DrawPosition.Y = (int)(Position.Y * yScale) + Viewer.CabYOffsetPixels + Viewer.CabYLetterboxPixels;
776-
DrawPosition.Width = (int)(Control.Width * xScale);
777-
DrawPosition.Height = (int)(Control.Height * yScale);
778-
if (Zoomed)
779-
{
780-
DrawPosition.Width = 640;
781-
DrawPosition.Height = 480;
782-
DMI.SizeTo(DrawPosition.Width, DrawPosition.Height);
783-
DrawPosition.X -= 320;
784-
DrawPosition.Y -= 240;
785-
DMI.ETCSDefaultWindow.BackgroundColor = ColorBackground;
786-
}
787-
else
788-
{
789-
DMI.SizeTo(DrawPosition.Width, DrawPosition.Height);
790-
DMI.ETCSDefaultWindow.BackgroundColor = Color.Transparent;
791-
}
792-
DMI.PrepareFrame(elapsedTime.ClockSeconds);
768+
if (!IsPowered)
769+
return;
770+
771+
base.PrepareFrame(frame, elapsedTime);
772+
var xScale = Viewer.CabWidthPixels / 640f;
773+
var yScale = Viewer.CabHeightPixels / 480f;
774+
DrawPosition.X = (int)(Position.X * xScale) - Viewer.CabXOffsetPixels + Viewer.CabXLetterboxPixels;
775+
DrawPosition.Y = (int)(Position.Y * yScale) + Viewer.CabYOffsetPixels + Viewer.CabYLetterboxPixels;
776+
DrawPosition.Width = (int)(Control.Width * xScale);
777+
DrawPosition.Height = (int)(Control.Height * yScale);
778+
if (Zoomed)
779+
{
780+
DrawPosition.Width = 640;
781+
DrawPosition.Height = 480;
782+
DMI.SizeTo(DrawPosition.Width, DrawPosition.Height);
783+
DrawPosition.X -= 320;
784+
DrawPosition.Y -= 240;
785+
DMI.ETCSDefaultWindow.BackgroundColor = ColorBackground;
786+
}
787+
else
788+
{
789+
DMI.SizeTo(DrawPosition.Width, DrawPosition.Height);
790+
DMI.ETCSDefaultWindow.BackgroundColor = Color.Transparent;
793791
}
792+
DMI.PrepareFrame(elapsedTime.ClockSeconds);
794793
}
795794

796795
public bool IsMouseWithin()

0 commit comments

Comments
 (0)