Skip to content

Commit e2c841d

Browse files
committed
Code refactoring for 3D cabs
1 parent 1306375 commit e2c841d

File tree

9 files changed

+144
-136
lines changed

9 files changed

+144
-136
lines changed

Source/Orts.Formats.Msts/CabViewFile.cs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,30 @@ public enum DiscreteStates
339339
CAB_SIGNAL_DISPLAY
340340
}
341341

342+
public struct CabViewControlType
343+
{
344+
public CABViewControlTypes Type;
345+
public int Subtype;
346+
public CabViewControlType(string name)
347+
{
348+
Type = CABViewControlTypes.NONE;
349+
Subtype = 0;
350+
if (name != null && name.ToUpperInvariant().StartsWith("ORTS_TCS"))
351+
{
352+
if (int.TryParse(name.Substring(8), out Subtype))
353+
{
354+
Type = CABViewControlTypes.ORTS_TCS;
355+
}
356+
}
357+
else Enum.TryParse(name, true, out Type);
358+
}
359+
public override string ToString()
360+
{
361+
if (Type == CABViewControlTypes.ORTS_TCS) return Type.ToString() + Subtype;
362+
return Type.ToString();
363+
}
364+
}
365+
342366
public class CabViewControls : List<CabViewControl>
343367
{
344368
public CabViewControls(STFReader stf, string basepath)
@@ -404,8 +428,7 @@ public class CabViewControl
404428
public List<string> Screens;
405429
public int CabViewpoint;
406430

407-
public CABViewControlTypes ControlType = CABViewControlTypes.NONE;
408-
public int ControlSubtype;
431+
public CabViewControlType ControlType;
409432
public CABViewControlStyles ControlStyle = CABViewControlStyles.NONE;
410433
public CABViewControlUnits Units = CABViewControlUnits.NONE;
411434

@@ -415,24 +438,11 @@ public class CabViewControl
415438
protected void ParseType(STFReader stf)
416439
{
417440
stf.MustMatch("(");
418-
try
419-
{
420-
string name = stf.ReadString();
421-
if (name != null && name.ToUpperInvariant().StartsWith("ORTS_TCS"))
422-
{
423-
ControlType = CABViewControlTypes.ORTS_TCS;
424-
ControlSubtype = int.Parse(name.Substring(8));
425-
}
426-
else
427-
{
428-
ControlType = (CABViewControlTypes)Enum.Parse(typeof(CABViewControlTypes), name);
429-
}
430-
}
431-
catch(ArgumentException)
441+
string name = stf.ReadString();
442+
ControlType = new CabViewControlType(name);
443+
if (ControlType.Type == CABViewControlTypes.NONE)
432444
{
433-
stf.StepBackOneItem();
434-
STFException.TraceInformation(stf, "Skipped unknown ControlType " + stf.ReadString());
435-
ControlType = CABViewControlTypes.NONE;
445+
STFException.TraceInformation(stf, "Skipped unknown ControlType " + name);
436446
}
437447
//stf.ReadItem(); // Skip repeated Class Type
438448
stf.SkipRestOfBlock();
@@ -579,7 +589,11 @@ public CVCDial(CABViewControlTypes dialtype, int maxvalue, STFReader stf, string
579589
new STFReader.TokenProcessor("graphic", ()=>{ ParseGraphic(stf, basepath); }),
580590
new STFReader.TokenProcessor("pivot", ()=>{ Center = stf.ReadFloatBlock(STFReader.UNITS.None, null); }),
581591
});
582-
ControlType = dialtype;
592+
ControlType = new CabViewControlType()
593+
{
594+
Type = dialtype,
595+
Subtype = 0,
596+
};
583597
ControlStyle = CABViewControlStyles.NEEDLE;
584598
Direction = 0;
585599
MaxValue = maxvalue;
@@ -1256,15 +1270,15 @@ public CVCDiscrete(STFReader stf, string basepath, DiscreteStates discreteState)
12561270
}
12571271

12581272
// MSTS ignores/overrides various settings by the following exceptional cases:
1259-
if (ControlType == CABViewControlTypes.CP_HANDLE)
1273+
if (ControlType.Type == CABViewControlTypes.CP_HANDLE)
12601274
ControlStyle = CABViewControlStyles.NOT_SPRUNG;
1261-
if (ControlType == CABViewControlTypes.PANTOGRAPH || ControlType == CABViewControlTypes.PANTOGRAPH2 ||
1262-
ControlType == CABViewControlTypes.ORTS_PANTOGRAPH3 || ControlType == CABViewControlTypes.ORTS_PANTOGRAPH4)
1275+
if (ControlType.Type == CABViewControlTypes.PANTOGRAPH || ControlType.Type == CABViewControlTypes.PANTOGRAPH2 ||
1276+
ControlType.Type == CABViewControlTypes.ORTS_PANTOGRAPH3 || ControlType.Type == CABViewControlTypes.ORTS_PANTOGRAPH4)
12631277
ControlStyle = CABViewControlStyles.ONOFF;
1264-
if (ControlType == CABViewControlTypes.HORN || ControlType == CABViewControlTypes.SANDERS || ControlType == CABViewControlTypes.BELL
1265-
|| ControlType == CABViewControlTypes.RESET || ControlType == CABViewControlTypes.VACUUM_EXHAUSTER)
1278+
if (ControlType.Type == CABViewControlTypes.HORN || ControlType.Type == CABViewControlTypes.SANDERS || ControlType.Type == CABViewControlTypes.BELL
1279+
|| ControlType.Type == CABViewControlTypes.RESET || ControlType.Type == CABViewControlTypes.VACUUM_EXHAUSTER)
12661280
ControlStyle = CABViewControlStyles.WHILE_PRESSED;
1267-
if (ControlType == CABViewControlTypes.DIRECTION && Orientation == 0)
1281+
if (ControlType.Type == CABViewControlTypes.DIRECTION && Orientation == 0)
12681282
Direction = 1 - Direction;
12691283

12701284
switch (discreteState)

Source/Orts.Simulation/Simulation/RollingStocks/MSTSDieselLocomotive.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ public override float GetDataOf(CabViewControl cvc)
848848
{
849849
float data = 0;
850850

851-
switch (cvc.ControlType)
851+
switch (cvc.ControlType.Type)
852852
{
853853
case CABViewControlTypes.GEARS:
854854
if (DieselEngines.HasGearBox)
@@ -1442,7 +1442,7 @@ protected void NormalizeParams()
14421442
{
14431443
foreach ( var control in cabView.CVFFile.CabViewControls)
14441444
{
1445-
if (control is CVCDiscrete && control.ControlType == CABViewControlTypes.THROTTLE && (control as CVCDiscrete).Values.Count > 0 && (control as CVCDiscrete).Values[(control as CVCDiscrete).Values.Count - 1] > 1)
1445+
if (control is CVCDiscrete && control.ControlType.Type == CABViewControlTypes.THROTTLE && (control as CVCDiscrete).Values.Count > 0 && (control as CVCDiscrete).Values[(control as CVCDiscrete).Values.Count - 1] > 1)
14461446
{
14471447
var discreteControl = (CVCDiscrete)control;
14481448
for (var i = 0; i < discreteControl.Values.Count; i++)

Source/Orts.Simulation/Simulation/RollingStocks/MSTSElectricLocomotive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public override float GetDataOf(CabViewControl cvc)
221221
{
222222
float data = 0;
223223

224-
switch (cvc.ControlType)
224+
switch (cvc.ControlType.Type)
225225
{
226226
case CABViewControlTypes.LINE_VOLTAGE:
227227
data = ElectricPowerSupply.PantographVoltageV;

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -634,16 +634,16 @@ protected MSTSNotchController BuildDPDynamicBrakeController()
634634
{
635635
msDisplay = (CVCMultiStateDisplay) cabView.CVFFile.CabViewControls.Where(
636636
control => control is CVCMultiStateDisplay &&
637-
(((CVCMultiStateDisplay) control).ControlType == CABViewControlTypes.DYNAMIC_BRAKE_DISPLAY ||
638-
((CVCMultiStateDisplay) control).ControlType == CABViewControlTypes.CPH_DISPLAY)).First();
637+
(((CVCMultiStateDisplay) control).ControlType.Type == CABViewControlTypes.DYNAMIC_BRAKE_DISPLAY ||
638+
((CVCMultiStateDisplay) control).ControlType.Type == CABViewControlTypes.CPH_DISPLAY)).First();
639639
}
640640
catch
641641
{
642642

643643
}
644644
if (msDisplay != null)
645645
{
646-
if (msDisplay.ControlType == CABViewControlTypes.DYNAMIC_BRAKE_DISPLAY)
646+
if (msDisplay.ControlType.Type == CABViewControlTypes.DYNAMIC_BRAKE_DISPLAY)
647647
{
648648
foreach (var switchval in msDisplay.Values)
649649
dpDynController.AddNotch((float) switchval);
@@ -705,11 +705,8 @@ protected void GetPressureUnit()
705705
CabViewControls cvcList = CabViewList[0].CVFFile.CabViewControls;
706706
foreach (CabViewControl cvc in cvcList)
707707
{
708-
if (brakeSystemComponents.ContainsKey(cvc.ControlType) && pressureUnits.ContainsKey(cvc.Units))
708+
if (brakeSystemComponents.TryGetValue(cvc.ControlType.Type, out var component) && pressureUnits.TryGetValue(cvc.Units, out var unit))
709709
{
710-
BrakeSystemComponent component = brakeSystemComponents[cvc.ControlType];
711-
PressureUnit unit = pressureUnits[cvc.Units];
712-
713710
BrakeSystemPressureUnits[component] = unit;
714711
}
715712
}
@@ -4992,7 +4989,7 @@ public override void SignalEvent(Event evt)
49924989
public virtual float GetDataOf(CabViewControl cvc)
49934990
{
49944991
float data = 0;
4995-
switch (cvc.ControlType)
4992+
switch (cvc.ControlType.Type)
49964993
{
49974994
case CABViewControlTypes.SPEEDOMETER:
49984995
{
@@ -5090,7 +5087,7 @@ public virtual float GetDataOf(CabViewControl cvc)
50905087
if (DynamicBrakePercent > 0 && MaxDynamicBrakeForceN > 0)
50915088
{
50925089
float rangeFactor;
5093-
if (cvc.ControlType == CABViewControlTypes.AMMETER_ABS)
5090+
if (cvc.ControlType.Type == CABViewControlTypes.AMMETER_ABS)
50945091
{
50955092
if (DynamicBrakeMaxCurrentA == 0)
50965093
rangeFactor = direction == 0 ? (float)cvc.MaxValue : (float)cvc.MinValue;
@@ -5108,11 +5105,11 @@ public virtual float GetDataOf(CabViewControl cvc)
51085105
}
51095106
if (direction == 1)
51105107
data = -data;
5111-
if (cvc.ControlType == CABViewControlTypes.AMMETER_ABS) data = Math.Abs(data);
5108+
if (cvc.ControlType.Type == CABViewControlTypes.AMMETER_ABS) data = Math.Abs(data);
51125109
break;
51135110
}
51145111
data = this.MotiveForceN / MaxForceN * MaxCurrentA;
5115-
if (cvc.ControlType == CABViewControlTypes.AMMETER_ABS) data = Math.Abs(data);
5112+
if (cvc.ControlType.Type == CABViewControlTypes.AMMETER_ABS) data = Math.Abs(data);
51165113
break;
51175114
}
51185115
case CABViewControlTypes.LOAD_METER:
@@ -5819,7 +5816,7 @@ public virtual float GetDataOf(CabViewControl cvc)
58195816
}
58205817

58215818
case CABViewControlTypes.ORTS_TCS:
5822-
TrainControlSystem.CabDisplayControls.TryGetValue(cvc.ControlSubtype - 1, out data);
5819+
TrainControlSystem.CabDisplayControls.TryGetValue(cvc.ControlType.Subtype - 1, out data);
58235820
break;
58245821

58255822
case CABViewControlTypes.ORTS_BATTERY_SWITCH_COMMAND_SWITCH:

Source/Orts.Simulation/Simulation/RollingStocks/MSTSSteamLocomotive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5856,7 +5856,7 @@ public override float GetDataOf(CabViewControl cvc)
58565856
{
58575857
float data;
58585858

5859-
switch (cvc.ControlType)
5859+
switch (cvc.ControlType.Type)
58605860
{
58615861
case CABViewControlTypes.WHISTLE:
58625862
data = Horn ? 1 : 0;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/CruiseControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ void SetTrainBrake(ref float brakePercent, float elapsedClockSeconds, float delt
14141414
public float GetDataOf(CabViewControl cvc)
14151415
{
14161416
float data = 0;
1417-
switch (cvc.ControlType)
1417+
switch (cvc.ControlType.Type)
14181418
{
14191419
case CABViewControlTypes.ORTS_SELECTED_SPEED:
14201420
case CABViewControlTypes.ORTS_SELECTED_SPEED_DISPLAY:

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/EOT.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public override void Copy(MSTSWagon copy)
229229
public float GetDataOf(CabViewControl cvc)
230230
{
231231
float data = 0;
232-
switch (cvc.ControlType)
232+
switch (cvc.ControlType.Type)
233233
{
234234
case CABViewControlTypes.ORTS_EOT_ID:
235235
data = ID;

0 commit comments

Comments
 (0)