2727using Orts . Simulation . RollingStocks . SubSystems . PowerSupplies ;
2828using ORTS . Common ;
2929using ORTS . Common . Input ;
30+ using ORTS . Scripting . Api ;
3031using System ;
3132using System . Collections . Generic ;
3233using System . IO ;
3334using System . Linq ;
35+ using System . Windows . Forms ;
3436
3537namespace Orts . Viewer3D . Popups
3638{
@@ -81,10 +83,9 @@ public class TrainCarOperationsViewerWindow : Window
8183
8284 public List < bool > AngleCockAPartiallyOpened = new List < bool > ( ) ;
8385 public List < bool > AngleCockBPartiallyOpened = new List < bool > ( ) ;
84- public string BatteryStatus ;
85- string CircuitBreakerState ;
86+ public bool BatterySwitchOn ;
87+ public PowerSupplyState PowerSupplyStatus ;
8688 public int LocoRowCount ;
87- public string PowerSupplyStatus ;
8889 public int RowsCount ;
8990 public int SpacerRowCount ;
9091 public int SymbolsRowCount ;
@@ -437,11 +438,9 @@ void AddSpace(bool full)
437438 }
438439 if ( isElectricDieselLocomotive )
439440 {
440- if ( locomotive . GetMultipleUnitsConfiguration ( ) != null )
441- {
442- line . Add ( new buttonToggleMU ( 0 , 0 , textHeight , Owner . Viewer , CarPosition ) ) ;
443- AddSpace ( false ) ;
444- }
441+ line . Add ( new buttonToggleMU ( 0 , 0 , textHeight , Owner . Viewer , CarPosition ) ) ;
442+ AddSpace ( false ) ;
443+
445444 line . Add ( new buttonTogglePower ( 0 , 0 , textHeight , Owner . Viewer , CarPosition ) ) ;
446445 AddSpace ( false ) ;
447446 if ( ( wagon != null ) && ( wagon . PowerSupply is IPowerSupply ) )
@@ -539,10 +538,8 @@ public override void PrepareFrame(ElapsedTime elapsedTime, bool updateFull)
539538 carOperations . CarOperationChanged = carOperations . Visible && carOperations . CarOperationChanged ;
540539 }
541540 // Updates power supply status
542- else if ( isElectricDieselLocomotive &&
543- ( PowerSupplyStatus != null && PowerSupplyStatus != Owner . Viewer . PlayerTrain . Cars [ CarPosition ] . GetStatus ( )
544- || ( BatteryStatus != null && BatteryStatus != Owner . Viewer . PlayerTrain . Cars [ CarPosition ] . GetStatus ( ) )
545- || ( CircuitBreakerState != null && CircuitBreakerState != ( trainCar as MSTSElectricLocomotive ) . ElectricPowerSupply . CircuitBreaker . State . ToString ( ) ) ) )
541+ else if ( ( trainCar is MSTSWagon wagon && wagon . PowerSupply != null && BatterySwitchOn != wagon . PowerSupply . BatterySwitch . On )
542+ || ( isElectricDieselLocomotive && PowerSupplyStatus != ( trainCar as MSTSLocomotive ) . LocomotivePowerSupply . GetPowerStatus ( ) ) )
546543 {
547544 Layout ( ) ;
548545 UpdateWindowSize ( ) ;
@@ -898,7 +895,6 @@ public buttonFrontAngleCock(int x, int y, int size, Viewer viewer, TrainCar car,
898895 Viewer = viewer ;
899896 TrainCarViewer = Viewer . TrainCarOperationsViewerWindow ;
900897 CurrentCar = Viewer . PlayerTrain . Cars [ carPosition ] ;
901- var first = car == Viewer . PlayerTrain . Cars . First ( ) ;
902898
903899 if ( CurrentCar . BrakeSystem is VacuumSinglePipe )
904900 {
@@ -908,15 +904,11 @@ public buttonFrontAngleCock(int x, int y, int size, Viewer viewer, TrainCar car,
908904 {
909905 var carAngleCockAOpenAmount = CurrentCar . BrakeSystem . AngleCockAOpenAmount ;
910906 var carAngleCockAOpen = ( CurrentCar as MSTSWagon ) . BrakeSystem . AngleCockAOpen ;
911- Texture = ! TrainCarViewer . TrainCarOperationsChanged && first ? FrontAngleCockClosed
912- : carAngleCockAOpenAmount > 0 && carAngleCockAOpenAmount < 1 ? FrontAngleCockPartial
907+ Texture = carAngleCockAOpenAmount > 0 && carAngleCockAOpenAmount < 1 ? FrontAngleCockPartial
913908 : carAngleCockAOpen ? FrontAngleCockOpened
914909 : FrontAngleCockClosed ;
915910
916- if ( ! first )
917- {
918- Click += new Action < Control , Point > ( buttonFrontAngleCock_Click ) ;
919- }
911+ Click += new Action < Control , Point > ( buttonFrontAngleCock_Click ) ;
920912 }
921913 Source = new Rectangle ( 0 , 0 , size , size ) ;
922914 }
@@ -949,7 +941,6 @@ public buttonRearAngleCock(int x, int y, int size, Viewer viewer, TrainCar car,
949941 Viewer = viewer ;
950942 TrainCarViewer = Viewer . TrainCarOperationsViewerWindow ;
951943 CurrentCar = Viewer . PlayerTrain . Cars [ carPosition ] ;
952- var last = car == Viewer . PlayerTrain . Cars . Last ( ) ;
953944
954945 if ( CurrentCar . BrakeSystem is VacuumSinglePipe )
955946 {
@@ -959,15 +950,11 @@ public buttonRearAngleCock(int x, int y, int size, Viewer viewer, TrainCar car,
959950 {
960951 var carAngleCockBOpenAmount = ( CurrentCar as MSTSWagon ) . BrakeSystem . AngleCockBOpenAmount ;
961952 var carAngleCockBOpen = ( CurrentCar as MSTSWagon ) . BrakeSystem . AngleCockBOpen ;
962- Texture = last ? RearAngleCockClosed
963- : carAngleCockBOpenAmount > 0 && carAngleCockBOpenAmount < 1 ? RearAngleCockPartial
953+ Texture = carAngleCockBOpenAmount > 0 && carAngleCockBOpenAmount < 1 ? RearAngleCockPartial
964954 : carAngleCockBOpen ? RearAngleCockOpened
965955 : RearAngleCockClosed ;
966956
967- if ( ! last )
968- {
969- Click += new Action < Control , Point > ( buttonRearAngleCock_Click ) ;
970- }
957+ Click += new Action < Control , Point > ( buttonRearAngleCock_Click ) ;
971958 }
972959 Source = new Rectangle ( 0 , 0 , size , size ) ;
973960 }
@@ -1094,18 +1081,16 @@ class buttonToggleMU : Image
10941081 readonly Viewer Viewer ;
10951082 readonly TrainCarOperationsViewerWindow TrainCarViewer ;
10961083 readonly TrainCar CurrentCar ;
1097- readonly string MultipleUnitsConfiguration ;
10981084 public buttonToggleMU ( int x , int y , int size , Viewer viewer , int carPosition )
10991085 : base ( x , y , size , size )
11001086 {
11011087 Viewer = viewer ;
11021088 TrainCarViewer = Viewer . TrainCarOperationsViewerWindow ;
11031089 CurrentCar = Viewer . PlayerTrain . Cars [ carPosition ] ;
11041090
1105- MultipleUnitsConfiguration = Viewer . PlayerLocomotive . GetMultipleUnitsConfiguration ( ) ;
1106- if ( CurrentCar is MSTSDieselLocomotive && MultipleUnitsConfiguration != null )
1091+ if ( CurrentCar is MSTSLocomotive )
11071092 {
1108- Texture = Viewer . TrainCarOperationsWindow . ModifiedSetting || ( ( CurrentCar as MSTSLocomotive ) . RemoteControlGroup == 0 && MultipleUnitsConfiguration != "1" ) ? MUconnected : MUdisconnected ;
1093+ Texture = Viewer . TrainCarOperationsWindow . ModifiedSetting || ( ( CurrentCar as MSTSLocomotive ) . RemoteControlGroup == 0 ) ? MUconnected : MUdisconnected ;
11091094 }
11101095 else
11111096 {
@@ -1116,12 +1101,12 @@ public buttonToggleMU(int x, int y, int size, Viewer viewer, int carPosition)
11161101 }
11171102 void buttonToggleMU_Click ( Control arg1 , Point arg2 )
11181103 {
1119- if ( CurrentCar is MSTSDieselLocomotive )
1104+ if ( CurrentCar is MSTSLocomotive )
11201105 {
11211106 MSTSLocomotive locomotive = CurrentCar as MSTSLocomotive ;
11221107
11231108 new ToggleMUCommand ( Viewer . Log , locomotive , locomotive . RemoteControlGroup < 0 ) ;
1124- if ( locomotive . RemoteControlGroup == 0 && MultipleUnitsConfiguration != "1" )
1109+ if ( locomotive . RemoteControlGroup == 0 )
11251110 {
11261111 Viewer . Simulator . Confirmer . Information ( Viewer . Catalog . GetString ( "MU signal connected" ) ) ;
11271112 Texture = MUconnected ;
@@ -1167,10 +1152,10 @@ void buttonTogglePower_Click(Control arg1, Point arg2)
11671152 if ( ( CurrentCar is MSTSElectricLocomotive ) || ( CurrentCar is MSTSDieselLocomotive ) )
11681153 {
11691154 MSTSLocomotive locomotive = CurrentCar as MSTSLocomotive ;
1155+ bool powerOn = locomotive . LocomotivePowerSupply . GetPowerStatus ( ) == PowerSupplyState . PowerOff ;
11701156
1171- new PowerCommand ( Viewer . Log , locomotive , ! locomotive . LocomotivePowerSupply . MainPowerSupplyOn ) ;
1172- var mainPowerSupplyOn = locomotive . LocomotivePowerSupply . MainPowerSupplyOn ;
1173- if ( mainPowerSupplyOn )
1157+ new PowerCommand ( Viewer . Log , locomotive , powerOn ) ;
1158+ if ( ! powerOn )
11741159 Viewer . Simulator . Confirmer . Information ( Viewer . Catalog . GetString ( "Power OFF command sent" ) ) ;
11751160 else
11761161 Viewer . Simulator . Confirmer . Information ( Viewer . Catalog . GetString ( "Power ON command sent" ) ) ;
@@ -1183,36 +1168,9 @@ void buttonTogglePower_Click(Control arg1, Point arg2)
11831168 }
11841169 public Texture2D locomotiveStatusPower ( int CarPosition )
11851170 {
1186- string locomotiveStatus = CurrentCar . GetStatus ( ) ;
1187- foreach ( string data in locomotiveStatus . Split ( '\n ' ) . Where ( ( string d ) => ! string . IsNullOrWhiteSpace ( d ) ) )
1188- {
1189- string [ ] parts = data . Split ( new string [ ] { " = " } , 2 , StringSplitOptions . None ) ;
1190- string keyPart = parts [ 0 ] ;
1191- string valuePart = parts ? [ 1 ] ;
1192- if ( keyPart . Contains ( Viewer . Catalog . GetParticularString ( "DieselEngine" , "Engine" ) ) )
1193- {
1194- TrainCarViewer . PowerSupplyStatus = locomotiveStatus ;
1195- Texture = valuePart . Contains ( Viewer . Catalog . GetParticularString ( "DieselEngine" , "Running" ) ) ? PowerOn
1196- : valuePart . Contains ( Viewer . Catalog . GetParticularString ( "DieselEngine" , "Stopped" ) ) ? PowerOff
1197- : PowerChanging ;
1198- break ;
1199- }
1200-
1201- MSTSElectricLocomotive locomotive = CurrentCar as MSTSElectricLocomotive ;
1202- switch ( locomotive . ElectricPowerSupply . CircuitBreaker . State )
1203- {
1204- case ORTS . Scripting . Api . CircuitBreakerState . Closed :
1205- Texture = PowerOn ;
1206- break ;
1207- case ORTS . Scripting . Api . CircuitBreakerState . Closing :
1208- Texture = PowerChanging ;
1209- break ;
1210- case ORTS . Scripting . Api . CircuitBreakerState . Open :
1211- Texture = PowerOff ;
1212- break ;
1213- }
1214- TrainCarViewer . CircuitBreakerState = locomotive . ElectricPowerSupply . CircuitBreaker . State . ToString ( ) ;
1215- }
1171+ var powerStatus = ( CurrentCar as MSTSLocomotive ) . LocomotivePowerSupply . GetPowerStatus ( ) ;
1172+ TrainCarViewer . PowerSupplyStatus = powerStatus ;
1173+ Texture = powerStatus == PowerSupplyState . PowerOn ? PowerOn : powerStatus == PowerSupplyState . PowerOff ? PowerOff : PowerChanging ;
12161174 return Texture ;
12171175 }
12181176 }
@@ -1235,6 +1193,7 @@ public ToggleBatterySwitch(int x, int y, int size, Viewer viewer, int carPositio
12351193 if ( wagon . PowerSupply . BatterySwitch . Mode == BatterySwitch . ModeType . AlwaysOn )
12361194 {
12371195 Texture = BattAlwaysOn32 ;
1196+ TrainCarViewer . BatterySwitchOn = wagon . PowerSupply . BatterySwitch . On ;
12381197 }
12391198 else
12401199 {
@@ -1272,18 +1231,11 @@ void ToggleBatterySwitch_Click(Control arg1, Point arg2)
12721231 }
12731232 public Texture2D locomotiveStatusBattery ( int CarPosition )
12741233 {
1275- string locomotiveStatus = CurrentCar . GetStatus ( ) ;
1276- foreach ( string data in locomotiveStatus . Split ( '\n ' ) . Where ( ( string d ) => ! string . IsNullOrWhiteSpace ( d ) ) )
1234+ if ( CurrentCar is MSTSWagon wagon && wagon . PowerSupply != null )
12771235 {
1278- string [ ] parts = data . Split ( new string [ ] { " = " } , 2 , StringSplitOptions . None ) ;
1279- string keyPart = parts [ 0 ] ;
1280- string valuePart = parts ? [ 1 ] ;
1281- if ( keyPart . Contains ( Viewer . Catalog . GetString ( "Battery" ) ) )
1282- {
1283- TrainCarViewer . BatteryStatus = locomotiveStatus ;
1284- Texture = valuePart . Contains ( Viewer . Catalog . GetString ( "On" ) ) ? BattOn32 : BattOff32 ;
1285- break ;
1286- }
1236+ bool on = wagon . PowerSupply . BatterySwitch . On ;
1237+ TrainCarViewer . BatterySwitchOn = on ;
1238+ Texture = on ? BattOn32 : BattOff32 ;
12871239 }
12881240 return Texture ;
12891241 }
0 commit comments