Skip to content

Commit 6e55f9c

Browse files
authored
Merge pull request #744 from Sharpe49/fix/disabledif-conditions
Fixed and improved cabview control conditions related to power supply state
2 parents 6cf0e64 + 1b56aa6 commit 6e55f9c

File tree

5 files changed

+121
-43
lines changed

5 files changed

+121
-43
lines changed

Source/Documentation/Manual/cabs.rst

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,24 @@ The following controls are available for the cabview:
7979
- ``ORTS_BATTERY_SWITCH_COMMAND_BUTTON_CLOSE`` and ``ORTS_BATTERY_SWITCH_COMMAND_BUTTON_OPEN`` can be used if the switch is controlled with two pushbuttons (one to close the switch and the other to open it)
8080
- ``ORTS_BATTERY_SWITCH_ON`` can be used to control a light on the cab showing the state of the battery switch
8181

82-
Other controls can be hidden if the low voltage power supply is not available using the following parameter::
82+
Other controls can be disabled if the low voltage power supply is not available using the following parameter::
8383

8484
TwoState (
85-
Type ( ORTS_CIRCUIT_BREAKER_CLOSED TWO_STATE)
85+
Type ( ORTS_CIRCUIT_BREAKER_CLOSED TWO_STATE )
8686
...
8787
DisabledIfLowVoltagePowerSupplyOff ( 1 )
8888
)
8989

90+
By default, the cabview control will be completely hidden. You can also set a specific value for the control when it is disabled::
91+
92+
TwoState (
93+
Type ( SPEEDOMETER DIAL )
94+
...
95+
DisabledIfLowVoltagePowerSupplyOff ( 1 )
96+
HideIfDisabled ( 0 )
97+
ValueIfDisabled ( 0 )
98+
)
99+
90100
.. _cabs-master-key:
91101

92102
Master key
@@ -105,12 +115,22 @@ The following controls are available for the cabview:
105115
- ``ORTS_CURRENT_CAB_IN_USE`` can be used to indicate that the current cab is active
106116
- ``ORTS_OTHER_CAB_IN_USE`` can be used to indicate that another cab of the train is active
107117

108-
Other controls can be hidden if the cab power supply is not available using the following parameter::
118+
Other controls can be disabled if the cab power supply is not available using the following parameter::
119+
120+
TwoState (
121+
Type ( ORTS_CIRCUIT_BREAKER_CLOSED TWO_STATE )
122+
...
123+
DisabledIfCabPowerSupplyOff ( 1 )
124+
)
125+
126+
By default, the cabview control will be completely hidden. You can also set a specific value for the control when it is disabled::
109127

110128
TwoState (
111-
Type ( ORTS_CIRCUIT_BREAKER_CLOSED TWO_STATE)
129+
Type ( SPEEDOMETER DIAL )
112130
...
113131
DisabledIfCabPowerSupplyOff ( 1 )
132+
HideIfDisabled ( 0 )
133+
ValueIfDisabled ( 0 )
114134
)
115135

116136
.. _cabs-service-retention:

Source/Orts.Formats.Msts/CabViewFile.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ public class CabViewControl
457457

458458
public bool DisabledIfLowVoltagePowerSupplyOff { get; private set; } = false;
459459
public bool DisabledIfCabPowerSupplyOff { get; private set; } = false;
460+
public bool HideIfDisabled { get; private set; } = true;
461+
public float? ValueIfDisabled { get; private set; }
460462

461463
protected void ParseType(STFReader stf)
462464
{
@@ -540,14 +542,27 @@ protected void ParseUnits(STFReader stf)
540542
}
541543
stf.SkipRestOfBlock();
542544
}
545+
543546
protected void ParseDisabledIfLowVoltagePowerSupplyOff(STFReader stf)
544547
{
545548
DisabledIfLowVoltagePowerSupplyOff = stf.ReadBoolBlock(false);
546549
}
550+
547551
protected void ParseDisabledIfCabPowerSupplyOff(STFReader stf)
548552
{
549553
DisabledIfCabPowerSupplyOff = stf.ReadBoolBlock(false);
550554
}
555+
556+
protected void ParseHideIfDisabled(STFReader stf)
557+
{
558+
HideIfDisabled = stf.ReadBoolBlock(true);
559+
}
560+
561+
protected void ParseValueIfDisabled(STFReader stf)
562+
{
563+
ValueIfDisabled = stf.ReadFloatBlock(STFReader.UNITS.None, 0f);
564+
}
565+
551566
// Used by subclasses CVCGauge and CVCDigital
552567
protected virtual color ParseControlColor( STFReader stf )
553568
{
@@ -638,6 +653,8 @@ public CVCDial(STFReader stf, string basepath)
638653
new STFReader.TokenProcessor("units", ()=>{ ParseUnits(stf); }),
639654
new STFReader.TokenProcessor("disablediflowvoltagepowersupplyoff", ()=>{ ParseDisabledIfLowVoltagePowerSupplyOff(stf); }),
640655
new STFReader.TokenProcessor("disabledifcabpowersupplyoff", ()=>{ ParseDisabledIfCabPowerSupplyOff(stf); }),
656+
new STFReader.TokenProcessor("hideifdisabled", ()=>{ ParseHideIfDisabled(stf); }),
657+
new STFReader.TokenProcessor("valueifdisabled", ()=>{ ParseValueIfDisabled(stf); }),
641658

642659
new STFReader.TokenProcessor("pivot", ()=>{ Center = stf.ReadFloatBlock(STFReader.UNITS.None, null); }),
643660
new STFReader.TokenProcessor("dirincrease", ()=>{ Direction = stf.ReadIntBlock(null); }),
@@ -692,6 +709,8 @@ public CVCGauge(STFReader stf, string basepath)
692709
new STFReader.TokenProcessor("units", ()=>{ ParseUnits(stf); }),
693710
new STFReader.TokenProcessor("disablediflowvoltagepowersupplyoff", ()=>{ ParseDisabledIfLowVoltagePowerSupplyOff(stf); }),
694711
new STFReader.TokenProcessor("disabledifcabpowersupplyoff", ()=>{ ParseDisabledIfCabPowerSupplyOff(stf); }),
712+
new STFReader.TokenProcessor("hideifdisabled", ()=>{ ParseHideIfDisabled(stf); }),
713+
new STFReader.TokenProcessor("valueifdisabled", ()=>{ ParseValueIfDisabled(stf); }),
695714

696715
new STFReader.TokenProcessor("zeropos", ()=>{ ZeroPos = stf.ReadIntBlock(null); }),
697716
new STFReader.TokenProcessor("orientation", ()=>{ Orientation = stf.ReadIntBlock(null); }),
@@ -831,6 +850,8 @@ public CVCDigital(STFReader stf, string basepath)
831850
new STFReader.TokenProcessor("units", ()=>{ ParseUnits(stf); }),
832851
new STFReader.TokenProcessor("disablediflowvoltagepowersupplyoff", ()=>{ ParseDisabledIfLowVoltagePowerSupplyOff(stf); }),
833852
new STFReader.TokenProcessor("disabledifcabpowersupplyoff", ()=>{ ParseDisabledIfCabPowerSupplyOff(stf); }),
853+
new STFReader.TokenProcessor("hideifdisabled", ()=>{ ParseHideIfDisabled(stf); }),
854+
new STFReader.TokenProcessor("valueifdisabled", ()=>{ ParseValueIfDisabled(stf); }),
834855
new STFReader.TokenProcessor("leadingzeros", ()=>{ ParseLeadingZeros(stf); }),
835856
new STFReader.TokenProcessor("accuracy", ()=>{ ParseAccuracy(stf); }),
836857
new STFReader.TokenProcessor("accuracyswitch", ()=>{ ParseAccuracySwitch(stf); }),
@@ -937,6 +958,8 @@ public CVCDigitalClock(STFReader stf, string basepath)
937958
new STFReader.TokenProcessor("style", ()=>{ ParseStyle(stf); }),
938959
new STFReader.TokenProcessor("disablediflowvoltagepowersupplyoff", ()=>{ ParseDisabledIfLowVoltagePowerSupplyOff(stf); }),
939960
new STFReader.TokenProcessor("disabledifcabpowersupplyoff", ()=>{ ParseDisabledIfCabPowerSupplyOff(stf); }),
961+
new STFReader.TokenProcessor("hideifdisabled", ()=>{ ParseHideIfDisabled(stf); }),
962+
new STFReader.TokenProcessor("valueifdisabled", ()=>{ ParseValueIfDisabled(stf); }),
940963
new STFReader.TokenProcessor("accuracy", ()=>{ ParseAccuracy(stf); }),
941964
new STFReader.TokenProcessor("controlcolour", ()=>{ PositiveColor = ParseControlColor(stf); }),
942965
new STFReader.TokenProcessor("ortsfont", ()=>{ParseFont(stf); }),
@@ -1002,6 +1025,8 @@ public CVCDiscrete(STFReader stf, string basepath, DiscreteStates discreteState)
10021025
new STFReader.TokenProcessor("units", ()=>{ ParseUnits(stf); }),
10031026
new STFReader.TokenProcessor("disablediflowvoltagepowersupplyoff", ()=>{ ParseDisabledIfLowVoltagePowerSupplyOff(stf); }),
10041027
new STFReader.TokenProcessor("disabledifcabpowersupplyoff", ()=>{ ParseDisabledIfCabPowerSupplyOff(stf); }),
1028+
new STFReader.TokenProcessor("hideifdisabled", ()=>{ ParseHideIfDisabled(stf); }),
1029+
new STFReader.TokenProcessor("valueifdisabled", ()=>{ ParseValueIfDisabled(stf); }),
10051030
new STFReader.TokenProcessor("mousecontrol", ()=>{ MouseControl = stf.ReadBoolBlock(false); }),
10061031
new STFReader.TokenProcessor("orientation", ()=>{ Orientation = stf.ReadIntBlock(null); }),
10071032
new STFReader.TokenProcessor("dirincrease", ()=>{ Direction = stf.ReadIntBlock(null); }),
@@ -1353,6 +1378,8 @@ public CVCMultiStateDisplay(STFReader stf, string basepath)
13531378
new STFReader.TokenProcessor("units", ()=>{ ParseUnits(stf); }),
13541379
new STFReader.TokenProcessor("disablediflowvoltagepowersupplyoff", ()=>{ ParseDisabledIfLowVoltagePowerSupplyOff(stf); }),
13551380
new STFReader.TokenProcessor("disabledifcabpowersupplyoff", ()=>{ ParseDisabledIfCabPowerSupplyOff(stf); }),
1381+
new STFReader.TokenProcessor("hideifdisabled", ()=>{ ParseHideIfDisabled(stf); }),
1382+
new STFReader.TokenProcessor("valueifdisabled", ()=>{ ParseValueIfDisabled(stf); }),
13561383

13571384
new STFReader.TokenProcessor("states", ()=>{
13581385
stf.MustMatch("(");
@@ -1458,6 +1485,8 @@ public CVCScreen(STFReader stf, string basepath)
14581485
new STFReader.TokenProcessor("parameters", ()=>{ ParseCustomParameters(stf); }),
14591486
new STFReader.TokenProcessor("disablediflowvoltagepowersupplyoff", ()=>{ ParseDisabledIfLowVoltagePowerSupplyOff(stf); }),
14601487
new STFReader.TokenProcessor("disabledifcabpowersupplyoff", ()=>{ ParseDisabledIfCabPowerSupplyOff(stf); }),
1488+
new STFReader.TokenProcessor("hideifdisabled", ()=>{ ParseHideIfDisabled(stf); }),
1489+
new STFReader.TokenProcessor("valueifdisabled", ()=>{ ParseValueIfDisabled(stf); }),
14611490
new STFReader.TokenProcessor("ortsdisplay", ()=>{ParseDisplay(stf); }),
14621491
new STFReader.TokenProcessor("ortsscreenpage", () => {ParseScreen(stf); }),
14631492
new STFReader.TokenProcessor("ortscabviewpoint", ()=>{ParseCabViewpoint(stf); }),

Source/RunActivity/Viewer3D/RollingStock/MSTSLocomotiveViewer.cs

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,13 @@ public CABViewControlTypes GetControlType()
16151615
/// <returns>Data value as fraction (from 0 to 1) of the range between Min and Max values</returns>
16161616
public float GetRangeFraction(bool offsetFromZero = false)
16171617
{
1618-
var data = Locomotive.GetDataOf(Control);
1618+
float data;
1619+
1620+
if (!IsPowered && Control.ValueIfDisabled != null)
1621+
data = (float)Control.ValueIfDisabled;
1622+
else
1623+
data = Locomotive.GetDataOf(Control);
1624+
16191625
if (data < Control.MinValue)
16201626
return 0;
16211627
if (data > Control.MaxValue)
@@ -1635,9 +1641,7 @@ public CABViewControlStyles GetStyle()
16351641
[CallOnThread("Updater")]
16361642
public virtual void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
16371643
{
1638-
var noPower = (Control.DisabledIfLowVoltagePowerSupplyOff && !Locomotive.LocomotivePowerSupply.LowVoltagePowerSupplyOn)
1639-
|| (Control.DisabledIfCabPowerSupplyOff && !Locomotive.LocomotivePowerSupply.CabPowerSupplyOn);
1640-
if (noPower)
1644+
if (!IsPowered && Control.HideIfDisabled)
16411645
return;
16421646

16431647
frame.AddPrimitive(ControlView, this, RenderPrimitiveGroup.Cab, ref Matrix);
@@ -1801,8 +1805,11 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
18011805
float percent, xpos, ypos, zeropos;
18021806

18031807
percent = IsFire ? 1f : GetRangeFraction();
1804-
// LoadMeterPositive = percent + Gauge.MinValue / (Gauge.MaxValue - Gauge.MinValue) >= 0;
1805-
Num = Locomotive.GetDataOf(Control);
1808+
1809+
if (!IsPowered && Control.ValueIfDisabled != null)
1810+
Num = (float)Control.ValueIfDisabled;
1811+
else
1812+
Num = Locomotive.GetDataOf(Control);
18061813

18071814
if (Gauge.Orientation == 0) // gauge horizontal
18081815
{
@@ -2049,7 +2056,12 @@ public override void Draw(GraphicsDevice graphicsDevice)
20492056
/// <returns>index of the Texture</returns>
20502057
public virtual int GetDrawIndex()
20512058
{
2052-
var data = Locomotive.GetDataOf(Control);
2059+
float data;
2060+
2061+
if (!IsPowered && Control.ValueIfDisabled != null)
2062+
data = (float)Control.ValueIfDisabled;
2063+
else
2064+
data = Locomotive.GetDataOf(Control);
20532065

20542066
var index = OldFrameIndex;
20552067
switch (ControlDiscrete.ControlType)
@@ -2887,7 +2899,14 @@ public CabViewAnimationsRenderer(Viewer viewer, MSTSLocomotive locomotive, CVCAn
28872899

28882900
public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
28892901
{
2890-
var animate = Locomotive.GetDataOf(Control) != 0;
2902+
float data;
2903+
2904+
if (!IsPowered && Control.ValueIfDisabled != null)
2905+
data = (float)Control.ValueIfDisabled;
2906+
else
2907+
data = Locomotive.GetDataOf(Control);
2908+
2909+
var animate = data != 0;
28912910
if (animate)
28922911
AnimationOn = true;
28932912

@@ -2965,7 +2984,11 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
29652984
{
29662985
var digital = Control as CVCDigital;
29672986

2968-
Num = Locomotive.GetDataOf(Control);
2987+
if (!IsPowered && Control.ValueIfDisabled != null)
2988+
Num = (float)Control.ValueIfDisabled;
2989+
else
2990+
Num = Locomotive.GetDataOf(Control);
2991+
29692992
if (digital.MinValue < digital.MaxValue) Num = MathHelper.Clamp(Num, (float)digital.MinValue, (float)digital.MaxValue);
29702993
if (Math.Abs(Num) < digital.AccuracySwitch)
29712994
Format = Format2;
@@ -3046,7 +3069,12 @@ public string GetDigits(out Color DrawColor)
30463069
{
30473070
var digital = Control as CVCDigital;
30483071
string displayedText = "";
3049-
Num = Locomotive.GetDataOf(Control);
3072+
3073+
if (!IsPowered && Control.ValueIfDisabled != null)
3074+
Num = (float)Control.ValueIfDisabled;
3075+
else
3076+
Num = Locomotive.GetDataOf(Control);
3077+
30503078
if (Math.Abs(Num) < digital.AccuracySwitch)
30513079
Format = Format2;
30523080
else
@@ -3119,7 +3147,12 @@ public string GetDigits(out Color DrawColor)
31193147
{
31203148
var digital = Control as CVCDigital;
31213149
string displayedText = "";
3122-
Num = Locomotive.GetDataOf(Control);
3150+
3151+
if (!IsPowered && Control.ValueIfDisabled != null)
3152+
Num = (float)Control.ValueIfDisabled;
3153+
else
3154+
Num = Locomotive.GetDataOf(Control);
3155+
31233156
if (digital.MinValue < digital.MaxValue) Num = MathHelper.Clamp(Num, (float)digital.MinValue, (float)digital.MaxValue);
31243157
if (Math.Abs(Num) < digital.AccuracySwitch)
31253158
Format = Format2;
@@ -3356,11 +3389,6 @@ public override void HandleUserInput(ElapsedTime elapsedTime)
33563389
/// </summary>
33573390
public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
33583391
{
3359-
var trainCarShape = LocoViewer.ThreeDimentionCabViewer.TrainCarShape;
3360-
var animatedParts = LocoViewer.ThreeDimentionCabViewer.AnimateParts;
3361-
var controlMap = LocoViewer.ThreeDimentionCabRenderer.ControlMap;
3362-
var doShow = true;
3363-
CabViewControlRenderer cabRenderer;
33643392
foreach (var p in AnimateParts)
33653393
{
33663394
if (p.Value.Type >= CABViewControlTypes.EXTERNALWIPERS) //for wipers, doors and mirrors
@@ -3397,32 +3425,31 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
33973425
}
33983426
else
33993427
{
3400-
doShow = true;
3401-
cabRenderer = null;
3402-
if (LocoViewer.ThreeDimentionCabRenderer.ControlMap.TryGetValue(p.Key, out cabRenderer))
3403-
{
3404-
if (cabRenderer is CabViewDiscreteRenderer)
3428+
var doShow = true;
3429+
if (LocoViewer.ThreeDimentionCabRenderer.ControlMap.TryGetValue(p.Key, out var cabRenderer))
3430+
{
3431+
if (!cabRenderer.IsPowered && cabRenderer.Control.HideIfDisabled)
3432+
{
3433+
doShow = false;
3434+
}
3435+
else if (cabRenderer is CabViewDiscreteRenderer)
34053436
{
34063437
var control = cabRenderer.Control;
34073438
if (control.Screens != null && control.Screens[0] != "all")
34083439
{
3409-
doShow = false;
3410-
foreach (var screen in control.Screens)
3411-
{
3412-
if (LocoViewer.ThreeDimentionCabRenderer.ActiveScreen[control.Display] == screen)
3413-
{
3414-
doShow = true;
3415-
break;
3416-
}
3417-
}
3440+
doShow = control.Screens.Any(screen =>
3441+
LocoViewer.ThreeDimentionCabRenderer.ActiveScreen[control.Display] == screen);
34183442
}
34193443
}
34203444
}
3445+
34213446
foreach (var matrixIndex in p.Value.MatrixIndexes)
34223447
MatrixVisible[matrixIndex] = doShow;
3423-
p.Value.Update(this.LocoViewer, elapsedTime); //for all other intruments with animations
3448+
3449+
p.Value.Update(LocoViewer, elapsedTime); //for all other instruments with animations
34243450
}
34253451
}
3452+
34263453
foreach (var p in DigitParts3D)
34273454
{
34283455
var digital = p.Value.CVFR.Control;
@@ -3440,6 +3467,7 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
34403467
}
34413468
p.Value.PrepareFrame(frame, elapsedTime);
34423469
}
3470+
34433471
foreach (var p in DPIDisplays3D)
34443472
{
34453473
var dpdisplay = p.Value.CVFR.Control;
@@ -3457,6 +3485,7 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
34573485
}
34583486
p.Value.PrepareFrame(frame, elapsedTime);
34593487
}
3488+
34603489
foreach (var p in Gauges)
34613490
{
34623491
var gauge = p.Value.CVFR.Control;
@@ -3763,7 +3792,7 @@ static float GetTextureCoordY(char c)
37633792

37643793
public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
37653794
{
3766-
if (!CVFR.IsPowered)
3795+
if (!CVFR.IsPowered && CVFR.Control.HideIfDisabled)
37673796
return;
37683797

37693798
UpdateDigit();
@@ -3982,7 +4011,7 @@ private void UpdateShapePrimitive(Material material)
39824011

39834012
public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
39844013
{
3985-
if (!CVFR.IsPowered)
4014+
if (!CVFR.IsPowered && CVFR.Control.HideIfDisabled)
39864015
return;
39874016

39884017
UpdateDigit();
@@ -4028,7 +4057,7 @@ public void Update(MSTSLocomotiveViewer locoViewer, ElapsedTime elapsedTime)
40284057
{
40294058
if (!locoViewer._has3DCabRenderer) return;
40304059

4031-
var scale = CVFR.IsPowered ? CVFR.GetRangeFraction() : 0f;
4060+
var scale = CVFR.IsPowered || CVFR.Control.ValueIfDisabled == null ? CVFR.GetRangeFraction() : (float)CVFR.Control.ValueIfDisabled;
40324061

40334062
if (CVFR.GetStyle() == CABViewControlStyles.POINTER)
40344063
{

Source/RunActivity/Viewer3D/RollingStock/SubSystems/DistributedPowerInterface.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ public DistributedPowerInterfaceRenderer(Viewer viewer, MSTSLocomotive locomotiv
540540

541541
public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
542542
{
543-
if (!IsPowered)
543+
if (!IsPowered && Control.HideIfDisabled)
544544
return;
545545

546546
base.PrepareFrame(frame, elapsedTime);
@@ -974,7 +974,7 @@ static float GetTextureCoordY(string param, int iChar, Color color)
974974

975975
public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
976976
{
977-
if (!CVFR.IsPowered)
977+
if (!CVFR.IsPowered && CVFR.Control.HideIfDisabled)
978978
return;
979979

980980
Update3DDPITable();
@@ -993,4 +993,4 @@ internal void Mark()
993993
}
994994
} // class ThreeDimCabDPI
995995

996-
}
996+
}

0 commit comments

Comments
 (0)